图片图层分两种,一种是动态图层,一种就是缓存图层。缓存图层就是已经切好图片,这种方式出图速度快,通常用作底图。关于图层相关概念请参考帮助文档《地图与图层》章节。

自定义缓存图层时,用户需要继承 TiledCachedLayer 类,Resolutions 和 Bounds 属性是必须设置的。如果是 SuperMap 切的图片,已知 Scales ,那么也要将 Scales 转成 Resolutions。最主要的是重写父类的 GetTileUrl 方法和 Initialize 方法,要在 Initialize 中获取必要的参数,在 GetTileUrl 中组织好对应缓存图片的 URL 。

下面是一个扩展 iServer2 缓存图片的例子。




  1. 代码实现:

C#

拷贝代码

public class CustomCachedLayer : TiledCachedLayer
{
private string uriFormat;
public CustomCachedLayer(string cachedUrl, Rectangle2D bounds, double[] scales, Rectangle2D referViewBounds, Rect referViewer, double referScale)
{
this.CachedUrl = cachedUrl;
this.Bounds = bounds;
this.Scales = scales;
this.Dpi = ScaleHelper.GetSmDpi(referViewBounds, referViewer, referScale);
double[] resolutions = new double[scales.Length];
for (int i = 0; i < scales.Length; i++)
{
resolutions[i] = ScaleHelper.ScaleConversion(scales[i], this.Dpi);
}
this.Resolutions = resolutions;
}
public override string GetTileUrl(int indexX, int indexY, int level)
{
string uri = string.Empty;
if (!string.IsNullOrEmpty(this.uriFormat))
{
double scale = 1 / Scales[level];
uri = String.Format(this.uriFormat, scale, indexX, indexY);
}
return uri;
}
public override void Initialize()
{
StringBuilder builder = new StringBuilder();
builder.Append(this.CachedUrl);
builder.AppendFormat("/{0}_{1}x{1}", this.MapName, 512);
builder.Append("/{0}/{1}/{2}.");
builder.AppendFormat("{0}", ImageFormat);
this.uriFormat = builder.ToString();
base.Initialize();
}
public string CachedUrl { get; set; }
public string MapName { get; set; }
private double[] scales = null;
public double[] Scales
{
get { return scales; }
set
{
if (value[0] > 1)
{
for (int i = 0; i < value.Length; i++)
{
value[i] = 1.0 / value[i];
}
}
scales = value;
}
}
}




  1. 定义 CustomCachedLayer 类之后,下面介绍一下使用方法。以下数据必须已知,可以从 Deskpro 中或调用 GetMapStatus 获取:

C#

拷贝代码

string cachedurl="http://localhost:7080/output";
Rectangle2D bounds = new Rectangle2D(-180, -90, 180, 90);
double[] scales = new double[] { 500000, 5000000, 50000000, 500000000 };
Rectangle2D referViewBounds=new Rectangle2D(-66.6154069176247, -49.9740487003936, 66.6154069176247, 49.9740487003936);
Rect referViewer = new Rect(-31984, -31976, 400, 300);
double referScale = 0.000000008987817752221122;
CustomCachedLayer clayer = new CustomCachedLayer(cachedurl, bounds, scales, referViewBounds, referViewer, referScale);
clayer.Url = "http://localhost:7080/demo";
clayer.MapName = "World";
MyMap.Layers.Add(clayer);