13693261870
2024-11-07 c63f5c032bee42eb339cbbd95c8cee4f7132cf7e
src/main/java/com/se/simu/service/GedbService.java
@@ -10,6 +10,7 @@
import com.se.simu.domain.dto.GeFile;
import com.se.simu.domain.dto.GeLayer;
import com.se.simu.domain.po.DataPo;
import com.se.simu.helper.CaffeineHelper;
import com.se.simu.helper.RsaHelper;
import com.se.simu.helper.ShpHelper;
import com.se.simu.helper.StringHelper;
@@ -18,7 +19,8 @@
import org.gdal.gdal.WarpOptions;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.springframework.beans.factory.annotation.Value;
import org.gdal.ogr.Geometry;
import org.gdal.osr.SpatialReference;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;
@@ -51,6 +53,8 @@
    @Resource
    RestTemplate restTemplate;
    private final static String TOKEN_KEY = "gedb_token";
    public boolean test(DataPo data) throws Exception {
        createPath(config.getInPath() + File.separator + data.getInPath());
@@ -73,11 +77,26 @@
    }
    public String getToken() throws Exception {
        Object obj = CaffeineHelper.get(TOKEN_KEY);
        if (obj instanceof String) {
            return obj.toString();
        }
        String token = getTokenByServer();
        if (null == token) throw new Exception("获取GEDB令牌失败");
        CaffeineHelper.put(TOKEN_KEY, token);
        return token;
    }
    private String getTokenByServer() throws Exception {
        Map<String, Object> map = new HashMap<>(2);
        map.put("userid", config.getUser());
        map.put("password", getPassword());
        JSONObject obj = restTemplate.postForObject(config.getHost() + "account-service/security/login", map, JSONObject.class);
        log.info(obj.toString());
        JSONObject data = obj.getJSONObject("data");
@@ -177,6 +196,15 @@
        }
    }
    public boolean queryBboxCount(String token, GeDb db, List<GeLayer> layers) {
        for (GeLayer layer : layers) {
            int count = getCount(token, db, layer);
            if (0 == count) return false;
        }
        return true;
    }
    private int getCount(String token, GeDb db, GeLayer layer) {
        Map<String, Object> map = new HashMap<>(6);
        map.put("token", token);
@@ -217,18 +245,135 @@
        List<GeLayer> layers = getLayers(token, db);
        queryData(token, db, layers);
        checkData(data, db, layers);
        createShps(basePath, layers);
        createZoneShp(basePath, data, db.getSpatialReference());
        if (data.getPid() > 0) {
            createFloodShp(basePath, data, db.getSpatialReference());
        }
    }
    private void checkData(DataPo data, GeDb db, List<GeLayer> layers) {
        GeLayer point = getLayerByName(layers, config.getLayerNames().get(0));
        GeLayer line = getLayerByName(layers, config.getLayerNames().get(1));
        GeLayer build = getLayerByName(layers, config.getLayerNames().get(2));
        Geometry extent = ShpHelper.createPolygon(db.getSpatialReference(), data.getMinx(), data.getMiny(), data.getMaxx(), data.getMaxy());
        checkSpatialRange(extent, point);
        checkSpatialRange(extent, build);
        List<String> bsm = getValues(point, "bsm");
        List<String> bsm2 = new ArrayList<>(bsm);
        List<String> qdbsm = getValues(line, "qdbsm");
        List<String> qdbsm2 = new ArrayList<>(qdbsm);
        List<String> zdbsm = getValues(line, "zdbsm");
        List<String> zdbsm2 = new ArrayList<>(zdbsm);
        qdbsm.removeAll(bsm2);
        zdbsm.removeAll(bsm2);
        removeValues(line, "qdbsm", qdbsm);
        removeValues(line, "zdbsm", zdbsm);
        qdbsm = getValues(line, "qdbsm");
        zdbsm = getValues(line, "zdbsm");
        bsm.removeAll(qdbsm);
        bsm.removeAll(zdbsm);
        removeValues(point, "bsm", bsm);
        GeLayer juncLayer = new GeLayer(point, filterLayerData(point.getData()));
        juncLayer.setName("集水点");
        juncLayer.setShpName(config.getJunctionName());
        layers.add(juncLayer);
    }
    private void checkSpatialRange(Geometry extent, GeLayer geLayer) {
        int i = 0;
        while (i < geLayer.getData().size()) {
            JSONObject geom = geLayer.getData().getJSONObject(i).getJSONObject("geometry");
            Geometry g = ShpHelper.createGeometry(geLayer, geom);
            g.AssignSpatialReference(extent.GetSpatialReference());
            if (!extent.Intersects(g)) {
                geLayer.getData().remove(i);
                continue;
            }
            i++;
        }
    }
    private GeLayer getLayerByName(List<GeLayer> layers, String name) {
        return layers.stream().filter(a -> a.getName().equals(name)).findFirst().orElse(null);
    }
    private List<String> getValues(GeLayer layer, String field) {
        JSONArray data = layer.getData();
        List<String> list = new ArrayList<>();
        int i = 0;
        while (i < data.size()) {
            JSONObject obj = data.getJSONObject(i).getJSONObject("properties");
            if (StringHelper.isEmpty(obj.getStr(field))) {
                data.remove(i);
                continue;
            }
            list.add(obj.getStr(field));
            i++;
        }
        return list;
    }
    private void removeValues(GeLayer layer, String field, List<String> values) {
        if (CollectionUtils.isEmpty(values)) return;
        int i = 0;
        JSONArray data = layer.getData();
        while (i < data.size()) {
            JSONObject obj = data.getJSONObject(i).getJSONObject("properties");
            if (values.contains(obj.getStr(field))) {
                data.remove(i);
                continue;
            }
            i++;
        }
    }
    private void createShps(String basePath, List<GeLayer> layers) throws Exception {
        for (GeLayer layer : layers) {
            String path = String.format("%s\\%s.shp", basePath, layer.getShpName());
            if (!ShpHelper.createShp(path, layer)) {
            String path = basePath + File.separator + layer.getShpName();
            if (layer.getData().isEmpty() || !ShpHelper.createShp(path, layer)) {
                throw new Exception(layer.getName() + ",创建ShapeFile文件失败!");
            }
        }
    }
    private JSONArray filterLayerData(JSONArray data) {
        JSONArray arr = new JSONArray();
        String[] strs = config.getJunctionFilter().split("=");
        for (int i = 0, c = data.size(); i < c; i++) {
            JSONObject obj = data.getJSONObject(i).getJSONObject("properties");
            if (strs[1].equals(obj.getStr(strs[0]))) {
                arr.put(data.getJSONObject(i));
            }
        }
        return arr;
    }
    private void createZoneShp(String basePath, DataPo data, SpatialReference sr) {
        String filePath = basePath + File.separator + config.getZoneName();
        ShpHelper.createShp(filePath, null, sr, data.getMinx(), data.getMiny(), data.getMaxx(), data.getMaxy());
    }
    private void createFloodShp(String basePath, DataPo data, SpatialReference sr) {
        String filePath = basePath + File.separator + config.getBarrierName();
        Map<String, Object> map = new HashMap<>();
        map.put("height", data.getFloodHeight());
        map.put("type", data.getFloodType());
        ShpHelper.createShp(filePath, map, sr, data.getFloodMinx(), data.getFloodMiny(), data.getFloodMaxx(), data.getFloodMaxy());
    }
    public void copeDem(String token, DataPo data) throws Exception {
        GeDb fileDb = getFileDb(token);
        String fileId = getFileId(token, fileDb.getDbid());