张洋洋
2025-02-13 30a47805b1e3233827a6a2db4d97bfb7f6250384
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
package com.se.simu.controller;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.io.Resources;
import com.se.simu.config.PropertiesConfig;
import com.se.simu.domain.dto.GeDb;
import com.se.simu.domain.dto.GeLayer;
import com.se.simu.domain.dto.GridDto;
import com.se.simu.domain.po.DataPo;
import com.se.simu.domain.po.SimuPo;
import com.se.simu.domain.vo.*;
import com.se.simu.service.*;
import com.se.simu.utils.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriUtils;
 
import javax.annotation.Resource;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
 
@Api(tags = "仿真管理")
@Slf4j
@RestController
@RequestMapping("/simu")
@SuppressWarnings("ALL")
public class SimuController extends BaseController {
    @Resource
    UwService uwService;
 
    @Resource
    GedbService gedbService;
 
    @Resource
    SimuService simuService;
 
    @Resource
    SimuFilesService simuFilesService;
 
    @Resource
    PropertiesConfig config;
 
    @Value("${simu-app.filePath}")
    private String uploadedFolder;
 
    @Value("${config.outPath}")
    private String outPath;
 
    @Value("${config.inPath}")
    private String inPath;
 
 
    @Resource
    ResultService resultService;
 
    private final List<String> FLOOD_TYPE = new ArrayList<>(Arrays.asList("沙袋", "防水板"));
 
    @ApiOperation(value = "创建")
    @PostMapping(value = "/create", produces = "application/json; charset=UTF-8")
    public R<Object> create(@RequestBody @ApiParam("创建仿真视图类") CreateSimuVo vo) {
        try {
            if (null == vo.getTotal() || vo.getTotal() < 1 || vo.getTotal() > 1000) {
                return fail("降雨量不能为空,且取值在1~1000之间");
            }
            if (null == vo.getDuration() || vo.getDuration() < 1 || vo.getDuration() > 10080) {
                return fail("仿真时长不能为空,且取值在1~10080之间");
            }
            if (null == vo.getMinx() || null == vo.getMiny() || null == vo.getMaxx() || null == vo.getMaxy()) {
                return fail("选择范围不能为空", false);
            }
            if (null == vo.getPid() || vo.getPid() < 0) {
                vo.setPid(0);
            }
            if (null == vo.getNum() || vo.getNum() < 1) {
                vo.setNum(simuService.getMaxId() + 1);
            }
            if (null == vo.getPid() || vo.getPid() < 0) {
                vo.setPid(0);
            }
            if (null == vo.getStartTime()) {
                vo.setStartTime(new Date());
            }
            if (vo.getPid() > 0) {
                SimuPo pp = simuService.getSimuByPid(vo.getPid());
                if (null == pp) {
                    return fail("pid不存在");
                }
                if (null == vo.getFloodStart() || vo.getFloodStart() < 1 || vo.getFloodStart() > vo.getDuration() * 60) {
                    return fail("防汛开始时间不能为空,且取值在1~" + (vo.getDuration() * 60) + "之间");
                }
                if (null == vo.getFloodEnd() || vo.getFloodEnd() < vo.getFloodStart() || vo.getFloodEnd() > vo.getDuration() * 60) {
                    return fail("防汛结束时间不能为空,且取值在" + vo.getFloodStart() + "~" + (vo.getDuration() * 60) + "之间");
                }
                if (null == vo.getFloodHeight() || vo.getFloodHeight() < 1 || vo.getFloodHeight() > 2000) {
                    return fail("防汛高度不能为空,且取值在1~2000之间");
                }
                if (!FLOOD_TYPE.contains(vo.getFloodType())) {
                    return fail("防汛类型不能为空,且只能是沙袋和防水板");
                }
                if (null == vo.getFloodMinx() || null == vo.getFloodMiny() || null == vo.getFloodMaxx() || null == vo.getFloodMaxy()) {
                    return fail("防汛范围不能为空", false);
                }
            }
 
            boolean flag = simuService.create(vo);
 
            return success(flag, flag ? "成功" : "失败");
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID", dataType = "Integer", paramType = "query", allowMultiple = true, example = "0")
    })
    @GetMapping(value = "/del")
    public R<Object> del(@RequestParam List<Integer> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return fail("日志ID不能为空", null);
            }
 
            int count = simuService.del(ids);
 
            return success(count);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @ApiOperation(value = "获取")
    @GetMapping("/get")
    public R<Object> get(@ApiParam("仿真视图类") SimuVo vo) {
        try {
            if (null == vo) {
                vo = new SimuVo();
            }
            if (null == vo.getPageSize() || vo.getPageSize() < 1) {
                vo.setPageSize(10);
            }
            if (null == vo.getPageIndex() || vo.getPageIndex() < 1) {
                vo.setPageIndex(1);
            }
 
            IPage<SimuPo> paged = simuService.get(vo);
            if (null == paged) {
                return success(null, 0);
            }
 
            return success(paged.getRecords(), paged.getTotal());
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @ApiOperation(value = "降水范围校验")
    @GetMapping("/rangeVerify")
    public R<Object> rangeVerify(@RequestParam @ApiParam("最小X") double minx,
                                 @RequestParam @ApiParam("最小y") double miny,
                                 @RequestParam @ApiParam("最大X") double maxx,
                                 @RequestParam @ApiParam("最大y") double maxy) {
        try {
            DataPo data = new DataPo();
            data.setMinx(minx);
            data.setMiny(miny);
            data.setMaxx(maxx);
            data.setMaxy(maxy);
            data.setEpsg(4326);
 
            String token = gedbService.getToken();
            GeDb db = gedbService.connectGedb(token, data);
            List<GeLayer> layers = gedbService.getLayers(token, db);
            boolean result = gedbService.queryBboxCount(token, db, layers);
            log.info("result = " + result);
            return success(result);
 
            //  return success(gedbService.queryBboxCount(token, db, layers));
        } catch (Exception ex) {
            return fail(ex, false);
        }
    }
 
    /**
     * 文件创建模拟
     * <p>
     * "状态:
     * 0-创建仿真任务,
     * 1-连接GEDB库,
     * 2-下载空间数据,
     * 3-下载高程数据,
     * 4-生成降雨文件,
     * 5-生成配置文件,
     * 6-模拟内涝仿真,
     * 7-处理水位文件,
     * 8-处理排水文件,
     * 9-处理仿真结果,
     * 10-完成,-10-出错
     *
     * @param vo VO
     * @return {@link R}<{@link Object}>
     */
    @ApiOperation(value = "文件创建")
    @PostMapping(value = "/file_create", produces = "application/json; charset=UTF-8")
    public R<Object> fileCreate(@RequestBody @ApiParam("创建仿真视图类") CreateFilesSimuVo vo) throws IOException {
        // 获取上传文件路径
        String targetDir = uploadedFolder;
        log.info("上传文件路径:{}", targetDir);
        // 获取各个文件的地址
        String floodFile = vo.getFloodFile();
        log.info("范围文件地址:{}", floodFile);
        try {
            // 判断是否绘制区域
            if (!StringUtils.isEmpty(vo.getMaxx().toString()) && !Objects.isNull(vo.getMaxx())) {
                rangeVerify(vo.getMinx(), vo.getMaxx(), vo.getMiny(), vo.getMaxy());
                log.info("绘制区域范围验证通过!");
            } else
                // 判断地址不为空
                if (StringUtils.isEmpty(floodFile)) {
                    return fail("范围文件地址不能为空", false);
                } else {
                    // 获取文件的后缀名
                    String fileName = floodFile.substring(floodFile.lastIndexOf("."));
                    // 判断后缀名是否为.shp
                    if (!fileName.equalsIgnoreCase(".shp")) {
                        return fail("范围文件格式不正确", false);
                    } else {
                        // 1 读取shp文件,获取范围值
                        JSONObject jsonObject = ShpToolUtils.readShp(floodFile);
                        // 2 获取jsonObject中的范围值
                        Double minX = jsonObject.getDouble("minY");
                        Double maxX = jsonObject.getDouble("maxY");
                        Double minY = jsonObject.getDouble("minX");
                        Double maxY = jsonObject.getDouble("maxX");
                        // 3 判断范围值是否为空
                        // vo.setMinx(jsonObject.getDouble("minX"));
                        // vo.setMaxx(jsonObject.getDouble("maxX"));
                        // vo.setMiny(jsonObject.getDouble("minY"));
                        // vo.setMaxy(jsonObject.getDouble("maxY"));
                        vo.setMinx(jsonObject.getDouble("minY"));
                        vo.setMaxx(jsonObject.getDouble("maxY"));
                        vo.setMiny(jsonObject.getDouble("minX"));
                        vo.setMaxy(jsonObject.getDouble("maxX"));
                        rangeVerify(minX, maxX, minY, maxY);
                    }
                }
            // todo: 解析范围文件 根据不同的的格式文件进行不同的解析,获取需要计算的范围值
            //  上传格式:.shp/.tiff/.img/.geojson
            //  重新给vo赋值
        } catch (Exception e) {
            log.error("解析范围文件失败");
            if (null == vo.getMinx() || null == vo.getMiny() || null == vo.getMaxx() || null == vo.getMaxy()) {
                return fail("解析范围文件失败!选择范围不能为空,请重新选择文件!", false);
            }
        }
        JSONArray jsonArray = new JSONArray();
        String stationFile = vo.getStationFile();
        log.info("站点文件shp地址:{}", stationFile);
        try {
            // 判断地址不为空
            if (StringUtils.isEmpty(stationFile)) {
                return fail("站点文件shp地址不能为空", false);
            }
            // 1 读取shp文件,获取站点坐标值
            jsonArray = ShpToolUtils.readShpGetLocal(stationFile);
            System.out.println("jsonArray = " + jsonArray);
        } catch (Exception e) {
            log.error("解析站点文件shp失败");
        }
 
        String stationRainFile = vo.getStationRainFile();
        log.info("站点雨量CSV文件地址:{}", floodFile);
        JSONArray array = new JSONArray();
        // 创建表名 时间戳
        String tableName = "station_rain_" + System.currentTimeMillis();
        try {
            // 判断地址不为空
            if (StringUtils.isEmpty(stationRainFile)) {
                return fail("站点雨量CSV文件地址不能为空", false);
            }
            // 1 读取CSV 文件
            array = CsvToSQLiteUtils.readCsvSaveLocal(stationRainFile, tableName);
            // 获取仿真时间 duration
            Integer duration = CsvToSQLiteUtils.getDuration(tableName);
            log.info("仿真时间 duration = {}", duration);
            vo.setDuration(duration);
 
            // 获取降雨总量 total
            Double total = CsvToSQLiteUtils.getTotal(tableName);
            log.info("降雨总量 total = {}", total);
            vo.setTotal(total);
 
        } catch (Exception e) {
            log.error("解析站点雨量CSV文件失败");
        }
        // TODO: 2024/12/24 根据这些文件的地址,获取文件内容,创建仿真视图
        try {
            if (null == vo.getDuration() || vo.getDuration() < 1 || vo.getDuration() > 10080) {
                return fail("仿真时长不能为空,且取值在1~10080之间");
            }
            if (null == vo.getPid() || vo.getPid() < 0) {
                vo.setPid(0);
            }
            if (null == vo.getNum() || vo.getNum() < 1) {
                vo.setNum(simuService.getMaxId() + 1);
            }
            if (null == vo.getStartTime()) {
                vo.setStartTime(new Date());
            }
            // 防汛作业
            if (vo.getPid() > 0) {
                SimuPo pp = simuService.getSimuByPid(vo.getPid());
                if (null == pp) {
                    return fail("pid不存在");
                }
                if (null == vo.getFloodStart() || vo.getFloodStart() < 1 || vo.getFloodStart() > vo.getDuration() * 60) {
                    return fail("防汛开始时间不能为空,且取值在1~" + (vo.getDuration() * 60) + "之间");
                }
                if (null == vo.getFloodEnd() || vo.getFloodEnd() < vo.getFloodStart() || vo.getFloodEnd() > vo.getDuration() * 60) {
                    return fail("防汛结束时间不能为空,且取值在" + vo.getFloodStart() + "~" + (vo.getDuration() * 60) + "之间");
                }
                if (null == vo.getFloodHeight() || vo.getFloodHeight() < 1 || vo.getFloodHeight() > 2000) {
                    return fail("防汛高度不能为空,且取值在1~2000之间");
                }
                if (!FLOOD_TYPE.contains(vo.getFloodType())) {
                    return fail("防汛类型不能为空,且只能是沙袋和防水板");
                }
                if (null == vo.getFloodMinx() || null == vo.getFloodMiny() || null == vo.getFloodMaxx() || null == vo.getFloodMaxy()) {
                    return fail("防汛范围不能为空", false);
                }
            }
            // 开始模拟计算
            boolean flag = simuFilesService.createByfiles(vo);
            return success(flag, flag ? "成功" : "失败");
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @ApiOperation(value = "tarr文件组装")
    @GetMapping(value = "/saveZarr", produces = "application/json; charset=UTF-8")
    public R<String> saveZarr(@RequestParam("name") String tableName) throws Exception {
        List<String> list = CsvToSQLiteUtils.getNameList(tableName);
        String path = outPath + "\\";
        String rainfall = "rainfall\\";
        String basePath = path + tableName + "\\" + rainfall;
        JSONObject jsonObject = getModule("rainfallmodule.json");
        for (String src : list
        ) {
            File directories = new File(basePath);
            if (!directories.exists()) {
                directories.mkdirs();
                System.out.println("Directories created successfully.");
            } else {
                System.out.println("Directories already exist.");
            }
            List<StationRainVo> stationRainVos = CsvToSQLiteUtils.getList(tableName, src);
            ZarrUtils.saveZarrRainfall(basePath + src, stationRainVos);
            ZarrUtils.saveZarrTime(basePath + src, stationRainVos);
            ZipUtils.toZarr(basePath + src, basePath + src + ".zip");
            System.out.println(src + "的zarr数据生成=====================");
            //json拼装
            String uuid = "UUID_" + UUID.randomUUID().toString();
            //拼装zarr
            JSONObject dynamizer = new JSONObject();
            dynamizer.put("url", rainfall.replace("\\", "/") + src + ".zarr");
            dynamizer.put("gmlId", uuid);
            jsonObject.getJSONArray("Dynamizers").add(dynamizer);
            //拼装坐标
            JSONArray vertice = new JSONArray();
            vertice.add(stationRainVos.get(0).getLongitude());
            vertice.add(stationRainVos.get(0).getLatitude());
            vertice.add(0.0);
            jsonObject.getJSONArray("vertices").add(vertice);
            //拼装基础信息
            JSONObject cityObject = new JSONObject();
            cityObject.put("type", "+Rainfall");
            JSONObject attribute = new JSONObject();
            attribute.put("name", src);
            cityObject.put("attributes", attribute);
            JSONArray geometry = new JSONArray();
            JSONObject metry = new JSONObject();
            metry.put("type", "MultiPoint");
            metry.put("lod", 0);
            JSONArray boundarie = new JSONArray();
            boundarie.add(jsonObject.getJSONArray("vertices").size() - 1);
            metry.put("boundaries", boundarie);
            geometry.add(metry);
            cityObject.put("geometry", geometry);
            jsonObject.getJSONObject("CityObjects").put(uuid, cityObject);
        }
        File jsonFile = new File(path + tableName + "\\降雨量.json");
        if (jsonFile.exists()) {
            jsonFile.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(path + tableName + "\\降雨量.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + tableName + "\\降雨量.json", filePatn + "raingage.sem");
        return success(filePatn + "raingage.sem");
    }
 
    @ApiOperation(value = "管点转sem")
    @PostMapping(value = "/pointTosem", produces = "application/json; charset=UTF-8")
    public R<String> pointTosem(@RequestBody List<GridDto> dtos) throws Exception {
        String path = outPath + "\\point\\";
        String publicKey = EntityLibraryUtils.getPublicKey();
        String token = EntityLibraryUtils.login();
        JSONArray jsonArrayList = EntityLibraryUtils.getPointInfo(dtos, token);
        JSONObject jsonObject = getModule("pointmodule.json");
        for (int s = 0; s < jsonArrayList.size(); s++) {
            JSONObject pointObject = jsonArrayList.getJSONObject(s);
            //拼装坐标
            JSONArray vertice = new JSONArray();
            vertice.add(Double.valueOf(pointObject.getString("lon")));
            vertice.add(Double.valueOf(pointObject.getString("lat")));
            vertice.add(0.0);
            jsonObject.getJSONArray("vertices").add(vertice);
            //拼装基础信息
            JSONObject cityObject = new JSONObject();
            cityObject.put("type", "+PipePoint");
            JSONObject attribute = new JSONObject();
            attribute.put("维护时间", pointObject.get("operatetime"));
            attribute.put("维护人", pointObject.get("operator"));
            attribute.put("唯一编码", pointObject.get("seid"));
            attribute.put("ID", pointObject.get("id"));
            attribute.put("enti_uuid", pointObject.get("enti_uuid"));
            attribute.put("标识码", pointObject.get("bsm"));
            attribute.put("管线种类", pointObject.get("gxzl"));
            attribute.put("地面高程", pointObject.get("dmgc"));
            attribute.put("特征", pointObject.get("tz"));
            attribute.put("附属物", pointObject.get("fsw"));
            attribute.put("建筑物", pointObject.get("jzw"));
            attribute.put("井底高程", pointObject.get("jdgc"));
            attribute.put("井底埋深", pointObject.get("jdms"));
            attribute.put("井脖埋深", pointObject.get("jbms"));
            attribute.put("井盖形状", pointObject.get("jgxz"));
            attribute.put("井盖尺寸", pointObject.get("jgcc"));
            attribute.put("井盖材质", pointObject.get("jgcz"));
            attribute.put("井盖状态", pointObject.get("jgzt"));
            attribute.put("井脖材质", pointObject.get("jbcz"));
            attribute.put("井脖尺寸", pointObject.get("jbcc"));
            attribute.put("井材质", pointObject.get("jcz"));
            attribute.put("井尺寸", pointObject.get("jcc"));
            attribute.put("使用状况", pointObject.get("syzk"));
            attribute.put("lon", Double.valueOf(pointObject.getString("lon")));
            attribute.put("lat", Double.valueOf(pointObject.getString("lat")));
            attribute.put("atti", pointObject.get("atti"));
            attribute.put("雨水集结点", pointObject.get("ysjjd"));
            cityObject.put("attributes", attribute);
            JSONArray geometry = new JSONArray();
            JSONObject metry = new JSONObject();
            metry.put("type", "MultiPoint");
            metry.put("lod", 0);
            JSONArray boundarie = new JSONArray();
            boundarie.add(jsonObject.getJSONArray("vertices").size() - 1);
            metry.put("boundaries", boundarie);
            geometry.add(metry);
            cityObject.put("geometry", geometry);
            jsonObject.getJSONObject("CityObjects").put("UUID_" + UUID.randomUUID().toString(), cityObject);
        }
        long times = System.currentTimeMillis();
        String pointPath = path + times + "\\管点.json";
        File dirFile = new File(path + times);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        FileWriter fileWriter = new FileWriter(pointPath);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(pointPath, filePatn + "node.sem");
        return success(filePatn + "node.sem");
    }
    @ApiOperation(value = "管点转cityjson")
    @GetMapping(value = "/pointToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> pointToCityJson(@RequestParam("shpPath") String shpPath) throws Exception {
        String path = outPath + "\\point\\";
        List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath);
        JSONObject jsonObject = getModule("pointmodule.json");
        for (Map<String, Object> map : list
        ) {
            //拼装坐标
            JSONArray vertice = new JSONArray();
            vertice.add(map.get("lon"));
            vertice.add(map.get("lat"));
            vertice.add(0.0);
            jsonObject.getJSONArray("vertices").add(vertice);
            //拼装基础信息
            JSONObject cityObject = new JSONObject();
            cityObject.put("type", "+PipePoint");
            JSONObject attribute = new JSONObject();
            attribute.put("name", map.get("fsw"));
            cityObject.put("attributes", attribute);
            JSONArray geometry = new JSONArray();
            JSONObject metry = new JSONObject();
            metry.put("type", "MultiPoint");
            metry.put("lod", 0);
            JSONArray boundarie = new JSONArray();
            boundarie.add(jsonObject.getJSONArray("vertices").size() - 1);
            metry.put("boundaries", boundarie);
            geometry.add(metry);
            cityObject.put("geometry", geometry);
            jsonObject.getJSONObject("CityObjects").put("UUID_" + UUID.randomUUID().toString(), cityObject);
        }
        long times = System.currentTimeMillis();
        String pointPath = path + times + "\\管点.json";
        File dirFile = new File(path + times);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        FileWriter fileWriter = new FileWriter(pointPath);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(pointPath, filePatn + "node.sem");
        return success(filePatn + "node.sem");
    }
 
    @ApiOperation(value = "管线sem生成")
    @PostMapping(value = "/lineToSem", produces = "application/json; charset=UTF-8")
    public R<String> lineToSem(@RequestBody List<GridDto> dtos) throws Exception {
        String path = outPath + "\\line\\";
        String publicKey = EntityLibraryUtils.getPublicKey();
        String token = EntityLibraryUtils.login();
        JSONArray jsonArrayList = EntityLibraryUtils.getLineInfo(dtos, token);
        if (jsonArrayList.size() == 0) {
            return fail("该区域不存在管线!");
        }
        StringBuffer stringBuffer = new StringBuffer("id in (");
        for (int i = 0; i < jsonArrayList.size(); i++) {
            jsonArrayList.getJSONObject(i).getInteger("id");
            stringBuffer.append("'" + jsonArrayList.getJSONObject(i).getInteger("id") + "',");
        }
        String sql = stringBuffer.toString();
        sql = sql.substring(0, sql.length() - 1) + ")";
        StringBuffer paramBuffer = new StringBuffer("");
        while (!"".equals(sql)) {
            if (sql.length() > 110) {
                String item = sql.substring(0, 110);
                paramBuffer.append(EntityLibraryUtils.encrypt(item, publicKey) + ",");
                sql = sql.replace(item, "");
            } else {
                paramBuffer.append(EntityLibraryUtils.encrypt(sql, publicKey) + ",");
                sql = "";
            }
        }
        String src = paramBuffer.toString().substring(0, paramBuffer.length() - 1);
        JSONArray arrayDetail = EntityLibraryUtils.getLineDetail(src, token);
        JSONObject jsonObject = getModule("linemodule.json");
        for (int i = 0; i < arrayDetail.size(); i++) {
            JSONObject detailJSONObject = arrayDetail.getJSONObject(i);
            //拼装基础信息
            JSONObject cityObject = new JSONObject();
            cityObject.put("type", "+PipeLine");
            JSONObject attribute = new JSONObject();
            attribute.put("维护时间", detailJSONObject.getJSONObject("properties").getLong("operatetime"));
            attribute.put("维护人", detailJSONObject.getJSONObject("properties").getString("operator"));
            attribute.put("唯一编码", detailJSONObject.getJSONObject("properties").get("seid"));
            attribute.put("ENTI_UUID", detailJSONObject.getJSONObject("properties").get("enti_uuid"));
            attribute.put("ID", detailJSONObject.getJSONObject("properties").get("id"));
            attribute.put("lon", detailJSONObject.getJSONObject("properties").get("lon"));
            attribute.put("lat", detailJSONObject.getJSONObject("properties").get("lat"));
            attribute.put("atti", detailJSONObject.getJSONObject("properties").get("atti"));
            attribute.put("管线种类", detailJSONObject.getJSONObject("properties").get("gxzl"));
            attribute.put("起点标识码", detailJSONObject.getJSONObject("properties").get("qdbsm"));
            attribute.put("止点标识码", detailJSONObject.getJSONObject("properties").get("zdbsm"));
            attribute.put("起点高程", detailJSONObject.getJSONObject("properties").get("qdgc"));
            attribute.put("起高程类型", detailJSONObject.getJSONObject("properties").get("qgclx"));
            attribute.put("起点埋深", detailJSONObject.getJSONObject("properties").get("qdms"));
            attribute.put("止点高程", detailJSONObject.getJSONObject("properties").get("zdgc"));
            attribute.put("止高程类型", detailJSONObject.getJSONObject("properties").get("zgclx"));
            attribute.put("止点埋深", detailJSONObject.getJSONObject("properties").get("zdms"));
            attribute.put("材质", detailJSONObject.getJSONObject("properties").get("cz"));
            attribute.put("埋设方式", detailJSONObject.getJSONObject("properties").get("msfs"));
            attribute.put("管径1", detailJSONObject.getJSONObject("properties").get("gja"));
            attribute.put("管径2", detailJSONObject.getJSONObject("properties").get("gjb"));
            attribute.put("使用状况", detailJSONObject.getJSONObject("properties").get("syzk"));
            attribute.put("管线段长度", detailJSONObject.getJSONObject("properties").get("gxdcd"));
            attribute.put("groupID", detailJSONObject.getJSONObject("properties").get("groupid"));
            attribute.put("Shape_Length", detailJSONObject.getJSONObject("properties").get("shape_length"));
            attribute.put("标识码", detailJSONObject.getJSONObject("properties").get("bsm"));
            JSONArray geometry = new JSONArray();
            JSONObject metry = new JSONObject();
            metry.put("type", "MultiLineString");
            metry.put("lod", 0);
            JSONArray boundarie = new JSONArray();
            JSONArray array = detailJSONObject.getJSONObject("geometry").getJSONArray("coordinates").getJSONArray(0);
            for (int m = 0; m < array.size(); m++) {
                JSONArray object = array.getJSONArray(m);
                jsonObject.getJSONArray("vertices").add(ProjectionToGeographicUtil.getPoint(object.getDouble(0), object.getDouble(1)));
                boundarie.add(jsonObject.getJSONArray("vertices").size() - 1);
            }
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(boundarie);
            metry.put("boundaries", jsonArray);
            JSONArray metryArray = new JSONArray();
            metryArray.add(metry);
            cityObject.put("geometry", metryArray);
            cityObject.put("attributes", attribute);
            cityObject.put("attributes", attribute);
            geometry.add(metry);
            jsonObject.getJSONObject("CityObjects").put("UUID_" + UUID.randomUUID().toString(), cityObject);
        }
        long times = System.currentTimeMillis();
        String pointPath = path + times + "\\管线.json";
        File dirFile = new File(path + times);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        FileWriter fileWriter = new FileWriter(pointPath);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(pointPath, filePatn + "link.sem");
        return success(filePatn + "link.sem");
    }
 
    @ApiOperation(value = "管线转cityjson")
    @GetMapping(value = "/lineToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> lineToCityJson(@RequestParam("shpPath") String shpPath) throws Exception {
        String path = outPath + "\\line\\";
        List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath);
        JSONObject jsonObject = getModule("linemodule.json");
        for (Map<String, Object> map : list
        ) {
            //拼装基础信息
            JSONObject cityObject = new JSONObject();
            cityObject.put("type", "+PipeLine");
            JSONObject attribute = new JSONObject();
            attribute.put("name", map.get("msfs"));
            JSONArray geometry = new JSONArray();
            JSONObject metry = new JSONObject();
            metry.put("type", "MultiLineString");
            metry.put("lod", 0);
            JSONArray boundarie = new JSONArray();
            JSONArray array = JSONObject.parseObject(map.get("the_geom").toString()).getJSONArray("coordinates");
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = JSONObject.parseObject(array.get(i).toString());
                jsonObject.getJSONArray("vertices").add(ProjectionToGeographicUtil.getPoint(Double.valueOf(object.get("x").toString()), Double.valueOf(object.get("y").toString())));
                boundarie.add(jsonObject.getJSONArray("vertices").size() - 1);
            }
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(boundarie);
            metry.put("boundaries", jsonArray);
            JSONArray metryArray = new JSONArray();
            metryArray.add(metry);
            cityObject.put("geometry", metryArray);
            cityObject.put("attributes", attribute);
            cityObject.put("attributes", attribute);
            geometry.add(metry);
            jsonObject.getJSONObject("CityObjects").put("UUID_" + UUID.randomUUID().toString(), cityObject);
        }
        long times = System.currentTimeMillis();
        String pointPath = path + times + "\\管线.json";
        File dirFile = new File(path + times);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        FileWriter fileWriter = new FileWriter(pointPath);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(pointPath, filePatn + "link.sem");
        return success(filePatn + "link.sem");
    }
    @ApiOperation(value = "地形转sem")
    @PostMapping(value = "/terrainToSem", produces = "application/json; charset=UTF-8")
    public R<String> terrainToSem(@RequestBody List<GridDto> dtos) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\terrain\\" + times + "\\";
        File dirFile = new File(path + "appearance");
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        double minx=180.0;
        double maxx=0.0;
        double miny=180.0;
        double maxy=0.0;
        for (GridDto dto:dtos
             ) {
            if (dto.getLon()>maxx){
                maxx=dto.getLon();
            }
            if (dto.getLon()<minx){
                minx=dto.getLon();
            }
            if (dto.getLat()>maxy){
                maxy=dto.getLat();
            }
            if (dto.getLat()<miny){
                miny=dto.getLat();
            }
        }
        JSONArray min= ProjectionToGeographicUtil.get4548Point(minx,miny);
        JSONArray max=ProjectionToGeographicUtil.get4548Point(maxx,maxy);
        String tifPath=path+"terrain.tif";
        TiffClipper.cropTiffByLatLon(config.getTifPath(),tifPath,min.getDouble(0),min.getDouble(1),max.getDouble(0),max.getDouble(1));
        String pngPath = path + "appearance\\terrain.png";
        TiffToRGBUtil.tifToPng(tifPath, pngPath);
        JSONObject jsonObject = getModule("terrainmodule.json");
        jsonObject.put("vertices", TiffCoordinateExtractorUtil.getCoordinate(tifPath));
        FileWriter fileWriter = new FileWriter(path + "terrain.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + "terrain.json", filePatn + "terrain.sem");
        return success(filePatn + "terrain.sem");
    }
 
    @ApiOperation(value = "地形转cityjson")
    @GetMapping(value = "/terrainToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> terrainToCityJson(@RequestParam("tifPath") String tifPath) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\terrain\\" + times + "\\";
        File dirFile = new File(path + "appearance");
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        String pngPath = path + "appearance\\terrain.png";
        TiffToRGBUtil.tifToPng(tifPath, pngPath);
        JSONObject jsonObject = getModule("terrainmodule.json");
        jsonObject.put("vertices", TiffCoordinateExtractorUtil.getCoordinate(tifPath));
        FileWriter fileWriter = new FileWriter(path + "terrain.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + "terrain.json", filePatn + "terrain.sem");
        return success(filePatn + "terrain.sem");
    }
 
    @ApiOperation(value = "grid转cityjson")
    @PostMapping(value = "/gridToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> gridToCityJson(@RequestBody List<GridDto> dtos) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\grid\\" + times + "\\";
        File dirFile = new File(path);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        JSONObject jsonObject = getModule("grid.json");
        JSONArray array = jsonObject.getJSONArray("vertices");
        for (GridDto dto : dtos
        ) {
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(dto.getLon());
            jsonArray.add(dto.getLat());
            jsonArray.add(0);
            array.add(jsonArray);
        }
        jsonObject.put("vertices", array);
        FileWriter fileWriter = new FileWriter(path + "grid.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + "grid.json", filePatn + "grid.sem");
        return success(filePatn + "grid.sem");
    }
 
    @ApiOperation(value = "river转sem")
    @GetMapping(value = "/riverToSem", produces = "application/json; charset=UTF-8")
    public R<String> riverToSem() throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\river\\" + times + "\\";
        File dirFile = new File(path);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        JSONObject jsonObject = getModule("river.json");
        FileWriter fileWriter = new FileWriter(path + "river.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + "river.json", filePatn + "river.sem");
        return success(filePatn + "river.sem");
    }
 
    @ApiOperation(value = "landuse转sem")
    @GetMapping(value = "/landuseToSem", produces = "application/json; charset=UTF-8")
    public R<String> landuseToSem() throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\landuse\\" + times + "\\";
        File dirFile = new File(path);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        JSONObject jsonObject = getModule("landuse.json");
        FileWriter fileWriter = new FileWriter(path + "landuse.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + "landuse.json", filePatn + "landuse.sem");
        return success(filePatn + "landuse.sem");
    }
 
    public JSONObject getModule(String moduleName) {
        JSONObject jsonObject = new JSONObject();
        try {
            URL resource = Resources.getResource(moduleName);
            String fileContent = Resources.toString(resource, StandardCharsets.UTF_8);
            jsonObject = JSONObject.parseObject(fileContent);
            System.out.println(fileContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
 
}