月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-09-14 3b74a18403fa5004e0b3820b0c171d90a615ca21
src/main/java/com/moon/server/service/data/RasterAnalysisService.java
@@ -4,6 +4,7 @@
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;
@@ -13,7 +14,10 @@
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 javax.annotation.Resource;
import java.io.File;
@@ -143,6 +147,58 @@
     * 分析线
     */
    public void analysisPolyline(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) {
        /*Double len = geo.Length();
        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]);
        double buffer = Math.max(pixelWidth, pixelHeight) * size;
        double[] env = new double[4];
        geo = geo.Buffer(buffer);
        geo.GetEnvelope(env);
        int xMinPixel = (int) Math.floor((env[0] - minX) / pixelWidth);
        int yMinPixel = (int) Math.floor((maxY - env[3]) / pixelHeight);
        int xMaxPixel = (int) Math.floor((env[1] - minX) / pixelWidth);
        int yMaxPixel = (int) Math.floor((maxY - env[2]) / pixelHeight);
        int bandCount = ds.getRasterCount();
        int xMin = Math.min(xMinPixel, xMaxPixel);
        int xMax = Math.max(xMinPixel, xMaxPixel);
        int yMin = Math.min(yMinPixel, yMaxPixel);
        int yMax = Math.max(yMinPixel, yMaxPixel);
        List<Double> list = new ArrayList<>();
        for (int y = yMin; y <= yMax; y++) {
            for (int x = xMin; x <= xMax; x++) {
                Geometry point = new Geometry(ogr.wkbPoint);
                point.AddPoint(minX + pixelWidth * x, maxY - pixelHeight * y);
                if (geo.Intersects(point)) {
                    for (int i = 1; i <= bandCount; i++) {
                        double[] values = new double[1];
                        ds.GetRasterBand(i).ReadRaster(x, y, 1, 1, values);
                        list.add(values[0]);
                    }
                }
            }
        }
        processResult(list, entity);*/
        //double dis = GeoHelper.getDistance1()
        double lineDis = getPolylineDistance(geo);
        double distance = lineDis / Math.max(geo.GetPointCount(), size);
        List<double[]> list = getPointsByDistance(geo, distance);
    }
    /**
     * 分析线
     */
    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]);
@@ -292,32 +348,34 @@
        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.");
    /**
     * 获取折线距离
     */
    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.getDistance2(xy1[0], xy1[1], xy2[0], xy2[1]);
        }
        Geometry geometry = Geometry.CreateFromWkt(geometryString);
        return dis;
    }
        Dataset clippedDataset = clipRaster(dataset, geometry);
    /**
     * 根据距离获取节点
     */
    private List<double[]> getPointsByDistance(Geometry geo, double distance) {
        List<double[]> list = new ArrayList<>();
        for (int i = 0, c = geo.GetPointCount() - 1; i < c; i++) {
            double[] xy1 = geo.GetPoint(i);
            double[] xy2 = geo.GetPoint(i + 1);
        int width = clippedDataset.GetRasterXSize();
        int height = clippedDataset.GetRasterYSize();
        int bandCount = clippedDataset.getRasterCount();
            double dis = GeoHelper.getDistance2(xy1[0], xy1[1], xy2[0], xy2[1]);
        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];
        }
        return list;
    }
}