转载 基于SharpMap扩展程序开发实例

35 篇文章 1 订阅
4 篇文章 0 订阅

基于SharpMap扩展程序开发实例

转载自:http://www.cnblogs.com/wobushixiaocai/archive/2009/02/24/SharpMap.html

SharpMap是基于.Net平台开发的GIS地图渲染组件。在SharpMap的内部设计了基于OGC标准的几何模型构架,设计了IProvider策略模式的多源矢量地图数据适配器接口,地图要素渲染的底层主要通过几何变换将Geometry转换为.Net支持的几何模型如System.Drawing.PointSystem.Drawing.RectangleSystem.Drawing.Drawing2D.GraphicsPath等,然后调用System.Drawing.Graphics类的Draw方法实现地图要素的绘制。在SharpMap内部由于没有设计Symbol的构架,因此,对于需要开发真正的GIS系统,需要封装Symbol架构,以便实现专题渲染和地图符号库。

SharpMap为我们提供了GIS系统最基本的功能集合,如地图可视化、空间查询等功能,因此我们可以利用SharpMap提供的部分功能为我们在.Net平台上实现地图可视化提供支持,而不需要借助一些商业组件。本文就将简单的介绍一些基于SharpMap实现部分程序的代码示例,希望对研究开源的朋友有所启示和帮助。注:我已经对SharpMap部分Bug做了修改,重新设计了系统的构架,也新开发了一些新的模块。这些都将在以后的文章中有所阐述

基于Jackey.Framework开发Windows应用程序示例

 

//初始化代码,请在窗体装载事件中调用该方法。

privatevoid init()

        {

           //Set buddy control.

           this.tocControl1.MapControl =this.mapControl1;

 

           //create a vector layer and set the default renderer.

           SharpMap.Layers.VectorLayerlayCity=new SharpMap.Layers.VectorLayer("City");

           layCity.DataSource=newSharpMap.Data.Providers.ShapeFileProvider(@"D:"ArcGIS"DeveloperKit"SamplesNET"data"GulfOfStLawrence"data"Can_Mjr_Cities.shp");

            ((SharpMap.Rendering.SimpleFeatureRenderer)layCity.Renderer).Symbol=newSharpMap.Symbols.MarkerSymbol(SharpMap.Symbols.SymbolType.Circle,Color.Blue,10f);

 

           //create a vector layer and set the default renderer.

           SharpMap.Layers.VectorLayerlayRoad = 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.VectorLayerlayCoasts = newSharpMap.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应用程序示例

 

页面代码为:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

 

publicpartial class _Default : System.Web.UI.Page

{

    private SharpMap.MapmyMap;

 

    /// <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.Imageimg = 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(newSystem.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.BoundingBoxbbox=myMap.VisibleBounds;

        bbox.Expand(0.5,0.5,true);

        myMap.ZoomToBox(bbox);

        GenerateMap();

    }

    protected void Button2_Click(object sender, EventArgs e)

{

       //ZoomOut

        SharpMap.Geometries.BoundingBoxbbox = 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();

    }

}

 

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Drawing;

usingSystem.Drawing.Drawing2D;

 

///<summary>

/// Summary description for CreateMap

///</summary>

publicclass MapHelper

{

    public static SharpMap.Map InitializeImageMap(System.Drawing.Sizesize)

    {

        //Initialize a new map of size 'imagesize'

        SharpMap.Map map = new SharpMap.Map(size);

 

        SharpMap.Layers.GdalRasterLayerimgLayer = newSharpMap.Layers.GdalRasterLayer("Aster",HttpContext.Current.Server.MapPath(@"~"App_data"aster.tif"));

        map.Layers.Add(imgLayer);

 

        //Set up the countries layer

        SharpMap.Layers.VectorLayerlayCoal = 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.VectorLayerlayRoad=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;

}

}

下面是我定制实现的示例。

图片上载的麻烦,请点击这里下载。

基于SharpMap扩展程序开发实例

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值