SharpMap简单试用

SharpMap是.Net上比较出名的开源GIS包了,功能中规中矩,支持各种数据源,但也有一些基础的Buffer之类的空间分析至今没有实现(?)。

打算在接下来的一段时间仔细看看.Net上的开源GIS,除了SharpMap还有DotSpatial、GMap等等,最好自己也能实现一个简单的GIS(比如几何对象模型比SFS还简单的那种,呵呵)。

这次先试了下SharpMap,主要功能都在SharpMap.dll中,模仿项目主页上HowTo部分,实现了加载数据(ShapeFile),拉框选中要素并显示属性表等功能。

代码:


using System;
using System.Drawing;
using System.Windows.Forms;
using SharpMap;
using SharpMap.Data;
using SharpMap.Data.Providers;
using SharpMap.Geometries;
using SharpMap.Layers;
using Point = System.Drawing.Point;

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

            // map and layers
            var map
                = new Map(new Size(584, 537))
                      {
                          BackColor = Color.White,
                      };
            var layerChina
                = new VectorLayer("china")
                      {
                          DataSource = new ShapeFile(@".\data\bou1_4l.shp"),
                          SRID = 4326,
                      };
            layerChina.Style.Line.DashPattern = new[] { 4.0f, 2.0f };
            var layerCity
                = new VectorLayer("city")
                      {
                          DataSource = new ShapeFile(@".\data\res1_4m.shp"),
                          SRID = 4326,
                          Style = { PointSize = 5 },
                      };

            map.Layers.Add(layerChina);
            map.Layers.Add(layerCity);
            map.ZoomToExtents();

            this.pictureBox1.BackgroundImage = map.GetMap();
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Center;

            // display coordinate
            this.pictureBox1.MouseMove
                += (s, e) =>
                       {
                           var mapPoint = map.ImageToWorld(new PointF(e.X, e.Y));
                           var strCoor = String.Format("({0},{1})--({2},{3})", e.X, e.Y, mapPoint.X, mapPoint.Y);
                           this.lblCoor.Text = strCoor;
                       };

            // select and display attributes
            bool inSelect = false;
            var p1 = new Point();
            this.pictureBox1.MouseDown
                += (s, e) =>
                       {
                           p1 = e.Location;
                           inSelect = true;
                       };

            this.pictureBox1.MouseMove
                 += (s, e) =>
                        {
                            if (!inSelect) return;
                            var p2 = e.Location;
                            var rect = new System.Drawing.Rectangle(p1.X, p1.Y, Math.Abs(p2.X - p1.X), Math.Abs(p1.Y - p2.Y));
                            var brush = new System.Drawing.SolidBrush(Color.Blue);
                            var g = this.pictureBox1.CreateGraphics();
                            var p = new Pen(brush);
                            g.DrawRectangle(p, rect);
                            pictureBox1.Refresh();
                            g.Dispose();
                            p.Dispose();
                        };


            this.pictureBox1.MouseUp
                += (s, e) =>
                       {
                           var bbox = new BoundingBox(map.ImageToWorld(p1), map.ImageToWorld(e.Location));
                           var ds = new FeatureDataSet();
                           layerCity.DataSource.Open();
                           layerCity.DataSource.ExecuteIntersectionQuery(bbox, ds);
                           dataGridView1.DataSource = ds.Tables[0];
                           layerCity.DataSource.Close();
                           inSelect = false;
                       };
        }
    }
}

截图:

分享到:
  1. Pingback: hm@suzhou:文字篇 « 收容站

  2. 您好,我是您的粉丝~
    从之前的您的《我应该怎么做我的GIS项目》开始关注您,觉得您对GIS很有想法。
    我是12届武大的GIS研究生,马上就要开学了。在GIS学习上,希望能与您多交流下,指引下迷茫的我。我的QQ:942535021,谢谢。