• baidu地图API叠加自定义图层(二)- 使用代理层做缓存


    通过结合《使用代理请求Geoserver服务》一文中实现的代理方式,通过代理层实现自定义图层的本地缓存,减少GIS后台服务端WMS实时渲染的性能损耗,提高地图的交互速度。
    为了实现代理层缓存的目的,需要在《使用代理请求Geoserver服务》文中的基础上做出修改,代码参考如下:

    `
    //代理层基类
    public abstract class GeoserverBaseProxy : IHttpHandler
    {
        protected abstract string GeoserverUrl
        {
            get;
        }
    
        public void  ProcessRequest(HttpContext context)
        {
            string targetUrl = GeoserverUrl;
    
            if ( PreProcessRequest(context))
            {
                return;
            }
            HttpRequest sourceRequest = context.Request;
            for (int i = 0, count = sourceRequest.QueryString.Count; i < count; i++)
            {
                targetUrl += sourceRequest.QueryString.Keys[i] + "="  + sourceRequest.QueryString.Get(i) + "&";
            }
            HttpWebRequest targetRequest = (HttpWebRequest)WebRequest.Create(targetUrl);
            targetRequest.UserAgent = sourceRequest.UserAgent;
            targetRequest.ContentType = sourceRequest.ContentType;
            targetRequest.Method = sourceRequest.HttpMethod;
    
            if (targetRequest.Method.ToUpper() == "POST")
            {
                Stream targetInputStream = targetRequest.GetRequestStream();
                Stream sourceInputStream = sourceRequest.InputStream;
                WriteStream(sourceInputStream, targetInputStream);
            }
            HttpWebResponse targetResponse = (HttpWebResponse)targetRequest.GetResponse();
    
            ProcessResponse(targetResponse, context);
        }
    
        protected void WriteStream(Stream inputStream, Stream outputStream)
        {
            byte[] buffer = new byte[16 * 1024];
            int bufferLength = 16 * 1024;
            try
            {
                int ret = inputStream.Read(buffer, 0, bufferLength);
                while (ret > 0)
                {
                    outputStream.Write(buffer, 0, ret);
                    ret = inputStream.Read(buffer, 0, bufferLength);
                }
            }
            finally
            {
                inputStream.Close();
            }
        }
    
        protected virtual bool PreProcessRequest(HttpContext sourceContext)
        {         
            return false;
        }
    
        protected virtual void ProcessResponse(HttpWebResponse inputResponse, HttpContext outputContext)
        {
            HttpResponse outputResponse = outputContext.Response;
            outputResponse.ContentType = inputResponse.ContentType;
            Stream inputStream = inputResponse.GetResponseStream();
            Stream outputStream = outputContext.Response.OutputStream;
    
            WriteStream(inputStream, outputStream);
        }
    
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    
    
         //代理缓存类: 
        public class GeoserverWmsCacheProxy : GeoserverBaseProxy
        {
            private static string _cacheFilePath = @"E:99 sourcesGisOpenLayersDEMOOpenLayersDemo_v_1_1OpenLayersDemoszgas_jd_tiles";
        const string __geoserverUrl = "http://localhost:8080/geoserver/szgas/wms?";
    
        protected override bool PreProcessRequest(HttpContext requestContext)
        {
            bool result = false;
            HttpRequest sourceRequest = requestContext.Request;
            string z = sourceRequest.QueryString["z"];
            string x = sourceRequest.QueryString["x"];
            string y = sourceRequest.QueryString["y"];
    
            string tilePath = Path.Combine(_cacheFilePath, z, x + "_" + y + ".png");
            if (File.Exists(tilePath))
            {
                using (FileStream fs = new FileStream(tilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    Stream outputStream = requestContext.Response.OutputStream;
                    WriteStream(fs, outputStream);
                }
                result = true;
            }
            return result;
        }
    
        protected override void ProcessResponse(HttpWebResponse inputResponse, HttpContext outputContext)
        {
            HttpRequest request = outputContext.Request;
            HttpResponse outputResponse = outputContext.Response;
            outputResponse.ContentType = inputResponse.ContentType;
    
            string z = request.QueryString["z"];
            string x = request.QueryString["x"];
            string y = request.QueryString["y"];
            string tileFoldPath = Path.Combine(_cacheFilePath, z);
            string tilePath = Path.Combine(tileFoldPath, x + "_" + y + ".png");
    
            if (!Directory.Exists(tileFoldPath))
            {
                Directory.CreateDirectory(tileFoldPath);
            }
    
            byte[] buffer = new byte[16 * 1024];
            int bufferLength = 16 * 1024;
            using (Stream cacheFileStream = new FileStream(tilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                using (Stream outputStream = outputContext.Response.OutputStream)
                {
                    using (Stream inputStream = inputResponse.GetResponseStream())
                    {
                        int ret = inputStream.Read(buffer, 0, bufferLength);
                        while (ret > 0)
                        {
                            cacheFileStream.Write(buffer, 0, ret);
                            outputStream.Write(buffer, 0, ret);
                            ret = inputStream.Read(buffer, 0, bufferLength);
                        }
                    }
                }
            }
        }
    
        protected override string GeoserverUrl
        {
            get
            {
                return __geoserverUrl;
            }
        }
    }

    `
    页面代码修改如下,在《baidu地图API叠加自定义图层(一)》的基础上修改一点点,将url的路径修改为代理层路径,同时将xyz通过链接发送到后台,后台以xyz保存瓦片:

    if (x < 0) { x = 'M' + (-x);} if (y < 0) { y = 'M' + (-y);}//根据geoserver WMS服务的规则设置URLvar url = '/GeoserverWmsCacheProxy.ashx?service=WMS&version=1.1.0&request=GetMap&FORMAT=image/png&layers=szgas:jd_baidu&styles=&width=256&height=256&srs=EPSG:3857&TRANSPARENT=true&bbox=' + bbox2.join(',') + '&z=' + zoom + '&x=' + x + '&y=' + y;return url;

    本文只是一个DEMO,使用过程中还应该根据自定义图层的地图范围作出限制,否则会导致缓存的瓦片过多,浪费性能。

  • 相关阅读:
    python中的字符数字之间的转换函数
    java中的this和super的作用和异同和C++中调用父类的构造函数
    夺命雷公狗---Smarty NO:19 html_options函数
    夺命雷公狗---Smarty NO:18 html_checkboxes
    夺命雷公狗---Smarty NO:17 html_table函数
    夺命雷公狗---Smarty NO:16 html_image函数
    夺命雷公狗---Smarty NO:15 fetch函数
    夺命雷公狗---Smarty NO:14 debug调试
    夺命雷公狗---Smarty NO:13 系统自定义函数(plugins)
    夺命雷公狗---Smarty NO:12 section函数
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317745.html
Copyright © 2020-2023  润新知