xingjinshuang
2025-02-20 0890b7861feae74bdcfd1851e577db6b9f31d484
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,6 +19,8 @@
import org.gdal.gdal.WarpOptions;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
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;
@@ -32,12 +35,6 @@
import java.util.*;
import java.util.stream.Collectors;
/**
 * GEDB服务类
 *
 * @author WWW
 * @date   2024-09-12
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
@@ -49,6 +46,10 @@
    @Resource
    RestTemplate restTemplate;
    private final static String DB_KEY = "gedb_db";
    private final static String TOKEN_KEY = "gedb_token";
    public boolean test(DataPo data) throws Exception {
        createPath(config.getInPath() + File.separator + data.getInPath());
@@ -72,11 +73,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");
@@ -93,7 +109,7 @@
        return password;
    }
    private String getPublicKey() {
    public String getPublicKey() {
        //{"datetime":"2024-09-12 17:24:38","code":200,"data":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtFwJCh2taVTEi05C8qT2oG7Y+rDmJhlO4zicpSeRtiro9LsytePeWI7BXM6sfDU0WeKun1izawcfgGkZgnoJuMBluAOKI1tL0uCrR+DreNLqMVtnXHwoWEIk/hGJedDWaf3q22aGDyEB5h9qCq0JklSShP1Ih4ppap4LmgxdTPQIDAQAB"}
        JSONObject obj = restTemplate.getForObject(config.getHost() + "account-service/security/publickey", JSONObject.class);
@@ -101,24 +117,32 @@
    }
    public GeDb connectGedb(String token, DataPo data) {
        GeDb db = getSeDb(token);
        GeDb db = getGeDb(token);
        db.setBbox(data.getBbox());
        data.setEpsg(db.getEpsg());
        return db;
    }
    private GeDb getSeDb(String token) {
    public GeDb getGeDb(String token) {
        Object obj = CaffeineHelper.get(DB_KEY);
        if (obj instanceof GeDb) {
            return (GeDb) obj;
        }
        Map<String, Object> map = new HashMap<>(1);
        map.put("token", token);
        JSONObject obj = restTemplate.postForObject(config.getHost() + "geo-service/entitydb/list/canview", map, JSONObject.class);
        JSONArray data = obj.getJSONArray("data");
        JSONObject jsonObject = restTemplate.postForObject(config.getHost() + "geo-service/entitydb/list/canview", map, JSONObject.class);
        JSONArray data = jsonObject.getJSONArray("data");
        List<GeDb> list = JSONUtil.toList(data, GeDb.class);
        if (CollectionUtils.isEmpty(list)) return null;
        return list.stream().filter(db -> null != db.getName() && db.getName().contains(config.getDbName())).findFirst().orElse(null);
        GeDb gedb = list.stream().filter(db -> null != db.getName() && db.getName().contains(config.getDbName())).findFirst().orElse(null);
        if (null != gedb) CaffeineHelper.put(DB_KEY, gedb);
        return gedb;
    }
    public List<GeLayer> getLayers(String token, GeDb db) {
@@ -147,7 +171,7 @@
        return layers;
    }
    private String getQueryType(JSONObject jb) {
    public static String getQueryType(JSONObject jb) {
        String qt = jb.getStr("pointlod");
        if (!StringHelper.isEmpty(qt)) {
            return "point";
@@ -174,6 +198,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) {
@@ -216,21 +249,104 @@
        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 = basePath + File.separator + layer.getShpName();
            if (!ShpHelper.createShp(path, layer)) {
            if (layer.getData().isEmpty() || !ShpHelper.createShp(path, layer)) {
                throw new Exception(layer.getName() + ",创建ShapeFile文件失败!");
            }
            // 管网集水点
            if (layer.getShpName().equals(config.getShpNames().get(0))) {
                GeLayer juncLayer = new GeLayer(layer, filterLayerData(layer.getData()));
                if (!ShpHelper.createShp(basePath + File.separator + config.getJunctionName(), juncLayer)) {
                    throw new Exception("集水点" + ",创建ShapeFile文件失败!");
                }
            }
        }
    }
@@ -248,6 +364,20 @@
        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());