From d4447ca99df2cfe978ca51790dd2891e9df0d492 Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Thu, 25 Jul 2024 16:19:05 +0800
Subject: [PATCH] 修改人工拍照图片保存,新增面状航线保存、修改接口,图斑文件上传格式修改

---
 src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java |  117 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 81 insertions(+), 36 deletions(-)

diff --git a/src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java b/src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java
index c1a8a54..58501cc 100644
--- a/src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java
+++ b/src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java
@@ -2,29 +2,32 @@
 
 import cn.hutool.core.io.FileUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.dji.sample.patches.model.dto.ShpDTO;
+import com.dji.sample.territory.utils.CoordinateSystemUtil;
 import org.geotools.data.DataStore;
 import org.geotools.data.DataStoreFinder;
 import org.geotools.data.FeatureSource;
 import org.geotools.feature.FeatureCollection;
 import org.geotools.feature.FeatureIterator;
 import org.geotools.geojson.feature.FeatureJSON;
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.Point;
+import org.locationtech.jts.io.ParseException;
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.simple.SimpleFeatureType;
 import org.opengis.filter.Filter;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.util.*;
 
-public class ShapeFileUtil {
-    /*
-     * @param zipFile: 压缩包文件地址
-     * @return FeatureCollection
-     * @author pangshicheng
-     * @description 解析shp压缩包,并返回解析出的 FeatureCollection
-     * @date 2023/7/18 16:02
-     */
+import static com.dji.sample.patches.utils.TimerUtil.getNowDay;
+import static com.dji.sample.patches.utils.TimerUtil.getNowTimeName;
 
+public class ShapeFileUtil {
+
+    //将文件解压
     public static FeatureCollection getFeatureCollectionByShpFile(File zipFile) throws IOException {
         try {
             String tempDir = FileUtil.getTmpDirPath();
@@ -61,44 +64,86 @@
     /**
      * @param zipFile:
      * @return JSONObject
-     * @author pangshicheng
      * @description 通过shp压缩文件,将其转换为GeoJson格式
-     * @date 2023/7/18 16:04
      */
-    public static List<String> shpToGeoJson(File zipFile) throws IOException {
+    //将解压后的文件转换成GeoJson格式
+    public static List<ShpDTO> shpToGeoJson(File zipFile) throws IOException, ParseException {
+        List<ShpDTO> dtoList = new ArrayList<>();
         FeatureJSON fjson = new FeatureJSON();
         JSONObject geoJsonObject = new JSONObject();
         geoJsonObject.put("type", "FeatureCollection");
-        List<String> strings = new ArrayList<>();
-        try {
-            // 获取FeatureCollection
-            FeatureCollection collection = getFeatureCollectionByShpFile(zipFile);
+        // 获取FeatureCollection
+        FeatureCollection collection = getFeatureCollectionByShpFile(zipFile);
 
-            FeatureIterator iterator = collection.features();
-            List<JSONObject> array = new ArrayList<JSONObject>();
-            //遍历feature转为json对象
-            while (iterator.hasNext()) {
-                SimpleFeature feature = (SimpleFeature) iterator.next();
-                StringWriter writer = new StringWriter();
-                fjson.writeFeature(feature, writer);
-                String temp = writer.toString();
-                byte[] b = temp.getBytes("iso8859-1");
-                temp = new String(b, "gbk");
-                JSONObject json = JSONObject.parseObject(temp);
-                String str2 = json.getJSONObject("geometry").get("coordinates").toString();
-                strings.add(str2);
-                array.add(json);
+        FeatureIterator iterator = collection.features();
+        //遍历feature转为json对象
+        while (iterator.hasNext()) {
+            ShpDTO shpDTO = new ShpDTO();
+            SimpleFeature feature = (SimpleFeature) iterator.next();
+            StringWriter writer = new StringWriter();
+            fjson.writeFeature(feature, writer);
+            String temp = writer.toString();
+            byte[] b = temp.getBytes("iso8859-1");
+            temp = new String(b, "gbk");
+            JSONObject json = JSONObject.parseObject(temp);
+            shpDTO.setDKFW(json.getJSONObject("geometry").getString("coordinates"));
+
+            if ((json.getJSONObject("geometry").get("type")) != null) {
+                shpDTO.setGEO(json.getJSONObject("geometry").getString("type"));
             }
-            iterator.close();
-            //添加到geojsonObject
-            geoJsonObject.put("features", array);
-            iterator.close();
 
-        } catch (Exception e) {
-            throw e;
+            if ((json.getJSONObject("properties").get("XZQDM")) != null) {
+                shpDTO.setXZQDM(json.getJSONObject("properties").getString("XZQDM"));
+            }
+            if ((json.getJSONObject("properties").getDouble("XZB")) != null &&
+                    json.getJSONObject("properties").getDouble("YZB") != null) {
+                double[] xy = getLongitudeLatitude(CoordinateSystemUtil.pointCGCStoWGS(
+                        json.getJSONObject("properties").getDouble("XZB"),
+                        json.getJSONObject("properties").getDouble("YZB")));
+                shpDTO.setXZB(xy[0]);
+                shpDTO.setYZB(xy[1]);
+            }
+            if ((json.getJSONObject("properties").getString("JCBH") != null)) {
+                shpDTO.setDKBH(json.getJSONObject("properties").getString("JCBH"));
+            }else {
+                shpDTO.setDKBH(getNowDay());
+            }
+            if (json.getJSONObject("properties").getDouble("JCMC") != null) {
+                shpDTO.setJCMJ(json.getJSONObject("properties").getDouble("JCMC"));
+            }
+            if (json.getJSONObject("properties").getString("TBLX") != null) {
+                shpDTO.setTBLX(json.getJSONObject("properties").getString("TBLX"));
+            }
+            if (json.getJSONObject("properties").getString("DDTC") != null) {
+                shpDTO.setDDTC(json.getJSONObject("properties").getString("DDTC"));
+            }
+            if (json.getJSONObject("properties").getString("HSX") != null) {
+                shpDTO.setHSX(json.getJSONObject("properties").getString("HSX"));
+            }
+            if (json.getJSONObject("properties").getString("JCLX") != null) {
+                shpDTO.setJCLX(json.getJSONObject("properties").getString("JCLX"));
+            }
+            dtoList.add(shpDTO);
         }
-        return strings;
+        iterator.close();
+        return dtoList;
     }
 
+    public static void main(String[] args) throws IOException, ParseException {
+        File file = new File("D:\\ceshi\\ceshi.zip");
+        shpToGeoJson(file);
+    }
+
+    public static double[] getLongitudeLatitude(Geometry wktPoint) throws ParseException {
+        Geometry geometry = wktPoint;
+        if (geometry instanceof Point) {
+            Point point = (Point) geometry;
+            double longitude = point.getX();
+            double latitude = point.getY();
+            return new double[]{longitude, latitude};
+        } else {
+            throw new IllegalArgumentException("The geometry is not a point.");
+        }
+    }
 }
 

--
Gitblit v1.9.3