C# SharpMap 学习总结

281 篇文章 6 订阅
167 篇文章 20 订阅

SharpMap V1.1 For Web教程系列之——地图展示



SharpMap V1.1 For Web教程系列之——地图展示 开篇先说本次的开发环境吧。采用Vs2010,.Net 


Framework 4.0。
为了更好的调试程序,建议在IIS中进行调试及运行,个人非常不喜欢利用VS自己提供的WebServer去调


试程序,而且很多在Web.config中的设置也需要在IIS中才能起到效果!
开发环境我就不要介绍了,先来说说SharpMap的组件要求吧。由于SharpMap的架构一直在变化和改进过


程中,因此参考网络上别人的事例代码,你会发现都运行不起来,不是接口没了,就是命名空间变了,


这点我也希望SharpMap早日稳定下来。
这次使用的SharpMap的版本是V1.1版本,官方意见提供最新稳定版的下载了,官方网址为:


http://sharpmap.codeplex.com/
SharpMap 1.1版本的下载地址为:http://sharpmap.codeplex.com/downloads/get/792797?,发布时间


为2014年12月11日;该版本只是SharpMap的核心库(Core+UI),下载完后,为了Web开发还必须下载一个


Web端的库,本人做完因为这一步走了好多弯路,网络上的教程也没有人写上着一点。在官网的


DOWNLOADS节点下有个下载界面,需要下载SharpMap.Web这个组件。
OK!所需库完成后,下面进行Asp.Net的网站开发!你也可以不看下面的代码,直接下载整个网站。解决


方案下载地址:http://pan.baidu.com/s/1i3vdUcd
打开VS2010,新建一个网站,?新建一个WebForm,我这里命名为“Map.aspx”,下面贴代码:
Map.aspx:地图展示页面


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Map.aspx.cs" Inherits="Map" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>SharpMap测试</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="rblMapTools" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem Value="0">Zoom in</asp:ListItem>
            <asp:ListItem Value="1">Zoom out</asp:ListItem>
            <asp:ListItem Value="2" Selected="True">Pan</asp:ListItem>
        </asp:RadioButtonList>
        <asp:ImageButton runat="server" Width="700" Height="400" ID="imgMap" 
            οnclick="imgMap_Click" />
    </div>
    </form>
</body>
</html>


View Code
Map.aspx.cx:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Map : System.Web.UI.Page
{
    private SharpMap.Map myMap;
    protected void Page_Load(object sender, EventArgs e)
    {
        myMap = MapHelper.InitializeMap(new System.Drawing.Size((int)imgMap.Width.Value, 


(int)imgMap.Height.Value));


        //SharpMap.Map
        if (this.IsPostBack)
        {
            myMap.Center = (GeoAPI.Geometries.Coordinate)ViewState["mapCenter"];
            myMap.Zoom = (double)ViewState["mapZoom"];
        }
        else
        {
            this.generateMap();
        }
    }
    protected void imgMap_Click(object sender, ImageClickEventArgs e)
    {
        myMap.Center = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
        //Set zoom value if any of the zoom tools were selected
        if (rblMapTools.SelectedValue == "0") //Zoom in
            myMap.Zoom = myMap.Zoom * 0.5;
        else if (rblMapTools.SelectedValue == "1") //Zoom out
            myMap.Zoom = myMap.Zoom * 2;
        //Create the map
        this.generateMap();
    }


    private void generateMap()
    {
        ViewState.Add("mapCenter", myMap.Center);
        ViewState.Add("mapZoom", myMap.Zoom);
        //myMap = MapHelper.InitializeMap(new System.Drawing.Size(256, 256));
        System.Drawing.Image img = myMap.GetMap();


        string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);
        
        imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);
    }
}


  Web.Config配置文件,在.Net 4.0下配置文件,红色部分表示这个地方和SharpMap官网以及互联网


上很多教程里面的区别。


<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
      <handlers>
          <add verb="*" name="test" path="GetMap.aspx" type="SharpMap.Web.HttpHandler" 


preCondition="integratedMode"/>
      </handlers>
      <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
</configuration>


MapHelper.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SharpMap;
using System.Drawing;
using SharpMap.Layers;
using SharpMap.Data.Providers;
using SharpMap.Styles;
using System.Drawing.Text;
using SharpMap.Rendering;


using ColorBlend=SharpMap.Rendering.Thematics.ColorBlend;
using Point=GeoAPI.Geometries.Coordinate;
using System.Drawing.Drawing2D;


/// <summary>
/// Summary description for MapHelper
/// </summary>
public class MapHelper
{
    public MapHelper()
    {
    }


    public static Map InitializeMap(Size size)
    {
        HttpContext.Current.Trace.Write("Initializing map...");


        //Initialize a new map of size 'imagesize'
        Map map = new Map(size);


        //Set up the countries layer
        VectorLayer layCountries = new VectorLayer("Countries");
        //Set the datasource to a shapefile in the App_data folder
        layCountries.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~


\App_data\countries.shp"), true);


        //Set fill-style to green
        layCountries.Style.Fill = new SolidBrush(Color.Green);
        //Set the polygons to have a black outline
        layCountries.Style.Outline = Pens.Black;
        layCountries.Style.EnableOutline = true;
        layCountries.SRID = 4326;


        //Set up a river layer
        VectorLayer layRivers = new VectorLayer("Rivers");
        //Set the datasource to a shapefile in the App_data folder
        layRivers.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~


\App_data\rivers.shp"), true);
        //Define a blue 1px wide pen
        layRivers.Style.Line = new Pen(Color.Blue, 1);
        layRivers.SRID = 4326;


        //Set up a river layer
        VectorLayer layCities = new VectorLayer("Cities");
        //Set the datasource to a shapefile in the App_data folder
        layCities.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~


\App_data\cities.shp"), true);
        //Define a blue 1px wide pen
        //layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~


\App_data\Taiyuan\icon.png"));
        layCities.Style.SymbolScale = 0.8f;
        layCities.MaxVisible = 40;
        layCities.SRID = 4326;


        //Set up a country label layer
        LabelLayer layLabel = new LabelLayer("Country labels");
        layLabel.DataSource = layCountries.DataSource;
        layLabel.Enabled = true;
        layLabel.LabelColumn = "Name";
        layLabel.Style = new LabelStyle();
        layLabel.Style.ForeColor = Color.White;
        layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
        layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
        layLabel.MaxVisible = 90;
        layLabel.MinVisible = 30;
        layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
        layLabel.SRID = 4326;
        layLabel.MultipartGeometryBehaviour = 


LabelLayer.MultipartGeometryBehaviourEnum.Largest;


        //Set up a city label layer
        LabelLayer layCityLabel = new LabelLayer("City labels");
        layCityLabel.DataSource = layCities.DataSource;
        layCityLabel.Enabled = true;
        layCityLabel.LabelColumn = "Name";
        layCityLabel.Style = new LabelStyle();
        layCityLabel.Style.ForeColor = Color.Black;
        layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
        layCityLabel.MaxVisible = layLabel.MinVisible;
        layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
        layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
        layCityLabel.Style.Offset = new PointF(3, 3);
        layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
        layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
        layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
        layCityLabel.SRID = 4326;
        layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
        layCityLabel.Style.CollisionDetection = true;


        //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 top
        map.Layers.Add(layCountries);
        map.Layers.Add(layRivers);
        map.Layers.Add(layCities);
        map.Layers.Add(layLabel);
        map.Layers.Add(layCityLabel);




        //limit the zoom to 360 degrees width
        map.MaximumZoom = 360;
        map.BackColor = Color.LightBlue;


        map.Zoom = 360;
        map.Center = new Point(0, 0);


        HttpContext.Current.Trace.Write("Map initialized");
        return map;
    }


    public static Map InitializeTaiyuanMap(Size size)
    {
        HttpContext.Current.Trace.Write("Initializing map...");


        //Initialize a new map of size 'imagesize'
        SharpMap.Map map = new SharpMap.Map(size);


        //设置太原市区域图层
        SharpMap.Layers.VectorLayer layTy = new SharpMap.Layers.VectorLayer("ty");
        layTy.DataSource = new SharpMap.Data.Providers.ShapeFile


(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\区县级行政区划R.shp"), true);
        layTy.Style.Fill = new SolidBrush(Color.FromArgb(242, 239, 233));
        layTy.Style.Outline = System.Drawing.Pens.Black;
        layTy.Style.EnableOutline = true;
        layTy.SRID = 4326;


        //设置镇的图层
        SharpMap.Layers.VectorLayer layZ = new SharpMap.Layers.VectorLayer("z");
        layZ.DataSource = new SharpMap.Data.Providers.ShapeFile


(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\镇.shp"), true);
        layZ.Style.Fill = new SolidBrush(Color.FromArgb(191, 237, 245));
        layZ.Style.Outline = System.Drawing.Pens.Black;
        layZ.Style.EnableOutline = true;
        layZ.SRID = 4326;


        //设置河流的图层
        SharpMap.Layers.VectorLayer layHl = new SharpMap.Layers.VectorLayer("Hl");
        layHl.DataSource = new SharpMap.Data.Providers.ShapeFile


(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\河流湖泊R.shp"), true);
        layHl.Style.Fill = new SolidBrush(Color.FromArgb(151, 219, 242));
        layHl.Style.Outline = System.Drawing.Pens.Black;
        layHl.Style.EnableOutline = true;
        layHl.SRID = 4326;


        //设置国道的图层
        SharpMap.Layers.VectorLayer layGd = new SharpMap.Layers.VectorLayer("gd");
        layGd.DataSource = new SharpMap.Data.Providers.ShapeFile


(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\国道L.shp"), true);
        layGd.Style.Fill = new SolidBrush(Color.FromArgb(255, 254, 169));
        layZ.Style.Outline = System.Drawing.Pens.Black;
        layZ.Style.EnableOutline = true;
        layGd.Style.Line = new Pen(Color.FromArgb(130, 130, 130), 1);
        layGd.SRID = 4326;


        //Set up the countries layer
        SharpMap.Layers.VectorLayer laySd = new SharpMap.Layers.VectorLayer("sd");
        //Set the datasource to a shapefile in the App_data folder
        laySd.DataSource = new SharpMap.Data.Providers.ShapeFile


(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\省道L.shp"), true);


        //Set fill-style to green
        laySd.Style.Fill = new SolidBrush(Color.FromArgb(255, 254, 169));
        //Set the polygons to have a black outline
        laySd.Style.Line = new Pen(Color.FromArgb(130, 130, 130), 1);


        layZ.Style.Outline = System.Drawing.Pens.Gainsboro;
        layZ.Style.EnableOutline = true;


        laySd.SRID = 4326;


        //Set up a river layer
        SharpMap.Layers.VectorLayer layRivers = new SharpMap.Layers.VectorLayer("Rivers");
        //Set the datasource to a shapefile in the App_data folder
        layRivers.DataSource = new SharpMap.Data.Providers.ShapeFile


(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\城市主干道L.shp"), true);


        layRivers.Style.Fill = new SolidBrush(Color.FromArgb(255, 254, 169));
        //Define a blue 1px wide pen
        layRivers.Style.Line = new Pen(Color.FromArgb(130, 130, 130), 1);
        layRivers.SRID = 4326;


        //Set up a river layer
        SharpMap.Layers.VectorLayer layCities = new SharpMap.Layers.VectorLayer("Cities");
        //Set the datasource to a shapefile in the App_data folder
        layCities.DataSource = new SharpMap.Data.Providers.ShapeFile


(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\城市次干道L.shp"), true);
        //Define a blue 1px wide pen
        //layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~


\App_data\Taiyuan\icon.png"));
        layCities.Style.SymbolScale = 0.8f;
        layCities.MaxVisible = 0.2;
        layCities.SRID = 4326;


        //Set up a river layer
        SharpMap.Layers.VectorLayer layDb = new SharpMap.Layers.VectorLayer("db");
        //Set the datasource to a shapefile in the App_data folder
        layDb.DataSource = new SharpMap.Data.Providers.ShapeFile


(HttpContext.Current.Server.MapPath(@"~\App_data\Taiyuan\基础地标.shp"), true);
        //Define a blue 1px wide pen
        //layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~


\App_data\Taiyuan\icon.png"));
        layDb.Style.SymbolScale = 0.8f;
        layDb.MaxVisible = 0.05;
        layDb.SRID = 4326;


        //Set up a 镇 label layer
        SharpMap.Layers.LabelLayer layZLabel = new SharpMap.Layers.LabelLayer("tyz 


labels");
        layZLabel.DataSource = layZ.DataSource;
        layZLabel.Enabled = true;
        layZLabel.LabelColumn = "Name";
        layZLabel.Style = new SharpMap.Styles.LabelStyle();
        layZLabel.Style.ForeColor = Color.White;
        layZLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
        layZLabel.Style.BackColor = new System.Drawing.SolidBrush(Color.Black);
        layZLabel.MaxVisible = 0.1;
        layZLabel.MinVisible = 0.05;
        layZLabel.Style.HorizontalAlignment = 


SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
        layZLabel.SRID = 4326;
        layZLabel.MultipartGeometryBehaviour = 


SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest;


        //Set up a city label layer
        SharpMap.Layers.LabelLayer layCityLabel = new SharpMap.Layers.LabelLayer("City 


labels");
        layCityLabel.DataSource = layCities.DataSource;
        layCityLabel.Enabled = true;
        layCityLabel.LabelColumn = "Name";
        layCityLabel.Style = new SharpMap.Styles.LabelStyle();
        layCityLabel.Style.ForeColor = Color.Black;
        layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
        layCityLabel.MaxVisible = layZLabel.MinVisible;
        layCityLabel.Style.HorizontalAlignment = 


SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left;
        layCityLabel.Style.VerticalAlignment = 


SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom;
        layCityLabel.Style.Offset = new PointF(3, 3);
        layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
        layCityLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
        layCityLabel.SRID = 4326;
        layCityLabel.LabelFilter = 


SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
        layCityLabel.Style.CollisionDetection = true;




        //Set up a city label layer
        SharpMap.Layers.LabelLayer layDbLabel = new SharpMap.Layers.LabelLayer("Db 


labels");
        layDbLabel.DataSource = layDb.DataSource;
        layDbLabel.Enabled = true;
        layDbLabel.LabelColumn = "Name";
        layDbLabel.Style = new SharpMap.Styles.LabelStyle();
        layDbLabel.Style.ForeColor = Color.Black;
        layDbLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
        layDbLabel.MaxVisible = layCityLabel.MinVisible;
        layDbLabel.Style.HorizontalAlignment = 


SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left;
        layDbLabel.Style.VerticalAlignment = 


SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom;
        layDbLabel.Style.Offset = new PointF(3, 3);
        layDbLabel.Style.Halo = new Pen(Color.Yellow, 2);
        layDbLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        layDbLabel.SmoothingMode = SmoothingMode.AntiAlias;
        layDbLabel.SRID = 4326;
        layDbLabel.LabelFilter = 


SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
        layCityLabel.Style.CollisionDetection = true;


        //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 top
        map.Layers.Add(layTy);
        map.Layers.Add(layHl);
        map.Layers.Add(layGd);
        map.Layers.Add(laySd);
        map.Layers.Add(layRivers);
        map.Layers.Add(layCities);
        map.Layers.Add(layDb);
        map.Layers.Add(layZLabel);
        map.Layers.Add(layCityLabel);
        map.Layers.Add(layDbLabel);


        //limit the zoom to 360 degrees width
        map.MaximumZoom = 4;
        map.BackColor = Color.White;


        map.Zoom = 4;
        //map.Center = new SharpMap.Geometries.Point(0, 0);
        map.Center = new Point(112.48, 37.86);


        HttpContext.Current.Trace.Write("Map initialized");
        return map;
    }
}
========

SharpMap创建应用程序教程



首先,下载SharpMap,下载地址:http://sharpmap.codeplex.com/releases/view/116326
下载完成后解压。再下载演示程序所用的地图文件,下载地址:
http://115.com/lb/5lbdeevtkasp
115网盘礼包码:5lbdeevtkasp
下载完解压,得到文件states_ugl.dbf,states_ugl.shp,states_ugl.shx。


一、创建一个包括MapControl的窗体
1、启动vs并且创建一个窗体应用程序。
2、切换至.net4 full framewark。
右击项目,点击属性,在应用程序标签内容内的目标框架处选择.NET Framework 4。
SharpMap创建应用程序教程
3、打开窗口设计
4、在工具箱“常规”下方的空白处右击,然后点击“选择项”。
SharpMap创建应用程序教程
5、在弹出的窗口中,点击“浏览”,选择SharpMap解压出来的文件中的SharpMap.UI.dll,再点击确定



完成之后会看到“常规”下面多了几项,其中包括MapBox。
SharpMap创建应用程序教程
6、拖动MapBox控件至刚才新建的窗全中,将会在窗体上增加一个地图。
这时候,一般会自动添加对SharpMap.dll的引用。
7、选择这个地图,在右侧背景色属性中改为白色以区别窗体本身的颜色。
SharpMap创建应用程序教程


二、在地图上增加一个层
在这一步,将会增加一个层到上一步创建的地图中。
1、如果在上一步中没有自动添加对SharpMap.dll的引用,请右击项目下的“引用”,添加引用。选择


SharpMap.dll。
2、查看窗体的代码,打开代码页。
3、添加以下代码至除始化方法中。


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile("states_ugl.shp", 


true);
            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();


         mapBox1.Refresh();
        }
    }
然后,把第一步中下载的三个地图文件,复制到程序的Debug文件夹中供程序使用。
4、启动调试,就可以看到一个疑似地图的图形了。
5、添加功能工具。


mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
把这行代码加到上面代码的后面。
6、再调试程序,将会看到可以鼠标的中轮滚动来放大或缩小地图。SharpMap创建应用程序教程


三、用UniqueValueRenderer改变层的风格样式。
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile("states_ugl.shp", 


true);


            //创建大陆的样式
            VectorStyle landStyle = new VectorStyle();
            landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232));




            //创建水域的样式。
            VectorStyle waterStyle = new VectorStyle();
            waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255));


            //创建样式组
            Dictionary styles = new Dictionary();
            styles.Add("land", landStyle);
            styles.Add("water", waterStyle);
            //添加样式至层
            vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme("class", 


styles, landStyle);


            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();


            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
        }
    }
再次调试运行,将会看到已经上色了。
SharpMap创建应用程序教程


四、上数据层
SharpMap.Layers.WmsLayer wmsL =
                new SharpMap.Layers.WmsLayer("US 


Cities","http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesR


ivers_USA/MapServer/WMSServer?request=GetCapabilities&service=WMS");
            //设定图标格式
            wmsL.SetImageFormat("image/png");
            //设定版本号
            wmsL.Version = "1.1.1";
            //为wms添加一个名字为2的层
            wmsL.AddLayer("2");
            //设定空间参考识别码,这个东西是什么意思,自己去百度吧。
            wmsL.SRID = 4326;
            mapBox1.Map.Layers.Add(wmsL);
再调试运行,将全看到一个城市的分布图。官网给的那地址打不开了,找了一个相关的。
SharpMap创建应用程序教程
不翻了,累,后面就差一节讲怎么加背景色的了。
========

Gis初学者之sharpmap



今天开始了解sharpmap,来做webgis的项目。为什么要用sharpmap呢?
第一,开源。开源代表着免费,对于那些商业版高达十几万甚至几十万的费用来说,小弟只能选择这个



第二,是.net 的,因为本人一直使用微软系列的东西进行开发,所以选择了这个。


准备工作,需要有vs2008 和 sharpmap 1.1的demo。
IIS 推荐大家提前安装好IIS ,如果是 server的系统 那么会自动安装IIS,如果是xp 系统请使用系统


光盘进行安装。参考下面的地址
http://blog.sina.com.cn/s/blog_4eac972c0100c8zh.html


vs2008 的下载请大家到迅雷去下载。 
sharpmap 1.1 的下载地址是 
http://download.codeplex.com/Project/Download/SourceControlFileDownload.ashx?


ProjectName=SharpMap&changeSetId=64449


sharpmap现在的版本是 2.0 版本。但是2.0版本没有提供web的例子,所以这里还是使用1.1 版本。很多


朋友能在网上找到的是0.9版本的中文,其实差不多。


首先可以直接双击Demo程序里面的 SharpMap.VS2008.sln 使用 vs2008 打开。
然后会有很多的项目被加载,请耐心等待。 加载完项目以后可以使用 ctrl + shift + b 进行编译。如


果下载的是 SharpMap-64449 那么会出现编译错误,提示 SharpMap.Data.Providers.0gr 未定义。这里


为了能正常运行程序,请修改所有这样的报错代码  SharpMap.Data.Providers.0gr 为 


SharpMap.Data.Providers.ShapeFile
到此就可以正常运行了。然后在Demowebsite 上右键选择 “设为启动项目”,然后选择Default.aspx 


然后右键选择设为起始页。然后按F5。 恭喜你现在已经可以正常的使用 sharpmap 的demo程序了。


==========================================================================

Gis初学者之sharpmap(一) 

http://www.cnblogs.com/52x/archive/2010/05/19/1739433.html


创建一个类似于 sample 的那样的程序。


1. 启动vs2008 点新建 -- 项目 


然后选择 web -- asp.net 应用程序。
选择程序要防止的位置 、程序解决方案 和 名称。然后确定。
可以看到程序会自动为您创建很多东西。接下来我们来认识这些东西。


2. default.aspx是默认创建的页面。也是我们今天要进行操作的页面。
首页双击 default.aspx 然后会看到如下的代码


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 


Inherits="Web._Default" %>


<%@ Register assembly="SharpMap.UI" namespace="SharpMap.Web.UI.Ajax" tagprefix="cc1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


然后我们在这里的 div 中间添加代码 
<asp:RadioButtonList ID="rblMapTools" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem Value="0">Zoom in</asp:ListItem>
            <asp:ListItem Value="1">Zoom out</asp:ListItem>
            <asp:ListItem Value="2" Selected="True">Pan</asp:ListItem>
        </asp:RadioButtonList>
        <asp:ImageButton runat="server" Width="700" Height="400" ID="imgMap" 


OnClick="imgMap_Click" />
然后点击左下角的设计。这样会变成设计视图,我们可以看到有一个单选按钮组和一个图片按钮。然后


双击图片按钮。


双击以后会进入程序代码界面。我把注释写在程序里面这样不介绍自动的生成代码了。
会看到如下的代码:


//程序需要使用的类的命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


//使用sharp的类需要引入的命名空间
using SharpMap.Geometries;
using SharpMap.Converters.WellKnownBinary;


//网站的命名空间
namespace Web
{
    //网站的页面对应的管理类的名称
    public partial class _Default : System.Web.UI.Page
    {


        /// <summary>
        /// 页面加载方法,页面加载的时候进行的操作放在这个方法里面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
        }


        /// <summary>
        /// 图片按钮点击触发的方法
        /// </summary>
        /// <param name="sender">触发的对象</param>
        /// <param name="e">事件对象</param>
        protected void imgMap_Click(object sender, ImageClickEventArgs e)
        {
        }
    }
}


ok 现在开始我们的程序了。 在类里面生成私有变量
//定义地图对象
 private SharpMap.Map myMap;


然后从官方给的 demo程序的sample.aspx.cs里面复制代码
private void GenerateMap()
        {
            //Save the current mapcenter and zoom in the viewstate
            ViewState.Add("mapCenter", myMap.Center);
            ViewState.Add("mapZoom", myMap.Zoom);
            //Render map
            this.Label1.Text = myMap.Layers[0].LayerName;
            System.Drawing.Image img = myMap.GetMap();
            string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);
            imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);
        }
到程序里面。


然后在Page_load 方法里面添加代码
myMap = MapHelper.InitializeGradientMap(new System.Drawing.Size((int)imgMap.Width.Value, 


(int)imgMap.Height.Value));
            if (Page.IsPostBack)
            {
                //Page is post back. Restore center and zoom-values from viewstate
                myMap.Center = (SharpMap.Geometries.Point)ViewState["mapCenter"];
                myMap.Zoom = (double)ViewState["mapZoom"];
            }
            else
            {
                GenerateMap();
            }
在imgMap_Click 方法里面添加
//Set center of the map to where the client clicked
            myMap.Center = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
            //Set zoom value if any of the zoom tools were selected
            if (rblMapTools.SelectedValue == "0") //Zoom in
                myMap.Zoom = myMap.Zoom * 0.5;
            else if (rblMapTools.SelectedValue == "1") //Zoom out
                myMap.Zoom = myMap.Zoom * 2;
            //Create the map
            GenerateMap();
接下来,在程序的
 
App_Data上点击右键,然后选择添加现有项。然后在官方Demo程序里面的App_Data 里面的文件全选。然


后点击确定。(这里是为了加载地图,以后我们可能使用别的地图。也使用同样的方法添加就可以了。





ok 到这里我们已经大功告成了。按F5 运行。
这时会出现报错
错误    1    当前上下文中不存在名称“MapHelper”    F:\程序\net\SharpMap\Web


\Default.aspx.cs    29    21    Web
这里提示我们没有 MapHelper 类。
如何解决呢,我们在我们这个图的 
 
带小地图图标的web(这里的web是我创建的网站的名称,如果大家使用别的名称可以选择你创建的网站


的名称)上点击右键选择添加现有项。然后选择官方Demo程序里面的MapHelper 文件。


这时候再F5 运行程序。
这时候会出现一个页面。就是我们的成果了,但是我们会发现,为什么图片是一个小叉叉,没有出现我


们理想的地图呢。。。。。


如何解决这个问题呢,很简单。双击web.config 然后从官方Demo程序里面可以看到有这样的代码
<httpHandlers>
            <add verb="*" path="GetMap.aspx" type="SharpMap.Web.HttpHandler,SharpMap"/>
        </httpHandlers>
我们复制 add 这行到我们网站的 web.config 的httphandlers 的里面。


现在我们F5 运行,是不是已经能看到地图了?而且可以放大缩小,哈哈。今天我们的任务完成了。
========

开源地图 SharpMap

http://www.cnblogs.com/hanwen/p/4067472.html
Step1 创建一个地图控件
1、启动Visual Studio 2012 并创建一个新的Windows应用程序


2、调整项目到.net Framework 4.0全框架


3、打开Form1的设计视图


4、在工具箱底部,常规右击点击“选择项” 


4、浏览SharpMap.UI.dll并添加


 SharpMap的dll和地图文件网盘共享地址:http://pan.baidu.com/s/1hqzG0de (内含Demo)


5、点击确定如图:


6、拖动MapBox控件插入Form1窗体中


7、将mapBox1控件背景色设置为白色,Dock属性设置为Fill
 
Step2 添加一个图层到地图控件
 
1、添加SharpMap.dll到项目


2、添加地图文件到项目
  
3、修改窗体构造函数Fomr1()


public Form1()
        {
            InitializeComponent();
            VectorLayer vlay = new VectorLayer("States")
            {
                DataSource = new ShapeFile(@"path_to_data\states_ugl.shp", true)
            };
            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;//设置平移
        }


4、运行地图可以看到地图,并操作放大、缩小、平移


 Step3 给图层添加样式


1、修改窗体构造函数Fomr2() 参见Dome


public Form2()
        {
            InitializeComponent();


            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data


\states_ugl.shp", true);


            //构造土地样式
            VectorStyle landStyle = new VectorStyle();
            landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232));


            //构造水样式
            VectorStyle waterStyle = new VectorStyle();
            waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255));


            //创建地图
            Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, 


IStyle>();
            styles.Add("land", landStyle);
            styles.Add("water", waterStyle);


            //分配主题
            vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>


("class", styles, landStyle);


            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
        }


2、运行程序,如图


  Step4 添加WMS-层到地图
1、修改From3构造函数()


   调用


http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA


/MapServer/WMSServer 服务器记载数据。


public Form3()
        {
            InitializeComponent();


            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data


\states_ugl.shp", true);


            //构造土地样式
            VectorStyle landStyle = new VectorStyle();
            landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232));


            //构造水样式
            VectorStyle waterStyle = new VectorStyle();
            waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255));


            //创建地图
            Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, 


IStyle>();
            styles.Add("land", landStyle);
            styles.Add("water", waterStyle);


            //分配主题
            vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>


("class", styles, landStyle);


            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;


            SharpMap.Layers.WmsLayer wmsL =new SharpMap.Layers.WmsLayer("US 


Cities","http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesR


ivers_USA/MapServer/WMSServer");
            //转换为PNG
            wmsL.SetImageFormat("image/png");
            //11.0版本
            wmsL.Version = "1.1.0";
            //添加城市图层 服务名称2
            wmsL.AddLayer("2");
            //设置 SRID
            wmsL.SRID = 4326;
            mapBox1.Map.Layers.Add(wmsL);
        }


 2、运行程序如图,显示城镇。


 Step5 添加一个平铺层作为背景
 在这个步骤中,可以结合网上瓦片服务器数据连同本地数据显示。


 1、添加BruTile.dll、ProjNet.dll、GeoAPI.dll 到项目中


 2、添加辅助方法来创建google坐标系


  private  GeoAPI.CoordinateSystems.IProjectedCoordinateSystem GetEPSG900913


(ProjNet.CoordinateSystems.CoordinateSystemFactory csFact)
        {
            List<GeoAPI.CoordinateSystems.ProjectionParameter> parameters = new 


List<GeoAPI.CoordinateSystems.ProjectionParameter>();
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("semi_major", 


6378137.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("semi_minor", 


6378137.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter


("latitude_of_origin", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter


("central_meridian", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("scale_factor", 


1.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter


("false_easting", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter


("false_northing", 0.0));
            GeoAPI.CoordinateSystems.IProjection projection = csFact.CreateProjection


("Google Mercator", "mercator_1sp", parameters);
            GeoAPI.CoordinateSystems.IGeographicCoordinateSystem wgs84 = 


csFact.CreateGeographicCoordinateSystem(
                "WGS 84", ProjNet.CoordinateSystems.AngularUnit.Degrees, 


ProjNet.CoordinateSystems.HorizontalDatum.WGS84, 


ProjNet.CoordinateSystems.PrimeMeridian.Greenwich,
                new GeoAPI.CoordinateSystems.AxisInfo("north", 


GeoAPI.CoordinateSystems.AxisOrientationEnum.North), new GeoAPI.CoordinateSystems.AxisInfo


("east", GeoAPI.CoordinateSystems.AxisOrientationEnum.East)
            );


            GeoAPI.CoordinateSystems.IProjectedCoordinateSystem epsg900913 = 


csFact.CreateProjectedCoordinateSystem("Google Mercator", wgs84, projection, 


ProjNet.CoordinateSystems.LinearUnit.Metre,
              new GeoAPI.CoordinateSystems.AxisInfo("East", 


GeoAPI.CoordinateSystems.AxisOrientationEnum.East), new GeoAPI.CoordinateSystems.AxisInfo


("North", GeoAPI.CoordinateSystems.AxisOrientationEnum.North));
            return epsg900913;
        }


 3、修改构造函数Form4()


        public Form4()
        {
            InitializeComponent();


            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"path_to_data


\states_ugl.shp", true);


            //构造土地样式
            VectorStyle landStyle = new VectorStyle();
            landStyle.Fill = new SolidBrush(Color.FromArgb(232, 232, 232));


            //创造水样式
            VectorStyle waterStyle = new VectorStyle();
            waterStyle.Fill = new SolidBrush(Color.FromArgb(198, 198, 255));


            //创造地图
            Dictionary<string, SharpMap.Styles.IStyle> styles = new Dictionary<string, 


IStyle>();
            styles.Add("land", landStyle);
            styles.Add("water", waterStyle);


            //分配主题
            vlay.Theme = new SharpMap.Rendering.Thematics.UniqueValuesTheme<string>


("class", styles, landStyle);


            mapBox1.Map.Layers.Add(vlay);


            ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory 


ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            ProjNet.CoordinateSystems.CoordinateSystemFactory csFact = new 


ProjNet.CoordinateSystems.CoordinateSystemFactory();
            vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems


(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, GetEPSG900913(csFact));
            vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems


(GetEPSG900913(csFact), ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);




            mapBox1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer(
                new BruTile.Web.OsmTileSource(), "OSM"));


            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;


        }


 4、运行程序,如图:
========
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值