ywf před 4 dny
rodič
revize
f5a918f2a3

+ 1 - 1
onemap-modules/onemap-file/src/main/java/com/onemap/file/service/impl/LocalSysFileServiceImpl.java

@@ -283,7 +283,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
         String publicpath = path.substring(0, path.indexOf("."));
         String shppath = path;
         String geojsonPath = publicpath + "_" + uuid + "_" + "geojson.json";
-        String geojson = new ParsingShpFileUtils().shape2Geojson(shppath, geojsonPath);
+        String geojson = new ParsingShpFileUtils().shape2Geojson2(shppath, geojsonPath);
         return geojson;
     }
 

+ 41 - 0
onemap-modules/onemap-file/src/main/java/com/onemap/file/utils/ParsingShpFileUtils.java

@@ -193,6 +193,47 @@ public class ParsingShpFileUtils {
      * @return
      */
     public static String shape2Geojson(String shpPath, String jsonPath) {
+        FeatureJSON fjson = new FeatureJSON();
+        StringBuffer sb = new StringBuffer();
+        try {
+            sb.append("{\"type\": \"FeatureCollection\",\"features\": ");
+            //读取shp
+            SimpleFeatureCollection colls = readShp(shpPath);
+            //拿到所有features
+            SimpleFeatureIterator itertor = colls.features();
+            JSONArray array = new JSONArray();
+            while (itertor.hasNext()) {
+                GeoJSONUtil e = new GeoJSONUtil();
+                SimpleFeature feature = itertor.next();
+                StringWriter writer = new StringWriter();
+                fjson.writeFeature(feature, writer);
+                JSONObject json = JSONObject.parseObject(writer.toString());
+                array.add(json);
+            }
+            itertor.close();
+            sb.append(array.toString());
+            sb.append("}");
+            if (!StringUtils.isEmpty(jsonPath)) {
+                //写入文件
+                FileOutputStream fos = new FileOutputStream(jsonPath, false);
+                //true表示在文件末尾追加
+                fos.write(sb.toString().getBytes());
+                fos.close();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return sb.toString();
+    }
+
+        /**
+     * shp转换为Geojson
+     *
+     * @param shpPath  shp文件地址
+     * @param jsonPath 要写入的json文件地址
+     * @return
+     */
+    public static String shape2Geojson2(String shpPath, String jsonPath) {
         FeatureJSON fjson = new FeatureJSON(new GeometryJSON(7));
         StringBuffer sb = new StringBuffer();
         try {