| | |
| | | import org.gdal.gdal.gdal; |
| | | import org.gdal.ogr.Geometry; |
| | | import org.gdal.ogr.ogr; |
| | | import org.gdal.osr.SpatialReference; |
| | | import org.opengis.referencing.crs.CoordinateReferenceSystem; |
| | | import org.springframework.stereotype.Service; |
| | | import org.geotools.referencing.CRS; |
| | | import sun.awt.IconInfo; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.awt.geom.Point2D; |
| | |
| | | int xPixel = (int) Math.floor((x - minX) / pixelWidth); |
| | | int yPixel = (int) Math.floor((maxY - y) / Math.abs(pixelHeight)); |
| | | |
| | | List<double[]> list = new ArrayList<>(); |
| | | for (int i = 1; i <= bandCount; i++) { |
| | | double[] pixelValues = new double[size * size]; |
| | | Band band = ds.GetRasterBand(i); |
| | | band.ReadRaster(xPixel, yPixel, size, size, pixelValues); |
| | | |
| | | list.add(pixelValues); |
| | | } |
| | | |
| | | processResult(entity, list); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | processResult(list, entity); |
| | | // processResult(list, entity) |
| | | } |
| | | |
| | | /** |
| | |
| | | int geoWidth = Math.abs(xMaxPixel - xMinPixel); |
| | | int geoHeight = Math.abs(yMaxPixel - yMinPixel); |
| | | |
| | | List<double[]> list = new ArrayList<>(); |
| | | for (int i = 1; i <= bandCount; i++) { |
| | | Band band = ds.GetRasterBand(i); |
| | | double[] pixelValues = new double[geoWidth * geoHeight]; |
| | | band.ReadRaster(xMinPixel, yMinPixel, geoWidth, geoHeight, pixelValues); |
| | | ds.GetRasterBand(i).ReadRaster(xMinPixel, yMinPixel, geoWidth, geoHeight, pixelValues); |
| | | |
| | | list.add(pixelValues); |
| | | setBandVals(entity, pixelValues); |
| | | } |
| | | |
| | | processResult(entity, list); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 处理结果 |
| | | * 设置Band值 |
| | | */ |
| | | private void processResult(AnalysisResultEntity entity, List<double[]> list) { |
| | | if (null == list || list.isEmpty()) { |
| | | private void setBandVals(AnalysisResultEntity entity, double[] pixelValues) { |
| | | if (null == pixelValues || pixelValues.length == 0) { |
| | | return; |
| | | } |
| | | |
| | | List<Double> rs = new ArrayList<>(); |
| | | for (double[] ds : list) { |
| | | if (null != ds && ds.length > 0) { |
| | | for (double d : ds) { |
| | | rs.add(d); |
| | | } |
| | | } |
| | | List<Double> list = new ArrayList<>(); |
| | | for (double val : pixelValues) { |
| | | list.add(val); |
| | | } |
| | | |
| | | processResult(rs, entity); |
| | | } |
| | | |
| | | /** |
| | | * 处理结果 |
| | | */ |
| | | private void processResult(List<Double> rs, AnalysisResultEntity entity) { |
| | | if (null == rs || rs.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | double avg = rs.stream().mapToDouble(Double::valueOf).average().getAsDouble(); |
| | | entity.setMin(Collections.min(rs)); |
| | | entity.setMax(Collections.max(rs)); |
| | | entity.setAvg(avg); |
| | | } |
| | | |
| | | public void processClippedDataByLine(String imagePath, String geometryString) { |
| | | // 注册GDAL驱动 |
| | | gdal.AllRegister(); |
| | | // 打开栅格图像数据集 |
| | | Dataset dataset = gdal.Open(imagePath); |
| | | if (dataset == null) { |
| | | throw new RuntimeException("Failed to open raster dataset."); |
| | | } |
| | | |
| | | Geometry geometry = Geometry.CreateFromWkt(geometryString); |
| | | |
| | | Dataset clippedDataset = clipRaster(dataset, geometry); |
| | | |
| | | int width = clippedDataset.GetRasterXSize(); |
| | | int height = clippedDataset.GetRasterYSize(); |
| | | int bandCount = clippedDataset.getRasterCount(); |
| | | |
| | | List<double[]> sum = new ArrayList<>(); |
| | | for (int bandIndex = 1; bandIndex <= bandCount; bandIndex++) { |
| | | Band band = clippedDataset.GetRasterBand(bandIndex); |
| | | |
| | | double[] pixelValues = new double[width * height]; |
| | | band.ReadRaster(0, 0, width, height, pixelValues); |
| | | |
| | | sum.add(pixelValues); |
| | | } |
| | | } |
| | | |
| | | public static Dataset clipRaster(Dataset dataset, Geometry geometry) { |
| | | Vector<String> warpOptions = new Vector<>(); |
| | | warpOptions.add("-crop_to_cutline"); |
| | | warpOptions.add("-cutline"); |
| | | warpOptions.add(geometry.ExportToWkt()); |
| | | warpOptions.add("-dstalpha"); |
| | | |
| | | return gdal.Warp("", new Dataset[]{dataset}, new WarpOptions(warpOptions)); |
| | | double min = Collections.min(list); |
| | | double max = Collections.max(list); |
| | | double avg = list.stream().mapToDouble(Double::valueOf).average().getAsDouble(); |
| | | entity.addBandVals(min, avg, max); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private void processClippedDataByLine(String imagePath, String geometryString) { |
| | | // 注册GDAL驱动 |
| | | gdal.AllRegister(); |
| | | // 打开栅格图像数据集 |
| | | Dataset dataset = gdal.Open(imagePath); |
| | | if (dataset == null) { |
| | | throw new RuntimeException("Failed to open raster dataset."); |
| | | } |
| | | |
| | | Geometry geometry = Geometry.CreateFromWkt(geometryString); |
| | | |
| | | Dataset clippedDataset = clipRaster(dataset, geometry); |
| | | |
| | | int width = clippedDataset.GetRasterXSize(); |
| | | int height = clippedDataset.GetRasterYSize(); |
| | | int bandCount = clippedDataset.getRasterCount(); |
| | | |
| | | List<double[]> sum = new ArrayList<>(); |
| | | for (int bandIndex = 1; bandIndex <= bandCount; bandIndex++) { |
| | | Band band = clippedDataset.GetRasterBand(bandIndex); |
| | | |
| | | double[] pixelValues = new double[width * height]; |
| | | band.ReadRaster(0, 0, width, height, pixelValues); |
| | | |
| | | sum.add(pixelValues); |
| | | } |
| | | } |
| | | |
| | | private Dataset clipRaster(Dataset dataset, Geometry geometry) { |
| | | Vector<String> warpOptions = new Vector<>(); |
| | | warpOptions.add("-crop_to_cutline"); |
| | | warpOptions.add("-cutline"); |
| | | warpOptions.add(geometry.ExportToWkt()); |
| | | warpOptions.add("-dstalpha"); |
| | | |
| | | return gdal.Warp("", new Dataset[]{dataset}, new WarpOptions(warpOptions)); |
| | | } |
| | | } |