[其它]纯C读取SHAPEFILE.doc

上传人:音乐台 文档编号:1968776 上传时间:2019-01-27 格式:DOC 页数:35 大小:2.59MB
返回 下载 相关 举报
[其它]纯C读取SHAPEFILE.doc_第1页
第1页 / 共35页
[其它]纯C读取SHAPEFILE.doc_第2页
第2页 / 共35页
[其它]纯C读取SHAPEFILE.doc_第3页
第3页 / 共35页
亲,该文档总共35页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《[其它]纯C读取SHAPEFILE.doc》由会员分享,可在线阅读,更多相关《[其它]纯C读取SHAPEFILE.doc(35页珍藏版)》请在三一文库上搜索。

1、综合性实验报告一、 实验名称:Shapefile文件的读写二、 实验目的:能够熟悉基本的原理,掌握基本技能,并能综合运用课程所学的基本知识和技能解决实践中的问题。根据此目标,特设计本综合性实验,作为该课程的完整组成部分。为后续课程有的放矢地开展与安排提供参考。三、 实验准备(数据与软件):开发环境安装:开发环境采用Visual studio 2008.Net,开发语言为C#相关文档阅读: 教材C#程序设计教程,孙践知等编著,清华大学出版社;实验指导书自编;ESRI公开的Shapefile白皮书ESRI Shapefile Technical Description;IBM公开的DBF文件格式。

2、四、 实验原理(方法):一个.shp文件由文件头和记录组成。文件头大小为100个字节,其布局如下表:Position Field Value Type OrderByte 0 File Code 9994 Integer BigByte 4 Unused 0 Integer BigByte 8 Unused 0 Integer BigByte 12 Unused 0 Integer BigByte 16 Unused 0 Integer BigByte 20 Unused 0 Integer BigByte 24 File Length File Length Integer BigByte

3、28 Version 1000 Integer LittleByte 32 Shape Type Shape TypeInteger LittleByte 36 Bounding Box Xmin Double LittleByte 44 Bounding Box Ymin Double LittleByte 52 Bounding Box Xmax Double LittleByte 60 Bounding Box Ymax Double LittleByte 68* Bounding Box Zmin Double LittleByte 76* Bounding Box Zmax Doub

4、le LittleByte 84* Bounding Box Mmin Double LittleByte 92* Bounding BoxMmax Double Little* Unused, with value 0.0, if not Measured or Z typeBig表示大尾(big endian)型字节顺序,即是高低位字节是反序的,主要适用于Sun or Motorola平台,而Little表示小尾(little endian)型字节顺序,高低位字节顺序不变,主要使用在PC or Intel平台。在读取的字节为Big时,需要进行字节顺序交换,才能得出正确的值。一个把Big顺序

5、转换为Little顺序的函数可以如下:int Big2LittleEndian(int num) int reverse; /返回结果 char bit0, bit1, bit2, bit3; bit0 = (num & 0x000000ff); bit1 = (num & 0x0000ff00) 8; bit2 = (num & 0x00ff0000) 16; bit3 = (num & 0xff000000) 24; reverse = (bit0 24) | (bit1 16) | (bit2 8) | (bit3); return reverse;文件头中第32-35位字节为一个整型,

6、其值反映了shapefile的图形对象类型,具体值对应含义如下:Value Shape Type0 Null Shape1 Point3 PolyLine5 Polygon8 MultiPoint11 PointZ13 PolyLineZ15 PolygonZ18 MultiPointZ21 PointM23 PolyLineM25 PolygonM28 MultiPointM31 MultiPatch第36-67个字节包含了图幅的范围大小。每条记录由记录头和实体内容组成,记录头格式固定,具体组成如下所示:Position Field Value Type Byte OrderByte 0Re

7、cord Number Record Number Integer BigByte 4 Content Length Content Length Integer Big记录头包含8个字节,0-3表示记录的顺序号,4-7表示记录实体自身的大小。实体的内容根据对象类型不同而变化,需根据具体情况处理。.shx文件由文件头和记录组成,文件头与前述.shp文件一致,但需注意File Length字段的值不一样。而每条记录由2个整型组成,具体内容如下:Position Field Value Type Byte OrderByte 0 Offset Offset Integer BigByte 4 Co

8、ntent Length Content Length Integer Big其中,offset表示该条记录在.shp文件中的字节位置,而Content Length说明了记录的大小。标准的DBF文件,是由头文件和实体信息两部分构成(如图1所示)。文件头记录1记录2记录3记录4记录n图1 DBF文件的结构文件头部分的长度是不定长的,它主要对DBF文件作了一些总体说明(表),其中最主要的是对这个DBF文件的记录项的信息进行了详细地描述,比如对每个记录项的名称、数据类型、长度等信息都有具体的说明。在文件中的位置内容说明01个字节表示当前的版本信息133个字节表示最近的更新日期,按照YYMMDD格式

9、。471个32位数文件中的记录条数。891个16位数文件头中的字节数。10111个16位数一条记录中的字节长度。12132个字节保留字节,用于以后添加新的说明性信息时使用,这里用0来填写。141个字节表示未完成的操作。 151个字节dBASE IV编密码标记。162712个字节保留字节,用于多用户处理时使用。281个字节DBF文件的MDX标识。在创建一个DBF 表时 ,如果使用了MDX 格式的索引文件,那么 DBF 表的表头中的这个字节就自动被设置了一个标志,当你下次试图重新打开这个DBF表的时候,数据引擎会自动识别这个标志,如果此标志为真,则数据引擎将试图打开相应的MDX 文件。291个字节

10、Language driver ID.30312个字节保留字节,用于以后添加新的说明性信息时使用,这里用0来填写。32X(n*32)个字节记录项信息描述数组。n表示记录项的个数。这个数组的结构在下表中有详细的解释。X11个字节作为记录项终止标识。文件(.dbf)的文件头位置内容说明01011个字节记录项名称,是ASCII码值。111个字节记录项的数据类型,是ASCII码值。(B、C、D、G、L、M和N,具体的解释见下表)。12154个字节保留字节,用于以后添加新的说明性信息时使用,这里用0来填写。161个字节记录项长度,二进制型。171个字节记录项的精度,二进制型。18192个字节保留字节,用

11、于以后添加新的说明性信息时使用,这里用0来填写。201个字节工作区ID。213010个字节保留字节,用于以后添加新的说明性信息时使用,这里用0来填写。311个字节MDX标识。如果存在一个MDX 格式的索引文件,那么这个记录项为真,否则为空。 记录项信息描述代码数据类型允许输入的数据B二进制型各种字符。C字符型各种字符。D日期型用于区分年、月、日的数字和一个字符,内部存储按照YYYYMMDD格式。G(General or OLE)各种字符。N数值型(Numeric)- . 0 1 2 3 4 5 6 7 8 9 L逻辑型(Logical)? Y y N n T t F f (? 表示没有初始化)

12、。 M(Memo)各种字符。dbf文件中的数据类型五、 内容与步骤:(1)阅读白皮书,理解ShapeFile的文件组织形式,以及各个组成部分的数据结构;(2)分析系统功能需求;(3)设计程序系统结构和模块;(4)采用面向对象方法设计程序各个类对象;(5)建立项目工程文件;(6)实现各个类;(7)调试程序;(8)撰写实验报告。六、 实验成果:代码展示:using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Data;using S

13、ystem.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using Microsoft.VisualBasic ;namespace MyCprogram public partial class Form1 : Form ArrayList polygons = new ArrayList(); ArrayList polylines = new ArrayList(); ArrayList points = new ArrayList(); Pen pen = new Pen(Color.Blue

14、, 2); int ShapeType; int count; double xmin, ymin, xmax, ymax; double n1, n2; public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e) MessageBox.Show(请读取后缀为.Shp的文件,否则将导致未知的程序错误,按钮声明,MessageBoxButtons.OK,MessageB

15、oxIcon.Warning); DialogResult dr = this.openFileDialog1.ShowDialog(); if (dr = DialogResult.OK) BinaryReader br = new BinaryReader(openFileDialog1.OpenFile(); /读取文件过程 br.ReadBytes(24); int FileLength = br.ReadInt32();/0代表数据长度未知 int FileBanben = br.ReadInt32(); ShapeType = br.ReadInt32(); xmin = br.R

16、eadDouble(); ymax = -1 * br.ReadDouble(); xmax = br.ReadDouble(); ymin = -1 * br.ReadDouble(); double width = xmax - xmin; double height = ymax - ymin; n1 = (float)(this.pictureBox1.Width * 0.9 / width);/x轴放大倍数 n2 = (float)(this.pictureBox1.Height * 0.9 / height);/y轴放大倍数 br.ReadBytes(32); switch (Sh

17、apeType) case 1: points.Clear(); while (br.PeekChar() != -1) Point_shape point = new Point_shape(); 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 StreamWr

18、iter(point.txt); foreach (Point_shape p in points) sw.WriteLine(0,1,2 , p.X, -1 * p.Y, 0); this.pictureBox1.Refresh(); sw.Close(); break; case 3: polylines.Clear(); while (br.PeekChar() != -1) PolyLine_shape polyline = new PolyLine_shape(); polyline.Box = new double4; polyline.Parts = new ArrayList(

19、); polyline.Points = new ArrayList(); uint RecordNum = br.ReadUInt32(); int DataLength = br.ReadInt32(); /读取第i个记录 br.ReadInt32(); polyline.Box0 = br.ReadDouble(); polyline.Box1 = br.ReadDouble(); polyline.Box2 = br.ReadDouble(); polyline.Box3 = br.ReadDouble(); polyline.NumParts = br.ReadInt32(); po

20、lyline.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_shape pointtemp = new Point_shape(); pointtemp.X = br.ReadDouble(); pointtemp.Y = -1 * br.ReadDouble()

21、; polyline.Points.Add(pointtemp); polylines.Add(polyline); StreamWriter sw2 = new StreamWriter(line.txt); count = 1; foreach (PolyLine_shape p in polylines) for (int i = 0; i p.NumParts; i+) int startpoint; int endpoint; if (i = p.NumParts - 1) startpoint = (int)p.Partsi; endpoint = p.NumPoints; els

22、e startpoint = (int)p.Partsi; endpoint = (int)p.Partsi + 1; sw2.WriteLine(线 + count.ToString() + :); for (int k = 0, j = startpoint; j endpoint; j+, k+) Point_shape ps = (Point_shape)p.Pointsj; sw2.WriteLine( 0,1,2 , ps.X, -1 * ps.Y, 0); count+; this.pictureBox1.Refresh(); sw2.Close(); break; case 5

23、: polygons.Clear(); while (br.PeekChar() != -1) Polygon_shape polygon = new Polygon_shape(); polygon.Parts = new ArrayList(); polygon.Points = new ArrayList(); uint RecordNum = br.ReadUInt32(); int DataLength = br.ReadInt32(); /读取第i个记录 int m = br.ReadInt32(); polygon.Box1 = br.ReadDouble(); polygon.

24、Box2 = br.ReadDouble(); polygon.Box3 = br.ReadDouble(); polygon.Box4 = 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.Nu

25、mPoints; j+) Point_shape pointtemp = new Point_shape(); 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_shape p in polygons) for (int i = 0; i p.NumP

26、arts; i+) int startpoint; int endpoint; if (i = p.NumParts - 1) startpoint = (int)p.Partsi; endpoint = p.NumPoints; else startpoint = (int)p.Partsi; endpoint = (int)p.Partsi + 1; sw1.WriteLine(多边形 + count.ToString() + :); for (int k = 0, j = startpoint; j endpoint; j+, k+) Point_shape ps = (Point_sh

27、ape)p.Pointsj; sw1.WriteLine( 0,1,2 , ps.X, -1 * ps.Y, 0); count+; this.pictureBox1.Refresh(); sw1.Close(); break; br.Close(); double Width = xmax - xmin; double Height = ymax - ymin; n1 = (float)(this.pictureBox1.Width * 0.9 / Width);/x轴放大倍数 n2 = (float)(this.pictureBox1.Height * 0.9 / Height);/y轴放

28、大倍数 this.pictureBox1.Refresh(); private void pictureBox1_Paint(object sender, PaintEventArgs e) PointF point; switch (ShapeType) case 1: foreach (Point_shape 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_shape p in polylines) for (int i = 0; i p.NumParts; i+) int startpoint; int endpoint

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1