From 0890b7861feae74bdcfd1851e577db6b9f31d484 Mon Sep 17 00:00:00 2001
From: xingjinshuang <xingjs@qq.com>
Date: 星期四, 20 二月 2025 14:40:39 +0800
Subject: [PATCH] @xingjs@20250220@添加处理sww相关接口类

---
 src/main/java/com/se/simu/service/GedbService.java |  347 +++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 239 insertions(+), 108 deletions(-)

diff --git a/src/main/java/com/se/simu/service/GedbService.java b/src/main/java/com/se/simu/service/GedbService.java
index 29e086e..9a82084 100644
--- a/src/main/java/com/se/simu/service/GedbService.java
+++ b/src/main/java/com/se/simu/service/GedbService.java
@@ -4,10 +4,13 @@
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
+import com.se.simu.config.PropertiesConfig;
 import com.se.simu.domain.dto.GeDb;
 import com.se.simu.domain.dto.GeField;
 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;
@@ -16,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;
@@ -31,82 +35,37 @@
 import java.util.*;
 import java.util.stream.Collectors;
 
-/**
- * GEDB鏈嶅姟绫�
- *
- * @author WWW
- * @date   2024-09-12
- */
 @Slf4j
 @Service
 @SuppressWarnings("ALL")
 public class GedbService {
-    @Value("${sys.path.in}")
-    String inPath;
-
-    @Value("${gedb.host}")
-    String host;
-
-    @Value("${gedb.user}")
-    String user;
-
-    @Value("${gedb.pwd}")
-    String pwd;
-
-    @Value("${gedb.dbName}")
-    String dbName;
-
-    @Value("${gedb.pageSize}")
-    Integer pageSize;
-
-    @Value("#{'${gedb.layerNames}'}")
-    List<String> layerNames;
-
-    @Value("#{'${gedb.shpNames}'}")
-    List<String> shpNames;
-
-    @Value("#{'${gedb.sysFields}'}")
-    List<String> sysFields;
-
-    @Value("${gedb.demFile}")
-    String demFile;
-
-    @Value("${gedb.demType}")
-    String demType;
-
-    @Value("${gedb.demName}")
-    String demName;
-
     String password;
+
+    @Resource
+    PropertiesConfig config;
 
     @Resource
     RestTemplate restTemplate;
 
-    public boolean test(String bbox, String taskName) throws Exception {
+    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());
+
         String token = getToken();
-        GeDb db = getSeDb(token);
-        db.setBbox(bbox);
+        GeDb db = connectGedb(token, data);
 
-        List<GeLayer> layers = getLayers(token, db);
-        queryData(token, db, layers);
+        copeVectors(token, data, db);
 
-        String basePath = inPath + File.separator + taskName;
-        createPath(basePath);
-        createShps(basePath, layers);
-
-        GeDb fileDb = getFileDb(token);
-        String fileId = getFileId(token, fileDb.getDbid());
-        List<GeFile> files = getFileNames(token, fileDb.getDbid(), fileId);
-
-        String filePath = inPath + File.separator + fileDb.getDbid();
-        downloadFiles(token, filePath, files, fileDb.getDbid(), fileId);
-        clipDemFile(filePath, files, basePath, bbox, 4528);
+        copeDem(token, data);
 
         return true;
     }
 
-    private void createPath(String basePath) {
-        File f = new File(basePath);
+    private void createPath(String path) {
+        File f = new File(path);
         if (f.exists() && f.isDirectory()) {
             FileUtil.del(f);
         }
@@ -114,22 +73,37 @@
     }
 
     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", user);
+        map.put("userid", config.getUser());
         map.put("password", getPassword());
 
-        JSONObject obj = restTemplate.postForObject(host + "account-service/security/login", map, JSONObject.class);
+        JSONObject obj = restTemplate.postForObject(config.getHost() + "account-service/security/login", map, JSONObject.class);
+        log.info(obj.toString());
 
         JSONObject data = obj.getJSONObject("data");
 
         return data.getStr("token");
     }
 
-    public String getPassword() throws Exception {
+    private String getPassword() throws Exception {
         if (StringHelper.isEmpty(password)) {
             String key = getPublicKey();
             RsaHelper.setPublicKey(key);
-            password = RsaHelper.encrypt(pwd);
+            password = RsaHelper.encrypt(config.getPwd());
         }
 
         return password;
@@ -137,26 +111,42 @@
 
     public String getPublicKey() {
         //{"datetime":"2024-09-12 17:24:38","code":200,"data":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtFwJCh2taVTEi05C8qT2oG7Y+rDmJhlO4zicpSeRtiro9LsytePeWI7BXM6sfDU0WeKun1izawcfgGkZgnoJuMBluAOKI1tL0uCrR+DreNLqMVtnXHwoWEIk/hGJedDWaf3q22aGDyEB5h9qCq0JklSShP1Ih4ppap4LmgxdTPQIDAQAB"}
-        JSONObject obj = restTemplate.getForObject(host + "account-service/security/publickey", JSONObject.class);
+        JSONObject obj = restTemplate.getForObject(config.getHost() + "account-service/security/publickey", JSONObject.class);
 
         return obj.getStr("data");
     }
 
-    public GeDb getSeDb(String token) {
+    public GeDb connectGedb(String token, DataPo data) {
+        GeDb db = getGeDb(token);
+        db.setBbox(data.getBbox());
+        data.setEpsg(db.getEpsg());
+
+        return db;
+    }
+
+    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(host + "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(dbName)).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) {
-        String uri = String.format("%sgeo-service/entitydb/map/config?dbid=%s&token=%s", host, db.getDbid(), token);
+        String uri = String.format("%sgeo-service/entitydb/map/config?dbid=%s&token=%s", config.getHost(), db.getDbid(), token);
         JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
 
         JSONObject data = obj.getJSONObject("data");
@@ -167,12 +157,12 @@
         for (int i = 0, c = arr.size(); i < c; i++) {
             JSONObject jb = arr.getJSONObject(i);
             String name = jb.getStr("name");
-            if (layerNames.contains(name)) {
+            if (config.getLayerNames().contains(name)) {
                 String id = jb.getStr("id");
                 String queryType = getQueryType(jb);
                 List<GeField> fields = JSONUtil.toList(jb.getJSONArray("fields"), GeField.class);
-                fields = fields.stream().filter(f -> !sysFields.contains(f.getName())).collect(Collectors.toList());
-                String shpName = shpNames.get(layerNames.indexOf(name));
+                fields = fields.stream().filter(f -> !config.getSysFields().contains(f.getName())).collect(Collectors.toList());
+                String shpName = config.getShpNames().get(config.getLayerNames().indexOf(name));
 
                 layers.add(new GeLayer(id, name, queryType, fields, shpName, db));
             }
@@ -181,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";
@@ -200,14 +190,23 @@
             int count = getCount(token, db, layer);
             if (0 == count) throw new Exception(layer.getName() + "锛屽浘灞傛暟鎹负绌�");
 
-            int pageCount = (count - 1) / pageSize + 1;
+            int pageCount = (count - 1) / config.getPageSize() + 1;
             for (int i = 0; i < pageCount; i++) {
-                JSONArray data = query(token, db, layer, i + 1, pageSize);
+                JSONArray data = query(token, db, layer, i + 1, config.getPageSize());
                 if (null != data && data.size() > 0) {
                     layer.addData(data);
                 }
             }
         }
+    }
+
+    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) {
@@ -219,7 +218,7 @@
         map.put("returnCountOnly", true);
         map.put("inSR", 4326);
 
-        JSONObject obj = restTemplate.postForObject(host + "geo-service/entitydbdata/layer/query", map, JSONObject.class);
+        JSONObject obj = restTemplate.postForObject(config.getHost() + "geo-service/entitydbdata/layer/query", map, JSONObject.class);
         if (null == obj || 200 != obj.getInt("code")) return 0;
 
         return obj.getInt("data");
@@ -237,7 +236,7 @@
         map.put("querytype", layer.getQueryType());
         map.put("inSR", 4326);
 
-        JSONObject obj = restTemplate.postForObject(host + "geo-service/entitydbdata/layer/query", map, JSONObject.class);
+        JSONObject obj = restTemplate.postForObject(config.getHost() + "geo-service/entitydbdata/layer/query", map, JSONObject.class);
         if (null == obj || 200 != obj.getInt("code")) return null;
 
         JSONObject data = obj.getJSONObject("data");
@@ -245,36 +244,171 @@
         return data.getJSONArray("features");
     }
 
-    public void createShps(String basePath, List<GeLayer> layers) throws Exception {
+    public void copeVectors(String token, DataPo data, GeDb db) throws Exception {
+        String basePath = config.getInPath() + File.separator + data.getInPath();
+
+        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() + "锛屽垱寤篠hapeFile鏂囦欢澶辫触锛�");
             }
         }
     }
 
-    public GeDb getFileDb(String token) {
-        String uri = String.format("%sfile-service/docdb/query/canview?token=%s", host, token);
+    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());
+        List<GeFile> files = getFileNames(token, fileDb.getDbid(), fileId);
+
+        String filePath = config.getInPath() + File.separator + fileDb.getDbid();
+        downloadFiles(token, filePath, files, fileDb.getDbid(), fileId);
+        clipDemFile(filePath, files, data);
+    }
+
+    private GeDb getFileDb(String token) {
+        String uri = String.format("%sfile-service/docdb/query/canview?token=%s", config.getHost(), token);
         JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
         JSONArray data = obj.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(dbName)).findFirst().orElse(null);
+        return list.stream().filter(db -> null != db.getName() && db.getName().contains(config.getDbName())).findFirst().orElse(null);
     }
 
-    public String getFileId(String token, String dbid) {
+    private String getFileId(String token, String dbid) {
         String uri = String.format("%sfile-service/doc/catagory/file/query?token=%s&dbid=%s&catagory=%s&count=%d&start=%d&like=",
-                host, token, dbid, "image", 9999, 1);
+                config.getHost(), token, dbid, "image", 9999, 1);
 
         JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
         JSONArray items = obj.getJSONObject("data").getJSONArray("items");
 
         for (int i = 0, c = items.size(); i < c; i++) {
             JSONObject jb = items.getJSONObject(i);
-            if (demName.equals(jb.getStr("filename"))) {
+            if (config.getDemName().equals(jb.getStr("filename"))) {
                 return jb.getStr("fileid");
             }
         }
@@ -282,9 +416,9 @@
         return null;
     }
 
-    public List<GeFile> getFileNames(String token, String dbid, String fileId) {
+    private List<GeFile> getFileNames(String token, String dbid, String fileId) {
         String uri = String.format("%sfile-service/doc/cluster/struct/list?token=%s&dbid=%s&cluster_fileid=%s&onlychild=true&folder_stairs=",
-                host, token, dbid, fileId);
+                config.getHost(), token, dbid, fileId);
 
         JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
         JSONArray data = obj.getJSONArray("data");
@@ -292,7 +426,7 @@
         return JSONUtil.toList(data, GeFile.class);
     }
 
-    public void downloadFiles(String token, String path, List<GeFile> files, String dbid, String fileId) throws IOException {
+    private void downloadFiles(String token, String path, List<GeFile> files, String dbid, String fileId) throws IOException {
         File f = new File(path);
         if (!f.exists() || !f.isDirectory()) {
             f.mkdirs();
@@ -309,7 +443,7 @@
             }
 
             String uri = String.format("%sfile-service/fileparser/cluster/download/%s?token=%s&dbid=%s&cluster_fileid=%s",
-                    host, geFile.getName(), token, dbid, fileId);
+                    config.getHost(), geFile.getName(), token, dbid, fileId);
             downloadFile(uri, filePath);
         }
     }
@@ -333,48 +467,45 @@
         is.close();
     }
 
-    private void clipDemFile(String filePath, List<GeFile> files, String basePath, String bbox, int epsg) throws Exception {
-        String target = basePath + File.separator + demName;
+    private void clipDemFile(String filePath, List<GeFile> files, DataPo data) throws Exception {
+        String target = config.getInPath() + File.separator + data.getInPath() + File.separator + config.getDemFile();
         for (GeFile file : files) {
-            if (file.getName().toLowerCase().endsWith(demType)) {
+            if (file.getName().toLowerCase().endsWith(config.getDemType())) {
                 String source = filePath + File.separator + file.getName();
-                clipDem(source, target, bbox, epsg);
+                clipDem(source, target, data);
                 break;
             }
         }
     }
 
-    private void clipDem(String source, String target, String bbox, int epsg) throws Exception {
+    private void clipDem(String source, String target, DataPo data) throws Exception {
         Dataset ds = null;
         try {
             ds = gdal.Open(source, gdalconst.GA_ReadOnly);
             if (null == ds || ds.getRasterCount() < 1 || null == ds.GetSpatialRef()) throw new Exception("DEM鏁版嵁鏃犳晥");
 
             // String bbox = "116.64388473935195,39.884315914604464,116.64754729082588,39.887069143903496";
-            String[] bb = bbox.split(",");
-
-            // gdalwarp -ot UInt16 -s_srs EPSG:4326 -t_srs EPSG:2382 -r bilinear -of GTiff -te 116.526854182 40.0481829856 116.532848182 40.0541769856
-            // -te_srs EPSG:4326 -co COMPRESS=DEFLATE -co PREDICTOR=1 -co ZLEVEL=6 -co TILED=YES -wo OPTIMIZE_SIZE=TRUE E:\GDALhomework\000002.tif E:/CSDN/warped1.tif
-
             Vector<String> vector = new Vector<>();
             //vector.add("-s_srs");
             //vector.add("EPSG:" + 4326);
             vector.add("-t_srs");
-            vector.add("EPSG:" + epsg);
+            vector.add("EPSG:" + data.getEpsg());
             vector.add("-r");
             vector.add("bilinear");
             vector.add("-of");
             vector.add("GTiff");
             vector.add("-te");
-            vector.add(bb[0]);
-            vector.add(bb[1]);
-            vector.add(bb[2]);
-            vector.add(bb[3]);
+            vector.add(data.getMinx().toString());
+            vector.add(data.getMiny().toString());
+            vector.add(data.getMaxx().toString());
+            vector.add(data.getMaxy().toString());
             vector.add("-te_srs");
             vector.add("EPSG:" + 4326);
-
             WarpOptions warpOptions = new WarpOptions(vector);
 
+            // gdalwarp -ot UInt16 -s_srs EPSG:4326 -t_srs EPSG:2382 -r bilinear -of GTiff -te 116.526854182 40.0481829856 116.532848182 40.0541769856
+            // -te_srs EPSG:4326 -co COMPRESS=DEFLATE -co PREDICTOR=1 -co ZLEVEL=6 -co TILED=YES -wo OPTIMIZE_SIZE=TRUE E:\GDALhomework\000002.tif E:/CSDN/warped1.tif
+
             Dataset destDs = gdal.Warp(target, new Dataset[]{ds}, warpOptions);
             destDs.delete();
         } finally {

--
Gitblit v1.9.3