13693261870
2024-10-31 c738483367653c6485ddc9a6dcdea019ad08cc63
根据坐标查询积水深度
已修改6个文件
222 ■■■■■ 文件已修改
src/main/java/com/se/simu/controller/WaterController.java 20 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/domain/po/SimuPo.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/helper/GdalHelper.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/helper/ShpHelper.java 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/SimuService.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/WaterService.java 100 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/controller/WaterController.java
@@ -1,8 +1,12 @@
package com.se.simu.controller;
import com.se.simu.domain.po.SimuPo;
import com.se.simu.helper.WebHelper;
import com.se.simu.service.SimuService;
import com.se.simu.service.WaterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
@@ -25,6 +29,9 @@
@RestController
@RequestMapping("/waterlogging")
public class WaterController {
    @Resource
    SimuService simuService;
    @Resource
    WaterService waterService;
@@ -121,14 +128,21 @@
    @ApiOperation(value = "根据坐标查询积水深度")
    @GetMapping("/{serviceName}/getWaterHeight")
    public Double getWaterHeight(@PathVariable String serviceName, Double x, Double y, Long timestamp, HttpServletResponse res) {
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceName", value = "服务名", dataType = "String", paramType = "path", example = "20241010095328"),
            @ApiImplicitParam(name = "x", value = "X", dataType = "double", paramType = "query", example = "116.6447998"),
            @ApiImplicitParam(name = "y", value = "Y", dataType = "double", paramType = "query", example = "39.8868915"),
            @ApiImplicitParam(name = "timestamp", value = "时间戳", dataType = "long", paramType = "query", example = "1730217660000")
    })
    public Double getWaterHeight(@PathVariable String serviceName, double x, double y, long timestamp, HttpServletResponse res) {
        try {
            if (!validate(serviceName, res)) {
            SimuPo simu = simuService.getSimuByServiceName(serviceName);
            if (null == simu) {
                return null;
            }
            // 根据服务名+时间戳+坐标,查询对应的积水深度
            return waterService.getWaterHeight(serviceName, x, y, timestamp);
            return waterService.getWaterHeight(simu, x, y, timestamp);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
src/main/java/com/se/simu/domain/po/SimuPo.java
@@ -29,6 +29,9 @@
    @ApiModelProperty("名称")
    private String name;
    @ApiModelProperty("服务名")
    private String serviceName;
    @ApiModelProperty("数据(JSON)")
    private String data;
@@ -91,6 +94,14 @@
        this.name = name;
    }
    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    public String getData() {
        return data;
    }
src/main/java/com/se/simu/helper/GdalHelper.java
@@ -6,6 +6,7 @@
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.*;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.gdal.osr.osr;
@@ -199,4 +200,44 @@
        return point;
    }
    /**
     * 转换为WGS84坐标
     */
    public static Geometry toWgs84(SpatialReference sr, double x, double y) {
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AssignSpatialReference(sr);
        point.AddPoint(x, y);
        point.TransformTo(GdalHelper.SR4326);
        //point.SwapXY();
        return point;
    }
    /**
     * WGS84转换为目标坐标
     */
    public static double[] fromWgs84(SpatialReference sr, double x, double y) {
        // https://blog.csdn.net/weixin_34910922/article/details/129208661
        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
        if (sr.IsProjected() != 1) {
            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        }
        return ct.TransformPoint(x, y);
    }
    /**
     * WGS84转换为目标坐标
     */
    public static int fromWgs84(SpatialReference sr, Geometry point) {
        // https://blog.csdn.net/weixin_34910922/article/details/129208661
        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
        if (sr.IsProjected() != 1) {
            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        }
        return point.TransformTo(sr);
    }
}
src/main/java/com/se/simu/helper/ShpHelper.java
@@ -7,13 +7,9 @@
import com.se.simu.domain.dto.GeLayer;
import lombok.extern.slf4j.Slf4j;
import org.gdal.ogr.*;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.gdal.osr.osr;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@@ -71,7 +67,7 @@
        }
    }
    private static void createFields(Layer layer,Map<String, Object> map) {
    private static void createFields(Layer layer, Map<String, Object> map) {
        for (String key : map.keySet()) {
            Object val = map.get(key);
            switch (val.getClass().getTypeName()) {
@@ -125,8 +121,8 @@
    private static Geometry createPolygon(SpatialReference sr, Double minx, Double miny, Double maxx, Double maxy) {
        String epsg = sr.GetAuthorityCode(null);
        if (!("4326".equals(epsg) || "4490".equals(epsg))) {
            double[] dmin = fromWgs84(sr, minx, miny);
            double[] dmax = fromWgs84(sr, maxx, maxy);
            double[] dmin = GdalHelper.fromWgs84(sr, minx, miny);
            double[] dmax = GdalHelper.fromWgs84(sr, maxx, maxy);
            minx = dmin[0];
            miny = dmin[1];
            maxx = dmax[0];
@@ -302,32 +298,5 @@
    private String getEpsg(SpatialReference sr) {
        return sr.GetAuthorityCode(null);
    }
    /**
     * 转换为WGS84坐标
     */
    public static Geometry toWgs84(SpatialReference sr, double x, double y) {
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AssignSpatialReference(sr);
        point.AddPoint(x, y);
        point.TransformTo(GdalHelper.SR4326);
        //point.SwapXY();
        return point;
    }
    /**
     * WGS84转换为目标坐标
     */
    public static double[] fromWgs84(SpatialReference sr, double x, double y) {
        // https://blog.csdn.net/weixin_34910922/article/details/129208661
        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
        if (sr.IsProjected() != 1) {
            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        }
        return ct.TransformPoint(x, y);
    }
}
src/main/java/com/se/simu/service/SimuService.java
@@ -135,6 +135,18 @@
        return simuMapper.selectOne(wrapper);
    }
    public SimuPo getSimuByServiceName(String serviceName) {
        if (StringHelper.isEmpty(serviceName)) {
            return null;
        }
        QueryWrapper<SimuPo> wrapper = new QueryWrapper<>();
        wrapper.eq("service_name", serviceName);
        wrapper.last("limit 1");
        return simuMapper.selectOne(wrapper);
    }
    public boolean create(CreateSimuVo vo) {
        Date now = new Date();
        String date = StringHelper.YMDHMS2_FORMAT.format(now);
@@ -147,6 +159,7 @@
        initPath(data);
        SimuPo simu = new SimuPo(vo.getNum(), vo.getPid(), vo.getName(), JSONUtil.toJsonStr(data), 0, vo.getBak());
        simu.setServiceName(date);
        simu.setCreateTime(new Timestamp(now.getTime()));
        int rows = simuMapper.insert(simu);
src/main/java/com/se/simu/service/WaterService.java
@@ -1,13 +1,17 @@
package com.se.simu.service;
import cn.hutool.json.JSONUtil;
import com.se.simu.config.PropertiesConfig;
import com.se.simu.domain.po.DataPo;
import com.se.simu.domain.po.SimuPo;
import com.se.simu.domain.vo.*;
import com.se.simu.helper.GdalHelper;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.gdalconst.gdalconst;
import org.gdal.osr.SpatialReference;
import org.gdal.osr.osr;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -25,6 +29,7 @@
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
public class WaterService {
    @Resource
    PropertiesConfig config;
@@ -123,64 +128,73 @@
    }
    /**
     * 根据坐标查询积水深度
     * 根据坐标查询积水深度:gdalconst.GA_Update
     */
    public Double getWaterHeight(String serviceName, Double x, Double y, Long timestamp) {
        String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "waters"
    public Double getWaterHeight(SimuPo simu, double x, double y, Long timestamp) {
        String filePath = config.getOutPath() + File.separator + simu.getServiceName() + File.separator + "waters"
                + File.separator + timestamp + File.separator + "water.tif";
        // 首先是所有gdal程序都需要的注册语句
        gdal.AllRegister();
        // 读取影像
        Dataset hDataset = gdal.Open(filePath, gdalconstConstants.GA_Update);
        Dataset ds = null;
        try {
            ds = gdal.Open(filePath, gdalconst.GA_ReadOnly);
            if (null == ds || ds.getRasterCount() < 1) {
                return null;
            }
            if (null == ds.GetSpatialRef()) {
                ds.SetSpatialRef(getSpatialRef(simu));
            }
        // 设置数据集的投影
        SpatialReference srs = new SpatialReference();
        srs.ImportFromEPSG(4548);
        hDataset.SetProjection(srs.ExportToWkt());
            double[] gt = ds.GetGeoTransform();
            double[] xy = GdalHelper.fromWgs84(ds.GetSpatialRef(), x, y);
            int[] XY = coordinates2ColRow(gt, xy[0], xy[1]);
        //获取栅格数量
        int numBands=hDataset.GetRasterCount();
        System.out.println("RasterCount: " + numBands);
        //构造仿射变换参数数组,并获取数据
        double[] gt = new double[6];
        hDataset.GetGeoTransform(gt);
        System.out.println("仿射变换参数"+ Arrays.toString(gt));
            if (XY[0] < 0 || XY[1] < 0 || XY[0] > ds.getRasterXSize() || XY[1] > ds.getRasterYSize()) {
                return null;
            }
        //经纬度转换为栅格像素坐标
        int[] ColRow=Coordinates2ColRow(gt,x,y);
            double[] values = new double[1];
            ds.GetRasterBand(1).ReadRaster(XY[0], XY[1], 1, 1, values);
            double val = values[0];
        //判断是否坐标超出范围
        if(ColRow[0]<0||ColRow[1]<0||ColRow[0]>hDataset.getRasterXSize()||ColRow[1]>hDataset.getRasterYSize()){
            System.out.println(Arrays.toString(ColRow)+"坐标值超出栅格范围!");
            return isValid(val) ? val : null;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        } finally {
            if (null != ds) ds.delete();
        }
    }
        //获取该点对应的波段的值
        Band band = hDataset.GetRasterBand(1);
        double[] values = new double[1];
        band.ReadRaster(ColRow[0], ColRow[1], 1, 1, values);
        double value = values[0];
    private SpatialReference getSpatialRef(SimuPo simu) {
        DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class);
        //释放资源
        hDataset.delete();
        return value;
        SpatialReference sr = new SpatialReference();
        sr.ImportFromEPSG(data.getEpsg());
        sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        return sr;
    }
    /**
     * 将地图坐标转换为栅格像素坐标
     *
     * @param gt 仿射变换参数
     * @param X 横坐标
     * @param Y 纵坐标
     * @return
     * @param x  横坐标
     * @param y  纵坐标
     * @return 像素坐标
     */
    public int[] Coordinates2ColRow(double[] gt, double X, double Y){
    public int[] coordinates2ColRow(double[] gt, double x, double y) {
        int[] ints = new int[2];
        //向下取整,如果向上取整会导致计算结果偏大,从而在后面读取到邻近像元的数据
        double  Yline = Math.floor(((Y - gt[3])*gt[1] - (X-gt[0])*gt[4]) / (gt[5]*gt[1]-gt[2]*gt[4]));
        double  Xpixel = Math.floor((X-gt[0] - Yline*gt[2])/gt[1]);
        ints[0] = new Double(Xpixel).intValue();
        ints[1] = new Double(Yline).intValue();
        return ints;
        // 向下取整,如果向上取整会导致计算结果偏大,从而在后面读取到邻近像元的数据
        //Double col = Math.floor(((y - gt[3]) * gt[1] - (x - gt[0]) * gt[4]) / (gt[5] * gt[1] - gt[2] * gt[4]));
        Double col = Math.floor((y * gt[1] - x * gt[4] + gt[0] * gt[4] - gt[3] * gt[1]) / (gt[5] * gt[1] - gt[2] * gt[4]));
        Double row = Math.floor((x - gt[0] - col * gt[2]) / gt[1]);
        return new int[]{row.intValue(), col.intValue()};
    }
    public static boolean isValid(double val) {
        return !Double.isNaN(val) && val > Integer.MIN_VALUE;
    }
}