From a4ae15934058cc2d2d51631bfd48fb17e636a868 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 18 九月 2023 16:35:03 +0800
Subject: [PATCH] 添加插入KML数据功能

---
 src/main/java/com/lf/server/service/data/UploadService.java |  138 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 137 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/lf/server/service/data/UploadService.java b/src/main/java/com/lf/server/service/data/UploadService.java
index bd0c0af..0b0faf2 100644
--- a/src/main/java/com/lf/server/service/data/UploadService.java
+++ b/src/main/java/com/lf/server/service/data/UploadService.java
@@ -17,6 +17,10 @@
 
 import javax.servlet.http.HttpServletRequest;
 import java.io.File;
+import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.List;
@@ -496,7 +500,7 @@
             MetaEntity me = createMeta(mf, metaId);
             if (StaticData.NGDB.equals(me.getType())) {
                 if (guids.contains(me.getGuid())) {
-                    me.setIsmeta((short)-1);
+                    me.setIsmeta((short) -1);
                 } else {
                     guids.add(me.getGuid());
                 }
@@ -605,4 +609,136 @@
 
         return meta;
     }
+
+    /**
+     * 鎻掑叆KML鏂囦欢
+     */
+    public void insertKml(UserEntity ue, List<MetaFileEntity> list) {
+        checkMetaFiles(ue, list);
+        loadKml(list);
+        copyFiles(list);
+        insertMetas(list);
+    }
+
+    /**
+     * 鍔犺浇Kml
+     */
+    private void loadKml(List<MetaFileEntity> list) {
+        for (MetaFileEntity mf : list) {
+            if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity()) || !StaticData.KML.equals(mf.getExtName())) {
+                continue;
+            }
+
+            String geom = readKml(mf.getPath());
+            if (StringHelper.isEmpty(geom)) {
+                continue;
+            }
+
+            loadKmlData(mf, geom);
+        }
+    }
+
+    /**
+     * 璇诲彇KML
+     */
+    private String readKml(String path) {
+        try {
+            List<String> list = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
+            for (String str : list) {
+                if (!str.contains("<coordinates>")) {
+                    continue;
+                }
+
+                return getWktFromCoordinates(str);
+            }
+
+            return null;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return null;
+        }
+    }
+
+    /**
+     * 鑾峰彇KWT
+     */
+    private String getWktFromCoordinates(String coords) {
+        String[] strs = coords.replace("<coordinates>", "").replace("</coordinates>", "").split(" ");
+        if (strs.length == 0) {
+            return null;
+        }
+
+        // MULTILINESTRING((108.99843373100003 34.736292580000054,108.99818970700005 34.73764945000005,108.99831780400007 34.738880547000065,108.99812184300004 34.739564029000064))
+        StringBuilder sb = new StringBuilder();
+        sb.append("MULTILINESTRING((");
+        for (String str : strs) {
+            if (StringHelper.isEmpty(str)) {
+                continue;
+            }
+
+            String[] vals = str.split(StaticData.COMMA);
+            sb.append(vals[0]).append(" ").append(vals[1]).append(StaticData.COMMA);
+        }
+        sb.deleteCharAt(sb.length() - 1);
+        sb.append("))");
+
+        return sb.toString();
+    }
+
+    /**
+     * 鍔犺浇Kml鏁版嵁
+     */
+    private void loadKmlData(MetaFileEntity mf, String geom) {
+        BasicMapper basicMapper = ClassHelper.getBasicMapper(mf.getEntity());
+        if (null == basicMapper) {
+            return;
+        }
+
+        String tabName = BaseQueryService.getTabName(basicMapper);
+        String className = ClassHelper.getClassName(basicMapper);
+        Class clazz = ClassHelper.getEntityClass(className);
+        if (null == clazz || null == tabName) {
+            return;
+        }
+
+        String name = FileHelper.getFileName(mf.getPath());
+        List<?> list = createKmlEntity(clazz, geom, name);
+        if (null == list || list.isEmpty()) {
+            return;
+        }
+        mf.setRecords(list.size());
+        setCreateInfo(list, mf);
+
+        int rows = batchInserts(basicMapper, list);
+        if (rows > 0) {
+            mf.setEntity(mf.getTab());
+            mf.setTab(tabName);
+            mf.setRows(rows);
+        }
+    }
+
+    /**
+     * 鍒涘缓KML瀹炰綋绫�
+     */
+    private <T> List<T> createKmlEntity(Class clazz, String geom, String name) {
+        try {
+            T t = (T) clazz.newInstance();
+
+            Field gField = clazz.getSuperclass().getDeclaredField("geom");
+            gField.setAccessible(true);
+            gField.set(t, geom);
+
+            Field pField = clazz.getSuperclass().getDeclaredField("pipename");
+            pField.setAccessible(true);
+            pField.set(t, name);
+
+            List<T> list = new ArrayList<>();
+            list.add(t);
+
+            return list;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return null;
+        }
+    }
 }

--
Gitblit v1.9.3