1.创建工程文件,选窗体应用程序,命名为:EsriShpReader

2.添加SplitContainer控件到窗体

3.在SplitContainer.panel1中添加两个按钮Button,text属性分别改为“打开”,“刷新”

4. 在SplitContainer.panel2中添加panel容器,用来显示图像

Dock设置为Fill,背景颜色设置为白色

5.界面图如下:

6.在过程文件中添加类文件,命名为ShpClass

编写点、线、面、三个类

class Point//点类

{

public double X;

public double Y;

}

class Polyline//线类

{

public double[] Box=new double[4];

public int NumParts;

public int NumPoints;

public ArrayList Parts; //在部分中第一个点的索引

public ArrayList Points; //所有部分的点

}

class Polygon : Polyline//面类

{ }

7.在Form1中添加

ArrayList polygons = new ArrayList();//面集合

ArrayList polylines = new ArrayList();//线集合

ArrayList points = new ArrayList();//点集合

Pen pen = new Pen(Color.Black, 1);//定义画笔

int ShapeType;//shp文件类型

int count;//计数

double xmin, ymin, xmax, ymax;

double n1, n2;//x,y轴放大倍数

8.添加button1的Click事件,添加代码

string shpfilepath = "";

openFileDialog1.Filter = "shapefiles(*.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();//<0代表数据长度未知

int FileBanben = br.ReadInt32();

ShapeType = br.ReadInt32();

xmin = br.ReadDouble();

ymax = -1 * br.ReadDouble();

xmax = br.ReadDouble();

ymin = -1 * br.ReadDouble();

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();

while (br.PeekChar() != -1)

{

Point point = new Point();

uint RecordNum = br.ReadUInt32();

int DataLength = br.ReadInt32();

//读取第i个记录

br.ReadInt32();

point.X = br.ReadDouble();

point.Y = -1 * br.ReadDouble();

points.Add(point);

}

StreamWriter sw = new StreamWriter("point.txt");

foreach (Point p in points)

{

sw.WriteLine("{0},{1},{2} ", p.X, -1 * p.Y, 0);

}

sw.Close();

break;

case 3:

polylines.Clear();

while (br.PeekChar() != -1)

{

Polyline polyline = new Polyline();

polyline.Box = new double[4];

polyline.Parts = new ArrayList();

polyline.Points = new ArrayList();

uint RecordNum = br.ReadUInt32();

int DataLength = br.ReadInt32();

//读取第i个记录

br.ReadInt32();

polyline.Box[0] = br.ReadDouble();

polyline.Box[1] = br.ReadDouble();

polyline.Box[2] = br.ReadDouble();

polyline.Box[3] = br.ReadDouble();

polyline.NumParts = br.ReadInt32();

polyline.NumPoints = br.ReadInt32();

for (int i = 0; i < polyline.NumParts; i++)

{

int parts = new int();

parts = br.ReadInt32();

polyline.Parts.Add(parts);

}

for (int j = 0; j < polyline.NumPoints; j++)

{

Point pointtemp = new Point();

pointtemp.X = br.ReadDouble();

pointtemp.Y = -1 * br.ReadDouble();

polyline.Points.Add(pointtemp);

}

polylines.Add(polyline);

}

StreamWriter sw2 = new StreamWriter("line.txt");

count = 1;

foreach (Polyline p in polylines)

{

for (int i = 0; i < p.NumParts; i++)

{

int startpoint;

int endpoint;

if (i == p.NumParts - 1)

{

startpoint = (int)p.Parts[i];

endpoint = p.NumPoints;

}

else

{

startpoint = (int)p.Parts[i];

endpoint = (int)p.Parts[i + 1];

}

sw2.WriteLine("线" + count.ToString() + ":");

for (int k = 0, j = startpoint; j < endpoint; j++, k++)

{

Point ps = (Point)p.Points[j];

sw2.WriteLine("    {0},{1},{2} ", ps.X, -1 * ps.Y, 0);

}

count++;

}

}

sw2.Close();

break;

case 5:

polygons.Clear();

while (br.PeekChar() != -1)

{

Polygon polygon = new Polygon();

polygon.Parts = new ArrayList();

polygon.Points = new ArrayList();

uint RecordNum = br.ReadUInt32();

int DataLength = br.ReadInt32();

//读取第i个记录

int m = br.ReadInt32();

for (int i = 0; i < 4; i++)

{

polygon.Box[i] = br.ReadDouble();

}

polygon.NumParts = br.ReadInt32();

polygon.NumPoints = br.ReadInt32();

for (int j = 0; j < polygon.NumParts; j++)

{

int parts = new int();

parts = br.ReadInt32();

polygon.Parts.Add(parts);

}

for (int j = 0; j < polygon.NumPoints; j++)

{

Point pointtemp = new Point();

pointtemp.X = br.ReadDouble();

pointtemp.Y = -1 * br.ReadDouble();

polygon.Points.Add(pointtemp);

}

polygons.Add(polygon);

}

StreamWriter sw1 = new StreamWriter("polygon.txt");

count = 1;

foreach (Polygon p in polygons)

{

for (int i = 0; i < p.NumParts; i++)

{

int startpoint;

int endpoint;

if (i == p.NumParts - 1)

{

startpoint = (int)p.Parts[i];

endpoint = p.NumPoints;

}

else

{

startpoint = (int)p.Parts[i];

endpoint= (int)p.Parts[i + 1];

}

sw1.WriteLine("多边形" + count.ToString() + ":");

for (int k = 0, j = startpoint; j < endpoint; j++, k++)

{

Point ps = (Point)p.Points[j];

sw1.WriteLine("    {0},{1},{2} ", ps.X, -1 * ps.Y, 0);

}

count++;

}

}

sw1.Close();

break;

}

}

9. 添加button2的Click事件,添加代码

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轴放大倍数

this.panel1.Refresh();

10.添加panel1的paint事件

private void panel1_Paint(object sender, PaintEventArgs e)

{

PointF[] point;

switch (ShapeType)

{

case 1://点类型

foreach (Point p in points)

{

PointF pp = new PointF();

pp.X = (float)(10 + (p.X - xmin) * n1);

pp.Y = (float)(10 + (p.Y - ymin) * n2);

e.Graphics.DrawEllipse(pen, pp.X, pp.Y, 1.5f, 1.5f);

}

break;

case 3://线类型

foreach (Polyline p in polylines)

{

for (int i = 0; i < p.NumParts; i++)

{

int startpoint;

int endpoint;

point = null;

if (i == p.NumParts - 1)

{

startpoint = (int)p.Parts[i];

endpoint = p.NumPoints;

}

else

{

startpoint = (int)p.Parts[i];

endpoint = (int)p.Parts[i + 1];

}

point = new PointF[endpoint - startpoint];

for (int k = 0, j = startpoint; j < endpoint; j++, k++)

{

Point ps = (Point)p.Points[j];

point[k].X = (float)(10 + (ps.X - xmin) * n1);

point[k].Y = (float)(10 + (ps.Y - ymin) * n2);

}

e.Graphics.DrawLines(pen, point);

}

}

break;

case 5://面类型

foreach (Polygon p in polygons)

{

for (int i = 0; i < p.NumParts; i++)

{

int startpoint;

int endpoint;

point = null;

if (i == p.NumParts - 1)

{

startpoint = (int)p.Parts[i];

endpoint = p.NumPoints;

}

else

{

startpoint = (int)p.Parts[i];

endpoint = (int)p.Parts[i + 1];

}

point = new PointF[endpoint - startpoint];

for (int k = 0, j = startpoint; j < endpoint; j++, k++)

{

Point ps = (Point)p.Points[j];

point[k].X = (float)(10 + (ps.X - xmin) * n1);

point[k].Y = (float)(10 + (ps.Y - ymin) * n2);

}

e.Graphics.DrawPolygon(pen, point);

}

}

break;

}

}

11.编译运行

最新文章

  1. Canvas制作天气预报走势图
  2. shell简单用法笔记(一)
  3. Merge在Sqlserver使用例子说明
  4. php7+apache2.4 (Windows7下),成功启动。(楼主另外提供了1个php7集成环境打包: http://pan.baidu.com/s/1qXwjpF2 ,如果你只是想了解一下,放在d盘根目录。)
  5. RxCache 的代码分析,含缓存时间duration的在代码中改变的自己实现的机制
  6. 26.怎样在Swift中定义宏?
  7. button属性值
  8. 转载--初识绘图工具plantUML
  9. Servlet添加
  10. xmlplus 组件设计系列之十 - 网格(DataGrid)
  11. E. Exposition
  12. 关于java中,json字符串转集合和对象,或者集合转json字符串的解决方法
  13. pycharm中的光标变粗的问题
  14. 优雅的玩PHP多进程
  15. C#动态调用泛型类、泛型方法
  16. #工具 Intellij IDEA中自定义的Maven Archetype管理
  17. 1.3:Render Pipeline and GPU Pipeline
  18. php编程 之 php进阶练习
  19. 【算法】二分查找法&amp;大O表示法
  20. Android 各种路径详细说明

热门文章

  1. ☆ fragment和fragmentactivity解析 (转)
  2. POJ 2299 逆序对
  3. 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
  4. HTML的表单元素
  5. Mesh系列文章 - 自定义Mesh
  6. SplendidCRM 如何添加及使用中文语言包
  7. [iOS] UIImage和CGImageRef
  8. QCheckBox 的按钮响应
  9. Json 数据
  10. sql 语句大小写的问题