张洋洋
2025-01-13 07b0c2b723f16d88c64a0e9151fdbef81a0a8a74
[add]管线json
已修改4个文件
129 ■■■■■ 文件已修改
pom.xml 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/controller/SimuController.java 50 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/utils/ShpReadUtils.java 30 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/linemodule.json 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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>
src/main/java/com/se/simu/controller/SimuController.java
@@ -406,7 +406,7 @@
    @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 + "\\";
        String path = outPath + "\\point\\";
        List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath);
        JSONObject jsonObject = getModule("pointmodule.json");
        for (Map<String, Object> map : list
@@ -446,6 +446,54 @@
        return success(pointPath);
    }
    @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());
                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);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        return success(pointPath);
    }
    public JSONObject getModule(String moduleName) {
        JSONObject jsonObject = new JSONObject();
        try {
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解析后的内容
@@ -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());// 必须是URL类型
        map.put("url", file.toURI().toURL());
        // 必须是URL类型
        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());
                }
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
    ]
  ]
}
{"type":"CityJSON","version":"1.0","CityObjects":{},"vertices":[]}