DotSpatial入门

一、DotSpatial是什么

DotSpatial是一个基于.Net Framework 4.0 版本编写的一个地理信息系统(GIS)库,以UserControl的形式提供地图控件。它可帮助开发人员把空间数据、空间分析的功能加入到他们的应用程序中,还可以帮助开发人员把地理信息系统功能扩展到社区。目前Dans Ames是DotSpatial的项目经理。

DotSpatial可以帮助您:

在WinForm或者ASP.NET中打开地图
打开用Shapefile格式存储的矢量图以及栅格、位图等形式表示的地图
渲染符号,不重叠的标签
简单的坐标系统转换,包括大地坐标系与投影坐标系之间的转换,内置支持北京54、西安80、国家CGCS2000等等投影坐标系
操作和显示属性数据
科学分析
读取GPS数据
官方提供的源代码目前还不能:

地图绘图系统绘制除点、线、多边形以外的几何图形,包括椭圆形、扇形、弧形
以谷歌的900913坐标系为代表的WebMecator系列投影坐标系与其它坐标系之间的互相转换
多次空间测量,圆形、扇形等多种图形的面积测量
DotSpatial绘图消息外部单独刷新某一个图层,不支持异步加载地图数据后展示

二、准备工作

1、开发工具
开发WinForm程序当然得VisualStudio
2、地图控件库
下载DotSpatial库进行引用或者源代码进行编译。DotSpatial的下载地址是:http://dotspatial.codeplex.com/,现在迁移到了GItHub上https://github.com/DotSpatial/DotSpatial

不过Winform程序嘛,直接引用-右键-Nuget包管理,下载对应模块就好了。不过nuget包,同样版本的,有时候在工具栏里不显示(手动黑人问号?),原因未知。

3、数据
进行地图控件开发当然得要必须的GIS数据。找了CSDN上别人提供的一个数据集。下载链接如下:http://download.csdn.net/detail/caoshiying/9602626

Demo

贴一个我的Demo,试验的时候可能有点小bug没有做修改。。。故仅供参考

功能包括
* 1、添加图层、图层清空
* 2、平移、放大、缩小、全图
* 3、坐标定位
* 4、以鼠标点为中心点重绘
* 5、shp转bmp
* 6、屏幕坐标转地理坐标

using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Projections;
using DotSpatial.Symbology;
using DotSpatial.Topology;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;


namespace DotSpatialTest
{
    public partial class Form1 : Form
    {
        private Map mapCtrl = null;
        private Label coordLabelCtrl = null;

        public Form1()
        {
            InitMapControl();
            InitCoordinateLabel();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Controls.Add(mapCtrl);
            coordLabelCtrl.Top = mapCtrl.Height - coordLabelCtrl.Height;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            mapCtrl.Size = this.Size;
            if (mapCtrl.Layers.SelectedLayer != null)
            {

            }
            coordLabelCtrl.Top = mapCtrl.Height - coordLabelCtrl.Height;
        }

        private void InitMapControl()
        {
            mapCtrl = new Map()
            {
                Name = "mapCtrl",
                Dock = DockStyle.Fill
            };

            mapCtrl.GeoMouseMove += mapCtrl_GeoMouseMove;
            mapCtrl.MouseClick += mapCtrl_GeoMouseClick;
        }

        private void InitCoordinateLabel()
        {
            coordLabelCtrl = new Label()
            {
                Name = "coordLabelCtrl",
                Text = "X:00.0000,Y:00.0000",
                Width = 200,
                BackColor = Color.Transparent
            };
            mapCtrl.Controls.Add(coordLabelCtrl);
        }

        void mapCtrl_GeoMouseMove(object sender, GeoMouseArgs e)
        {
            string locStr = "X:" + e.GeographicLocation.X.ToString("F6");
            locStr += "Y:" + e.GeographicLocation.Y.ToString("F6");
            coordLabelCtrl.Text = locStr;
        }

        void mapCtrl_GeoMouseClick(object sender,MouseEventArgs e)
        {
            if (mapCtrl.FunctionMode == FunctionMode.None)
            {
                GeoMouseArgs args = new GeoMouseArgs(e, mapCtrl); //屏幕坐标到地图坐标转换
                var _startPoint = e.Location;//屏幕起始点坐标
                var _geoStartPoint = args.GeographicLocation;//地图起始点坐标

                var curViewExtent = mapCtrl.ViewExtents;
                var curCenter = curViewExtent.Center;

                mapCtrl.ViewExtents.SetCenter(_geoStartPoint);
                mapCtrl.Refresh();
            }
        }

        private void btnPan_Click(object sender, EventArgs e)
        {
            mapCtrl.FunctionMode = FunctionMode.Pan;
        }

        private void btnZoomIn_Click(object sender, EventArgs e)
        {
            mapCtrl.FunctionMode = FunctionMode.ZoomIn;
        }

        private void btnZoomOut_Click(object sender, EventArgs e)
        {
            //窗口缩放后,可能需要先zoom in,然后才能zoom out
            mapCtrl.FunctionMode = FunctionMode.ZoomOut;
        }

        private void btnMapNone_Click(object sender, EventArgs e)
        {
            var aa = mapCtrl.FunctionMode;
            mapCtrl.FunctionMode = FunctionMode.None;
        }

        private void ZoomToCoordinates()
        {
            var xx = mapCtrl.ViewExtents;
        }

        private void btnZoom2Coordinates_Click(object sender, EventArgs e)
        {
            ZoomToCoordinatesDialog xxx = new ZoomToCoordinatesDialog(mapCtrl);
            xxx.ShowDialog();
        }

        private void btnAddLayer_Click(object sender, EventArgs e)
        {
            var shp = Shapefile.OpenFile("Shapefiles\\tttbou2_4l.shp");
            shp.Projection = KnownCoordinateSystems.Geographic.World.WGS1984;
            var layer = mapCtrl.Layers.Add(shp) as MapLineLayer;
            layer.Symbolizer = new DotSpatial.Symbology.LineSymbolizer(Color.FromArgb(0x33, 0x33, 0x33), 1);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            mapCtrl.ClearLayers();
        }

        private void btnZoom2MaxExtent_Click(object sender, EventArgs e)
        {
            mapCtrl.ZoomToMaxExtent();
            mapCtrl.Refresh();
        }

        private void btnCenterAt_Click(object sender, EventArgs e)
        {

        }

        private void btnBmp_Click(object sender, EventArgs e)
        {
            var test=mapCtrl.SnapShot();
            test.Save("Shapefiles\\test0.bmp");
        }

        private void btnTest_Click(object sender, EventArgs e)
        {

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值