1、SharpMap简介
SharpMap是一个基于.net Framework使用C#开发的Map渲染类库,可以渲染ESRI Shape、PostGIS、MS SQL等格式的GIS数据,通过扩展地图数据Provider,还可以支持其他格式,例如由其他作者开发的 Oracle Spatial provider。SharpMap可应用于桌面和Web程序。
2、官网和git地址
官网:http://sharpmap.codeplex.com/
git地址:https://github.com/SharpMap/SharpMap
3、环境
vs2010/vs2013
4、入门学习
为了先建立一个直观的软件基本能力的概念,先根据其官网提供的入门教程和例子数据简单走下tutorail
4.1,新建工程,放上地图容器控件MapBox
即新建一个winform应用程序,通过 NuGet Package Manager添加SharpMap and SharpMap.UI 引用,并将SharpMap.UI通过工具箱的选择项添加到工具箱里然后将MapBox控件拖拽到当前项目主窗口中,停靠方式为平铺,并设置背景为白色;
4.2 直接添加一个shp数据到显示控件上
<span style="white-space:pre"> </span> //Create the layer
SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
//Assign the data source
vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"E:\开源GIS\SharpMap\states_ugl\states_ugl.shp", true);
//Add layer to map
mapBoxControl.Map.Layers.Add(vlay);
mapBoxControl.Map.ZoomToExtents();
mapBoxControl.Refresh();
4.3、为图层设置唯一值符号化渲染风格样式
//Create the style for Land
SharpMap.Styles.VectorStyle landStyle = new SharpMap.Styles.VectorStyle();
landStyle.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(232, 232, 232));
//Create the style for Water
SharpMap.Styles.VectorStyle waterStyle = new SharpMap.Styles.VectorStyle();
waterStyle.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(198, 198, 255));
//Create the theme items
Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, SharpMap.Styles.IStyle>();
styles.Add("land", landStyle);
styles.Add("water", waterStyle);
//Assign the theme
vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle);
4.4、添加wms类型图层,数据源来自esri
SharpMap.Layers.WmsLayer wmsL = new SharpMap.Layers.WmsLayer( "US Cities",
"http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer");
//Force PNG format. Else we can't see through
wmsL.SetImageFormat("image/png");
//Force version 1.1.0
wmsL.Version = "1.1.0";
//Add layer named 2 in the service (Cities)
wmsL.AddLayer("2");
//Set the SRID
wmsL.SRID = 4326;
//Add layer to map
mapBoxControl.Map.Layers.Add(wmsL);
<img src="https://img-blog.csdn.net/20160107004207121?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
4.5、添加一个切片图层作为地图背景面
ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);
vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
mapBoxControl.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer(
new BruTile.Web.OsmTileSource(), "OSM"));
4.6 添加一个自定义工具来响应鼠标右键按下事件显示当前地图坐标
Event | Delegate | Description |
---|---|---|
MouseMove | MouseEventHandler | Fires when mouse moves over the map |
MouseDown | MouseEventHandler | Fires when the map receives a mouse click |
MouseUp | MouseEventHandler | Fires when mouse is released |
MouseDrag | MouseEventHandler | Fires when mouse is dragging |
MapRefreshed | EventHandler | Fires when map has been refreshed |
MapChanging | CancelEventHandler | Fires when map is about to change |
MapChanged | EventHandler | Fires when map has changed |
MapZoomChanged | MapZoomHandler | Fires when the map zoom was changed |
MapZooming | MapZoomHandler | Fires when the map zoom is changing |
MapQueried | MapQueryHandler | Fires for each layer selected for query. This is depending on QueryLayerIndex and QuerySettings |
MapQueryStarted | EventHandler | Fires when a query request has started |
MapQueryDone | EventHandler | Fires when a query request has been finished |
MapCenterChanged | MapCenterChangedHandler | Fires when the map's center has changed |
ActiveToolChanged | ActiveToolChangedHandler | Fires when one of MapBox' builtin tools has been selected |
GeometryDefined | GeometryDefinedHandler | Fires when a new geometry has been defined |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpMap.Forms;
using System.ComponentModel;
using GeoAPI.Geometries;
using System.Windows.Forms;
namespace CreateFormWithMapControl
{
public abstract class CustomMapTool
{
private MapBox _mapControl;
private bool _enabled;
/// <summary>
/// Event raised when the <see cref="MapControl"/> is about to change.
/// </summary>
public event CancelEventHandler MapControlChanging;
/// <summary>
/// Event invoker for the <see cref="MapControlChanging"/> event
/// </summary>
/// <remarks>Override this method to unwire <see cref="MapBox"/>s events. Don't forget to call <c>base.OnMapControlChanging(cea);</c> to invoke the event.</remarks>
/// <param name="cea">The cancel event arguments</param>
protected virtual void OnMapControlChanging(CancelEventArgs cea)
{
var h = MapControlChanging;
if (h != null) h(this, cea);
}
/// <summary>
/// Event raised when the <see cref="MapControl"/> has changed.
/// </summary>
public event EventHandler MapControlChanged;
/// <summary>
/// The event invoker
/// </summary>
/// <remarks>Override this method to wire to <see cref="MapBox"/>s events. Don't forget to call <c>base.OnMapControlChanged(e);</c> to invoke the event.</remarks>
/// <param name="e">The event arguments</param>
protected virtual void OnMapControlChanged(EventArgs e)
{
var h = MapControlChanged;
if (h != null) h(this, e);
}
/// <summary>
/// Gets or sets a value indicating the map control
/// </summary>
public MapBox MapControl
{
get { return _mapControl; }
set
{
if (value == _mapControl)
return;
var cea = new CancelEventArgs(false);
OnMapControlChanging(cea);
if (cea.Cancel) return;
_mapControl = value;
OnMapControlChanged(EventArgs.Empty);
}
}
/// <summary>
/// Event raised when <see cref="Enabled"/> changed.
/// </summary>
public event EventHandler EnabledChanged;
/// <summary>
/// Event invoker for the <see cref="EnabledChanged"/> event.
/// </summary>
/// <param name="e"></param>
protected virtual void OnEnabledChanged(EventArgs e)
{
var h = EnabledChanged;
if (h != null) h(this, e);
}
/// <summary>
/// Gets or sets a value indicating if the tool is enabled or not
/// </summary>
public bool Enabled
{
get { return _mapControl != null && _enabled; }
set
{
if (value == _enabled)
return;
_enabled = value;
OnEnabledChanged(EventArgs.Empty);
}
}
}
public class PopupTool : CustomMapTool
{
protected override void OnMapControlChanging(CancelEventArgs cea)
{
base.OnMapControlChanging(cea);
if (cea.Cancel) return;
if (MapControl == null) return;
MapControl.MouseDown -= HandleMouseDown;
}
protected override void OnMapControlChanged(EventArgs e)
{
base.OnMapControlChanged(e);
if (MapControl == null) return;
MapControl.MouseDown += HandleMouseDown;
}
private void HandleMouseDown(Coordinate worldpos, MouseEventArgs imagepos)
{
if (!Enabled)
return;
MessageBox.Show(string.Format("You clicked at {0}!", worldpos));
Enabled = false;
}
}
}
4.7、主窗口完整源码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CreateFormWithMapControl
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
//Create the layer
SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
//Assign the data source
vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"E:\开源GIS\SharpMap\states_ugl\states_ugl.shp", true);
//Create the style for Land
SharpMap.Styles.VectorStyle landStyle = new SharpMap.Styles.VectorStyle();
landStyle.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(232, 232, 232));
//Create the style for Water
SharpMap.Styles.VectorStyle waterStyle = new SharpMap.Styles.VectorStyle();
waterStyle.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(198, 198, 255));
//Create the theme items
Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, SharpMap.Styles.IStyle>();
styles.Add("land", landStyle);
styles.Add("water", waterStyle);
//Assign the theme
vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>("class", styles, landStyle);
//Add layer to map
mapBoxControl.Map.Layers.Add(vlay);
SharpMap.Layers.WmsLayer wmsL = new SharpMap.Layers.WmsLayer( "US Cities",
"http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer");
//Force PNG format. Else we can't see through
wmsL.SetImageFormat("image/png");
//Force version 1.1.0
wmsL.Version = "1.1.0";
//Add layer named 2 in the service (Cities)
wmsL.AddLayer("2");
//Set the SRID
wmsL.SRID = 4326;
//Add layer to map
mapBoxControl.Map.Layers.Add(wmsL);
ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);
vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
mapBoxControl.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer(
new BruTile.Web.OsmTileSource(), "OSM"));
mapBoxControl.Map.ZoomToExtents();
mapBoxControl.Refresh();
mapBoxControl.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
}
private void MainForm_Load(object sender, EventArgs e)
{
var pt = new PopupTool();
pt.MapControl = mapBoxControl;
pt.Enabled = true;
}
}
}