From 024e90554d19c2342f27a26f91bbea378f84da82 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 13 十一月 2024 17:25:18 +0800
Subject: [PATCH] 1

---
 src/main/java/com/moon/server/helper/GdbHelper.java |  180 +++++++++++++++++++++++-------------------------------------
 1 files changed, 69 insertions(+), 111 deletions(-)

diff --git a/src/main/java/com/moon/server/helper/GdbHelper.java b/src/main/java/com/moon/server/helper/GdbHelper.java
index fd780e8..c037585 100644
--- a/src/main/java/com/moon/server/helper/GdbHelper.java
+++ b/src/main/java/com/moon/server/helper/GdbHelper.java
@@ -8,7 +8,9 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.gdal.ogr.*;
+import org.gdal.osr.CoordinateTransformation;
 import org.gdal.osr.SpatialReference;
+import org.gdal.osr.osr;
 
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
@@ -18,16 +20,10 @@
 import java.time.LocalTime;
 import java.util.*;
 
-/**
- * GDB甯姪绫�
- * @author WWW
- */
+@SuppressWarnings("ALL")
 public class GdbHelper {
     private final static Log log = LogFactory.getLog(GdbHelper.class);
 
-    /**
-     * 閿�姣佽祫婧�
-     */
     public static void delete(Layer layer) {
         try {
             if (null != layer) {
@@ -38,9 +34,6 @@
         }
     }
 
-    /**
-     * 閿�姣佽祫婧�
-     */
     public static void delete(DataSource dataSource, Driver driver) {
         try {
             if (null != dataSource) {
@@ -59,17 +52,11 @@
         }
     }
 
-    /**
-     * 閿�姣佽祫婧�
-     */
     public static void delete(Layer layer, DataSource dataSource, Driver driver) {
         delete(layer);
         delete(dataSource, driver);
     }
 
-    /**
-     * 鑾峰彇琛ㄥ悕
-     */
     public static List<String> getTabNames(String filePath) {
         List<String> list = new ArrayList<>();
 
@@ -104,10 +91,7 @@
         return list;
     }
 
-    /**
-     * 璇诲彇鏁版嵁
-     */
-    public static <T> List<T> readData(Class clazz, String filePath, String layerName) {
+    public static <T> List<T> readData(Class clazz, String filePath, String layerName, boolean isTransform) {
         List<T> list = new ArrayList<>();
 
         Driver driver = null;
@@ -127,7 +111,7 @@
             for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) {
                 Layer layer = dataSource.GetLayer(i);
                 if (layer.GetName().equals(layerName)) {
-                    GdbHelper.readLayer(clazz, layer, list);
+                    GdbHelper.readLayer(clazz, layer, list, isTransform);
                     break;
                 }
 
@@ -142,10 +126,7 @@
         return list;
     }
 
-    /**
-     * 璇诲彇鍥惧眰
-     */
-    public static <T> void readLayer(Class clazz, Layer layer, List<T> list) {
+    public static <T> void readLayer(Class clazz, Layer layer, List<T> list, boolean isTransform) {
         try {
             Field gField = getGeomField(clazz);
 
@@ -155,6 +136,8 @@
                 return;
             }
 
+            CoordinateTransformation ct = getCoordinateTransformation(layer, isTransform);
+
             do {
                 Feature f = layer.GetNextFeature();
                 if (null == f) {
@@ -162,7 +145,7 @@
                 }
 
                 T t = (T) clazz.newInstance();
-                readFeature(t, f, map, gField);
+                readFeature(t, f, map, gField, ct);
                 list.add(t);
             } while (true);
         } catch (Exception ex) {
@@ -172,9 +155,30 @@
         }
     }
 
-    /**
-     * 鑾峰彇 geom 瀛楁
-     */
+    private static CoordinateTransformation getCoordinateTransformation(Layer layer, boolean isTransform) {
+        if (!isTransform) {
+            return null;
+        }
+
+        String epsg = layer.GetSpatialRef().GetAttrValue("AUTHORITY", 1);
+        if (StringHelper.isEmpty(epsg)) {
+            return null;
+        }
+
+        int epsgId = Integer.parseInt(epsg);
+        if (StaticData.I4326 == epsgId || StaticData.I4490 == epsgId || StaticData.I104903 == epsgId) {
+            return null;
+        }
+
+        SpatialReference srTarget = new SpatialReference();
+        srTarget.ImportFromEPSG(StaticData.I4490);
+
+        layer.GetSpatialRef().SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
+        srTarget.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
+
+        return CoordinateTransformation.CreateCoordinateTransformation(layer.GetSpatialRef(), srTarget);
+    }
+
     private static Field getGeomField(Class clazz) {
         try {
             Field gField = clazz.getSuperclass().getDeclaredField("geom");
@@ -186,21 +190,17 @@
         }
     }
 
-    /**
-     * 鑾峰彇瀛楁鏄犲皠
-     */
     private static void getFieldMapper(Class clazz, Layer layer, Map<Integer, Field> map) {
         try {
             FeatureDefn fd = layer.GetLayerDefn();
             for (int i = 0, count = fd.GetFieldCount(); i < count; i++) {
-                FieldDefn fieldDefn = fd.GetFieldDefn(i);
                 try {
-                    String name = fieldDefn.GetName().toLowerCase();
+                    String name = fd.GetFieldDefn(i).GetName().toLowerCase();
                     if (StaticData.READ_EXCLUDE_FIELDS.contains(name)) {
                         continue;
                     }
 
-                    Field field = clazz.getDeclaredField(name);
+                    Field field = clazz.getDeclaredField("gbcode".equals(name) ? "gb" : name);
                     field.setAccessible(true);
 
                     map.put(i, field);
@@ -213,14 +213,11 @@
                 getFieldMapper(clazz.getSuperclass(), layer, map);
             }
         } catch (Exception ex) {
-            //
+            log.error(ex);
         }
     }
 
-    /**
-     * 璇诲彇Feature
-     */
-    private static <T> void readFeature(T t, Feature f, Map<Integer, Field> map, Field gField) {
+    private static <T> void readFeature(T t, Feature f, Map<Integer, Field> map, Field gField, CoordinateTransformation ct) {
         for (Integer i : map.keySet()) {
             try {
                 Field field = map.get(i);
@@ -231,13 +228,10 @@
         }
 
         if (null != gField) {
-            setGeom(t, f, gField);
+            setGeom(t, gField, f.GetGeometryRef(), ct);
         }
     }
 
-    /**
-     * 璁剧疆鍊�
-     */
     public static <T> void setValue(T t, Feature f, Field field, Integer i) throws Exception {
         switch (field.getType().getName()) {
             case "java.math.BigDecimal":
@@ -274,46 +268,37 @@
         }
     }
 
-    /**
-     * 璁剧疆 geom 瀛楁鍊�
-     * <p>
-     * wkbUnknown = 0,
-     * wkbPoint = 1,
-     * wkbLineString = 2,
-     * wkbPolygon = 3,
-     * wkbMultiPoint = 4,
-     * wkbMultiLineString = 5,
-     * wkbMultiPolygon = 6,
-     * wkbGeometryCollection = 7,
-     * wkbNone = 100,
-     * wkbLinearRing = 101
-     */
-    private static <T> void setGeom(T t, Feature f, Field gField) {
+    private static <T> void setGeom(T t, Field gField, Geometry geometry, CoordinateTransformation ct) {
         try {
-            String geo = "null";
-            if (null != f.GetGeometryRef()) {
-                String wkt = f.GetGeometryRef().ExportToWkt();
-                // f.GetGeometryRef().GetGeometryType()
-                if (wkt.contains(StaticData.LINESTRING) && !wkt.contains(StaticData.MULTILINESTRING)) {
-                    wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")";
-                }
-                if (wkt.contains(StaticData.POLYGON) && !wkt.contains(StaticData.MULTIPOLYGON)) {
-                    wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")";
-                }
-                wkt = wkt.replace(" 0,", ",").replace(" 0)", ")");
-
-                geo = String.format("ST_GeomFromText('%s')", wkt);
+            if (null == geometry) {
+                gField.set(t, "null");
+                return;
+            }
+            if (null != ct) {
+                int flag = geometry.Transform(ct);
             }
 
-            gField.set(t, geo);
+            String wkt = geometry.ExportToWkt();
+            if (wkt.contains(StaticData.LINESTRING) && !wkt.contains(StaticData.MULTILINESTRING)) {
+                wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")";
+            }
+            if (wkt.contains(StaticData.POLYGON) && !wkt.contains(StaticData.MULTIPOLYGON)) {
+                wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")";
+            }
+            wkt = wkt.replace(" 0,", ",").replace(" 0)", ")");
+            if (wkt.contains(StaticData.MULTICURVE)) {
+                wkt = wkt.replace("MULTICURVE (", "MULTILINESTRING (").replace("CIRCULARSTRING ", "");
+                if (wkt.contains(StaticData.COMPOUNDCURVE)) {
+                    wkt = wkt.replace("COMPOUNDCURVE (", "").replace(")))", "))");
+                }
+            }
+
+            gField.set(t, String.format("ST_GeomFromText('%s')", wkt));
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
         }
     }
 
-    /**
-     * 鑾峰彇 Timestamp
-     */
     public static Timestamp getTimestamp(Feature f, int index) {
         int[] pnYear = new int[1];
         int[] pnMonth = new int[1];
@@ -341,9 +326,6 @@
         return Timestamp.valueOf(localDateTime);
     }
 
-    /**
-     * 鑾峰彇 LocalDate
-     */
     public static LocalDate getLocalDate(Feature f, int index) {
         int[] pnYear = new int[1];
         int[] pnMonth = new int[1];
@@ -361,9 +343,6 @@
         return LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]);
     }
 
-    /**
-     * 鍒涘缓GDB
-     */
     public static void createGdb(String filePath,  Map<String, List<?>> map) throws Exception {
         Driver driver = null;
         DataSource dataSource = null;
@@ -419,9 +398,6 @@
         }
     }
 
-    /**
-     * 鍒涘缓鍥惧眰
-     */
     private static Layer createLayer(DataSource dataSource, BasicMapper baseMapper ) {
         String tab = BaseQueryService.getTabName(baseMapper);
         if (StringHelper.isNull(tab)) {
@@ -437,15 +413,18 @@
             srid = geomMapper.selectSrid(tab);
         }
 
-        SpatialReference sr = new SpatialReference();
-        sr.ImportFromEPSG(null == srid ? 4490 : srid);
+        // SpatialReference sr = new SpatialReference(); sr.ImportFromEPSG(null == srid || 0 == srid ? StaticData.I104903 : srid)
+        SpatialReference sr;
+        if (null == srid || StaticData.DEFAULT_EPSG.contains(srid)) {
+            sr = new SpatialReference(StaticData.MOON_2000_WKT);
+        } else {
+            sr = new SpatialReference();
+            sr.ImportFromEPSG(srid);
+        }
 
         return dataSource.CreateLayer(tab.replace(".", "_"), sr, getGeomType(geomType), null);
     }
 
-    /**
-     * 鑾峰彇Geom绫诲埆
-     */
     private static Integer getGeomType(String geomType) {
         if (StringHelper.isEmpty(geomType)) {
             return ogr.wkbPoint;
@@ -469,9 +448,6 @@
         }
     }
 
-    /**
-     * 鑾峰彇瀛楁
-     */
     public static void getFields(Class clazz, List<Field> list, List<String> excludeFields) {
         try {
             Field[] fields = clazz.getDeclaredFields();
@@ -492,9 +468,6 @@
         }
     }
 
-    /**
-     * 娣诲姞鍥惧眰瀛楁
-     */
     public static void addLayerField(Layer layer, List<Field> list) {
         for (int i = 0, c = list.size(); i < c; i++) {
             Field f = list.get(i);
@@ -506,9 +479,6 @@
         }
     }
 
-    /**
-     * 鑾峰彇瀛楁绫诲瀷
-     */
     private static Integer getFieldType(Field f) {
         switch (f.getType().getName()) {
             case "java.math.BigDecimal":
@@ -530,9 +500,6 @@
         }
     }
 
-    /**
-     * 璁剧疆鍥惧眰鏁版嵁
-     */
     private static <T> void setLayerData(Layer layer,  List<Field> fields, List<T> list) throws Exception {
         for (T t : list) {
             Feature f = new Feature(layer.GetLayerDefn());
@@ -550,9 +517,6 @@
         }
     }
 
-    /**
-     * 璁剧疆瑕佺礌鐨勬暟鎹�
-     */
     public static <T> void setFeatureData(Feature f, List<Field> fields, T t) throws Exception {
         for (int i = 0, c = fields.size(); i < c; i++) {
             Field field = fields.get(i);
@@ -597,9 +561,6 @@
         }
     }
 
-    /**
-     * 璁剧疆Timestamp
-     */
     private static void setTimestamp(Feature f, int i, Timestamp time) {
         if (null == time) {
             return;
@@ -609,9 +570,6 @@
         f.SetField(i, local.getYear(), local.getMonthValue(), local.getDayOfMonth(), local.getHour(), local.getMinute(), local.getSecond(), 8);
     }
 
-    /**
-     * 璁剧疆LocalDate
-     */
     private static void setLocalDate(Feature f, int i, LocalDate local) {
         if (null == local) {
             return;

--
Gitblit v1.9.3