From 9e21f5d87384580c9935da6b551ed9f789974cfc Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 19 九月 2023 14:43:29 +0800
Subject: [PATCH] 1

---
 ExportMap/cs/TerrainUtils.cs |   77 +++++++++++++++++++++++++++++---------
 1 files changed, 58 insertions(+), 19 deletions(-)

diff --git a/ExportMap/cs/TerrainUtils.cs b/ExportMap/cs/TerrainUtils.cs
index 9f7e109..bd61cfc 100644
--- a/ExportMap/cs/TerrainUtils.cs
+++ b/ExportMap/cs/TerrainUtils.cs
@@ -13,6 +13,8 @@
     {
         private static int terrainMaxLevel = 0;
 
+        private const string EPSG4326 = "EPSG:4326";
+
         /// <summary>
         /// 鍦板舰鏈�澶х骇鍒�
         /// </summary>
@@ -62,7 +64,6 @@
         /// <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')");
@@ -77,6 +78,7 @@
                 int pubid = PubDBHelper.GetPushlishId(args.dircode, "DEM");
                 List<int> metsIds = PubDBHelper.GetPublishMetaId(pubid);
 
+                List<string> files = new List<string>();
                 string uploadFolder = Tools.GetSetting("uploadFolder");
                 foreach (SysMeta meta in metas)
                 {
@@ -86,28 +88,18 @@
                     if (!File.Exists(sourceFile)) return PrintInfo("鍏冩暟鎹甗" + meta.id + "] 涓嶅瓨鍦ㄣ��");
 
                     string targetFile = Path.Combine(subPath, meta.name);
-                    Reproject(sourceFile, targetFile, "", "EPSG:4326");
+                    ConvertRaster(sourceFile, targetFile);
+                    if (File.Exists(targetFile))
+                    {
+                        files.Add(targetFile);
+                        CreateTerrain(args, targetFile, ref err);
+                    }
                 }
 
-
-
-                /*tifFile = Merge(metas, args, ref err);
-                if (!File.Exists(tifFile))
-                {
-                    LogOut.Info("TerraUtils锛氭壘涓嶅埌tifFile鏁版嵁銆�");
-                    return null;
-                }
+                string vrt = CreateLayerJson(args, files, ref err);
 
                 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;
-                }*/
+                if (!File.Exists(json)) return PrintInfo("鎵句笉鍒發ayer.json鏂囦欢");
 
                 Complement(args);
                 pubid = InsertToDB(metas, args);
@@ -131,7 +123,22 @@
             return null;
         }
 
+        /// <summary>
+        /// 杞崲鏍呮牸鏂囦欢
+        /// </summary>
+        private static void ConvertRaster(string sourceFile, string targetFile)
+        {
+            if (File.Exists(targetFile)) return;
 
+            string epsg = Tools.GetEPSG(sourceFile);
+            if (epsg == EPSG4326)
+            {
+                File.Copy(sourceFile, targetFile);
+                return;
+            }
+
+            Reproject(sourceFile, targetFile, epsg, EPSG4326);
+        }
 
         /// <summary>
         /// 鍒涘缓楂樼▼鍒囩墖
@@ -153,6 +160,38 @@
         }
 
         /// <summary>
+        /// 鍒涘缓layer.json
+        /// </summary>
+        private static string CreateLayerJson(XYZArgs args, List<string> files, ref string err)
+        {
+            string dirPath = GetTerrainPath(args.dircode);
+            string vrt = Path.Combine(dirPath, "subs", "dem.vrt");
+            CreateVrt(files, vrt);
+            if (!File.Exists(vrt)) return null;
+
+            string ctbPath = Tools.GetSetting("ctbPath");
+            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}\"", ctbPath, dirPath, vrt);
+            Tools.ExecCmd(new List<string>() { gdal_data, createLayer });
+
+            return vrt;
+        }
+
+        /// <summary>
+        /// 鍒涘缓VRT
+        /// </summary>
+        private static void CreateVrt(List<string> files, string vrt)
+        {
+            string str = string.Join("\r\n", files);
+            string filelist = vrt.Replace("dem.vrt", "list.txt");
+            File.WriteAllText(filelist, str);
+
+            string gdalPath = Tools.GetSetting("gdalPath");
+            string cmd = string.Format("\"{0}\\gdalbuildvrt.exe\" -input_file_list \"{1}\" \"{2}\" -overwrite", gdalPath, filelist, vrt);
+            Tools.ExecCmd(new List<string> { cmd });
+        }
+
+        /// <summary>
         /// 鑾峰彇鍦板舰鏈�澶х骇鍒�
         /// </summary>
         private static int GetTerrainMaxLevel(XYZArgs args, string tifFile)

--
Gitblit v1.9.3