-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathShapeUtility.cpp
More file actions
423 lines (393 loc) · 12.4 KB
/
ShapeUtility.cpp
File metadata and controls
423 lines (393 loc) · 12.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "StdAfx.h"
#include "ShapeUtility.h"
#include "ShapeWrapperCOM.h"
#include "ShapeWrapperEmpty.h"
#include "ShapeWrapperPoint.h"
#include "ShapeWrapper.h"
#include "Shape.h"
// **************************************************************
// IsM()
// **************************************************************
bool ShapeUtility::IsM(const ShpfileType shpType)
{
return shpType == SHP_POINTM
|| shpType == SHP_MULTIPOINTM
|| shpType == SHP_POLYLINEM
|| shpType == SHP_POLYGONM;
}
// **************************************************************
// ShapeTypeIsZ()
// **************************************************************
bool ShapeUtility::IsZ(const ShpfileType shpType)
{
return shpType == SHP_POINTZ
|| shpType == SHP_MULTIPOINTZ
|| shpType == SHP_POLYLINEZ
|| shpType == SHP_POLYGONZ;
}
// **************************************************************
// HaveM()
// **************************************************************
bool ShapeUtility::HaveM(const ShpfileType shpType)
{
return shpType == SHP_POINTM || shpType == SHP_POINTZ
|| shpType == SHP_MULTIPOINTM || shpType == SHP_MULTIPOINTZ
|| shpType == SHP_POLYLINEM || shpType == SHP_POLYLINEZ
|| shpType == SHP_POLYGONM || shpType == SHP_POLYGONZ;
}
// **************************************************************
// Convert2D()
// **************************************************************
ShpfileType ShapeUtility::Convert2D(const ShpfileType shpType)
{
if (shpType == SHP_NULLSHAPE) return SHP_NULLSHAPE;
if (shpType == SHP_POINT || shpType == SHP_POINTM || shpType == SHP_POINTZ) return SHP_POINT;
if (shpType == SHP_MULTIPOINT || shpType == SHP_MULTIPOINTM || shpType == SHP_MULTIPOINTZ) return SHP_MULTIPOINT;
if (shpType == SHP_POLYGON || shpType == SHP_POLYGONM || shpType == SHP_POLYGONZ) return SHP_POLYGON;
if (shpType == SHP_POLYLINE || shpType == SHP_POLYLINEM || shpType == SHP_POLYLINEZ) return SHP_POLYLINE;
return shpType;
}
// **************************************************************
// SwapEndian()
// **************************************************************
void ShapeUtility::SwapEndian(char* a, const int size)
{
for (int i = 0; i < size * .5; i++)
{
const char hold = a[i];
a[i] = a[size - i - 1];
a[size - i - 1] = hold;
}
}
// **************************************************************
// get_ContentLength
// **************************************************************
int ShapeUtility::get_ContentLength(const ShpfileType shptype, const int numPoints, const int numParts)
{
int contentLength;
if (shptype == SHP_NULLSHAPE)
contentLength = sizeof(int); // type is stored
else if (shptype == SHP_POINT)
{
contentLength = sizeof(int) +
sizeof(double) * 2;
}
else if (shptype == SHP_POINTZ)
{
contentLength = sizeof(int) +
sizeof(double) * 4;
}
else if (shptype == SHP_POINTM)
{
contentLength = sizeof(int) +
sizeof(double) * 3;
}
else if (shptype == SHP_POLYLINE)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(int) +
sizeof(int) * numParts +
sizeof(double) * 2 * numPoints;
}
else if (shptype == SHP_POLYLINEZ)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(int) +
sizeof(int) * numParts +
sizeof(double) * 2 * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints;
}
else if (shptype == SHP_POLYLINEM)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(int) +
sizeof(int) * numParts +
sizeof(double) * 2 * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints;
}
else if (shptype == SHP_POLYGON)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(int) +
sizeof(int) * numParts +
sizeof(double) * 2 * numPoints;
}
else if (shptype == SHP_POLYGONZ)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(int) +
sizeof(int) * numParts +
sizeof(double) * 2 * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints;
}
else if (shptype == SHP_POLYGONM)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(int) +
sizeof(int) * numParts +
sizeof(double) * 2 * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints;
}
else if (shptype == SHP_MULTIPOINT)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(double) * 2 * numPoints;
}
else if (shptype == SHP_MULTIPOINTZ)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(double) * 2 * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints;
}
else if (shptype == SHP_MULTIPOINTM)
{
contentLength = sizeof(int) +
sizeof(double) * 4 +
sizeof(int) +
sizeof(double) * 2 * numPoints +
sizeof(double) * 2 +
sizeof(double) * numPoints;
}
else
{
return 0;
}
return contentLength;
}
// **************************************************************
// CreateFastWrapper
// **************************************************************
IShapeWrapper* ShapeUtility::CreateWrapper(char* data, const int recordLength, const bool forceCom)
{
if (!data) {
Debug::WriteError("CreateWrapper: shape data was expected.");
return nullptr;
}
ShpfileType shpType = (ShpfileType) * (int*)data; // TODO: Fix compile warning
//ShpfileType shpType2D = ShapeUtility::Convert2D(shpType);
switch (GetShapeWrapperType(shpType, forceCom))
{
case ShapeWrapperType::swtPoint:
return new CShapeWrapperPoint(data, recordLength);
case ShapeWrapperType::swtFast:
return new CShapeWrapper(data, recordLength);
case ShapeWrapperType::swtCom:
return new CShapeWrapperCOM(data, recordLength);
case ShapeWrapperType::swtEmpty:
return new CShapeWrapperEmpty();
}
// let's return a stub for any unforeseen situations, it stands a better chance of avoiding a crash
return new CShapeWrapperEmpty();
}
// **************************************************************
// CreateWrapper
// **************************************************************
IShapeWrapper* ShapeUtility::CreateWrapper(const ShpfileType shpType, const bool forceCom)
{
switch (GetShapeWrapperType(shpType, forceCom))
{
case ShapeWrapperType::swtEmpty:
return new CShapeWrapperEmpty();
case ShapeWrapperType::swtPoint:
return new CShapeWrapperPoint(shpType);
case ShapeWrapperType::swtFast:
return new CShapeWrapper(shpType);
case ShapeWrapperType::swtCom:
return new CShapeWrapperCOM(shpType);
}
// let's return a stub for any unforeseen situations, it stands a better chance of avoiding a crash
return new CShapeWrapperEmpty();
}
// **************************************************************
// GetShapeWrapperType
// **************************************************************
ShapeWrapperType ShapeUtility::GetShapeWrapperType(const ShpfileType shpType, const bool forceCom)
{
if (forceCom) {
return ShapeWrapperType::swtCom;
}
const ShpfileType shpType2D = ShapeUtility::Convert2D(shpType);
if (shpType == SHP_NULLSHAPE)
{
return ShapeWrapperType::swtEmpty;
}
if (shpType2D == SHP_POINT)
{
return ShapeWrapperType::swtPoint;
}
if (IsM(shpType) || IsZ(shpType)) {
return ShapeWrapperType::swtCom;
}
return ShapeWrapperType::swtFast;
}
// **************************************************************
// CreateEmptyWrapper
// **************************************************************
IShapeWrapper* ShapeUtility::CreateEmptyWrapper(const bool forceCom)
{
if (forceCom) {
return new CShapeWrapperCOM(SHP_NULLSHAPE);
}
return new CShapeWrapperEmpty();
}
// **************************************************************
// WriteBigEndian
// **************************************************************
void ShapeUtility::WriteBigEndian(FILE* file, int value)
{
if (!file) return;
void* intbuf = (char*)&value; // TODO: Fix compile warning
ShapeUtility::SwapEndian((char*)intbuf, sizeof(int)); // TODO: Fix compile warning
const size_t size = fwrite(intbuf, sizeof(int), 1, file);
if (size != 1)
{
CallbackHelper::ErrorMsg("Failed to write int value");
}
}
// **************************************************************
// ReadIntBigEndian
// **************************************************************
long ShapeUtility::ReadIntBigEndian(FILE* file)
{
int buf;
fread(&buf, sizeof(int), 1, file);
ShapeUtility::SwapEndian((char*)&buf, sizeof(int)); // TODO: Fix compile warning
return buf;
}
// **************************************************************
// WritePointXY
// **************************************************************
void ShapeUtility::WritePointXY(IShapeWrapper* shape, const int pointIndex, FILE* file)
{
double x, y;
shape->get_PointXY(pointIndex, x, y);
fwrite(&x, sizeof(double), 1, file);
fwrite(&y, sizeof(double), 1, file);
}
// **************************************************************
// WritePointXYM
// **************************************************************
void ShapeUtility::WritePointXYM(IShapeWrapper* shape, const int pointIndex, FILE* file)
{
double x, y, m, z;
shape->get_PointXYZM(pointIndex, x, y, z, m);
fwrite(&x, sizeof(double), 1, file);
fwrite(&y, sizeof(double), 1, file);
fwrite(&m, sizeof(double), 1, file);
}
// **************************************************************
// WritePointXYZ
// **************************************************************
void ShapeUtility::WritePointXYZ(IShapeWrapper* shape, const int pointIndex, FILE* file)
{
double x, y, m, z;
shape->get_PointXYZM(pointIndex, x, y, z, m);
fwrite(&x, sizeof(double), 1, file);
fwrite(&y, sizeof(double), 1, file);
fwrite(&z, sizeof(double), 1, file);
fwrite(&m, sizeof(double), 1, file);
}
// **************************************************************
// WritePointZ
// **************************************************************
void ShapeUtility::WritePointZ(IShapeWrapper* shape, const int pointIndex, FILE* file)
{
double z;
shape->get_PointZ(pointIndex, z);
fwrite(&z, sizeof(double), 1, file);
}
// **************************************************************
// WritePointM
// **************************************************************
void ShapeUtility::WritePointM(IShapeWrapper* shape, const int pointIndex, FILE* file)
{
double m;
shape->get_PointM(pointIndex, m);
fwrite(&m, sizeof(double), 1, file);
}
// **************************************************************
// WriteExtentsXY
// **************************************************************
void ShapeUtility::WriteExtentsXY(IShapeWrapper* shape, FILE* file)
{
double xMin, yMin, xMax, yMax;
shape->get_BoundsXY(xMin, xMax, yMin, yMax);
fwrite(&xMin, sizeof(double), 1, file);
fwrite(&yMin, sizeof(double), 1, file);
fwrite(&xMax, sizeof(double), 1, file);
fwrite(&yMax, sizeof(double), 1, file);
}
// **************************************************************
// WriteExtentsM
// **************************************************************
void ShapeUtility::WriteExtentsM(IShapeWrapper* shape, FILE* file)
{
double xMin, yMin, xMax, yMax, zMin, zMax, mMin, mMax;
shape->get_Bounds(xMin, yMin, xMax, yMax, zMin, zMax, mMin, mMax);
fwrite(&mMin, sizeof(double), 1, file);
fwrite(&mMax, sizeof(double), 1, file);
}
// **************************************************************
// WriteExtentsZ
// **************************************************************
void ShapeUtility::WriteExtentsZ(IShapeWrapper* shape, FILE* file)
{
double xMin, yMin, xMax, yMax, zMin, zMax, mMin, mMax;
shape->get_Bounds(xMin, yMin, xMax, yMax, zMin, zMax, mMin, mMax);
fwrite(&zMin, sizeof(double), 1, file);
fwrite(&zMax, sizeof(double), 1, file);
}
// **************************************************************
// Get25DShapeType
// **************************************************************
ShpfileType ShapeUtility::Get25DShapeType(const ShpfileType shpTypeBase, const bool isZ, const bool isM)
{
switch (shpTypeBase)
{
case SHP_POINT:
if (isZ) return SHP_POINTZ;
if (isM) return SHP_POINTM;
return SHP_POINT;
case SHP_MULTIPOINT:
if (isZ) return SHP_MULTIPOINTZ;
if (isM) return SHP_MULTIPOINTM;
return SHP_MULTIPOINT;
case SHP_POLYLINE:
if (isZ) return SHP_POLYLINEZ;
if (isM) return SHP_POLYLINEM;
return SHP_POLYLINE;
case SHP_POLYGON:
if (isZ) return SHP_POLYGONZ;
if (isM) return SHP_POLYGONM;
return SHP_POLYGON;
default:
return shpTypeBase;
}
}