📄 map.cs
字号:
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Text;
namespace SharpMap
{
/// <summary>
/// Map class
/// </summary>
/// <example>
/// Creating a new map instance, adding layers and rendering the map:
/// <code lang="C#">
/// SharpMap.Map myMap = new SharpMap.Map(picMap.Size);
/// myMap.MinimumZoom = 100;
/// myMap.BackgroundColor = Color.White;
///
/// SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My layer");
/// string ConnStr = "Server=127.0.0.1;Port=5432;User Id=postgres;Password=password;Database=myGisDb;";
/// myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "myTable", "the_geom", 32632);
/// myLayer.FillStyle = new SolidBrush(Color.FromArgb(240,240,240)); //Applies to polygon types only
/// myLayer.OutlineStyle = new Pen(Color.Blue, 1); //Applies to polygon and linetypes only
/// //Setup linestyle (applies to line types only)
/// myLayer.Style.Line.Width = 2;
/// myLayer.Style.Line.Color = Color.Black;
/// myLayer.Style.Line.EndCap = System.Drawing.Drawing2D.LineCap.Round; //Round end
/// myLayer.Style.Line.StartCap = layRailroad.LineStyle.EndCap; //Round start
/// myLayer.Style.Line.DashPattern = new float[] { 4.0f, 2.0f }; //Dashed linestyle
/// myLayer.Style.EnableOutline = true;
/// myLayer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Render smooth lines
/// myLayer.MaxVisible = 40000;
///
/// myMap.Layers.Add(myLayer);
/// // [add more layers...]
///
/// myMap.Center = new SharpMap.Geometries.Point(725000, 6180000); //Set center of map
/// myMap.Zoom = 1200; //Set zoom level
/// myMap.Size = new System.Drawing.Size(300,200); //Set output size
///
/// System.Drawing.Image imgMap = myMap.GetMap(); //Renders the map
/// </code>
/// </example>
public class Map : IDisposable
{
/// <summary>
/// Used for converting numbers to/from strings
/// </summary>
internal static System.Globalization.NumberFormatInfo numberFormat_EnUS = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
/// <summary>
/// Initializes a new map
/// </summary>
public Map() : this(new System.Drawing.Size(300,150))
{
}
/// <summary>
/// Initializes a new map
/// </summary>
/// <param name="size">Size of map in pixels</param>
public Map(System.Drawing.Size size)
{
this.Size = size;
this.Layers = new List<SharpMap.Layers.ILayer>();
this.BackColor = System.Drawing.Color.Transparent;
this._MaximumZoom = double.MaxValue;
this._MinimumZoom = 0;
_MapTransform = new System.Drawing.Drawing2D.Matrix();
MapTransformInverted = new System.Drawing.Drawing2D.Matrix();
_Center = new SharpMap.Geometries.Point(0, 0);
_Zoom = 1;
_PixelAspectRatio = 1.0;
}
/// <summary>
/// Disposes the map object
/// </summary>
public void Dispose()
{
foreach (SharpMap.Layers.Layer layer in this.Layers)
if (layer is IDisposable)
((IDisposable)layer).Dispose();
this.Layers.Clear();
}
#region Events
/// <summary>
/// EventHandler for event fired when the maps layer list has been changed
/// </summary>
public delegate void LayersChangedEventHandler();
/// <summary>
/// Event fired when the maps layer list have been changed
/// </summary>
public event LayersChangedEventHandler LayersChanged;
/// <summary>
/// EventHandler for event fired when the zoomlevel or the center point has been changed
/// </summary>
public delegate void MapViewChangedHandler();
/// <summary>
/// Event fired when the zoomlevel or the center point has been changed
/// </summary>
public event MapViewChangedHandler MapViewOnChange;
/// <summary>
/// EventHandler for event fired when all layers have been rendered
/// </summary>
public delegate void MapRenderedEventHandler(System.Drawing.Graphics g);
/// <summary>
/// Event fired when all layers have been rendered
/// </summary>
public event MapRenderedEventHandler MapRendered;
#endregion
#region Methods
/// <summary>
/// Renders the map to an image
/// </summary>
/// <returns></returns>
public System.Drawing.Image GetMap()
{
if (Layers == null || Layers.Count == 0)
throw new InvalidOperationException("No layers to render");
System.Drawing.Image img = new System.Drawing.Bitmap(this.Size.Width, this.Size.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
g.Transform = this.MapTransform;
g.Clear(this.BackColor);
g.PageUnit = System.Drawing.GraphicsUnit.Pixel;
int SRID = (Layers.Count > 0 ? Layers[0].SRID : -1); //Get the SRID of the first layer
for (int i = 0; i < _Layers.Count; i++)
{
if (_Layers[i].Enabled && _Layers[i].MaxVisible >= this.Zoom && _Layers[i].MinVisible < this.Zoom)
_Layers[i].Render(g, this);
}
if (MapRendered != null) MapRendered(g); //Fire render event
g.Dispose();
return img;
}
/// <summary>
/// Returns an enumerable for all layers containing the search parameter in the LayerName property
/// </summary>
/// <param name="layername">Search parameter</param>
/// <returns>IEnumerable</returns>
public IEnumerable<SharpMap.Layers.ILayer> FindLayer(string layername)
{
foreach (SharpMap.Layers.ILayer l in this.Layers)
if (l.LayerName.Contains(layername))
yield return l;
}
/// <summary>
/// Returns a layer by its name
/// </summary>
/// <param name="name">Name of layer</param>
/// <returns>Layer</returns>
public SharpMap.Layers.ILayer GetLayerByName(string name)
{
return _Layers.Find(delegate(SharpMap.Layers.ILayer layer) { return layer.LayerName.Equals(name); });
}
/// <summary>
/// Zooms to the extents of all layers
/// </summary>
public void ZoomToExtents()
{
this.ZoomToBox(this.GetExtents());
}
/// <summary>
/// Zooms the map to fit a bounding box
/// </summary>
/// <remarks>
/// NOTE: If the aspect ratio of the box and the aspect ratio of the mapsize
/// isn't the same, the resulting map-envelope will be adjusted so that it contains
/// the bounding box, thus making the resulting envelope larger!
/// </remarks>
/// <param name="bbox"></param>
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();
}
/// <summary>
/// Converts a point from world coordinates to image coordinates based on the current
/// zoom, center and mapsize.
/// </summary>
/// <param name="p">Point in world coordinates</param>
/// <returns>Point in image coordinates</returns>
public System.Drawing.PointF WorldToImage(SharpMap.Geometries.Point p)
{
return Utilities.Transform.WorldtoMap(p, this);
}
/// <summary>
/// Converts a point from image coordinates to world coordinates based on the current
/// zoom, center and mapsize.
/// </summary>
/// <param name="p">Point in image coordinates</param>
/// <returns>Point in world coordinates</returns>
public SharpMap.Geometries.Point ImageToWorld(System.Drawing.PointF p)
{
return Utilities.Transform.MapToWorld(p, this);
}
#endregion
#region Properties
/// <summary>
/// Gets the extents of the current map based on the current zoom, center and mapsize
/// </summary>
public SharpMap.Geometries.BoundingBox Envelope
{
get {
return new SharpMap.Geometries.BoundingBox(
new SharpMap.Geometries.Point(this.Center.X - this.Zoom * .5, this.Center.Y - this.MapHeight * .5),
new SharpMap.Geometries.Point(this.Center.X + this.Zoom * .5, this.Center.Y + this.MapHeight * .5));
}
}
private System.Drawing.Drawing2D.Matrix _MapTransform;
internal System.Drawing.Drawing2D.Matrix MapTransformInverted;
/// <summary>
/// Using the <see cref="MapTransform"/> you can alter the coordinate system of the map rendering.
/// This makes it possible to rotate or rescale the image, for instance to have another direction than north upwards.
/// </summary>
/// <example>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -