-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathShapefileReader.cpp
More file actions
161 lines (136 loc) · 4.48 KB
/
ShapefileReader.cpp
File metadata and controls
161 lines (136 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "stdafx.h"
#include "Macros.h"
#include "ShapefileReader.h"
#include <afxmt.h>
// ****************************************************************
// ReadShapefileIndex()
// ****************************************************************
// Fast reading for drawing procedure without bounds, file code, etc
bool CShapefileReader::ReadShapefileIndex(CStringW filename, FILE* shpFile, CCriticalSection* readLock)
{
if (filename.GetLength() < 4)
{
return false;
}
// reading shape index (SHX)
CStringW sFilename = filename;
sFilename.SetAt(sFilename.GetLength() - 1, L'x');
FILE* shxfile = _wfopen(sFilename, L"rb");
if (!shxfile )
{
_shpfile = NULL;
_indexData = NULL;
USES_CONVERSION;
CallbackHelper::ErrorMsg(Debug::Format("Failed to open SHX file: %s", OLE2A(sFilename)));
return false;
}
fseek (shxfile, 0, SEEK_END);
int indexFileSize = ftell(shxfile);
rewind(shxfile);
// 100 is for header
fseek(shxfile, 100, SEEK_SET);
_indexData = new char[indexFileSize - 100];
long result = fread(_indexData, sizeof(char), indexFileSize - 100, shxfile);
fclose(shxfile);
//_shpHeader.numShapes = (indexFileSize - 100)/8; // 2 int on record
_readLock = readLock;
_shpfile = shpFile;
rewind(shpFile);
return true;
}
void SwapEndian(char* c)
{
char ctmp;
SWAP_LOCAL(c[0],c[3],ctmp);
SWAP_LOCAL(c[1], c[2], ctmp);
}
// ****************************************************************
// ReadShapeData()
// ****************************************************************
// Reads a single shape from the shapefile
char* CShapefileReader::ReadShapeData(int& offset, int& recordLength)
{
recordLength = 0;
// index records are 8 bytes;
char* data = _indexData + offset*8;
SwapEndian(data);
int readOffset = (*(int*)data) * 2;
data = _indexData + offset * 8 + 4;
SwapEndian(data);
int contentLength = (*(int*)data);
if (contentLength > 0)
{
CSingleLock lock(_readLock);
lock.Lock();
int ret = fseek(_shpfile, (long)readOffset + 2 * sizeof(int), SEEK_SET);
if (ret != 0) return NULL;
// *2: for conversion from 16-bit words to 8-bit words
int length = contentLength * 2;
char* shapeData = new char[length];
int count = (int)fread(shapeData, sizeof(char), length, _shpfile);
recordLength = length;
return shapeData;
}
return NULL;
}
// ****************************************************************
// ReadPolyShape()
// ****************************************************************
PolygonData* CShapefileReader::ReadPolygonData(char* data)
{
PolygonData* shapeData = new PolygonData();
shapeData->partCount = *(int*)(data + 36);
shapeData->pointCount = *(int*)(data + 40);
shapeData->parts = (int*)(data + 44);
shapeData->points = (double*)(data + 44 + sizeof(int) * shapeData->partCount);
return shapeData;
}
PolygonData* CShapefileReader::ReadMultiPointData(char* data)
{
PolygonData* shapeData = new PolygonData();
shapeData->partCount = 0;
shapeData->pointCount = *(int*)(data + 36);
shapeData->parts = NULL;
shapeData->points = (double*)(data + 40);
return shapeData;
}
//// ****************************************************************
//// ReadShape()
//// ****************************************************************
//// Reads the given shape from the shapefile and returns CShapeWrapper object
//CShapeWrapper* CShapefileReader::ReadShape(int ShapeIndex)
//{
// char* data = this->ReadShapeData(ShapeIndex);
// if (data)
// {
// CShapeWrapper* shp = new CShapeWrapper(data);
// delete[] data;
// return shp;
// }
// else
// return NULL;
//}
//// ****************************************************************
//// ReadShape()
//// ****************************************************************
//// Reads the given shape from the shapefile and eeturns the CShapeWrapper
//// object, but only in case it falls in the given extents.
//CShapeWrapper* CShapefileReader::ReadShapeWithinExtents(int ShapeIndex, Extent& extents)
//{
// char* data = this->ReadShapeData(ShapeIndex);
// if (data)
// {
// CShapeWrapper* shp = new CShapeWrapper(data, &extents);
// delete[] data;
// return shp;
// }
// else
// return NULL;
//}
//// ****************************************************************
//// get_ShapefileHeader()
//// ****************************************************************
//ShapefileHeader* CShapefileReader::get_ShapefileHeader()
//{
// return &_shpHeader;
//}