From 07b0c2b723f16d88c64a0e9151fdbef81a0a8a74 Mon Sep 17 00:00:00 2001 From: 张洋洋 <10611411+yang-yang-z@user.noreply.gitee.com> Date: 星期一, 13 一月 2025 17:54:10 +0800 Subject: [PATCH] [add]管线json --- src/main/resources/linemodule.json | 34 ----------- src/main/java/com/se/simu/utils/ShpReadUtils.java | 30 ++++++++- pom.xml | 15 +++++ src/main/java/com/se/simu/controller/SimuController.java | 60 ++++++++++++++++++-- 4 files changed, 95 insertions(+), 44 deletions(-) diff --git a/pom.xml b/pom.xml index 6062e1d..455621b 100644 --- a/pom.xml +++ b/pom.xml @@ -283,6 +283,21 @@ <artifactId>gt-shapefile</artifactId> <version>22-RC</version> </dependency> + <dependency> + <groupId>com.vividsolutions</groupId> + <artifactId>jts</artifactId> + <version>1.13</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>2.13.3</version> + </dependency> + <dependency> + <groupId>com.vividsolutions</groupId> + <artifactId>jts</artifactId> + <version>1.13</version> + </dependency> </dependencies> <repositories> <repository> diff --git a/src/main/java/com/se/simu/controller/SimuController.java b/src/main/java/com/se/simu/controller/SimuController.java index d1337f8..5879100 100644 --- a/src/main/java/com/se/simu/controller/SimuController.java +++ b/src/main/java/com/se/simu/controller/SimuController.java @@ -406,7 +406,7 @@ @ApiOperation(value = "绠$偣杞琧ityjson") @GetMapping(value = "/pointToCityJson", produces = "application/json; charset=UTF-8") public R<String> pointToCityJson(@RequestParam("shpPath") String shpPath) throws Exception { - String path = outPath + "\\"; + String path = outPath + "\\point\\"; List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath); JSONObject jsonObject = getModule("pointmodule.json"); for (Map<String, Object> map : list @@ -432,12 +432,60 @@ metry.put("boundaries", boundarie); geometry.add(metry); cityObject.put("geometry", geometry); - jsonObject.getJSONObject("CityObjects").put("UUID_"+UUID.randomUUID().toString(), cityObject); + 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()){ + 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(); + return success(pointPath); + } + + @ApiOperation(value = "绠$嚎杞琧ityjson") + @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()); + JSONArray vertice = new JSONArray(); + //todo 鏈潵淇敼鍧愭爣绯� + vertice.add(object.get("x")); + vertice.add(object.get("y")); + vertice.add(0.0); + jsonObject.getJSONArray("vertices").add(vertice); + boundarie.add(jsonObject.getJSONArray("vertices").size()); + } + metry.put("boundaries", boundarie); + cityObject.put("geometry",metry); + 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); diff --git a/src/main/java/com/se/simu/utils/ShpReadUtils.java b/src/main/java/com/se/simu/utils/ShpReadUtils.java index 25b5e19..137ff83 100644 --- a/src/main/java/com/se/simu/utils/ShpReadUtils.java +++ b/src/main/java/com/se/simu/utils/ShpReadUtils.java @@ -1,5 +1,10 @@ package com.se.simu.utils; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.module.SimpleModule; import org.geotools.data.DataStore; import org.geotools.data.DataStoreFinder; import org.geotools.data.FeatureSource; @@ -13,15 +18,24 @@ import org.opengis.feature.simple.SimpleFeatureType; import java.io.File; +import java.io.IOException; import java.nio.charset.Charset; import java.util.*; public class ShpReadUtils { public static void main(String[] args) throws Exception { - readPointShp("D:\\鍩庡競鍐呮稘\\sem\\绠$偣\\pipeline-point.shp"); - //readPointShp("D:\\鍩庡競鍐呮稘\\sem\\绠$嚎\\pipeline-conduit.shp"); + //readPointShp("D:\\鍩庡競鍐呮稘\\sem\\绠$偣\\pipeline-point.shp"); + readPointShp("D:\\鍩庡競鍐呮稘\\sem\\绠$嚎\\pipeline-conduit.shp"); } - + static class GeometrySerializer extends JsonSerializer<Geometry> { + @Override + public void serialize(Geometry value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeStartObject(); + gen.writeStringField("type", value.getGeometryType()); + gen.writeObjectField("coordinates", value.getCoordinates()); + gen.writeEndObject(); + } + } /** * @param url shp鏂囦欢璺緞 * @return shp瑙f瀽鍚庣殑鍐呭 @@ -30,7 +44,8 @@ public static List<Map<String, Object>> readPointShp(String url) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); File file = new File(url); - map.put("url", file.toURI().toURL());// 蹇呴』鏄疷RL绫诲瀷 + map.put("url", file.toURI().toURL()); + // 蹇呴』鏄疷RL绫诲瀷 DataStore dataStore = DataStoreFinder.getDataStore(map); //瀛楃杞爜锛岄槻姝腑鏂囦贡鐮� ((ShapefileDataStore) dataStore).setCharset(Charset.forName("utf8")); @@ -49,7 +64,12 @@ Property property = iterator.next(); if ("the_geom".equals(property.getName().toString())) { Geometry geometry = reader.read(property.getValue().toString()); - objectMap.put(property.getName().toString(), geometry); + SimpleModule module = new SimpleModule(); + module.addSerializer(Geometry.class, new GeometrySerializer()); + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(module); + String jsonString = mapper.writeValueAsString(geometry); + objectMap.put(property.getName().toString(), jsonString); } else { objectMap.put(property.getName().toString(), property.getValue()); } diff --git a/src/main/resources/linemodule.json b/src/main/resources/linemodule.json index 5c7c39c..9780e6e 100644 --- a/src/main/resources/linemodule.json +++ b/src/main/resources/linemodule.json @@ -1,33 +1 @@ -{ - "type": "CityJSON", - "version": "1.0", - "CityObjects": { - "UUID_005f52a3-47c2-492c-a941-187c3a342901": { - "type": "+PipeLine", - "attributes": { - "name": "绠¢亾" - }, - "geometry": [ - { - "type": "MultiLineString", - "boundaries": [ - [1,2] - ], - "lod": 0 - } - ] - } - }, - "vertices": [ - [ - 116.7058333, - 39.98416667, - 0.0 - ], - [ - 116.7058326, - 39.98416637, - 0.0 - ] - ] -} \ No newline at end of file +{"type":"CityJSON","version":"1.0","CityObjects":{},"vertices":[]} \ No newline at end of file -- Gitblit v1.9.3