扩展DynamicMapServiceLayer和TiledMapServiceLayer,实现访问wms数据和自定义切图数据

一下代码是silverlight写的,flex思路都一样,只是一些方式有些不一样,flex里都是重载属性,flex的例子里都有,自己下载下来看看

1:扩展DynamicMapServiceLayer

    如果后台数据通过geoserver发布wms数据,我们可以通过继承DynamicMapServiceLayer的接口实现访问geoserver发布的wms服务,可以参考http://bbs.esrichina-bj.cn/ESRI/thread-45582-1-1.html

代码如下

调用代码

          /// <summary>
        /// gepserver的wms数据加载
        /// </summary>
        private void LoadWmsMapServiceLayer()
        {
            int KWID = 4326;
            Envelope extent=  new ESRI.ArcGIS.Client.Geometry.Envelope(-124.731, 24.956, -66.97, 49.372);
            extent.SpatialReference = new SpatialReference(KWID);
            string Url = “http://localhost:8080/geoserver/wms”;

      string [] layers=new string []{‘topp:states’};
            WMSMapServiceLayer wms = new WMSMapServiceLayer(extent, KWID, Url,layers);
            this.mymap.Layers.Add(wms);

        }

 

 

自定义wms类

  public class WMSMapServiceLayer:DynamicMapServiceLayer  继承接口,实现GetUrl方法
    {
        List<string> _layers = new List<string>();
        public WMSMapServiceLayer(Envelope extent, int KWID, string url,string [] layers)
        {
            this.FullExtent = extent;    //地图范围
            this.SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(KWID);  //空间参考
            this.Url = url;     wms地址

            this.Version = “1.1.1”;   //wms版本
            this.Layers = layers;    //请求的图层
           // _layers.Add(“topp:states”);

        }

        public string Url { get; set; }
        public string Version { get; set; }
        public string[] Layers
        {  
            get { return _layers.ToArray(); }
            set { _layers = new List<string>(value); OnLayerChanged(); }
        }
      public override void Initialize()
        {
            this.FullExtent = Extent;   设置范围  //不设置就无法实现

 this.SpatialReference = new SpatialReference(KWID);
            base.Initialize();
         
        }

//重写获取数据接口的方法

        public override void GetUrl(ESRI.ArcGIS.Client.Geometry.Envelope extent, int width, int height, DynamicMapServiceLayer.OnUrlComplete onComplete)
        {
            int extentWKID = extent.SpatialReference.WKID;
            StringBuilder mapURL = new StringBuilder();
            mapURL.Append( Url);
            mapURL.Append(“?service=WMS”);
            mapURL.Append(“&request=GetMap”);
            mapURL.AppendFormat(“&width={0}”, width);
            mapURL.AppendFormat(“&height={0}”, height);
            mapURL.AppendFormat(“&format={0}”, “image/png”);
            mapURL.AppendFormat(“&layers={0}”, String.Join(“,”, Layers));
            mapURL.Append(“&styles=”);
            mapURL.AppendFormat(“&bgcolor={0}”, “0xFFFFFF”);
            mapURL.AppendFormat(“&transparent={0}”, “true”);
            mapURL.AppendFormat(“&version={0}”, Version);
            switch (Version)
            {
                case (“1.1.1”): mapURL.AppendFormat(“&SRS=EPSG:{0}”, extentWKID);
                    mapURL.AppendFormat(CultureInfo.InvariantCulture, “&bbox={0},{1},{2},{3}”,
                        extent.XMin, extent.YMin, extent.XMax, extent.YMax); break;
            }
          //把哪些字符串拼接起来,跟openlayer一样的
            onComplete(mapURL.ToString(), width, height, new ESRI.ArcGIS.Client.Geometry.Envelope() 
            { 
                XMin = extent.XMin, YMin = extent.YMin, XMax = extent.XMax, YMax = extent.YMax        
            });      
        }
       
    }

 

 

 

2:扩展TiledMapServiceLayer  ,跟扩展DynamicMapServiceLayer差不多,关键就是设置一些访问必须的属性

 public class CustomTileCache:TiledMapServiceLayer
    {
        public CustomTileCache(Envelope extent, int KWID, string url, int width, int height, int level, double resolution)
        {
            this.Extent = extent;
            this.KWID = KWID;
            this.Url = url;
            this.Width = width;
            this.Height = height;
            this.Resolution = resolution;
            this.Level = level;
        }
        public Envelope Extent { get; set;}
        public int KWID { get; set; }
        public string Url { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public int Level { get; set; }
        public double Resolution { get; set; }
        public override void Initialize()
        {
            this.FullExtent = Extent;   //范围
            this.SpatialReference = new SpatialReference(this.KWID);  空间参考
            this.TileInfo = new TileInfo()      瓦片的具体信息
            {
                Height = Height,  每张瓦片的高度,一般为256或者512
                Width = Width,
                Origin = new MapPoint(this.FullExtent.XMin, this.FullExtent.YMax)   //下图有解释

                {
                    SpatialReference = new SpatialReference(KWID)
                },
                Lods = new Lod[Level]
                //Lod [] lods=new Lod[];
            };
            for (int i = 0; i < this.TileInfo.Lods.Length; i++)
            {

                TileInfo.Lods[i] = new Lod()    设置分辨率。 切图方式不一样,这里就不一样,我是比例尺是1/2方式去切的
                {
                    Resolution = Resolution
                };
                Resolution = Resolution / 2;

            }
            base.Initialize();
        }
        public override string GetTileUrl(int level, int row, int col)
        {

//自己数据的切片规则
            string url = Url + “zoom_” + (level + 1).ToString() + “/x” + col.ToString() + “/” + (level + 1).ToString() + “_” + col.ToString() + “_” + row.ToString() + “.png”;
            return url;

        }
    }

 可以查一些瓦片切图的资料

 

转载自:https://blog.csdn.net/xinruogis/article/details/5567103

You may also like...