月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-09-11 9ba86955948dff0655ce7f028beddfdce2bad0b4
栅格分析-分析点数据
已修改7个文件
224 ■■■■ 文件已修改
src/main/java/com/moon/server/controller/data/RasterAnalysisController.java 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/entity/all/StaticData.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/mapper/data/PublishMapper.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/service/data/PublishService.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/service/data/RasterAnalysisService.java 172 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/data/PublishMapper.xml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/controller/data/RasterAnalysisController.java
@@ -30,7 +30,7 @@
    @Resource
    RasterAnalysisService rasterService;
    private final static List<Integer> pixels = new ArrayList<>(Arrays.asList(1, 2, 4, 8, 16, 32, 64, 128, 256));
    private final static List<Integer> PIXELS = new ArrayList<>(Arrays.asList(1, 2, 4, 8, 16, 32, 64, 128, 256));
    @SysLog()
    @ApiOperation(value = "查询点分析")
@@ -49,11 +49,11 @@
            if (null == geo || geo.GetGeometryType() != ogr.wkbPoint) {
                return fail("WKT字符串不正确");
            }
            if (null == pixel || !pixels.contains(pixel)) {
                return fail("像点值只能为:" + StringHelper.join(pixels, ", "));
            if (null == pixel || !PIXELS.contains(pixel)) {
                return fail("像点值只能为:" + StringHelper.join(PIXELS, ", "));
            }
            List<?> rs = rasterService.analysisPoint(wkt, pixel);
            List<?> rs = rasterService.analysisPoint(geo, pixel);
            return success(rs.size(), rs);
        } catch (Exception ex) {
@@ -77,7 +77,7 @@
                return fail("WKT字符串不正确");
            }
            List<?> rs = rasterService.analysisPolyline(wkt);
            List<?> rs = rasterService.analysisPolyline(geo);
            return success(rs.size(), rs);
        } catch (Exception ex) {
@@ -101,7 +101,7 @@
                return fail("WKT字符串不正确");
            }
            List<?> rs = rasterService.analysisPolygon(wkt);
            List<?> rs = rasterService.analysisPolygon(geo);
            return success(rs.size(), rs);
        } catch (Exception ex) {
src/main/java/com/moon/server/entity/all/StaticData.java
@@ -72,6 +72,10 @@
    public static final int I180_NEG = -180;
    public static final int I200 = 200;
    public static final int I500 = 500;
    public static final int I1000 = 1000;
    public static final int I2050 = 2050;
src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java
@@ -21,6 +21,10 @@
    private Double max;
    private int code;
    private String info;
    public String getLayerName() {
        return layerName;
    }
@@ -52,4 +56,20 @@
    public void setMax(Double max) {
        this.max = max;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
}
src/main/java/com/moon/server/mapper/data/PublishMapper.java
@@ -69,6 +69,13 @@
    public List<MetaEntity> selectMetasByPubid(Integer pubid);
    /**
     * 查询栅格数据发布
     *
     * @return 发布实体类
     */
    public List<PublishEntity> selectRaster();
    /**
     * 根据目录查询DOM和DEM的编码
     *
     * @param dircode 目录
src/main/java/com/moon/server/service/data/PublishService.java
@@ -73,6 +73,11 @@
    }
    @Override
    public List<PublishEntity> selectRaster() {
        return publishMapper.selectRaster();
    }
    @Override
    public List<String> selectCodesForDir(String dircode, Integer isDom) {
        return publishMapper.selectCodesForDir(dircode, isDom);
    }
src/main/java/com/moon/server/service/data/RasterAnalysisService.java
@@ -1,6 +1,12 @@
package com.moon.server.service.data;
import com.moon.server.entity.all.StaticData;
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.PathHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.WarpOptions;
@@ -9,6 +15,8 @@
import org.gdal.ogr.ogr;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
@@ -20,67 +28,127 @@
 */
@Service
public class RasterAnalysisService {
    public List<AnalysisResultEntity> analysisPoint(String wkt, Integer pixel) {
    @Resource
    PathHelper pathHelper;
    @Resource
    PublishService publishService;
    private final static Log log = LogFactory.getLog(RasterAnalysisService.class);
    /**
     * 分析点
     */
    public List<AnalysisResultEntity> analysisPoint(Geometry point, int size) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
        //
        return rs;
    }
    public List<AnalysisResultEntity> analysisPolyline(String wkt) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
        //
        return rs;
    }
    public List<AnalysisResultEntity> analysisPolygon(String wkt) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
        //
        return rs;
    }
    public double processPoint(String imagePath, String geometryString, int size) {
        // 打开栅格图像数据集
        Dataset dataset = gdal.Open(imagePath);
        if (dataset == null) {
            throw new RuntimeException("Failed to open raster dataset.");
        List<PublishEntity> pubs = publishService.selectRaster();
        if (null == pubs || pubs.isEmpty()) {
            return rs;
        }
        Geometry geometry = Geometry.CreateFromWkt(geometryString);
        for (PublishEntity pub : pubs) {
            AnalysisResultEntity entity = new AnalysisResultEntity();
            entity.setLayerName(pub.getName());
        double[] geoTransform = dataset.GetGeoTransform();
        double minX = geoTransform[0];
        double pixelWidth = geoTransform[1];
        double rotationX = geoTransform[2];
        double maxY = geoTransform[3];
        double rotationY = geoTransform[4];
        double pixelHeight = geoTransform[5];
        double x = geometry.GetX();
        double y = geometry.GetY();
        int xPixel = (int) Math.floor((x - minX) / pixelWidth);
        int yPixel = (int) Math.floor((maxY - y) / Math.abs(pixelHeight));
        int bandCount = dataset.getRasterCount();
        List<double[]> sum = new ArrayList<>();
        for (int bandIndex = 1; bandIndex <= bandCount; bandIndex++) {
            double[] pixelValues = new double[size * size];
            Band band = dataset.GetRasterBand(bandIndex);
            band.ReadRaster(xPixel, yPixel, size, size, pixelValues);
            sum.add(pixelValues);
            processPoint(entity, pub, point, size);
            rs.add(entity);
        }
        // 释放资源
        gdal.GDALDestroyDriverManager();
        return average(calculateAverage(sum));
        return rs;
    }
    /**
     * 设置错误
     */
    private void setError(AnalysisResultEntity entity, String info) {
        entity.setCode(StaticData.I500);
        entity.setInfo(info);
    }
    /**
     * 处理点
     */
    private void processPoint(AnalysisResultEntity entity, PublishEntity pub, Geometry point, int size) {
        List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId());
        if (null == metas || metas.isEmpty()) {
            setError(entity, "找不到发布数据");
            return;
        }
        String filePath = pathHelper.getConfig().getUploadPath() + File.separator + metas.get(0).getPath();
        File file = new File(filePath);
        if (!file.exists() || file.isDirectory()) {
            setError(entity, "源数据不存在");
            return;
        }
        processPoint(entity, filePath, point, size);
    }
    /**
     * 处理点
     */
    public void processPoint(AnalysisResultEntity entity, String filePath, Geometry point, int size) {
        Dataset ds = null;
        try {
            ds = gdal.Open(filePath);
            if (null == ds) {
                throw new Exception("打开栅格数据失败");
            }
            double x = point.GetX(), y = point.GetY();
            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];
            int xPixel = (int) Math.floor((x - minX) / pixelWidth);
            int yPixel = (int) Math.floor((maxY - y) / Math.abs(pixelHeight));
            int bandCount = ds.getRasterCount();
            List<double[]> sum = new ArrayList<>();
            for (int bandIndex = 1; bandIndex <= bandCount; bandIndex++) {
                double[] pixelValues = new double[size * size];
                Band band = ds.GetRasterBand(bandIndex);
                band.ReadRaster(xPixel, yPixel, size, size, pixelValues);
                sum.add(pixelValues);
            }
            //return average(calculateAverage(sum));
        } catch (Exception ex) {
            setError(entity, ex.getMessage());
            log.error(ex.getMessage(), ex);
        } finally {
            //  gdal.GDALDestroyDriverManager()
            if (null != ds) {
                ds.delete();
            }
        }
    }
    /**
     * 分析线
     */
    public List<AnalysisResultEntity> analysisPolyline(Geometry polyline) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
        //
        return rs;
    }
    /**
     * 分析面
     */
    public List<AnalysisResultEntity> analysisPolygon(Geometry polygon) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
        //
        return rs;
    }
    public List<Double> processLine(String imagePath, String geometryString) {
        // 打开栅格图像数据集
        Dataset dataset = gdal.Open(imagePath);
src/main/resources/mapper/data/PublishMapper.xml
@@ -102,6 +102,10 @@
        where b.pubid = #{pubid};
    </select>
    <select id="selectRaster" resultType="com.moon.server.entity.data.PublishEntity">
        select * from lf.sys_publish where status = 3 and type in ('DOM', 'DEM');
    </select>
    <select id="selectCodesForDir" resultType="java.lang.String">
        select code
        from lf.sys_dir