| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | |
| | | // double rotationX = transform[2]; double rotationY = transform[4] |
| | | double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = transform[5]; |
| | | |
| | | double len = 0; |
| | | int bandCount = ds.getRasterCount(); |
| | | double lineDis = getPolylineDistance(geo); |
| | | double distance = lineDis / Math.max(geo.GetPointCount(), size); |
| | | |
| | | List<double[]> points = getPointsByDistance(geo, distance); |
| | | for (double[] xy : points) { |
| | | for (int i = 0, c = points.size(); i < c; i++) { |
| | | double[] xy = points.get(i); |
| | | if (i > 0) { |
| | | double[] p = points.get(i - 1); |
| | | len += GeoHelper.getDistance(p[0], p[1], xy[0], xy[1]); |
| | | } |
| | | |
| | | int xPixel = (int) Math.floor((xy[0] - minX) / pixelWidth); |
| | | int yPixel = (int) Math.floor((maxY - xy[1]) / Math.abs(pixelHeight)); |
| | | |
| | | List<Double> vals = new ArrayList<>(); |
| | | for (int i = 1; i <= bandCount; i++) { |
| | | for (int j = 1; j <= bandCount; j++) { |
| | | double[] pixelValues = new double[1]; |
| | | ds.GetRasterBand(i).ReadRaster(xPixel, yPixel, 1, 1, pixelValues); |
| | | ds.GetRasterBand(j).ReadRaster(xPixel, yPixel, 1, 1, pixelValues); |
| | | vals.add(pixelValues[0]); |
| | | } |
| | | entity.addPoint(xy[0], xy[1], vals); |
| | | entity.addPoint(xy[0], xy[1], len, vals); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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)); |
| | | } |
| | | } |