xingjinshuang
2025-02-20 0890b7861feae74bdcfd1851e577db6b9f31d484
src/main/java/com/se/simu/service/WaterService.java
@@ -4,9 +4,9 @@
import cn.hutool.json.JSONUtil;
import com.se.simu.config.PropertiesConfig;
import com.se.simu.domain.po.DataPo;
import com.se.simu.domain.po.PondingPo;
import com.se.simu.domain.po.SimuPo;
import com.se.simu.domain.vo.*;
import com.se.simu.domain.vo.Layer;
import com.se.simu.helper.GdalHelper;
import com.se.simu.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
@@ -21,17 +21,9 @@
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 内涝服务类
 *
 * @author WWW
 * @date   2024-07-16
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
@@ -39,9 +31,6 @@
    @Resource
    PropertiesConfig config;
    /**
     * 获取元数据信息
     */
    public byte[] getson(String serviceName, String json) {
        try {
            String filePath = config.getOutPath() + File.separator + serviceName + File.separator + json;
@@ -64,54 +53,18 @@
        }
    }
    /**
     * 获取地形高度图
     */
    public String getTerraMap(String serviceName, Integer width, Integer height) {
        return config.getOutPath() + File.separator + serviceName + File.separator + "terrain" + File.separator + width + "_" + height + ".png";
    }
    /**
     * 获取水面高度图
     */
    public String getWaterMap(String serviceName, Integer width, Integer height, Long timestamp) {
        return config.getOutPath() + File.separator + serviceName + File.separator + "waters" + File.separator + timestamp + File.separator + width + "_" + height + ".png";
    }
    /**
     * 获取水流向流速图
     */
    public String getFlowMap(String serviceName, Integer width, Integer height, Long timestamp) {
        return config.getOutPath() + File.separator + serviceName + File.separator + "flows" + File.separator + timestamp + File.separator + width + "_" + height + ".png";
    }
    /**
     * 获取图层 *
     */
    public Layer getLayer(String serviceName) {
        Layer layer = new Layer();
        layer.setVersion(config.getVer());
        layer.setDuration(new Duration(1719812810225L, 1719812810225L));
        layer.setExtension(new Extension(2.11062743358, 0.53812160220, 2.11070827834, 0.53819799453, 1.151, 38.83));
        List<Integer[]> sizes = new ArrayList<>();
        sizes.add(new Integer[]{64, 64});
        sizes.add(new Integer[]{128, 128});
        sizes.add(new Integer[]{256, 256});
        sizes.add(new Integer[]{512, 512});
        sizes.add(new Integer[]{1024, 1024});
        sizes.add(new Integer[]{2048, 2048});
        layer.setTerrain(new Terrain(sizes));
        List<Long> data = new ArrayList<>(Arrays.asList(1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L));
        layer.setWaters(new Water(data));
        return layer;
    }
    /**
     * 根据坐标查询积水深度:gdalconst.GA_Update
     */
    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";
@@ -153,16 +106,7 @@
        return data.getSpatialReference();
    }
    /**
     * 将地图坐标转换为栅格像素坐标
     *
     * @param gt 仿射变换参数
     * @param x  横坐标
     * @param y  纵坐标
     * @return 像素坐标
     */
    public int[] coordinates2ColRow(double[] gt, double x, double y) {
        // 向下取整,如果向上取整会导致计算结果偏大,从而在后面读取到邻近像元的数据
        //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]);
@@ -175,6 +119,40 @@
    }
    public Double getWaterArea(SimuPo simu, double x, double y, Long timestamp) {
        List<PondingPo> list = readWaterJson(simu.getServiceName(), timestamp);
        if (CollectionUtils.isEmpty(list)) return null;
        //DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class);
        //SpatialReference sr = GdalHelper.createSpatialReference(data.getEpsg());
        Geometry p = new Geometry(ogr.wkbPoint);
        p.AddPoint_2D(x, y);
        p.AssignSpatialReference(GdalHelper.SR4326);
        for (PondingPo po : list) {
            if (StringHelper.isEmpty(po.getPolygon())) continue;
            Geometry polygon = Geometry.CreateFromWkt(po.getPolygon());
            polygon.AssignSpatialReference(GdalHelper.SR4326);
            if (polygon.Contains(p)) {
                return po.getArea();
            }
        }
        return null;
    }
    private List<PondingPo> readWaterJson(String serviceName, Long timestamp) {
        String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "waters" + File.separator + timestamp + File.separator + "water.json";
        String json = getText(filePath);
        if (StringHelper.isEmpty(json)) {
            return null;
        }
        return JSONUtil.toList(json, PondingPo.class);
    }
    public Double getWaterArea2(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.geojson";
        if (!FileUtil.exist(filePath)) return null;
@@ -217,9 +195,6 @@
        }
    }
    /**
     * 根据seid查询建筑物涉水深度
     */
    public List<BuildingDepthVo> getBuildingDepthBySeid(String serviceName, String seid) {
        List<BuildingDepthVo> list = readBuildingJson(serviceName);
        if (CollectionUtils.isEmpty(list)) return null;