Map类 是核心类 包含着各个图层layers的引用 GetMap进行渲染
除了GetMap渲染方法外 还有
1.Dispose方法 对layers集合内每个图层进行dispose 最后清空集合
public void Dispose() { foreach (SharpMap.Layers.Layer layer in this.Layers) if (layer is IDisposable) ((IDisposable)layer).Dispose(); this.Layers.Clear(); }
2.FindLayer方法 返回图层集合 通过对比每个图层的名字得到要查询的图层集合
public IEnumerable FindLayer(string layername) { foreach (SharpMap.Layers.ILayer l in this.Layers) if (l.LayerName.Contains(layername)) yield return l; }
3.GetLayerByName 通过名字搜索图层
public SharpMap.Layers.ILayer GetLayerByName(string name) { return _Layers.Find(delegate(SharpMap.Layers.ILayer layer) { return layer.LayerName.Equals(name); }); }
4.GetExtents方法 获得全图时的BBox 通过join每个图层的Envelope实现
public SharpMap.Geometries.BoundingBox GetExtents() { if (this.Layers == null || this.Layers.Count == 0) throw (new InvalidOperationException("No layers to zoom to")); SharpMap.Geometries.BoundingBox bbox = null; for (int i = 0; i < this.Layers.Count; i++) { if (bbox == null) bbox = this.Layers[i].Envelope; else bbox = bbox.Join(this.Layers[i].Envelope); } return bbox; }
5.ZoomToBox方法 通过BBox设置新的Zoom与Center
public void ZoomToBox(SharpMap.Geometries.BoundingBox bbox) { this._Zoom = bbox.Width; //Set the private center value so we only fire one MapOnViewChange event if (this.Envelope.Height < bbox.Height) this._Zoom *= bbox.Height / this.Envelope.Height; this.Center = bbox.GetCentroid(); }
6.ZoomToExtents方法 使用ZoomToBox方法把当前视图设置为全图(通过Map.GetExtents获得全图的BBox)
public void ZoomToExtents() { this.ZoomToBox(this.GetExtents()); }
7.ImageToWorld和WorldToImage两个方法 通过工具类Transform实现图片坐标与实际坐标转换
public System.Drawing.PointF WorldToImage(SharpMap.Geometries.Point p) { return Utilities.Transform.WorldtoMap(p, this); } public SharpMap.Geometries.Point ImageToWorld(System.Drawing.PointF p) { return Utilities.Transform.MapToWorld(p, this); }
Transform类只有这两个静态方法
Transform的MapToWorld方法 通过Map.Envelope的最小X值和最大Y值(图片Y坐标方向与地理坐标Y方向相反)获得实际地理坐标
public static SharpMap.Geometries.Point MapToWorld(System.Drawing.PointF p, SharpMap.Map map) { SharpMap.Geometries.BoundingBox env = map.Envelope; return new SharpMap.Geometries.Point(env.Min.X + p.X * map.PixelWidth, env.Max.Y - p.Y * map.PixelHeight); }
map.PixelWidth=PixelSize即每像素代表实际地理坐标长度
public double PixelSize { get { return this.Zoom / this.Size.Width; } } public double PixelWidth { get { return PixelSize; } }
map.PixelHeight即每像素代表实际地理坐标宽度 通过像素比例(像素高宽比默认为1.0)与PixelWidth得到
public double PixelHeight { get { return PixelSize * _PixelAspectRatio; } } private double _PixelAspectRatio = 1.0;
Transform的WorldToImage方法 left top相当于env.Min.X和env.Max.Y
其实现如下:
public static System.Drawing.PointF WorldtoMap(SharpMap.Geometries.Point p, SharpMap.Map map) { //if (map.MapTransform != null && !map.MapTransform.IsIdentity) // map.MapTransform.TransformPoints(new System.Drawing.PointF[] { p }); System.Drawing.PointF result = new System.Drawing.Point(); double Height = (map.Zoom * map.Size.Height) / map.Size.Width; double left = map.Center.X - map.Zoom*0.5; double top = map.Center.Y + Height * 0.5 * map.PixelAspectRatio; result.X = (float)((p.X - left) / map.PixelWidth); result.Y = (float)((top - p.Y) / map.PixelHeight); return result; }
Map的属性里 除了渲染流程里面涉及到的Size Zoom Center Envelop MapHeight以及上面提到的PixelWidth,PixelSize,PixelHeight,PixelAspectRatio之外
还有BackColor;Layers;允许的最大最小Zoom:MinimumZoom MaximumZoom
总结:Map是各种Layer的容器,负责渲染地图(GetMap),设定图片大小(Size),设定地理范围(Zoom+Center或Envelop或ZoomToBox),保存地图最大外框(GetExtent),转换图片坐标与地理坐标,是最重要的用户接口。