13693261870
2024-07-17 ef22eddce187cfc2177e731459045c41e035b9bc
读取DEM数据
已修改3个文件
170 ■■■■ 文件已修改
src/main/java/com/se/simu/domain/LayerVo.java 16 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/helper/GdalHelper.java 139 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/WaterService.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/domain/LayerVo.java
@@ -136,16 +136,16 @@
    }
    public static class Terrain {
        int maxHeight;
        int maxWidth;
        int maxHeight;
        public Terrain() {
        }
        public Terrain(int maxHeight, int maxWidth) {
            this.maxHeight = maxHeight;
        public Terrain(int maxWidth, int maxHeight) {
            this.maxWidth = maxWidth;
            this.maxHeight = maxHeight;
        }
        public int getMaxHeight() {
@@ -166,18 +166,18 @@
    }
    public static class Water {
        int maxHeight;
        int maxWidth;
        int maxHeight;
        List<Long> data;
        public Water() {
        }
        public Water(int maxHeight, int maxWidth, List<Long> data) {
            this.maxHeight = maxHeight;
        public Water(int maxWidth, int maxHeight, List<Long> data) {
            this.maxWidth = maxWidth;
            this.maxHeight = maxHeight;
            this.data = data;
        }
src/main/java/com/se/simu/helper/GdalHelper.java
@@ -1,12 +1,14 @@
package com.se.simu.helper;
import com.se.simu.domain.LayerVo;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.Geometry;
import org.gdal.ogr.ogr;
import org.gdal.osr.SpatialReference;
import java.io.File;
@@ -19,6 +21,16 @@
@Slf4j
@SuppressWarnings("ALL")
public class GdalHelper {
    public final static int I4326 = 4326;
    public final static int I4490 = 4490;
    public static SpatialReference SR4326;
    public static SpatialReference SR4490;
    public final static String CGCS2000 = "CGCS2000";
    /**
     * 初始化
     */
@@ -46,19 +58,136 @@
        // 注册所有的驱动
        gdal.AllRegister();
        ogr.RegisterAll();
        initSr();
    }
    /**
     * 初始化坐标系
     */
    public static void initSr() {
        try {
            SR4326 = new SpatialReference();
            SR4326.ImportFromEPSG(I4326);
            SR4490 = new SpatialReference();
            SR4490.ImportFromEPSG(I4490);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
    /**
     * 读取DEM边界
     */
    public static LayerVo readDemExtent(String file) {
        LayerVo layer = new LayerVo();
        Dataset ds = null;
        try {
            File f = new File(file);
            if (!f.exists() || f.isDirectory()) {
                return null;
            }
            ds = gdal.Open(file, gdalconst.GA_ReadOnly);
            if (null == ds) {
                return null;
            }
            Band band = ds.GetRasterBand(1);
            double[] mm = new double[2];
            band.ComputeRasterMinMax(mm);
            Geometry minPoint = getMinPoint(ds);
            Geometry maxPoint = getMaxPoint(ds);
            layer.setExtension(new LayerVo.Extension(minPoint.GetX(), minPoint.GetY(), maxPoint.GetX(), maxPoint.GetY(), mm[0], mm[1]));
            layer.setTerrain(new LayerVo.Terrain(band.getXSize(), band.getYSize()));
            layer.setWater(new LayerVo.Water(band.getXSize(), band.getYSize(), null));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            if (null != ds) {
                ds.delete();
            }
        }
        return layer;
    }
    /**
     * 获取Dataset的最小点
     */
    private static Geometry getMinPoint(Dataset ds) {
        double[] transform = new double[6];
        ds.GetGeoTransform(transform);
        double xMin = transform[0];
        double yMin = transform[3] - ds.getRasterYSize() * transform[1];
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AddPoint(xMin, yMin, 0);
        return transform(ds, point);
    }
    /**
     * 获取Dataset的最大点
     */
    private static Geometry getMaxPoint(Dataset ds) {
        /*
         * transform[0] 左上角x坐标
         * transform[1] 东西方向分辨率
         * transform[2] 旋转角度, 0表示图像 "北方朝上"
         *
         * transform[3] 左上角y坐标
         * transform[4] 旋转角度, 0表示图像 "北方朝上"
         * transform[5] 南北方向分辨率
         */
        double[] transform = new double[6];
        ds.GetGeoTransform(transform);
        double xMax = transform[0] + (ds.getRasterXSize() * transform[1]);
        double yMax = transform[3];
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AddPoint(xMax, yMax, 0);
        return transform(ds, point);
    }
    /**
     * 坐标转换
     */
    private static Geometry transform(Dataset ds, Geometry point) {
        point.AssignSpatialReference(ds.GetSpatialRef());
        if (ds.GetSpatialRef().IsGeographic() > 0) {
            return point;
        }
        String srsName = ds.GetSpatialRef().GetName();
        if (srsName.contains(CGCS2000)) {
            point.TransformTo(SR4490);
        } else {
            point.TransformTo(SR4326);
        }
        point.SwapXY();
        return point;
    }
    /**
     * 创建金字塔
     */
    public static void createPyramid(String file) {
        Dataset ds = null;
        try {
            File f = new File(file);
            if (!f.exists() || f.isDirectory()) {
                return;
            }
            Dataset ds = gdal.Open(file, gdalconst.GA_ReadOnly);
            ds = gdal.Open(file, gdalconst.GA_ReadOnly);
            if (null == ds) {
                return;
            }
@@ -68,10 +197,12 @@
            if (0 == band.GetOverviewCount()) {
                ds.BuildOverviews("nearest", new int[]{2, 4, 6, 8, 16}, null);
            }
            ds.delete();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            if (null != ds) {
                ds.delete();
            }
        }
    }
}
src/main/java/com/se/simu/service/WaterService.java
@@ -35,14 +35,17 @@
        String file2 = "D:/simu/test/DOM_M.tif";
        GdalHelper.createPyramid(file2);
        LayerVo layer = new LayerVo();
        LayerVo layer = GdalHelper.readDemExtent(file);
        if (null == layer) {
            return null;
        }
        layer.setVer(ver);
        layer.setDuration(new LayerVo.Duration(1719812810225L, 1719812810225L));
        layer.setExtension(new LayerVo.Extension(2.11062743358, 0.53812160220, 2.11070827834, 0.53819799453, 1.151, 38.83));
        layer.setTerrain(new LayerVo.Terrain(10000, 10000));
        //layer.setDuration(new LayerVo.Duration(1719812810225L, 1719812810225L));
        //layer.setExtension(new LayerVo.Extension(2.11062743358, 0.53812160220, 2.11070827834, 0.53819799453, 1.151, 38.83));
        //layer.setTerrain(new LayerVo.Terrain(10000, 10000));
        List<Long> data = new ArrayList<>(Arrays.asList(1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L));
        layer.setWater(new LayerVo.Water(10000, 10000, data));
        //List<Long> data = new ArrayList<>(Arrays.asList(1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L));
        //layer.setWater(new LayerVo.Water(10000, 10000, data));
        return layer;
    }