From a9350a6b293bc71eae543cf7bc3794dc4996be00 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 19 九月 2023 13:22:26 +0800
Subject: [PATCH] 1

---
 ExportMap/cs/TerrainUtils.cs |  247 +++++++++++++++++++++++++++++++++++++++++++++++++
 ExportMap/cs/TerraUtils.cs   |    4 
 ExportMap/Web.config         |    2 
 3 files changed, 249 insertions(+), 4 deletions(-)

diff --git a/ExportMap/Web.config b/ExportMap/Web.config
index 4b3d2e9..579682d 100644
--- a/ExportMap/Web.config
+++ b/ExportMap/Web.config
@@ -4,7 +4,7 @@
     <!-- 寤婂潑鏈嶅姟 -->
     <add key="lfServer" value="http://127.0.0.1:12316/server"/>
     <!-- 鍦板舰鏈�澶х骇鍒� -->
-    <add key="terrainMaxLevel" value="14"/>
+    <add key="terrainMaxLevel" value="16"/>
     <!-- 鏈満IP -->
     <add key="localIP" value="192.168.20.106"/>
     <!-- 涓存椂鐩綍 -->
diff --git a/ExportMap/cs/TerraUtils.cs b/ExportMap/cs/TerraUtils.cs
index 42e626c..3c93157 100644
--- a/ExportMap/cs/TerraUtils.cs
+++ b/ExportMap/cs/TerraUtils.cs
@@ -38,7 +38,7 @@
         /// <summary>
         /// 榛樿鏈�澶ф枃浠跺ぇ灏忥細5GB
         /// </summary>
-        public static long DDEFAULT_MAX_SIZE = 5L * 1024 * 1024 * 1024;
+        public static long DEFAULT_MAX_SIZE = 5L * 1024 * 1024 * 1024;
 
         /// <summary>
         /// 鑾峰彇Python鏂囦欢
@@ -254,7 +254,7 @@
         private static int GetTerrainMaxLevel(XYZArgs args, string tifFile)
         {
             FileInfo fi = new FileInfo(tifFile);
-            if (fi.Length > DDEFAULT_MAX_SIZE) return TERRAIN_MAX_LEVEL;
+            if (fi.Length > DEFAULT_MAX_SIZE) return TERRAIN_MAX_LEVEL;
 
             string ctbPath = Tools.GetSetting("ctbPath");
             string dirPath = GetTerrainPath(args.dircode);
diff --git a/ExportMap/cs/TerrainUtils.cs b/ExportMap/cs/TerrainUtils.cs
index 5a0f3ce..9f7e109 100644
--- a/ExportMap/cs/TerrainUtils.cs
+++ b/ExportMap/cs/TerrainUtils.cs
@@ -1,7 +1,10 @@
-锘縰sing ExportMap.Models;
+锘縰sing ExportMap.db;
+using ExportMap.Models;
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
+using System.Text;
 using System.Web;
 
 namespace ExportMap.cs
@@ -31,6 +34,248 @@
         }
 
         /// <summary>
+        /// 榛樿鏈�澶ф枃浠跺ぇ灏忥細5GB
+        /// </summary>
+        public static long DEFAULT_MAX_SIZE = 5L * 1024 * 1024 * 1024;
+
+        /// <summary>
+        /// 鑾峰彇鍙戝竷鍦板潃
+        /// </summary>
+        public static string GetReleaseUrl(string dircode)
+        {
+            return "http://{host}/LFData/3d/terrain/" + dircode;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鍦板舰璺緞
+        /// </summary>
+        public static string GetTerrainPath(string dircode)
+        {
+            return Path.Combine(SGUtils.LFData, "3d\\terrain", dircode);
+        }
+
+        /// <summary>
+        /// 鐢熸垚
+        /// </summary>
+        /// <param name="args">XYZ鍙傛暟</param>
+        /// <param name="err">閿欒淇℃伅</param>
+        /// <returns>鏁版嵁鍙戝竷ID闆嗗悎</returns>
+        public static List<int> Generate(XYZArgs args, ref string err)
+        {
+            string tifFile = null;
+            try
+            {
+                List<SysMeta> metas = XYZUtils.SelectMetas(args.ids, "and type in ('tif', 'tiff')");
+                if (null == metas || metas.Count == 0) return PrintInfo("鎵句笉鍒板厓鏁版嵁");
+
+                string dirPath = GetTerrainPath(args.dircode);
+                //if (Directory.Exists(dirPath)) Tools.DelPath(dirPath); // 宸插瓨鍦ㄧ殑锛屽垹闄�
+                if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
+                string subPath = Path.Combine(dirPath, "subs");
+                if (!Directory.Exists(subPath)) Directory.CreateDirectory(subPath);
+
+                int pubid = PubDBHelper.GetPushlishId(args.dircode, "DEM");
+                List<int> metsIds = PubDBHelper.GetPublishMetaId(pubid);
+
+                string uploadFolder = Tools.GetSetting("uploadFolder");
+                foreach (SysMeta meta in metas)
+                {
+                    if (metsIds.Contains(meta.id)) return PrintInfo("鍏冩暟鎹甗" + meta.id + "] 宸插彂甯冦��");
+
+                    string sourceFile = Path.Combine(uploadFolder, meta.path);
+                    if (!File.Exists(sourceFile)) return PrintInfo("鍏冩暟鎹甗" + meta.id + "] 涓嶅瓨鍦ㄣ��");
+
+                    string targetFile = Path.Combine(subPath, meta.name);
+                    Reproject(sourceFile, targetFile, "", "EPSG:4326");
+                }
+
+
+
+                /*tifFile = Merge(metas, args, ref err);
+                if (!File.Exists(tifFile))
+                {
+                    LogOut.Info("TerraUtils锛氭壘涓嶅埌tifFile鏁版嵁銆�");
+                    return null;
+                }
+
+                string json = Path.Combine(dirPath, "layer.json");
+                if (File.Exists(json)) File.Delete(json);
+                //Generate(args, tifFile, ref err);
+                CreateTerrain(args, tifFile, ref err);
+
+                if (!File.Exists(json))
+                {
+                    LogOut.Info("TerraUtils锛氭壘涓嶅埌layer.json鏂囦欢銆�");
+                    return null;
+                }*/
+
+                Complement(args);
+                pubid = InsertToDB(metas, args);
+
+                return new List<int> { pubid };
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+                err = ex.Message;
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// 鎵撳嵃淇℃伅
+        /// </summary>
+        private static List<int> PrintInfo(string info)
+        {
+            LogOut.Info("TerrainUtils锛�" + info);
+            return null;
+        }
+
+
+
+        /// <summary>
+        /// 鍒涘缓楂樼▼鍒囩墖
+        /// </summary>
+        private static void CreateTerrain(XYZArgs args, string tifFile, ref string err)
+        {
+            string ctbPath = Tools.GetSetting("ctbPath");
+            string dirPath = GetTerrainPath(args.dircode);
+            int maxLevel = GetTerrainMaxLevel(args, tifFile);
+
+            string gdal_data = string.Format("set GDAL_DATA={0}\\data", ctbPath);
+            // -N 椤剁偣娉曠嚎, -C 寮哄埗鍒涘缓缂哄け鏍圭摝鐗�, -R 涓嶈鐩栫幇鏈夋枃浠�
+            string createMesh = string.Format("{0}\\ctb-tile.exe -R -C -s {4} -o \"{1}\" -f Mesh \"{2}\\{3}.tif\"", ctbPath, dirPath, dirPath, args.dircode, maxLevel);
+            //string createLayer = string.Format("{0}\\ctb-tile.exe -l -s {4} -o \"{1}\" -f Mesh \"{2}\\{3}.tif\"", ctbPath, dirPath, dirPath, args.dircode, maxLevel);
+
+            List<string> list = new List<string>() { gdal_data, createMesh };
+            SysTask task = TaskDBHelper.CreateTask(args, "DEM", "楂樼▼鏁版嵁(DEM)");
+            err = Tools.ExecCmd(task, list);
+        }
+
+        /// <summary>
+        /// 鑾峰彇鍦板舰鏈�澶х骇鍒�
+        /// </summary>
+        private static int GetTerrainMaxLevel(XYZArgs args, string tifFile)
+        {
+            FileInfo fi = new FileInfo(tifFile);
+            if (fi.Length > DEFAULT_MAX_SIZE) return TERRAIN_MAX_LEVEL;
+
+            string ctbPath = Tools.GetSetting("ctbPath");
+            string dirPath = GetTerrainPath(args.dircode);
+
+            string gdal_data = string.Format("set GDAL_DATA={0}\\data", ctbPath);
+            string createLayer = string.Format("{0}\\ctb-tile.exe -l -o \"{1}\" -f Mesh \"{2}\\{3}.tif\"", ctbPath, dirPath, dirPath, args.dircode);
+
+            Tools.ExecCmd(new List<string>() { gdal_data, createLayer });
+
+            string layerJson = Path.Combine(dirPath, "layer.json");
+            if (!File.Exists(layerJson)) return TERRAIN_MAX_LEVEL;
+
+            string[] lines = File.ReadAllLines(layerJson, Encoding.UTF8);
+
+            int level = -1;
+            foreach (string line in lines)
+            {
+                if (line.IndexOf("startX") > -1) level++;
+            }
+            if (File.Exists(layerJson)) File.Delete(layerJson);
+
+            return level > TERRAIN_MAX_LEVEL ? TERRAIN_MAX_LEVEL : level;
+        }
+
+        /// <summary>
+        /// 琛ュ厖鏂囦欢
+        /// </summary>
+        private static void Complement(XYZArgs args)
+        {
+            string dirPath = GetTerrainPath(args.dircode);
+            string p_0_0 = Path.Combine(dirPath, "0", "0");
+            if (!Directory.Exists(p_0_0)) Directory.CreateDirectory(p_0_0);
+
+            string p_0_1 = Path.Combine(dirPath, "0", "1");
+            if (!Directory.Exists(p_0_1)) Directory.CreateDirectory(p_0_1);
+
+            string s_0_0_0 = Path.Combine(SGUtils.LFData, "dem", "0", "0", "0.terrain");
+            string d_0_0_0 = Path.Combine(dirPath, "0", "0", "0.terrain");
+            if (!File.Exists(d_0_0_0)) File.Copy(s_0_0_0, d_0_0_0, true);
+
+            string s_0_1_0 = Path.Combine(SGUtils.LFData, "dem", "0", "1", "0.terrain");
+            string d_0_1_0 = Path.Combine(dirPath, "0", "1", "0.terrain");
+            if (!File.Exists(d_0_1_0)) File.Copy(s_0_1_0, d_0_1_0, true);
+
+            string layerJson = Path.Combine(dirPath, "layer.json");
+            string[] lines = File.ReadAllLines(layerJson, Encoding.UTF8);
+            //lines[12] = "    [ { \"startX\": 0, \"startY\": 0, \"endX\": 1, \"endY\": 0 } ]";
+            for (int i = 0, c = lines.Length; i < c; i++)
+            {
+                if (lines[i].IndexOf("startX") > -1)
+                {
+                    lines[i] = "    [ { \"startX\": 0, \"startY\": 0, \"endX\": 1, \"endY\": 0 } ]";
+                    break;
+                }
+            }
+
+            File.WriteAllLines(layerJson, lines, Encoding.UTF8);
+        }
+
+        /// <summary>
+        /// 鎻掑叆鏁版嵁搴�
+        /// </summary>
+        private static int InsertToDB(List<SysMeta> metas, XYZArgs args)
+        {
+            //if (PubDBHelper.IsPublish(args.dircode, "DEM")) return 1;
+            int pubid = PubDBHelper.GetPushlishId(args.dircode, "DEM");
+            if (pubid > 0) // 鏇存柊鍙戝竷
+            {
+                List<int> ids = PubDBHelper.GetPublishMetaId(pubid);
+                foreach (SysMeta m in metas)
+                {
+                    if (!ids.Contains(m.id)) PubDBHelper.InsertMetaPub(m.id, pubid, args.userId);
+                }
+                string geom = GetPointZ(args);
+                PubDBHelper.UpdatePublish(pubid, args.name, args.userId, geom);
+
+                return pubid;
+            }
+
+            SysMeta meta = metas[0];
+            meta.type = "DEM";
+            meta.name = args.name;
+            meta.dircode = args.dircode;
+
+            SysPublish sys = Tools.NewPublish(meta, args, GetReleaseUrl(args.dircode), "3d\\terrain\\" + args.dircode);
+            sys.geom = GetPointZ(args);
+
+            pubid = PubDBHelper.InsertPublish(sys);
+            if (pubid > 0)
+            {
+                sys.id = pubid;
+                PubDBHelper.InsertLayer(sys, new SysMeta()
+                {
+                    name = args.name,
+                    type = meta.type,
+                    dirname = meta.dirname
+                });
+                foreach (SysMeta m in metas)
+                {
+                    PubDBHelper.InsertMetaPub(m.id, pubid, args.userId);
+                }
+            }
+
+            return pubid;
+        }
+
+        /// <summary>
+        /// 鑾峰彇涓績鐐�
+        /// </summary>
+        private static string GetPointZ(XYZArgs args)
+        {
+            double x = 0, y = 0;
+
+            return string.Format("ST_GeomFromText('POINT Z ({0} {1} {2})')", x, y, 12);
+        }
+
+        /// <summary>
         /// 閲嶆姇褰�
         /// </summary>
         public static void Reproject(string sourceFile, string targetFile, string sourceSrs, string targetSrs)

--
Gitblit v1.9.3