1 基于NET 20的GIS开源项目SharpMap分析手记_第1页
1 基于NET 20的GIS开源项目SharpMap分析手记_第2页
1 基于NET 20的GIS开源项目SharpMap分析手记_第3页
1 基于NET 20的GIS开源项目SharpMap分析手记_第4页
1 基于NET 20的GIS开源项目SharpMap分析手记_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于.NET 2.0的GIS开源项目SharpMap分析手记(一):第一次亲密接触SharpMap介绍:SharpMap是一个“小巧可爱”的基于.net 2.0使用C#开发的Map渲染类库,可以渲染各类GIS数据(目前支持ESRI Shape和PostGIS格式),可应用于桌面和Web程序。其网址为:http:/sharpmap.iter.dk/ 和/SharpMap SharpMap的发布许可(License)为GNU General Public License,开发者为Morten Nielsen(http:/www.iter.dk/)。目前的稳定版本为0.8(9.0beta已发布),代码行数近10000行,实现了以下功能:支持的数据格式:PostGreSQL/PostGIS,ESRI Shapefile支持WMS layersu 支持ECW 和JPEG2000 栅格数据格式Windows Forms 控件,可以移动和缩放通过HttpHandler支持ASP.net程序点、线、多边形、多点、多线和多多边形等几何类型和几何集合(GeometryCollections)等OpenGIS Simple Features Specification 可通过Data Providers(增加数据类型支持)、Layer Types(增加层类型)和Geometry Types等扩展 图形使用GDI+渲染,支持anti-aliased等专题图 上面这段话是根据马兄的BLOG整理的,如果不希望我引用请马兄告知我删除。 下面就是实用的STEP BY STEP。(1)下载VS .NET 2005由于SharpMap基于.NET 2.0,为了分析SharpMap,首先要作的就是下载一个VS .NET 2005,这个比较大,估计有1.6G,所以建议找个速度快点的网站或者(呵呵,武汉的朋友去电脑城地下室找找,长沙的朋友去QQ电脑城4楼逛逛);(2)安装VS .NET 2005还算比较好装,我装的时候没碰到什么问题,一次成功;(3)设置IIS ASP.NET为2.0这点必须注意,因为默认的IIS ASP.NET为1.1(我安装操作系统的是Windows Server 2003);做法是打开IIS信息服务管理器,在“默认网站”上单击右键选择“属性”,弹出如下对话框,在ASP.NET页设置ASP.NET的版本为2.0.50727;图1设置IIS ASP.NET为2.0(4)下载SharpMap到/SharpMap去下载一个SharpMap,我下载的是0.9版;(5)解压和试运行解压后可以看到一个demo文件夹,在VS 2005中打开Simple.aspx,在VS 2005中单击右键,选择“在浏览器中查看”,则弹出浏览器如下图所示:图2简单地图浏览图3 样式图浏览图4 分级图浏览图5 饼图浏览图6 AJAX应用图7 WMS客户端应用图8 WMS服务器地图图9 实时坐标变换图10 坐标变换测试本文来自CSDN博客,转载请标明出处:/suen/archive/2006/11/29/1420455.aspx基于.NET 2.0的GIS开源项目SharpMap分析手记(二):源代码总体结构分析通过前面的“第一次亲密接触”,我们对SharpMap的功能已经有一个总体的了解,现在我们再来分析SharpMap的源代码总体结构。解压下载的0.9版源代码,有两个文件夹:SharpMap和SharpMap.UI,如图1所示。 图1 SharpMap源代码结构 其中SharpMap.UI是用户界面相关命名空间,如窗体Forms、Ajax等。SharpMap工程是主体,包括数据转换、坐标、数据、几何体、图层等命名空间,下面分别介绍:SharpMap命名空间,包括Map类,通过创建Map对象的实例来生成地图。Map对象由包含Layer对象组成Layers集合,通过GetMap方法来Render地图。Converts命名空间,提供数据转换服务。CoordinateSystems命名空间,提供坐标系统及其投影和转换。Data命名空间,提供对各种数据支持,现在包括MSSQL和ShapeFile支持。Providers名称空间,包括了IProvider接口和Shape文件、PostGIS数据的读取实现。该名称空间为SharpMap提供数据读(写)支持,通过面向接口的设计,可以比较容易的增加各类数据格式。Geometries命名空间,包括了SharpMap要使用到的各种几何类及其接口类,例如点、线、面等类。是SharpMap的基础之一,所有几何对象都继承自Geometry这个抽象类,其中定义了几何对象应该具备的公共操作,例如大小、ID、外接矩阵、几何运算等等。Layers命名空间,提供各种图层支持,包括注记层、矢量层等。Layer是一个抽象类,实现了ILayer接口,Layer目前有3个子类,分别是VectorLayer、LabelLayer和WmsLayer,分别代3种不同数据类型的图层。Rendering命名空间,目前包括矢量渲染器类和几个专题图渲染器类,该类可以将几何对象根据其Style设置渲染为一个System.Drawing.Graphics对象。 Styles命名空间,该命名空间主要提供了图层的样式设置类,例如线样式、点样式、填充样式等。Utilities名称空间包括Algorithms类(目前仅实现了一个方法);Providers类,是Provider的一个Helper,应用了反射机制;Surrogates主要用于系统的Pen和Brush的序列化;Transform提供了从图片坐标到地理坐标的互相变换,也即桌面GIS的二次开发中经常使用的屏幕坐标和地理坐标的转换,主要用于地图的渲染、交互操作等。Utilities.SpatialIndexing用于对象的空间索引, Web名称空间实现了HttpHandler和Caching类,用于网络环境。Web命名空间,包括对网络支持如HTTP等,Web.Wms提供对WMS的支持。SharpMap.UI工程包括:Forms名称空间,包含MapImage控件,一个简单的User Control(用户控件),封装了Map类,用于Windows Form编程。Web.UI.Ajax提供对Ajax支持。 本文来自CSDN博客,转载请标明出处:/suen/archive/2006/12/05/1431269.aspx基于.NET 2.0的GIS开源项目SharpMap分析手记(三):地图渲染分析1 运行过程我们通过实例来讲述SharpMap的运行过程和渲染(绘制)机制。首先打开Simple.aspx,可知此页面有一组单选框(3个,分别是放大、缩小和漫游)和一个图像按钮,用于显示地图。它的代码在Simple.aspx.cs中。打开Simple.aspx.cs,在Page_Load函数中是页面初始化代码。可知地图生成分两步:1.1 初始化地图/Set up the map. We use the method in the App_Code folder for initializing the mapmyMap = MapHelper.InitializeMap(new System.Drawing.Size(int)imgMap.Width.Value,(int)imgMap.Height.Value);我们找到MapHelper.InitializeMap函数,发现地图的初始化分为以下几步:(1)创建地图,创建图层/Initialize a new map of size imagesizeSharpMap.Map map = new SharpMap.Map(size); /Set up the countries layerSharpMap.Layers.VectorLayer layCountries = new SharpMap.Layers.VectorLayer(Countries);/Set the datasource to a shapefile in the App_data folderlayCountries.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(App_datacountries.shp), true); (2)基本的图层显示设置/Set fill-style to greenlayCountries.Style.Fill = new SolidBrush(Color.Green);/Set the polygons to have a black outlinelayCountries.Style.Outline = System.Drawing.Pens.Black;layCountries.Style.EnableOutline = true;layCountries.SRID = 4326; (3)加入图层到地图/Add the layers to the map object./The order we add them in are the order they are drawn, so we add the rivers last to put them on topmap.Layers.Add(layCountries);map.Layers.Add(layRivers);map.Layers.Add(layCities);map.Layers.Add(layLabel);map.Layers.Add(layCityLabel); (4)地图放缩、背景、中心等设置/limit the zoom to 360 degrees widthmap.MaximumZoom = 360;map.BackColor = Color.LightBlue; map.Zoom = 360;map.Center = new SharpMap.Geometries.Point(0,0); 1.2 绘制并生成地图我们回到Simple.aspx.cs的Page_Load函数,发现下一步调用GenerateMap();/This is the initial view of the map. Zoom to the extents of the map:/myMap.ZoomToExtents();/or center on 0,0 and zoom to full earth (360 degrees)/myMap.Center = new SharpMap.Geometries.Point(0,0);/myMap.Zoom = 360;/Create the mapGenerateMap();在同一文件中,GenerateMap()包含以下两步:(1)保存当前地图状态/Save the current mapcenter and zoom in the viewstateViewState.Add(mapCenter, myMap.Center);ViewState.Add(mapZoom, myMap.Zoom);(2)渲染地图/Render mapSystem.Drawing.Image img = myMap.GetMap();string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);imgMap.ImageUrl = getmap.aspx?ID= + HttpUtility.UrlEncode(imgID);它的核心就是通过myMap.GetMap()创建了一个地图图片。我们来看看这个GetMap()函数。在Map.cs文件中找到GetMap()函数,它分为以下几步:创建图像,得到图像绘制环境参数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 ? Layers0.SRID : -1); /Get the SRID of the first layerfor (int i = 0; i = this.Zoom & _Layersi.MinVisible this.Zoom)_Layersi.Render(g, this);关键在于_Layersi.Render(g, this),它是ILayer接口的一个函数,我们找VectorLayer的实现来看看,它分为两种,一种是专题地图,一种是非专题地图绘制。对于专题地图,它针对SharpMap.Data.FeatureDataTable的每一个feature进行绘制,先绘制其外框,再绘制内部几何体。对于非专题地图,它针对this.DataSource.GetGeometriesInView(envelope)得到的几何体进行绘制,也是先绘制其外框,再绘制内部几何体。最后的绘制工作都调用SharpMap.Rendering.VectorRenderer类的函数完成。SharpMap.Rendering.VectorRenderer类包含绘制注记、线串、多线串、多点、多多边形、点、多边形等。绘制好图层后将激发LayerRendered事件:if(LayerRendered!=null) LayerRendered(this, g); /Fire event绘制好地图后将激发Map类的MapRendered 事件:if (MapRendered != null) MapRendered(g); /Fire render event2 SharpMap.Rendering.VectorRenderer的各个函数具体分析现在我们对渲染地图的全过程已经有了个总体概念和初步理解,下面来具体看一下SharpMap.Rendering.VectorRenderer的各个函数:2.1 DrawLabel / / Renders a label to the map. / / Graphics reference / Label placement / Offset of label in screen coordinates / Font used for rendering / Font forecolor / Background color / Color of halo / Text rotation in degrees / Text to render / Map referencepublic static void DrawLabel(System.Drawing.Graphics g, System.Drawing.PointF LabelPoint, System.Drawing.PointF Offset, System.Drawing.Font font, System.Drawing.Color forecolor, System.Drawing.Brush backcolor, System.Drawing.Pen halo, float rotation, string text, SharpMap.Map map)这里面要注意的是这是绘制使用的函数是DrawPath和FillPath,而且是作者从DrawString改进的,可能是为了绘制过程的统一,因为其它几何体的具体绘制也使用DrawPath。if (halo != null)g.DrawPath(halo, path);g.FillPath(new System.Drawing.SolidBrush(forecolor), path);/g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), LabelPoint.X, LabelPoint.Y);2.2 DrawLineString/ / Renders a LineString to the map./ / Graphics reference/ LineString to render/ Pen style used for rendering/ Map referencepublic static void DrawLineString(System.Drawing.Graphics g, Geometries.LineString line, System.Drawing.Pen pen, SharpMap.Map map)这个函数很简单,也是采用DrawPath进行具体绘制。2.3 DrawMultiLineString/ / Renders a MultiLineString to the map./ / Graphics reference/ MultiLineString to be rendered/ Pen style used for rendering/ Map referencepublic static void DrawMultiLineString(System.Drawing.Graphics g, Geometries.MultiLineString lines, System.Drawing.Pen pen, SharpMap.Map map)它通过循环调用DrawLineString来进行绘制。2.4 DrawMultiPoint/ / Renders a to the map./ / Graphics reference/ MultiPoint to render/ Symbol to place over point/ The amount that the symbol should be scaled. A scale of 1 equals to no scaling/ Symbol offset af scale=1/ Symbol rotation in degrees/ Map referencepublic static void DrawMultiPoint(System.Drawing.Graphics g, Geometries.MultiPoint points, System.Drawing.Bitmap symbol, float symbolscale, System.Drawing.PointF offset, float rotation, SharpMap.Map map)它通过循环调用DrawPoint来进行绘制。2.5 DrawMultiPolygon/ / Renders a multipolygon byt rendering each polygon in the collection by calling DrawPolygon./ / Graphics reference/ MultiPolygon to render/ Brush used for filling (null or transparent for no filling)/ Outline pen style (null if no outline)/ Specifies whether polygon clipping should be applied/ Map referencepublic static void DrawMultiPolygon(System.Drawing.Graphics g, Geometries.MultiPolygon pols, System.Drawing.Brush brush, System.Drawing.Pen pen, bool clip, SharpMap.Map map)它通过循环调用DrawPolygon来进行绘制。2.6 DrawPoint/ / Renders a point to the map./ / Graphics reference/ Point to render/ Symbol to place over point/ The amount that the symbol should be scaled. A scale of 1 equals to no scaling/ Symbol offset af scale=1/ Symbol rotation in degrees/ Map referencepublic static void DrawPoint(System.Drawing.Graphics g, SharpMap.Geometries.Point point, System.Drawing.Bitmap symbol, float symbolscale, System.Drawing.PointF offset, float rotation, SharpMap.Map map)其中,symbol参数为一个位图,代表点的符号,这说明SharpMap暂时只支持位图符号,不支持矢量符号绘制。最终绘制调用DrawImage。2.7 DrawPolygon/ / Renders a polygon to the map./ / Graphics reference/ Polygon to render/ Brush used for filling (null or transparent for no filling)/ Outline pen style (null if no outline)/ Specifies whether polygon clipping should be applied/ Map referencepublic static void DrawPolygon(System.Drawing.Graphics g, SharpMap.Geometries.Polygon pol, System.Drawing.Brush brush, System.Drawing.Pen pen, bool clip, SharpMap.Map map)它只支持用不同的画刷和画笔来绘制多边形,也是采用DrawPath进行具体绘制。3 总结结合源代码,本文对SharpMap的渲染机制进行了初步探讨。现阶段SharpMap(0.9版)只支持位图点符号和基本的线和多边形风格,对于矢量符号还没有什么支持。这对于网上电子地图还是可以的,但如果要用于打印出版就略嫌不足,希望以后能扩充这方面。另外,对于用XAML渲染地图和分级图(Gradient Map)等,也将放在以后做进一步讨论。基于.NET 2.0的GIS开源项目SharpMap分析手记(四):地图数据访问机制分析前面初略分析了SharpMap的渲染机制,下面再来分析下它的数据访问机制,SharpMap的数据访问机制有两个关键:Provider模式和空间索引。1 运行机制分析SharpMap中矢量图层类(SharpMap.Layers.VectorLayer)和注记层(SharpMap.Layers.LabelLayer)的数据源属性(DataSource)其实就是一个IProvider接口(SharpMap.Data.Providers.IProvider):/ / Gets or sets the datasource/ public SharpMap.Data.Providers.IProvider DataSource get return _DataSource; set _DataSource = value; 因此,SharpMap的所有数据操作都是在IProvider上进行。下面来看看数据的初始化。1.1 数据源的初始化我们再来看看MapHelper.cs文件中的InitializeMap函数,其中图层数据初始化如下:/Set the datasource to a shapefile in the App_data folderlayCountries.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(App_datacountries.shp), true);即生成一个ShapeFile类来初始化DataSource。初始化代码在ShapeFile.cs中,分为三步:(1)初始化DBF文件/Initialize DBFstring dbffile = _Filename.Substring(0, _Filename.LastIndexOf(.) + .dbf;if (File.Exists(dbffile)dbaseFile = new DbaseReader(dbffile);(2)解析shape文件头/Parse shape headerParseHeader(); (3)读取投影文件/Read projection fileParseProjection();1.2 数据源的打开数据源的打开使用DataSource的Open函数。/ / Opens the datasource/ public void Open()ShapeFile的Open函数分为两步:(1)初始化Shape文件主要是InitializeShape函数,其主要功能是装载空间索引:LoadSpatialIndex(FileBasedIndex); /Load spatial index以后将对空间索引进行介绍。(2)打开DBF文件if (dbaseFile != null)dbaseFile.Open();1.3 相交查询执行相交查询的是IProvider接口的ExecuteIntersectionQuery函数。/ / Returns all objects whose boundingbox intersects bbox./ / / / Please note that this method doesnt guarantee that the geometries returned actually intersect bbox, but only/ that their boundingbox intersects bbox./ / / / / public void ExecuteIntersectionQuery(SharpMap.Geometries.BoundingBox bbox, SharpMap.Data.FeatureDataSet ds)它分为以下几步:(1)得到bbox范围框中的所有对象ID/Use the spatial index to get a list of features whose boundingbox intersects bboxList objectlist = GetObjectIDsInView(bbox);这个函数的实现如下:/Use the spatial index to get a list of features whose boundingbox intersects bboxreturn tree.Search(bbox);所以,它实际使用四叉树的搜索功能。以后将在空间索引中予以介绍。(2)根据ID得到属性信息并加入空间数据集SharpMap.Data.FeatureDataTable dt = dbaseFile.NewTable; for (int i = 0; i objectlist.Count; i+) SharpMap.Data.FeatureDataRow fdr = dbaseFile.GetFeature(objectlisti, dt); fdr.Geometry = ReadGeometry(objectlisti); if (fdr.Geometry != null) if (fdr.Geometry.GetBoundingBox().Intersects(bbox) if (FilterDelegate = null | FilterDelegate(fdr) dt.AddRow(fdr);ds.Tables.Add(dt);2 IProvider接口的其它函数(1)关闭函数/ / Closes the datasource/ void

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论