From 914a1a800b7f8758cfd4d08704e8bbd0cf3e83be Mon Sep 17 00:00:00 2001
From: aix <vip_xiaobin810@163.com>
Date: Mon, 15 Jul 2024 14:05:19 +0800
Subject: [PATCH] 航线云台俯仰角事件

---
 src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java |   70 +++++++++++++++++++++++------------
 1 files changed, 46 insertions(+), 24 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 72df6b3..218745b 100644
--- a/src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java
+++ b/src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java
@@ -2,12 +2,17 @@
 
 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;
@@ -18,14 +23,8 @@
 import java.util.*;
 
 public class ShapeFileUtil {
-    /*
-     * @param zipFile: 压缩包文件地址
-     * @return FeatureCollection
-     * @author pangshicheng
-     * @description 解析shp压缩包,并返回解析出的 FeatureCollection
-     * @date 2023/7/18 16:02
-     */
 
+    //将文件解压
     public static FeatureCollection getFeatureCollectionByShpFile(File zipFile) throws IOException {
         try {
             String tempDir = FileUtil.getTmpDirPath();
@@ -54,29 +53,29 @@
             Filter filter = Filter.INCLUDE;
             FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
             return collection;
-        }catch (Exception e){
+        } catch (Exception e) {
             throw e;
         }
     }
+
     /**
      * @param zipFile:
      * @return JSONObject
-     * @author pangshicheng
      * @description 通过shp压缩文件,将其转换为GeoJson格式
-     * @date 2023/7/18 16:04
      */
-    public static JSONObject 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");
-        try {
+        JSONObject geoJsonObject = new JSONObject();
+        geoJsonObject.put("type", "FeatureCollection");
             // 获取FeatureCollection
             FeatureCollection collection = getFeatureCollectionByShpFile(zipFile);
 
             FeatureIterator iterator = collection.features();
-            List<JSONObject> array  = new ArrayList<JSONObject>();
             //遍历feature转为json对象
             while (iterator.hasNext()) {
+                ShpDTO shpDTO = new ShpDTO();
                 SimpleFeature feature = (SimpleFeature) iterator.next();
                 StringWriter writer = new StringWriter();
                 fjson.writeFeature(feature, writer);
@@ -84,18 +83,41 @@
                 byte[] b = temp.getBytes("iso8859-1");
                 temp = new String(b, "gbk");
                 JSONObject json = JSONObject.parseObject(temp);
-                array.add(json);
+                shpDTO.setDKFW(json.getJSONObject("geometry").get("coordinates").toString());
+                shpDTO.setGEO(json.getJSONObject("geometry").get("type").toString());
+                shpDTO.setXZQDM(json.getJSONObject("properties").get("XZQDM").toString());
+                double[] xy = getLongitudeLatitude(CoordinateSystemUtil.pointCGCStoWGS(
+                                json.getJSONObject("properties").getDouble("XZB"),
+                                json.getJSONObject("properties").getDouble("YZB")));
+                shpDTO.setXZB(xy[0]);
+                shpDTO.setYZB(xy[1]);
+                shpDTO.setDKBH(json.getJSONObject("properties").getString("JCBH"));
+                shpDTO.setJCMJ(json.getJSONObject("properties").getDouble("JCMC"));
+                shpDTO.setTBLX(json.getJSONObject("properties").getString("TBLX"));
+                shpDTO.setDDTC(json.getJSONObject("properties").getString("DDTC"));
+                shpDTO.setHSX(json.getJSONObject("properties").getString("HSX"));
+                shpDTO.setJCLX(json.getJSONObject("properties").getString("JCLX"));
+                dtoList.add(shpDTO);
             }
             iterator.close();
-            //添加到geojsonObject
-            geoJsonObject.put("features",array);
-            iterator.close();
-
-        }catch (Exception e){
-            throw e;
-        }
-        return geoJsonObject;
+        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