xingjinshuang
2025-02-20 0890b7861feae74bdcfd1851e577db6b9f31d484
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package com.se.simu.service;
 
import cn.hutool.core.io.FileUtil;
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;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Dataset;
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;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.stream.Collectors;
 
@Slf4j
@Service
@SuppressWarnings("ALL")
public class GedbService {
    String password;
 
    @Resource
    PropertiesConfig config;
 
    @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());
 
        String token = getToken();
        GeDb db = connectGedb(token, data);
 
        copeVectors(token, data, db);
 
        copeDem(token, data);
 
        return true;
    }
 
    private void createPath(String path) {
        File f = new File(path);
        if (f.exists() && f.isDirectory()) {
            FileUtil.del(f);
        }
        f.mkdirs();
    }
 
    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");
 
        return data.getStr("token");
    }
 
    private String getPassword() throws Exception {
        if (StringHelper.isEmpty(password)) {
            String key = getPublicKey();
            RsaHelper.setPublicKey(key);
            password = RsaHelper.encrypt(config.getPwd());
        }
 
        return password;
    }
 
    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);
 
        return obj.getStr("data");
    }
 
    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 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;
 
        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", config.getHost(), db.getDbid(), token);
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
 
        JSONObject data = obj.getJSONObject("data");
        JSONArray arr = data.getJSONArray("layers");
        if (null == arr || arr.size() == 0) return null;
 
        List<GeLayer> layers = new ArrayList<>();
        for (int i = 0, c = arr.size(); i < c; i++) {
            JSONObject jb = arr.getJSONObject(i);
            String name = jb.getStr("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 -> !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));
            }
        }
 
        return layers;
    }
 
    public static String getQueryType(JSONObject jb) {
        String qt = jb.getStr("pointlod");
        if (!StringHelper.isEmpty(qt)) {
            return "point";
        }
 
        qt = jb.getStr("polylinelod");
        if (!StringHelper.isEmpty(qt)) {
            return "polyline";
        }
 
        return "polygon";
    }
 
    public void queryData(String token, GeDb db, List<GeLayer> layers) throws Exception {
        for (GeLayer layer : layers) {
            int count = getCount(token, db, layer);
            if (0 == count) throw new Exception(layer.getName() + ",图层数据为空");
 
            int pageCount = (count - 1) / config.getPageSize() + 1;
            for (int i = 0; i < pageCount; i++) {
                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) {
        Map<String, Object> map = new HashMap<>(6);
        map.put("token", token);
        map.put("dbid", db.getDbid());
        map.put("bbox", db.getBbox());
        map.put("layerid", layer.getId());
        map.put("returnCountOnly", true);
        map.put("inSR", 4326);
 
        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");
    }
 
    private JSONArray query(String token, GeDb db, GeLayer layer, int start, int count) {
        Map<String, Object> map = new HashMap<>(9);
        map.put("token", token);
        map.put("start", start);
        map.put("count", count);
        map.put("dbid", db.getDbid());
        map.put("bbox", db.getBbox());
        map.put("containCount", false);
        map.put("layerid", layer.getId());
        map.put("querytype", layer.getQueryType());
        map.put("inSR", 4326);
 
        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");
 
        return data.getJSONArray("features");
    }
 
    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 = 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());
        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(config.getDbName())).findFirst().orElse(null);
    }
 
    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=",
                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 (config.getDemName().equals(jb.getStr("filename"))) {
                return jb.getStr("fileid");
            }
        }
 
        return null;
    }
 
    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=",
                config.getHost(), token, dbid, fileId);
 
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
        JSONArray data = obj.getJSONArray("data");
 
        return JSONUtil.toList(data, GeFile.class);
    }
 
    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();
        }
 
        for (GeFile geFile : files) {
            String filePath = path + File.separator + geFile.getName();
            f = new File(filePath);
            if (f.exists() && f.length() == geFile.getSize()) {
                continue;
            }
            if (f.exists() && f.length() < geFile.getSize()) {
                f.delete();
            }
 
            String uri = String.format("%sfile-service/fileparser/cluster/download/%s?token=%s&dbid=%s&cluster_fileid=%s",
                    config.getHost(), geFile.getName(), token, dbid, fileId);
            downloadFile(uri, filePath);
        }
    }
 
    private void downloadFile(String uri, String filePath) throws IOException {
        URL url = new URL(uri);
        URLConnection conn = url.openConnection();
        InputStream is = conn.getInputStream();
 
        byte[] buffer = new byte[1024];
        FileOutputStream fs = new FileOutputStream(filePath);
 
        int read = 0, sum = 0;
        while ((read = is.read(buffer)) != -1) {
            sum += read;
            fs.write(buffer, 0, read);
        }
 
        fs.flush();
        fs.close();
        is.close();
    }
 
    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(config.getDemType())) {
                String source = filePath + File.separator + file.getName();
                clipDem(source, target, data);
                break;
            }
        }
    }
 
    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";
            Vector<String> vector = new Vector<>();
            //vector.add("-s_srs");
            //vector.add("EPSG:" + 4326);
            vector.add("-t_srs");
            vector.add("EPSG:" + data.getEpsg());
            vector.add("-r");
            vector.add("bilinear");
            vector.add("-of");
            vector.add("GTiff");
            vector.add("-te");
            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 {
            if (null != ds) ds.delete();
        }
    }
}