C#读取shapefile文件(不用AE)
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.编译运行
C#读取shapefile文件(不用AE)的更多相关文章
- AE中Shapefile文件添加到SDE数据集
linder_lee 原文 AE中Shapefile文件添加到SDE数据集(c#) 主要完成用C#,通过AE将本地Shapefile文件导入到SDE的指定数据集下面. 首先说下思路: (1) 通过Op ...
- python-geopandas读取、创建shapefile文件
作者:fungis 描述:一个热带生活.乐于分享.努力搬砖的giser 交流邮箱:fungis@163.com shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Fea ...
- 高效读取大文件,再也不用担心 OOM 了!
内存读取 第一个版本,采用内存读取的方式,所有的数据首先读读取到内存中,程序代码如下: Stopwatch stopwatch = Stopwatch.createStarted(); // 将全部行 ...
- maven下读取资源文件的问题(转)
原文链接:http://shenchao.me/2016/04/20/maven%E4%B8%8B%E8%AF%BB%E5%8F%96%E8%B5%84%E6%BA%90%E6%96%87%E4%BB ...
- C++ AO读取shapefile的属性值
C++ AO读取一个shapefile文件的所有属性值 #include "stdafx.h" #include "iostream.h" #inc ...
- Unity3D移动平台动态读取外部文件全解析
前言: 一直有个想法,就是把工作中遇到的坑通过自己的深挖,总结成一套相同问题的解决方案供各位同行拍砖探讨.眼瞅着2015年第一个工作日就要来到了,小匹夫也休息的差不多了,寻思着也该写点东西活动活动大脑 ...
- SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)
读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...
- 在C#中创建和读取XML文件
1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...
- 读取Excel文件的两种方法
第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...
随机推荐
- Office Word 2013发布带数学公式的博客
今日在自学冈萨雷斯的<数字图像处理>一书,想做笔记来记录,并分享一些MATLAB代码,又加上刚刚注册博客园的账户,便想着如何能够较为方便的来书写博客.本文主要涉及到的问题有: 如何用wor ...
- OracleDBConsoleorcl是具体管什么的服务(转)
这个服务是oracle EM的就是oracle企业管理器这个工具可以通过网页的方式监控数据库,对数据库参数等做修改.里面还有oracle本身对于当前系统内存,SQL,参数等的建议.DBA可以根绝这些建 ...
- UVALive 6885 Flowery Trails 最短路枚举
题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...
- IoC模式(转)
IoC模式 1.依赖 依赖就是有联系,有地方使用到它就是有依赖它,一个系统不可能完全避免依赖.如果你的一个类或者模块在项目中没有用到它,恭喜你,可以从项目中剔除它或者排除它了,因为没有一个地方会依赖它 ...
- javascript优化--04高质量编码
库和API的设计: 在参数设计中保持好的习惯:如顺序,width,height;top,right,bottom,left;如命名: 将undefined看作没有值而不要表示非特定的值: 在允许0,空 ...
- 简单几何(线段与直线的位置) POJ 3304 Segments
题目传送门 题意:有若干线段,问是否存在一条直线,所有线段投影到直线上时至少有一个公共点 分析:有一个很好的解题报告:二维平面上线段与直线位置关系的判定.首先原问题可以转换为是否存在一条直线与所有线段 ...
- 拓扑排序 POJ 1049 Sorting It All Out
题目传送门 /* 拓扑排序裸题:有三种情况: 1. 输入时发现与之前的矛盾,Inconsistency 2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环): ...
- quick 关于触摸的问题
以前遇到一个问题就是,如果触摸层不在最后,会导致触摸失效.这是由于下面添加的层挡住了触摸层,而后添加的层会位于上面,默认是不可点击,点击不可穿透的.所以我们必须将触摸层放置到最上面. Logic.lu ...
- 【转】Profiling application LLC cache misses under Linux using Perf Events
转自:http://ariasprado.name/2011/11/30/profiling-application-llc-cache-misses-under-linux-using-perf-e ...
- 【教程】模拟登陆百度之Java代码版
[背景] 之前已经写了教程,分析模拟登陆百度的逻辑: [教程]手把手教你如何利用工具(IE9的F12)去分析模拟登陆网站(百度首页)的内部逻辑过程 然后又去用不同的语言: Python的: [教程]模 ...