您的当前位置:首页正文

SHP文件的常识介绍及C++算法与C#算法实现

2023-10-18 来源:爱站旅游
导读SHP文件的常识介绍及C++算法与C#算法实现


shp文件的读取

首先了解一下shp文件的一些简单知识

Shapefile文件是美国环境系统研究所(ESRI)所研制的GIS文件系统格式文件,是工业标准的矢量数据文件。 Shapefile将空间特征表中的非拓扑几何对象和属性信息存储在数据集中,特征表中的几何对象存为以坐标点集表示的图形文件—SHP文件,Shapefile文件并不含拓扑(Topological)数据结构。一个Shape文件包括三个文件:一个主文件(*.shp),一个索引文件(*.shx),和一个dBASE(*.dbf)表。主文件是一个直接存取,变长度记录的文件,其中每个记录描述构成一个地理特征(Feature)的所有vertices坐标值。在索引文件中,每条记录包含对应主文件记录距离主文件头开始的偏移量,dBASE表包含SHP文件中每一个Feature的特征属性,表中几何记录和属性数据之间的一一对应关系是基于记录数目的ID。在dBASE文件中的属性记录必须和主文件中的记录顺序是相同的。图形数据和属性数据通过索引号建立一一对应的关系

Shapefile中坐标文件(.shp)由固定长度的文件头和接着的变长度空间数据记录组成。文件头由100字节的说明信息组成的,主要说明文件的长度、Shape类型、整个Shape图层的范围等等,这些信息构成了空间数据的元数据。在导入空间数据时首先要读入文件头获取Shape文件的基本信息,并以此信息为基础建立相应的元数据表。而变长度空间数据记录是由固定长度的记录头和变长度记录内容组成,其记录结构基本类似,每条记录都有记录头和记录内容组成(空间坐标对)。记录头的内容包括记录号(Record Number)和坐标记录长度(Content Length)两个记录项,Shapefile文件中的记录号都是从1开始的,坐标记录长度是按16位字来衡量的。记录内容包括目标的几何类型(ShapeType)和具体的坐标记录(X,Y),记录内容因要素几何类型的不同,其具体的内容和格式都有所不同。对于具体的记录主要包括空Shape记录,点记录,线记录和多边形记录。

属性文件(.dbf)用于记录属性信息。它是一个标准的DBF文件,也是由头文件和实体信息两部分构

成。其中文件头部分的长度是不定长的,它主要对DBF文件作了一些总体说明,其中最主要的是对这个DBF文件的记录项的信息进行了详细的描述,比如对每个记录项的名称,数据类型,长度等信息都有具体的说明。属性文件的实体信息部分就是一条条属性记录,每条记录都是由若干个记录项构成,因此只要依次循环读取每条记录就可以了。

索引文件(.shx)主要包含坐标文件的索引信息,文件中每个记录包含对应的坐标文件记录距离坐标文件的文件头的偏移量。通过索引文件可以很方便地在坐标文件中定位到指定目标地坐标信息。索引文件也是由文件头和实体信息两部分构成的,其中文件头部分是一个长度固定(100 bytes)的记录段,其内容与坐标文件的文件头基本一致。它的实体信息以记录为基本单位,每一条记录包括偏移量(Offset)和记录段长度(Content Length)两个记录项。

接下来我们再看一下代码(c++):

SHP文件的读取

view plain

1. FeatureClass* CGISMapDoc::ImportShapeFileData( FILE* fpShp, FILE* fpDbf ) 2. { 3. //读Shp文件头开始 4. int fileCode = -1; 5. int fileLength = -1;

6. int version = -1; 7. int shapeType = -1; 8. fread(&fileCode , sizeof(int) , 1 , fpShp) ; 9. fileCode = ReverseBytes(fileCode) ; 10. if (fileCode != 9994) 11. { 12. CString strTemp ; 13. strTemp.Format(\" WARNING filecode %d \", fileCode ); 14. AfxMessageBox(strTemp); 15. } 16. for( int i = 0 ; i < 5 ; i ++ ) 17. fread(&fileCode , sizeof(int) , 1 , fpShp) ; 18. fread(&fileLength , sizeof(int) , 1 , fpShp) ;

19. fileLength = ReverseBytes(fileLength) ; 20. fread(&version , sizeof(int) , 1 , fpShp) ; 21. fread(&shapeType , sizeof(int) , 1 , fpShp) ; 22. double tempOriginX , tempOriginY ; 23. fread( &tempOriginX , sizeof(double) , 1 , fpShp ) ; 24. fread( &tempOriginY , sizeof(double) , 1 , fpShp ) ; 25. double xMaxLayer , yMaxLayer ; 26. fread( &xMaxLayer , sizeof(double) , 1 , fpShp ) ; 27. fread( &yMaxLayer , sizeof(double) , 1 , fpShp ) ; 28. double* skip = new double[4] ; 29. fread( skip , sizeof(double) , 4 , fpShp ) ; 30. delete []skip ; 31. skip = 0 ;

32. //读Shp文件头结束 33. int uniqueID = this->m_pDataSource->GetUniqueID() ; 34. FeatureClass* pShpDataSet = 0 ; 35. //根据目标类型创建相应的图层DataSet。 36. switch( shapeType ) 37. { 38. case 1 : 39. pShpDataSet = (FeatureClass*)&(m_pDataSource->CreateDataSet(uniqueID , POINTDATASET , layerName)) ; 40. break ; 41. case 3 : 42. case 23 : 43. pShpDataSet = (FeatureClass*)&(m_pDataSource->CreateDataSet(uniqueID , LINEDATASET , layerName)) ;

44. break ; 45. case 5 : 46. pShpDataSet = (FeatureClass*)&(m_pDataSource->CreateDataSet(uniqueID , POLYGONDATASET , layerName)) ; 47. break ; 48. } 49. if ( pShpDataSet == 0 ) return 0; 50. // 读DBF文件头---------begin------------ 51. struct DBFHeader 52. { 53. char m_nValid; 54. char m_aDate[3]; 55. char m_nNumRecords[4]; 56. char m_nHeaderBytes[2];

57. char m_nRecordBytes[2]; 58. char m_nReserved1[3]; 59. char m_nReserved2[13]; 60. char m_nReserved3[4]; 61. }dbfheader; 62. struct DBFFIELDDescriptor 63. { 64. char m_sName[10];//应该为char m_sName[11] 65. char m_nType; 66. char m_nAddress[4]; 67. char m_nFieldLength; 68. char m_nFieldDecimal; 69. char m_nReserved1[2];

70. char m_nWorkArea; 71. char m_nReserved2[2]; 72. char m_nSetFieldsFlag; 73. char m_nReserved3[8]; 74. }; 75. fread(&dbfheader,sizeof(DBFHeader),1,fpDbf); 76. /*int recordsNum = *((int*)dbfheader.m_nNumRecords); 77. int headLen = *((short*)dbfheader.m_nHeaderBytes); 78. int everyRecordLen = *((short*)dbfheader.m_nRecordBytes); 79. if ( recordsNum == 0 || headLen == 0 || everyRecordLen == 0 ) 80. return 0 ; 81. int fieldCount = (headLen - 1 - sizeof(DBFHeader))/sizeof(DBFFIELDDescriptor); 82. DBFFIELDDescriptor *pFields = new DBFFIELDDescriptor[fieldCount];

83. for ( i = 0; i < fieldCount; i ++ ) 84. fread(&pFields[i],sizeof(DBFFIELDDescriptor),1,fpDbf); 85. char endByte; 86. fread(&endByte,sizeof(char),1,fpDbf); 87. 88. if ( endByte != 0x0D) 89. { 90. delete []pFields; 91. pFields = 0; 92. return 0; 93. }*/ 94. Fields& fields = pShpDataSet->GetFields(); 95. DBFFIELDDescriptor field ;

96. BYTE endByte = ' '; 97. char fieldName[12]; 98. int fieldDecimal, fieldLen, everyRecordLen = 0 ; 99. while ( !feof(fpDbf) ) 100. { 101. fread(&endByte,sizeof(BYTE),1,fpDbf); 102. if ( endByte == 0x0D) break ; 103. fread(&field,sizeof(DBFFIELDDescriptor),1,fpDbf); 104. 105. fieldName[0] = endByte; 106. for ( i = 0; i < 10; i ++ ) 107. fieldName[i+1] = field.m_sName[i]; 108. fieldName[11] = '/0';

109. fieldDecimal = field.m_nFieldDecimal; 110. fieldLen = field.m_nFieldLength; 111. switch( field.m_nType ) 112. { 113. case 'C': 114. fields.AddField(fieldName,fieldName,FIELD_STRING,fieldLen); 115. break; 116. case 'F': 117. fields.AddField(fieldName,fieldName,FIELD_DOUBLE,fieldLen); 118. break; 119. case 'N': 120. { 121. if ( fieldDecimal == 0 )

122. fields.AddField(fieldName,fieldName,FIELD_INT,fieldLen); 123. else fields.AddField(fieldName,fieldName,FIELD_DOUBLE,fieldLen); 124. } 125. break; 126. } 127. everyRecordLen += fieldLen ; 128. } 129. // 读DBF文件头---------end------------ 130. while( !feof(fpShp) ) 131. { 132. //读记录头开始 133. int recordNumber = -1 ; 134. int contentLength = -1 ;

135. fread(&recordNumber , sizeof(int) , 1 , fpShp) ; 136. fread(&contentLength , sizeof(int) , 1 , fpShp) ; 137. recordNumber = ReverseBytes(recordNumber) ; 138. contentLength = ReverseBytes(contentLength) ; 139. //读记录头结束 140. switch( shapeType ) 141. { 142. case 1: // '/001' 143. //读取点目标开始 144. { 145. Fields &featureFields = pShpDataSet->GetFields(); 146. Feature *pFeature = new Feature(recordNumber , 1 , &featureFields) ; 147. int pointShapeType ;

148. fread(&pointShapeType , sizeof(int) , 1 , fpShp) ; 149. double xValue , yValue ; 150. fread(&xValue , sizeof(double) , 1 , fpShp) ; 151. fread(&yValue , sizeof(double) , 1 , fpShp) ; 152. GeoPoint *pNewGeoPoint = new GeoPoint( xValue , yValue ) ; 153. pFeature->SetBound(xValue , yValue , 0 , 0 ) ; 154. pFeature->SetGeometry(pNewGeoPoint) ; 155. this->LoadAttributeData(pFeature,fpDbf,everyRecordLen); 156. pShpDataSet->AddRow(pFeature) ; 157. } 158. break ; 159. //读取点目标结束 160. case 3: // '/003'

161. //读取线目标开始 162. { 163. Fields &featureFields = pShpDataSet->GetFields(); 164. Feature *pFeature = new Feature(recordNumber , 1 , &featureFields) ; 165. int arcShapeType ; 166. fread(&arcShapeType , sizeof(int) , 1 , fpShp) ; 167. double objMinX , objMinY , objMaxX , objMaxY ; 168. fread(&objMinX , sizeof(double) , 1 , fpShp) ; 169. fread(&objMinY , sizeof(double) , 1 , fpShp) ; 170. fread(&objMaxX , sizeof(double) , 1 , fpShp) ; 171. fread(&objMaxY , sizeof(double) , 1 , fpShp) ; 172. GeoPolyline *pNewGeoLine = new GeoPolyline(); 173. double width = objMaxX - objMinX ;

174. double height = objMaxY - objMinY ; 175. pFeature->SetBound(objMinX , objMinY , width , height) ; 176. int numParts , numPoints ; 177. fread(&numParts , sizeof(int) , 1 , fpShp) ; 178. fread(&numPoints , sizeof(int) , 1 , fpShp) ; 179. //存储各段线的起点索引 180. int* startOfPart = new int[numParts] ; 181. for( int i = 0 ; i < numParts ; i++ ) 182. { 183. int indexFirstPoint ; 184. fread(&indexFirstPoint , sizeof(int) , 1 , fpShp) ; 185. startOfPart[i] = indexFirstPoint ; 186. }

187. //处理单个目标有多条线的问题 188. pNewGeoLine->SetPointsCount( numParts ) ; 189. for( i = 0 ; i < numParts ; i++ ) 190. { 191. GeoPoints& points = pNewGeoLine->GetPoints(i) ; 192. int curPosIndex = startOfPart[i] ; 193. int nextPosIndex = 0 ; 194. int curPointCount = 0 ; 195. if( i == numParts - 1 ) 196. curPointCount = numPoints - curPosIndex ; 197. else 198. { 199. nextPosIndex = startOfPart[i + 1] ;

200. curPointCount = nextPosIndex - curPosIndex ; 201. } 202. points.SetPointCount( curPointCount ) ; 203. //加载一条线段的坐标 204. for( int iteratorPoint = 0 ; iteratorPoint < curPointCount ; iteratorPoint ++ ) 205. { 206. double x , y ; 207. fread(&x , sizeof(double) , 1 , fpShp) ; 208. fread(&y , sizeof(double) , 1 , fpShp) ; 209. GeoPoint newVertex(x, y); 210. points.SetPoint(iteratorPoint, newVertex); 211. } 212. }

213. delete []startOfPart ; 214. startOfPart = 0 ; 215. pFeature->SetGeometry(pNewGeoLine) ; 216. this->LoadAttributeData(pFeature,fpDbf,everyRecordLen); 217. pShpDataSet->AddRow(pFeature) ; 218. } 219. break ; 220. //读取线目标结束 221. case 5: // '/005' 222. //读取面目标开始 223. { 224. Fields &featureFields = pShpDataSet->GetFields(); 225. Feature *pFeature = new Feature(recordNumber , 1 , &featureFields) ;

226. int polygonShapeType ; 227. fread(&polygonShapeType , sizeof(int) , 1 ,fpShp) ; 228. if( polygonShapeType != 5 ) 229. AfxMessageBox( \"Error: Attempt to load non polygon shape as polygon.\" ) ; 230. double objMinX , objMinY , objMaxX , objMaxY ; 231. fread(&objMinX , sizeof(double) , 1 , fpShp) ; 232. fread(&objMinY , sizeof(double) , 1 , fpShp) ; 233. fread(&objMaxX , sizeof(double) , 1 , fpShp) ; 234. fread(&objMaxY , sizeof(double) , 1 , fpShp) ; 235. GeoPolygon *pNewGeoPolygon = new GeoPolygon(); 236. double width = objMaxX - objMinX ; 237. double height = objMaxY - objMinY ; 238. pFeature->SetBound(objMinX , objMinY , width , height) ;

239. int numParts , numPoints ; 240. fread(&numParts , sizeof(int) , 1 , fpShp) ; 241. fread(&numPoints , sizeof(int) , 1 , fpShp) ; 242. //存储各个面的起点索引 243. int* startOfPart = new int[numParts] ; 244. for( int i = 0 ; i < numParts ; i++ ) 245. { 246. int indexFirstPoint ; 247. fread(&indexFirstPoint , sizeof(int) , 1 , fpShp) ; 248. startOfPart[i] = indexFirstPoint ; 249. } 250. //处理单个目标有多面问题 251. pNewGeoPolygon->SetPointsCount( numParts ) ;

252. for( i = 0 ; i < numParts ; i++ ) 253. { 254. GeoPoints& points = pNewGeoPolygon->GetPoints(i) ; 255. int curPosIndex = startOfPart[i] ; 256. int nextPosIndex = 0 ; 257. int curPointCount = 0 ; 258. if( i == numParts - 1 ) 259. curPointCount = numPoints - curPosIndex ; 260. else 261. { 262. nextPosIndex = startOfPart[i + 1]; 263. curPointCount = nextPosIndex - curPosIndex ; 264. }

265. points.SetPointCount( curPointCount ) ; 266. //加载一个面(多边形)的坐标 267. for( int iteratorPoint = 0 ; iteratorPoint < curPointCount ; iteratorPoint ++ ) 268. { 269. double x , y ; 270. fread(&x , sizeof(double) , 1 , fpShp) ; 271. fread(&y , sizeof(double) , 1 , fpShp) ; 272. GeoPoint newVertex(x, y); 273. points.SetPoint(iteratorPoint, newVertex); 274. } 275. } 276. delete []startOfPart ; 277. startOfPart = 0 ;

278. pFeature->SetGeometry(pNewGeoPolygon) ; 279. this->LoadAttributeData(pFeature,fpDbf,everyRecordLen); 280. pShpDataSet->AddRow(pFeature) ; 281. } 282. break ; 283. //读取面目标结束 284. case 23: // '/027' 285. //读取Measure形线目标开始 286. { 287. Fields &featureFields = pShpDataSet->GetFields(); 288. Feature *pFeature = new Feature(recordNumber , 1 , &featureFields) ; 289. int arcMShapeType ; 290. fread(&arcMShapeType , sizeof(int) , 1 , fpShp) ;

291. double objMinX , objMinY , objMaxX , objMaxY ; 292. fread(&objMinX , sizeof(double) , 1 , fpShp) ; 293. fread(&objMinY , sizeof(double) , 1 , fpShp) ; 294. fread(&objMaxX , sizeof(double) , 1 , fpShp) ; 295. fread(&objMaxY , sizeof(double) , 1 , fpShp) ; 296. GeoPolyline *pNewGeoLine = new GeoPolyline(); 297. double width = objMaxX - objMinX ; 298. double height = objMaxY - objMinY ; 299. pFeature->SetBound(objMinX , objMinY , width , height) ; 300. int numParts , numPoints ; 301. fread(&numParts , sizeof(int) , 1 , fpShp) ; 302. fread(&numPoints , sizeof(int) , 1 , fpShp) ; 303. //存储各段线的起点索引

304. int* startOfPart = new int[numParts] ; 305. for( int i = 0 ; i < numParts ; i++ ) 306. { 307. int indexFirstPoint ; 308. fread(&indexFirstPoint , sizeof(int) , 1 , fpShp) ; 309. startOfPart[i] = indexFirstPoint ; 310. } 311. //处理单个目标有多条线的问题 312. pNewGeoLine->SetPointsCount( numParts ) ; 313. for( i = 0 ; i < numParts ; i++ ) 314. { 315. GeoPoints& points = pNewGeoLine->GetPoints(i) ; 316. int curPosIndex = startOfPart[i] ;

317. int nextPosIndex = 0 ; 318. int curPointCount = 0 ; 319. if( i == numParts - 1 ) 320. curPointCount = numPoints - curPosIndex ; 321. else 322. { 323. nextPosIndex = startOfPart[i + 1] ; 324. curPointCount = nextPosIndex - curPosIndex ; 325. } 326. points.SetPointCount( curPointCount ) ; 327. //加载一条线段的坐标 328. for( int iteratorPoint = 0 ; iteratorPoint < curPointCount ; iteratorPoint ++ ) 329. {

330. double x , y ; 331. fread(&x , sizeof(double) , 1 , fpShp) ; 332. fread(&y , sizeof(double) , 1 , fpShp) ; 333. GeoPoint newVertex(x, y); 334. points.SetPoint(iteratorPoint, newVertex); 335. } 336. } 337. delete []startOfPart ; 338. startOfPart = 0 ; 339. double* value = new double[2 + numPoints] ; 340. fread( value , sizeof(double) , 2+numPoints, fpShp) ; 341. delete []value ; 342. value = 0 ;

343. pFeature->SetGeometry(pNewGeoLine) ; 344. this->LoadAttributeData(pFeature,fpDbf,everyRecordLen); 345. pShpDataSet->AddRow(pFeature); 346. } 347. break ; 348. //读取Measure形线目标结束 349. } 350. } 351. return pShpDataSet ; 352. } 一下为C#算法:

直接读取SHAPE文件

2011-01-14 16:01

转载!

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

namespace readSHAPE

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public struct ESRI_POINT

{

public int oid;

public double x;

public double y;

}

public struct ESRI_POLYLINE

{

public int OID;

public double[] XPOINTS;

public double[] YPOINTS;

}

public struct ESRI_POLYGON

{

public int OID;

public double[] XPOINTS;

public double[] YPOINTS;

}

//struct shapeGeo

//{

// public ESRI_POINT point;

//}

IList Geomtry;

private void buttonX1_Click(object sender, EventArgs e)

{

string filename=null;

Geomtry = new List();

openDialog.Filter = \"shape files (*.shp)|*.shp|All files (*.*)|*.*\";

openDialog.FilterIndex = 1;

openDialog.RestoreDirectory = true;

if(openDialog.ShowDialog() == DialogResult.OK)

filename = openDialog.FileName;

filename = Path.GetFileNameWithoutExtension(filename);

string shpfilepath = filename + \".shp\";

string shxfilepath =filename + \".shx\";

try

{

//先读取.shx文件,得到文件的总字节长度

FileStream FileAccess.Read);

fs = new FileStream(shxfilepath, FileMode.Open,

//文件流形式

BinaryReader BinaryFile = new BinaryReader(fs); //二进制读取文件的对象

long BytesSum = fs.Length; //得到文件的字节总长

int shapecount = (int)(BytesSum - 100) / 8; //得以总记录数目

BinaryFile.Close();

fs.Close();

//打开shp文件

if (shxfilepath == \"\")

{

return ;

}

的域。

/********************

* 主文件头

主文件头100字节长。表1显示带有字节位置,值,类型和字节顺序的文件头中在此表中,位置是相对于文件的开始。

表 1 主文件头的描述

位置 域 值 类型 字节顺序

0字节 文件代码 9994 整数 大

4字节 未被使用 0 整数 大

8字节 未被使用 0 整数 大

12字节 未被使用 0 整数 大

16字节 未被使用 0 整数 大

20字节 未被使用 0 整数 大

24字节 文件长度 文件长度 整数 大

28字节 32字节 36字节 44字节 52字节 60字节 68字节* 76字节* 84字节* 92字节* 版本 Shape类型 边界盒 边界盒 边界盒 边界盒 边界盒 边界盒 边界盒 边界盒 1000 Shape类型 Xmin Ymin Xmax Ymax Zmin Zmax Mmin Mmax 整数 整数 双精度 双精度 双精度 双精度 双精度 双精度 双精度 双精度 小

小 小

小 小 小

************************* */

//打开.shp文件,读取x,y坐标的信息

fs = new FileStream(shpfilepath, FileMode.Open, FileAccess.Read); //文件流形式

BinaryFile = new BinaryReader(fs); //打开二进制文件

BinaryFile.ReadBytes(32); //先读出36个字节,紧接着是Box边界合

int shapetype = BinaryFile.ReadInt32();

double Left = BinaryFile.ReadDouble(); //读出整个shp图层的边界合

double Bottom = BinaryFile.ReadDouble();

double Right = BinaryFile.ReadDouble();

double Top = BinaryFile.ReadDouble();

BinaryFile.ReadBytes(32); // shp中尚未使用的边界盒

//Get Shape Data From Here On

int stype;

double x, y;

double left, right, top, bottom;

int partcount;

shapetype

int pointcount;

switch (shapetype)

{

case 1://single point

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

{

ESRI_POINT shapPoint = new ESRI_POINT();

BinaryFile.ReadBytes(12); //记录头8个字节和一个int(4个字节)的 x = BinaryFile.ReadDouble();

y = BinaryFile.ReadDouble();

shapPoint.oid = i;

shapPoint.x = x;

shapPoint.y = y;

//shapPoint.z = 0;

Geomtry.Add(shapPoint);

}

break;

case 8://multi points layer

break;

case 3://Polyline layer

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

{

ESRI_POLYLINE shapePolyline = new ESRI_POLYLINE();

BinaryFile.ReadBytes(12); //记录头8个字节和一个int(4个字节)的shapetype

//获得图形外边框

left = BinaryFile.ReadDouble();

bottom = BinaryFile.ReadDouble();

right = BinaryFile.ReadDouble();

top = BinaryFile.ReadDouble();

partcount = BinaryFile.ReadInt32();

pointcount = BinaryFile.ReadInt32();

int[] parts = new int[partcount];

int[] partspos = new int[partcount];

double[] xpoints = new double[pointcount];

double[] ypoints = new double[pointcount];

double[] zpoints = new double[pointcount];

//节点开始位置

for (int j = 0; j < partcount; j++)

{

parts[j] = BinaryFile.ReadInt32();

}

//shift them to be points count included in parts

if (partcount > 0)

partspos[0] = 0;

int newpos = 0;

for (int j = 0; j <= partcount - 2; j++)

{

parts[j] = parts[j + 1] - parts[j];

newpos += parts[j];

partspos[j + 1] = newpos;

}

parts[partcount - 1] = pointcount - parts[partcount - 1];

//读坐标

for (int j = 0; j < pointcount; j++)

{

x = BinaryFile.ReadDouble();

y = BinaryFile.ReadDouble();

xpoints[j] = x;

ypoints[j] = y;

zpoints[j] = 0;

}

if (pointcount > 1)

{

shapePolyline.OID = i;

shapePolyline.XPOINTS = xpoints;

shapetype

shapePolyline.YPOINTS = ypoints;

}

}

break;

case 5://Polygon layer

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

{

ESRI_POLYGON shapePolygon = new ESRI_POLYGON();

BinaryFile.ReadBytes(12); //记录头8个字节和一个int(4个字节)的 left = BinaryFile.ReadDouble();

bottom = BinaryFile.ReadDouble();

right = BinaryFile.ReadDouble();

top = BinaryFile.ReadDouble();

partcount = BinaryFile.ReadInt32();

pointcount = BinaryFile.ReadInt32();

int[] parts = new int[partcount];

int[] partspos = new int[partcount];

double[] xpoints = new double[pointcount];

double[] ypoints = new double[pointcount];

double[] zpoints = new double[pointcount];

//firstly read out parts begin pos in file

for (int j = 0; j < partcount; j++)

{

parts[j] =BinaryFile.ReadInt32();

}

//shift them to be points count included in parts

if (partcount > 0)

partspos[0] = 0;

int newpos = 0;

for (int j = 0; j <= partcount - 2; j++)

{

parts[j] = parts[j + 1] - parts[j];

newpos += parts[j];

partspos[j + 1] = newpos;

}

parts[partcount - 1] = pointcount - parts[partcount - 1];

//读坐标

for (int j = 0; j < pointcount; j++)

{

x = BinaryFile.ReadDouble();

y = BinaryFile.ReadDouble();

xpoints[j] = x;

ypoints[j] = y;

zpoints[j] = 0;

}

if (pointcount > 1)

{

shapePolygon.OID = i;

shapePolygon.XPOINTS = xpoints;

shapePolygon.YPOINTS = ypoints;

}

}

break;

default:

break ;

}

}

catch (FileNotFoundException ee)

{

ee.ToString();

}

}

}

}

因篇幅问题不能全部显示,请点此查看更多更全内容