基于SharpMap扩展程序开发实例
基于SharpMap扩展程序开发实例
SharpMap是基于.Net平台开发的GIS地图渲染组件。在SharpMap的内部设计了基于OGC 标准的几何模型构架,设计了IProvider策略模式的多源矢量地图数据适配器接口,地图要素渲染的底层主要通过几何变换将Geometry转换为.Net支持的几何模型如System.Drawing.Point、System.Drawing.Rectangle、System.Drawing.Drawing2D.GraphicsPath等,然后调用System.Drawing.Graphics类的Draw方法实现地图要素的绘制。在SharpMap内部由于没有设计Symbol的构架,因此,对于需要开发真正的GIS系统,需要封装Symbol架构,以便实现专题渲染和地图符号库。
SharpMap为我们提供了GIS系统最基本的功能集合,如地图可视化、空间查询等功能,因此我们可以利用SharpMap提供的部分功能为我们在.Net平台上实现地图可视化提供支持,而不需要借助一些商业组件。本文就将简单的介绍一些基于SharpMap实现部分程序的代码示例,希望对研究开源的朋友有所启示和帮助。注:我已经对SharpMap部分Bug做了修改,重新设计了系统的构架,也新开发了一些新的模块。这些都将在以后的文章中有所阐述。
基于Jackey.Framework开发Windows应用程序示例
//初始化代码,请在窗体装载事件中调用该方法。 private void init() { //Set buddy control. this.tocControl1.MapControl = this.mapControl1;
//create a vector layer and set the default renderer. SharpMap.Layers.VectorLayer layCity=new SharpMap.Layers.VectorLayer("City"); layCity.DataSource=new SharpMap.Data.Providers.ShapeFileProvider(@"D:"ArcGIS"DeveloperKit"SamplesNET"data"GulfOfStLawrence"data"Can_Mjr_Cities.shp"); ((SharpMap.Rendering.SimpleFeatureRenderer)layCity.Renderer).Symbol=new SharpMap.Symbols.MarkerSymbol(SharpMap.Symbols.SymbolType.Circle,Color.Blue,10f);
//create a vector layer and set the default renderer. SharpMap.Layers.VectorLayer layRoad = new SharpMap.Layers.VectorLayer("Road"); layRoad.DataSource = new SharpMap.Data.Providers.ShapeFileProvider(@"D:"ArcGIS"DeveloperKit"SamplesNET"data"GulfOfStLawrence"data"mjrroads.shp"); ((SharpMap.Rendering.SimpleFeatureRenderer)layRoad.Renderer).Symbol = new SharpMap.Symbols.LineSymbol(Color.Green, 2f);
//create a vector layer and set the default renderer. SharpMap.Layers.VectorLayer layCoasts = new SharpMap.Layers.VectorLayer("Coasts"); layCoasts.DataSource = new SharpMap.Data.Providers.ShapeFileProvider(@"D:"ArcGIS"DeveloperKit"SamplesNET"data"GulfOfStLawrence"data"Coasts.shp"); ((SharpMap.Rendering.SimpleFeatureRenderer)layCoasts.Renderer).Symbol = new SharpMap.Symbols.FillSymbol(Color.LightCyan);
//add the layer to the map this.mapControl1.Map.Layers.Add(layCoasts); this.mapControl1.Map.Layers.Add(layRoad); this.mapControl1.Map.Layers.Add(layCity);
//zoom the map to the full extent this.mapControl1.ZoomToFullExtent(); } |
开发Asp.Net应用程序示例
页面代码为:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page { private SharpMap.Map myMap;
/// <summary> /// Creates the map, inserts it into the cache and sets the ImageButton Url /// </summary> private void GenerateMap() { //Save the current mapcenter and zoom in the viewstate ViewState.Add("mapCenter", myMap.Center); ViewState.Add("mapZoom", myMap.Zoom);
//Render map System.Drawing.Image img = myMap.GetMap();
//img.Save("c:""testsharpmapimg.bmp"); string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img); imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID); }
protected void Page_Load(object sender, EventArgs e) { //Set up the map. We use the method in the App_Code folder for initializing the map myMap = MapHelper.InitializeImageMap(new System.Drawing.Size((int)imgMap.Width.Value, (int)imgMap.Height.Value));
//页面首次被加载 if (!Page.IsPostBack){ GenerateMap(); } else{ //Page is post back. Restore center and zoom-values from viewstate myMap.Center = (SharpMap.Geometries.Point)ViewState["mapCenter"]; myMap.Zoom = (double)ViewState["mapZoom"]; } }
protected void Button1_Click(object sender, EventArgs e) { //ZoomIn SharpMap.Geometries.BoundingBox bbox=myMap.VisibleBounds; bbox.Expand(0.5,0.5,true); myMap.ZoomToBox(bbox); GenerateMap(); } protected void Button2_Click(object sender, EventArgs e) { //ZoomOut SharpMap.Geometries.BoundingBox bbox = myMap.VisibleBounds; bbox.Expand(1.5, 1.5, true); myMap.ZoomToBox(bbox); GenerateMap(); } protected void Button4_Click(object sender, EventArgs e) { //FullExtent myMap.ZoomToExtents(); GenerateMap(); } protected void imgMap_Click1(object sender, ImageClickEventArgs e) { //Set center of the map to where the client clicked myMap.Center = myMap.ToMapPoint(e.X, e.Y); GenerateMap(); } } |
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.Drawing.Drawing2D;
/// <summary> /// Summary description for CreateMap /// </summary> public class MapHelper { public static SharpMap.Map InitializeImageMap(System.Drawing.Size size) { //Initialize a new map of size 'imagesize' SharpMap.Map map = new SharpMap.Map(size);
SharpMap.Layers.GdalRasterLayer imgLayer = new SharpMap.Layers.GdalRasterLayer("Aster", HttpContext.Current.Server.MapPath(@"~"App_data"aster.tif")); map.Layers.Add(imgLayer);
//Set up the countries layer SharpMap.Layers.VectorLayer layCoal = new SharpMap.Layers.VectorLayer("Coal"); layCoal.DataSource = new SharpMap.Data.Providers.ShapeFileProvider(HttpContext.Current.Server.MapPath(@"~"App_data"huoqu.shp"),true); ((SharpMap.Rendering.SimpleFeatureRenderer)layCoal.Renderer).Symbol = new SharpMap.Symbols.FillSymbol(Color.Red); map.Layers.Add(layCoal);
//Set the datasource to a shapefile in the App_data folder SharpMap.Layers.VectorLayer layRoad=new SharpMap.Layers.VectorLayer("Road"); layRoad.DataSource = new SharpMap.Data.Providers.ShapeFileProvider(HttpContext.Current.Server.MapPath(@"~"App_data"daolu.shp"), true); ((SharpMap.Rendering.SimpleFeatureRenderer)layRoad.Renderer).Symbol = new SharpMap.Symbols.LineSymbol(Color.Blue); map.Layers.Add(layRoad);
map.ZoomToExtents(); return map; } } |
下面是我定制实现的示例。
图片上载的麻烦,请点击这里下载。