| | |
| | | import com.moon.server.entity.data.AnalysisResultEntity; |
| | | import com.moon.server.entity.data.MetaEntity; |
| | | import com.moon.server.entity.data.PublishEntity; |
| | | import com.moon.server.helper.GeoHelper; |
| | | import com.moon.server.helper.PathHelper; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.awt.geom.Point2D; |
| | | import java.io.File; |
| | | import java.util.*; |
| | | |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * 分析线 |
| | | */ |
| | | public void analysisPolyline(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) { |
| | | double[] transform = ds.GetGeoTransform(); |
| | | // 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 (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 j = 1; j <= bandCount; j++) { |
| | | double[] pixelValues = new double[1]; |
| | | ds.GetRasterBand(j).ReadRaster(xPixel, yPixel, 1, 1, pixelValues); |
| | | vals.add(pixelValues[0]); |
| | | } |
| | | entity.addPoint(xy[0], xy[1], len, vals); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 分析线 |
| | | */ |
| | | public void analysisPolyline2(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) { |
| | | double[] transform = ds.GetGeoTransform(); |
| | | // double rotationX = transform[2]; double rotationY = transform[4] |
| | | double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | double min = Collections.min(list); |
| | | double max = Collections.max(list); |
| | | double avg = list.stream().mapToDouble(Double::valueOf).average().getAsDouble(); |
| | | entity.addBandVals(min, avg, max); |
| | | } |
| | | |
| | | /** |
| | | * 处理结果 |
| | | * 获取折线距离 |
| | | */ |
| | | private void processResult(List<Double> rs, AnalysisResultEntity entity) { |
| | | if (null == rs || rs.isEmpty()) { |
| | | return; |
| | | private double getPolylineDistance(Geometry geo) { |
| | | double dis = 0; |
| | | for (int i = 0, c = geo.GetPointCount() - 1; i < c; i++) { |
| | | double[] xy1 = geo.GetPoint(i); |
| | | double[] xy2 = geo.GetPoint(i + 1); |
| | | |
| | | dis += GeoHelper.getDistance(xy1[0], xy1[1], xy2[0], xy2[1]); |
| | | } |
| | | |
| | | double avg = rs.stream().mapToDouble(Double::valueOf).average().getAsDouble(); |
| | | entity.setMin(Collections.min(rs)); |
| | | entity.setMax(Collections.max(rs)); |
| | | entity.setAvg(avg); |
| | | return dis; |
| | | } |
| | | |
| | | public void processClippedDataByLine(String imagePath, String geometryString) { |
| | | /** |
| | | * 根据距离获取节点 |
| | | */ |
| | | private List<double[]> getPointsByDistance(Geometry geo, double distance) { |
| | | List<double[]> list = new ArrayList<>(); |
| | | |
| | | int c = geo.GetPointCount() - 1; |
| | | for (int i = 0; i < c; i++) { |
| | | double[] xy1 = geo.GetPoint(i); |
| | | double[] xy2 = geo.GetPoint(i + 1); |
| | | list.add(xy1); |
| | | |
| | | double lineDis = GeoHelper.getDistance(xy1[0], xy1[1], xy2[0], xy2[1]); |
| | | if (lineDis < distance) { |
| | | continue; |
| | | } |
| | | |
| | | double d = distance; |
| | | double angle = GeoHelper.getAngle(xy1[0], xy1[1], xy2[0], xy2[1]); |
| | | while (d + distance * StaticData.D05 < lineDis) { |
| | | Point2D point = GeoHelper.getPointByDistanceAndAngle(xy1[0], xy1[1], angle, d); |
| | | list.add(new double[]{point.getX(), point.getY()}); |
| | | d += distance; |
| | | } |
| | | } |
| | | |
| | | list.add(geo.GetPoint(c)); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private void processClippedDataByLine(String imagePath, String geometryString) { |
| | | // 注册GDAL驱动 |
| | | gdal.AllRegister(); |
| | | // 打开栅格图像数据集 |
| | |
| | | } |
| | | } |
| | | |
| | | public static Dataset clipRaster(Dataset dataset, Geometry geometry) { |
| | | private Dataset clipRaster(Dataset dataset, Geometry geometry) { |
| | | Vector<String> warpOptions = new Vector<>(); |
| | | warpOptions.add("-crop_to_cutline"); |
| | | warpOptions.add("-cutline"); |
| | |
| | | warpOptions.add("-dstalpha"); |
| | | |
| | | return gdal.Warp("", new Dataset[]{dataset}, new WarpOptions(warpOptions)); |
| | | } |
| | | |
| | | public void processClippedDataByPolygon(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(); |
| | | |
| | | double[] bandSum = new double[bandCount]; |
| | | 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); |
| | | |
| | | // 多波段归一化处理 |
| | | bandSum[bandIndex - 1] = pixelValues[0]; |
| | | } |
| | | } |
| | | } |