赞
踩
上一篇介绍了Shape files文件结构,在这一篇,以面文件为例,写一下如何读取.shp文件,以面文件为例。
首先在ArcMap里面新建一个名为 三角形面 的 shp文件,用做测试数据。如下
测试数据下载地址为:http://download.csdn.net/detail/gis0911178/9650967
在Arcmap中显示如下
测试数据准备好之后,就可以在C#里面写代码来读取这个shp文件;代码如下
private void button1_Click(object sender, EventArgs e)
{
string shpfilepath = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "shapefile(*.shp)|*.shp|All files(*.*)|*.*"; //打开文件路径
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
shpfilepath = openFileDialog1.FileName;
BinaryReader br = new BinaryReader(openFileDialog1.OpenFile());
//读取文件过程
br.ReadBytes(24);
int FileLength = br.ReadInt32();
Console.WriteLine("文件长度:" + ChangeByteOrder(FileLength));
int FileBanben = br.ReadInt32();
ShapeType = br.ReadInt32();
xmin = br.ReadDouble();
ymin = br.ReadDouble();
xmax = br.ReadDouble();
ymax = br.ReadDouble();
Console.WriteLine("Xmin:" + xmin);
Console.WriteLine("Ymin:" + ymin);
Console.WriteLine("Xmax:" + xmax);
Console.WriteLine("Ymax:" + ymax);
double width = xmax - xmin;
double height = ymax - ymin;
n1 = (float)(this.panel1.Width * 0.9 / width);//x轴放大倍数
n2 = (float)(this.panel1.Height * 0.9 / height);//y轴放大倍数
br.ReadBytes(32);
switch (ShapeType)
{
case 1:
points.Clear();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。