1.最简单的空间对象
点对象是构成其他对象的最基本单元。
GIS的三种点对象如下:
- 节点 (vertex):用于构成其他实体或指代空间中的任意位置。
- 结点 (node):节点的一种,仅指构成折线的起始和终止节点。
- 点实体 (point):单独节点构成的空间对象实体。
2.空间对象变成代码
运行VS,建立一个Windows窗体应用程序。
项目一Lesson_1中的BasicClasses.cs文件包含基本类。
先定义节点和点实体如下:
namespace MyGIS
{
class GISVertex //描述节点
{
double x;
double y;
}
class GISPoint //表示点实体
{
GISVertex Location;//Location是GISVertex的一个实例
}
}
继续定义线实体和面实体:
(List为可变数组,<>中的类代表数组中元素的数据类型。)
class GISLine
{
List<GISVertex> Allvertexs;
}
class GISPolygon
{
List<GISVertex> Allvertices;
}
3.迷你GIS
基本功能:1)空间数据和属性数据的输入。2)空间数据和属性数据的显示。3)根据空间对象查询属性数据。
首先只处理点实体,则修改GISPoint类
(“public GISPoint(GISVertex onevertex, string onestring)”为构造函数,前面必须有public前缀且不能有返回值。这个函数作用是给类的两个成员赋值。)
class GISPoint //表示点实体
{
public GISVertex Location;//Location是GISVertex的一个实例
public string Attribute;
public GISPoint(GISVertex onevertex, string onestring) //给类的两个成员赋值
{
Location = onevertex;
Attribute = onestring;
}
public void DrawPoint(Graphics graphics) //画点实体
{
graphics.FillEllipse(new SolidBrush(Color.Red),
new Rectangle((int)(Location.x) - 3, (int)(Location.y) - 3, 6, 6)); //以3为半径画圆
}
public void DrawAttribute(Graphics graphics)
{
graphics.DrawString(Attribute, new Font("宋体", 20), new SolidBrush(Color.Green), new
PointF((int)(Location.x), (int)(Location.y)));
}
public double Distance(GISVertex anothervertex) //计算该点实体与另一个点实体的直线距离
{
return Location.Distance(anothervertex);
}
}
没有定义Distance函数,完善GISVertex类
class GISVertex //描述节点
{
public double x;
public double y;
public GISVertex(double _x, double _y) //为成员赋值
{
x = _x;
y = _y;
}
public double Distance(GISVertex anothervertex)
{
return Math.Sqrt((x - anothervertex.x) * (x - anothervertex.x) + (y - anothervertex.y) * (y - anothervertex.y));
}
}
完善好后,在解决方案资源管理器中双击【Form.1】,打开窗体设计视图,(ToolBox)分别增加三个Label和TextBox以及一个Button,在属性窗口修改其Text属性。
打开Form1.cs的代码文件,在其类定义中增加一个List类型的points来记录所有点实体。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lesson_1
{
public partial class Form1 : Form
{
List<GISPoint> points = new List<GISPoint>();
public Form1()
{
InitializeComponent();
}
}
}
回到设计视图,双击【添加点实体】按钮,添加一个点击事件的处理函数。
private void button1_Click(object sender, EventArgs e)
{
double x = Convert.ToDouble(textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
string attribute = textBox3.Text;
GISVertex onevertex = new GISVertex(x, y);
GISPoint onepoint = new GISPoint(onevertex, attribute);
Graphics graphics = this.CreateGraphics();
onepoint.DrawPoint(graphics);
onepoint.DrawAttribute(graphics);
points.Add(onepoint);
}
目前已经实现了前两项功能,接下来实现第三项——根据空间对象查询属性信息。
回到Form1设计视图,打开事件列表,选择MouseClick,双击鼠标添加一个鼠标点击事件。
目的是打开一个对话框,显示点击处附近点实体的属性值,如果没有就显示错误提示信息。
private void Form1_MouseClick(object sender, MouseEventArgs e) //在鼠标点击处打开对话框
{
GISVertex onevertex = new GISVertex((double)e.X, (double)e.Y);
double mindistance = Double.MaxValue;
int findid = -1;
for (int i = 0; i < points.Count; i++)
{
double distance = points[i].Distance(onevertex);
if (distance < mindistance)
{
mindistance = distance;
findid = i;
}
}
if (mindistance > 5 || findid == -1)
{
MessageBox.Show("没有点实体或者鼠标点击位置不准确");
}
else
MessageBox.Show(points[findid].Attribute);
}
至此,三个功能就全部实现了。