From 6de2aa3b55fb547ca4c28c5be0819d9a5ff8a96c Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期六, 23 九月 2023 16:30:07 +0800
Subject: [PATCH] 添加地形服务

---
 ExportMap/Sources/empty.terrain        |    0 
 /dev/null                              |   41 --------
 ExportMap/App_Start/WebApiConfig.cs    |    6 +
 ExportMap/cs/WebUtils.cs               |  169 +++++++++++++++++++++++++++++++++
 ExportMap/ExportMap.csproj             |    5 
 ExportMap/Controllers/WebController.cs |   69 +++++++++++++
 ExportMap/Web.config                   |   10 +
 7 files changed, 253 insertions(+), 47 deletions(-)

diff --git a/ExportMap/App_Start/WebApiConfig.cs b/ExportMap/App_Start/WebApiConfig.cs
index d35bbad..bc6bfdd 100644
--- a/ExportMap/App_Start/WebApiConfig.cs
+++ b/ExportMap/App_Start/WebApiConfig.cs
@@ -1,4 +1,5 @@
-锘縰sing ExportMap.cs;
+锘縰sing ExportMap.Controllers;
+using ExportMap.cs;
 using MultipartDataMediaFormatter;
 using Newtonsoft.Json.Converters;
 using System;
@@ -37,7 +38,10 @@
             timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
             GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(timeConverter);
 
+            // ServicePoint 瀵硅薄鍏佽鐨勬渶澶у苟鍙戣繛鎺ユ暟銆傚浜庢墭绠$殑搴旂敤绋嬪簭锛岄粯璁よ繛鎺ラ檺鍒朵负 10锛屽浜庢墍鏈夊叾浠栧簲鐢ㄧ▼搴忥紝榛樿杩炴帴闄愬埗涓� 2銆�
             //System.Net.ServicePointManager.DefaultConnectionLimit = 20; // 2
+            
+            //config.MessageHandlers.Add(new WebHandler());
         }
     }
 }
diff --git a/ExportMap/Controllers/TerraController.cs b/ExportMap/Controllers/TerraController.cs
deleted file mode 100644
index 0578ef6..0000000
--- a/ExportMap/Controllers/TerraController.cs
+++ /dev/null
@@ -1,41 +0,0 @@
-锘縰sing System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net;
-using System.Net.Http;
-using System.Web.Http;
-
-namespace ExportMap.Controllers
-{
-    public class TerraController : ApiController
-    {
-        // http://localhost/ExportMap/terra/dem/layer.json
-        [Route("terra/{path}")]
-        [HttpGet]
-        public string GetLayerJson(string path)
-        {
-            return "layer.json";
-        }
-
-        // http://localhost/ExportMap/terra/dem/3/1/2.terrain
-        [Route("terra/{path}/{z}/{x}/{y}")]
-        [HttpGet]
-        public string GetTerrain(string path, int z, int x, int y)
-        {
-            return ".terrain";
-        }
-
-        /*[Route("api/order/{id:int=3}/ordertype")]
-        [HttpGet]
-        public IHttpActionResult GetById(int id)
-        {
-            return Ok<string>("Success" + id);
-        }
-
-        [AcceptVerbs("GET", "POST")]
-        public IHttpActionResult GetById(int id)
-        {
-            return Ok<string>("Success" + id);
-        }*/
-    }
-}
diff --git a/ExportMap/Controllers/WebController.cs b/ExportMap/Controllers/WebController.cs
new file mode 100644
index 0000000..273a251
--- /dev/null
+++ b/ExportMap/Controllers/WebController.cs
@@ -0,0 +1,69 @@
+锘縰sing ExportMap.cs;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Web.Http;
+
+namespace ExportMap.Controllers
+{
+    //[RoutePrefix("Web")]
+    public class WebController : ApiController
+    {
+        // http://localhost/ExportMap/web/dem/layer.json
+        [Route("web/{path}/layer.json")]
+        [HttpGet]
+        public string GetLayerJsonTest(string path)
+        {
+            return path + "/layer.json";
+        }
+
+        // http://localhost/ExportMap/web/dem/3/1/2.terrain
+        [Route("web/{path}/{z}/{x}/{y}.terrain")]
+        [HttpGet]
+        public string GetTerrainTest(string path, int z, int x, int y)
+        {
+            return path + "/" + z + "/" + x + "/" + y + ".terrain";
+        }
+
+
+        /*[Route("api/order/{id:int=3}/ordertype")]
+        [HttpGet]
+        public IHttpActionResult GetById(int id)
+        {
+            return Ok<string>("Success" + id);
+        }
+
+        [AcceptVerbs("GET", "POST")]
+        public IHttpActionResult GetById(int id)
+        {
+            return Ok<string>("Success" + id);
+        }*/
+
+
+        // http://localhost/ExportMap/terra/layer.json?path=3d/terrain/dem/t
+        [Route("terra/layer.json")]
+        [HttpGet]
+        public HttpResponseMessage GetLayerJson([FromUri]string path)
+        {
+            return WebUtils.GetLayerJson(Request, path);
+        }
+
+        // http://localhost/ExportMap/terra/0/1/0.terrain?path=3d/terrain/dem/t
+        [Route("terra/{z}/{x}/{y}.terrain")]
+        [AcceptVerbs("GET", "POST")]
+        public HttpResponseMessage GetTerrain(int z, int x, int y, [FromUri]string path)
+        {
+            return WebUtils.GetTerrain(Request, path, z, x, y);
+        }
+
+        // http://localhost/ExportMap/terra0/1/3/0.terrain?path=3d/terrain/dem/t
+        [Route("terra0/{z}/{x}/{y}.terrain")]
+        [AcceptVerbs("GET", "POST")]
+        public HttpResponseMessage GetTerrain0(int z, int x, int y, [FromUri]string path)
+        {
+            return WebUtils.GetTerrain0(Request, path, z, x, y);
+        }
+    }
+}
diff --git a/ExportMap/ExportMap.csproj b/ExportMap/ExportMap.csproj
index 58b4c2d..ddc2b07 100644
--- a/ExportMap/ExportMap.csproj
+++ b/ExportMap/ExportMap.csproj
@@ -120,6 +120,7 @@
     <Content Include="js\tumap.js" />
     <Content Include="js\turf.min.6.5.js" />
     <Content Include="lf.html" />
+    <Content Include="Sources\empty.png" />
     <Content Include="Sources\Lengend.png" />
     <Content Include="Sources\logo.png" />
     <Content Include="Sources\merge.py" />
@@ -142,7 +143,7 @@
     <Compile Include="Controllers\FloatServerController.cs" />
     <Compile Include="Controllers\licenseEncryptionController.cs" />
     <Compile Include="Controllers\TBController.cs" />
-    <Compile Include="Controllers\TerraController.cs" />
+    <Compile Include="Controllers\WebController.cs" />
     <Compile Include="Controllers\UploadController.cs" />
     <Compile Include="cs\CacheUtils.cs" />
     <Compile Include="cs\CommonUtils.cs" />
@@ -159,6 +160,7 @@
     <Compile Include="cs\TerrainUtils.cs" />
     <Compile Include="cs\TerraUtils.cs" />
     <Compile Include="cs\Tools.cs" />
+    <Compile Include="cs\WebUtils.cs" />
     <Compile Include="cs\XYZUtils.cs" />
     <Compile Include="db\ModelHandler.cs" />
     <Compile Include="db\PostgreHelper.cs" />
@@ -204,6 +206,7 @@
     </Content>
     <Content Include="Sources\xyz.qgz" />
     <Content Include="TerraBuilder\tb.tbp" />
+    <Content Include="Sources\empty.terrain" />
   </ItemGroup>
   <PropertyGroup>
     <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
diff --git a/ExportMap/Sources/empty.terrain b/ExportMap/Sources/empty.terrain
new file mode 100644
index 0000000..bbaa62e
--- /dev/null
+++ b/ExportMap/Sources/empty.terrain
Binary files differ
diff --git a/ExportMap/Web.config b/ExportMap/Web.config
index 48109e4..d8ad8c7 100644
--- a/ExportMap/Web.config
+++ b/ExportMap/Web.config
@@ -49,6 +49,7 @@
     <pages controlRenderingCompatibilityVersion="4.0"/>
   </system.web>
   <system.webServer>
+    <modules runAllManagedModulesForAllRequests="true"/>
     <security>
       <requestFiltering>
         <!-- 1GB = 1073741824 -->
@@ -60,6 +61,7 @@
       <remove name="OPTIONSVerbHandler"/>
       <remove name="TRACEVerbHandler"/>
       <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
+      <!--<add name="HtmlFileHandler" path="*.json,*.terrain" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>-->
     </handlers>
     <staticContent>
       <remove fileExtension=".wsv"/>
@@ -82,8 +84,8 @@
       <mimeMap fileExtension=".bgltf" mimeType="model/gltf-binary"/>
       <remove fileExtension=".glb"/>
       <mimeMap fileExtension=".glb" mimeType="model/gltf-binary"/>
-      <remove fileExtension=".json"/>
-      <mimeMap fileExtension=".json" mimeType="application/json"/>
+      <!--<remove fileExtension=".json"/>
+      <mimeMap fileExtension=".json" mimeType="application/json"/>-->
       <remove fileExtension=".geojson"/>
       <mimeMap fileExtension=".geojson" mimeType="application/json"/>
       <remove fileExtension=".topojson"/>
@@ -100,8 +102,8 @@
       <mimeMap fileExtension=".kmz" mimeType="application/vnd.google-earth.kmz"/>
       <remove fileExtension=".svg"/>
       <mimeMap fileExtension=".svg" mimeType="image/svg+xml"/>
-      <remove fileExtension=".terrain"/>
-      <mimeMap fileExtension=".terrain" mimeType="application/vnd.quantized-mesh"/>
+      <!--<remove fileExtension=".terrain"/>
+      <mimeMap fileExtension=".terrain" mimeType="application/vnd.quantized-mesh"/>-->
       <remove fileExtension=".ktx"/>
       <mimeMap fileExtension=".ktx" mimeType="image/ktx"/>
       <remove fileExtension=".crn"/>
diff --git a/ExportMap/cs/WebUtils.cs b/ExportMap/cs/WebUtils.cs
new file mode 100644
index 0000000..cd1d6ac
--- /dev/null
+++ b/ExportMap/cs/WebUtils.cs
@@ -0,0 +1,169 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Text;
+using System.Web;
+using System.Web.Http;
+
+namespace ExportMap.cs
+{
+    public class WebUtils
+    {
+        #region LFData
+        private static string _lfData;
+
+        public static string LFData
+        {
+            get
+            {
+                if (string.IsNullOrEmpty(_lfData))
+                {
+                    _lfData = Tools.GetSetting("lfData");
+                }
+
+                return _lfData;
+            }
+        }
+        #endregion
+
+        /// <summary>
+        /// 鑾峰彇鍦板舰鍥惧眰鏂囦欢
+        /// </summary>
+        public static HttpResponseMessage GetLayerJson(HttpRequestMessage Request, string path)
+        {
+            string basePath = Path.Combine(LFData, path);
+            if (!Directory.Exists(basePath))
+            {
+                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "璺緞涓嶅瓨鍦紒"));
+            }
+
+            string file = basePath + "\\layer.json";
+            if (!File.Exists(file))
+            {
+                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "鏂囦欢涓嶅瓨鍦紒"));
+            }
+
+            return DownloadFile(file, "layer.json");
+        }
+
+        /// <summary>
+        /// 鑾峰彇鍦板舰鏂囦欢
+        /// </summary>
+        public static HttpResponseMessage GetTerrain(HttpRequestMessage Request, string path, int z, int x, int y)
+        {
+            string basePath = Path.Combine(LFData, path);
+            if (!Directory.Exists(basePath))
+            {
+                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "璺緞涓嶅瓨鍦紒"));
+            }
+
+            string file = basePath + "\\" + z + "\\" + x + "\\" + y + ".terrain";
+            if (!File.Exists(file))
+            {
+                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "鏂囦欢涓嶅瓨鍦紒"));
+            }
+
+            return DownloadFile(file, y + ".terrain");
+        }
+
+        /// <summary>
+        /// 鑾峰彇鍦板舰鏂囦欢锛屼笉瀛樺湪杩斿洖绌烘枃浠�
+        /// </summary>
+        public static HttpResponseMessage GetTerrain0(HttpRequestMessage Request, string path, int z, int x, int y)
+        {
+            string basePath = Path.Combine(LFData, path);
+            if (!Directory.Exists(basePath))
+            {
+                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "璺緞涓嶅瓨鍦紒"));
+            }
+
+            string file = basePath + "\\" + z + "\\" + x + "\\" + y + ".terrain";
+            if (!File.Exists(file))
+            {
+                return DownloadFile(Tools.BaseDir + "\\Sources\\empty.terrain", y + ".terrain");
+            }
+
+            return DownloadFile(file, y + ".terrain");
+        }
+
+        /// <summary>
+        /// 涓嬭浇鏂囦欢
+        /// </summary>
+        public static HttpResponseMessage DownloadFile(string file, string fileName)
+        {
+            //string fileName = Path.GetFileName(file);
+            //FileStream stream = new FileStream(file, FileMode.Open);
+            FileStream stream = File.OpenRead(file);
+
+            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
+            response.Content = new StreamContent(stream);
+            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
+            response.Content.Headers.ContentDisposition.FileName = HttpUtility.UrlEncode(fileName);
+            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
+
+            //response.Content.Headers.Add("Cache-Control", "max-age=2592000"); // 缂撳瓨1涓湀
+            response.Content.Headers.Expires = DateTimeOffset.Now.AddMonths(1); // new DateTimeOffset(DateTime.Now.AddMonths(1));
+
+            return response;
+        }
+
+        /// <summary>
+        /// Get璇锋眰
+        /// </summary>
+        public static string GetData(string url)
+        {
+            /*WebClient wc = new WebClient();
+            Byte[] pageData = wc.DownloadData(url);
+
+            Encoding coding = Encoding.GetEncoding("UTF-8");
+            string str = coding.GetString(pageData);
+
+            return str;*/
+
+            Uri uri = new Uri(url);
+            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
+            request.Method = "GET";
+            request.ContentType = "application/x-www-form-urlencoded";
+
+            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
+            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
+            string str = reader.ReadToEnd();
+            reader.Close();
+
+            return str;
+        }
+
+        /// <summary>
+        /// Post璇锋眰
+        /// string data = string.Format("token={0}&f=json&where={1}", Token, "");
+        /// </summary>
+        public static string PostData(string url, string data)
+        {
+            Uri uri = new Uri(url);
+            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
+            request.Method = "POST";
+            request.ContentType = "application/x-www-form-urlencoded";
+
+            if (data != null)
+            {
+                byte[] bytes = Encoding.UTF8.GetBytes(data);
+                request.ContentLength = bytes.Length;
+                Stream outstream = request.GetRequestStream();
+                outstream.Write(bytes, 0, bytes.Length);
+                outstream.Flush();
+                outstream.Close();
+            }
+
+            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
+            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
+            string result = reader.ReadToEnd();
+            reader.Close();
+
+            return result;
+        }
+    }
+}

--
Gitblit v1.9.3