-
Notifications
You must be signed in to change notification settings - Fork 155
/
ShpFile.cpp
590 lines (493 loc) · 15.8 KB
/
ShpFile.cpp
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/**
* GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoDa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <boost/functional/hash.hpp>
#include "ShpFile.h"
#include "GenUtils.h"
bool Shapefile::operator==(Point const& a, Point const& b)
{
return a.x == b.x && a.y == b.y;
}
std::size_t Shapefile::hash_value(Point const& p)
{
std::size_t seed=0;
boost::hash_combine(seed, p.x);
boost::hash_combine(seed, p.y);
return seed;
}
bool Shapefile::operator==(Edge const& e1, Edge const& e2)
{
return e1.a == e2.a && e1.b == e2.b;
}
std::size_t Shapefile::hash_value(Edge const& e)
{
std::size_t seed=0;
boost::hash_combine(seed, e.a.x);
boost::hash_combine(seed, e.a.y);
boost::hash_combine(seed, e.b.x);
boost::hash_combine(seed, e.b.y);
return seed;
}
int Shapefile::calcNumIndexHeaderRecords(const Shapefile::Header& header)
{
// The header length is 50, 16-byte words, and header.file_length records
// the number of 16-byte words. So, (header.file_length - 50) is the number
// of non-header 16-byte words. Since each header record is 16-bytes long,
// we divide by 4 to get the number of records.
return (header.file_length - 50)/4;
}
bool Shapefile::populatePointMainRecords(std::vector<MainRecord>& mr,
std::ifstream& file,
bool skip_m, bool skip_z)
{
bool success = true;
wxInt32 integer32;
wxInt32* integer32p = &integer32;
wxFloat64 float64;
wxFloat64* float64p = &float64;
int start_seek_pos = 100; // beginning of data
file.seekg(start_seek_pos, std::ios::beg);
int total_records = mr.size();
int rec_num;
for (int i=0; i<total_records && success; i++) {
file.read((char*) integer32p, 4);
rec_num = myINT_SWAP_ON_LE(integer32);
if (rec_num < 1 || rec_num > total_records ||
mr[rec_num-1].header.record_number != 0) {
success = false;
} else { // we have a non-duplicated, valid record number
mr[rec_num-1].header.record_number = rec_num;
file.read((char*) integer32p, 4);
mr[rec_num-1].header.content_length = myINT_SWAP_ON_LE(integer32);
PointContents* pc =
dynamic_cast<PointContents*>(mr[rec_num-1].contents_p);
file.read((char*) integer32p, 4);
pc->shape_type = myINT_SWAP_ON_BE(integer32);
file.read((char*) float64p, 8);
pc->x = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->y = myDOUBLE_SWAP_ON_BE(float64);
if (skip_z) file.read((char*) float64p, 8);
if (skip_m) file.read((char*) float64p, 8);
}
}
return success;
}
bool Shapefile::populatePolyLineMainRecords(std::vector<MainRecord>& mr,
std::ifstream& file,
bool skip_m, bool skip_z)
{
bool success = true;
wxInt32 integer32;
wxInt32* integer32p = &integer32;
wxFloat64 float64;
wxFloat64* float64p = &float64;
int start_seek_pos = 100; // beginning of data
file.seekg(start_seek_pos, std::ios::beg);
int total_records = mr.size();
int rec_num;
for (int i=0; i<total_records && success; i++) {
file.read((char*) integer32p, 4);
rec_num = myINT_SWAP_ON_LE(integer32);
if (rec_num < 1 || rec_num > total_records ||
mr[rec_num-1].header.record_number != 0) {
success = false;
} else { // we have a non-duplicated, valid record number
mr[rec_num-1].header.record_number = rec_num;
file.read((char*) integer32p, 4);
mr[rec_num-1].header.content_length = myINT_SWAP_ON_LE(integer32);
PolyLineContents* pc =
dynamic_cast<PolyLineContents*>(mr[rec_num-1].contents_p);
file.read((char*) integer32p, 4);
pc->shape_type = myINT_SWAP_ON_BE(integer32);
file.read((char*) float64p, 8);
pc->box[0] = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->box[1] = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->box[2] = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->box[3] = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) integer32p, 4);
pc->num_parts = myINT_SWAP_ON_BE(integer32);
pc->parts.resize(pc->num_parts);
file.read((char*) integer32p, 4);
pc->num_points = myINT_SWAP_ON_BE(integer32);
pc->points.resize(pc->num_points);
for (int j=0; j < pc->num_parts; j++) {
file.read((char*) integer32p, 4);
pc->parts[j] = myINT_SWAP_ON_BE(integer32);
}
for (int j=0; j < pc->num_points; j++) {
file.read((char*) float64p, 8);
pc->points[j].x = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->points[j].y = myDOUBLE_SWAP_ON_BE(float64);
}
if (skip_z) {
file.read((char*) float64p, 8); // bounding z range
file.read((char*) float64p, 8); // bounding z range
for (int j=0; j < pc->num_points; j++) {
file.read((char*) float64p, 8); // read z value
}
}
if (skip_m) {
file.read((char*) float64p, 8); // bounding m range
file.read((char*) float64p, 8); // bounding m range
for (int j=0; j < pc->num_points; j++) {
file.read((char*) float64p, 8); // read m value
}
}
}
}
return success;
}
bool Shapefile::populatePolygonMainRecords(std::vector<MainRecord>& mr,
std::ifstream& file,
bool skip_m, bool skip_z)
{
bool success = true;
wxInt32 integer32;
wxInt32* integer32p = &integer32;
wxFloat64 float64;
wxFloat64* float64p = &float64;
int start_seek_pos = 100; // beginning of data
file.seekg(start_seek_pos, std::ios::beg);
int total_records = mr.size();
int rec_num;
for (int i=0; i<total_records && success; i++) {
file.read((char*) integer32p, 4);
rec_num = myINT_SWAP_ON_LE(integer32);
if (rec_num < 1 || rec_num > total_records ||
mr[rec_num-1].header.record_number != 0) {
success = false;
} else { // we have a non-duplicated, valid record number
mr[rec_num-1].header.record_number = rec_num;
file.read((char*) integer32p, 4);
mr[rec_num-1].header.content_length = myINT_SWAP_ON_LE(integer32);
PolygonContents* pc =
dynamic_cast<PolygonContents*>(mr[rec_num-1].contents_p);
file.read((char*) integer32p, 4);
pc->shape_type = myINT_SWAP_ON_BE(integer32);
file.read((char*) float64p, 8);
pc->box[0] = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->box[1] = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->box[2] = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->box[3] = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) integer32p, 4);
pc->num_parts = myINT_SWAP_ON_BE(integer32);
pc->parts.resize(pc->num_parts);
file.read((char*) integer32p, 4);
pc->num_points = myINT_SWAP_ON_BE(integer32);
pc->points.resize(pc->num_points);
for (int j=0; j < pc->num_parts; j++) {
file.read((char*) integer32p, 4);
pc->parts[j] = myINT_SWAP_ON_BE(integer32);
}
for (int j=0; j < pc->num_points; j++) {
file.read((char*) float64p, 8);
pc->points[j].x = myDOUBLE_SWAP_ON_BE(float64);
file.read((char*) float64p, 8);
pc->points[j].y = myDOUBLE_SWAP_ON_BE(float64);
}
if (skip_z) {
file.read((char*) float64p, 8); // bounding z range
file.read((char*) float64p, 8); // bounding z range
for (int j=0; j < pc->num_points; j++) {
file.read((char*) float64p, 8); // read z value
}
}
if (skip_m) {
file.read((char*) float64p, 8); // bounding m range
file.read((char*) float64p, 8); // bounding m range
for (int j=0; j < pc->num_points; j++) {
file.read((char*) float64p, 8); // read m value
}
}
}
}
return success;
}
bool Shapefile::writeHeader(std::ofstream& out_file,
const Shapefile::Header& header,
wxString& err_msg)
{
if (!(out_file.is_open() && out_file.good())) {
err_msg += "Problem writing shapefile header";
return false;
}
wxInt32 integer32;
wxInt32* integer32p = &integer32;
wxFloat64 float64;
wxFloat64* float64p = &float64;
// First write out Header bytes 0 through 99
// byte 0-3
integer32 = header.file_code;
integer32 = myINT_SWAP_ON_LE(integer32);
out_file.write((char*) integer32p, 4);
// byte 4-23 undefined, so write out 0
integer32 = 0;
out_file.write((char*) integer32p, 4); // bytes 4-7
out_file.write((char*) integer32p, 4); // bytes 8-11
out_file.write((char*) integer32p, 4); // bytes 12-15
out_file.write((char*) integer32p, 4); // bytes 16-19
out_file.write((char*) integer32p, 4); // bytes 20-23
// byte 24-27
integer32 = header.file_length;
integer32 = myINT_SWAP_ON_LE(integer32);
out_file.write((char*) integer32p, 4);
// byte 28-31
integer32 = header.version;
integer32 = myINT_SWAP_ON_BE(integer32);
out_file.write((char*) integer32p, 4);
// byte 32-35
integer32 = header.shape_type;
integer32 = myINT_SWAP_ON_BE(integer32);
out_file.write((char*) integer32p, 4);
// byte 36-44
float64 = header.bbox_x_min;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
// byte 44-51
float64 = header.bbox_y_min;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
// byte 52-59
float64 = header.bbox_x_max;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
// byte 60-67
float64 = header.bbox_y_max;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
// byte 68-75
float64 = header.bbox_z_min;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
// byte 76-83
float64 = header.bbox_z_max;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
// byte 84-91
float64 = header.bbox_m_min;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
// byte 92-99
float64 = header.bbox_m_max;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
return true;
}
bool Shapefile::writePointIndexFile(const wxString& fname,
const Index& index, wxString& err_msg)
{
if (index.header.shape_type != Shapefile::POINT_TYP) {
err_msg += "Not a Point Shapefile";
return false;
}
std::ofstream out_file;
out_file.open(GET_ENCODED_FILENAME(fname), std::ios::out | std::ios::binary);
if (!(out_file.is_open() && out_file.good())) {
err_msg += "Problem opening \"" + fname + "\"";
return false;
}
wxInt32 integer32;
wxInt32* integer32p = &integer32;
wxFloat64 float64;
wxFloat64* float64p = &float64;
// First write out Header bytes 0 through 99
if (!writeHeader(out_file, index.header, err_msg)) return false;
// Write out Index, starting from byte 100
int total_index_records = calcNumIndexHeaderRecords(index.header);
for (int i=0; i<total_index_records; i++) {
integer32 = index.records[i].offset;
integer32 = myINT_SWAP_ON_LE(integer32);
out_file.write((char*) integer32p, 4);
integer32 = index.records[i].content_length;
integer32 = myINT_SWAP_ON_LE(integer32);
out_file.write((char*) integer32p, 4);
}
out_file.close();
return true;
}
bool Shapefile::writePointMainFile(const wxString& fname,
const Main& main, wxString& err_msg)
{
if (main.header.shape_type != Shapefile::POINT_TYP) {
err_msg += "Not a Point Shapefile";
return false;
}
std::ofstream out_file;
out_file.open(GET_ENCODED_FILENAME(fname),std::ios::out | std::ios::binary);
if (!(out_file.is_open() && out_file.good())) {
err_msg += "Problem opening \"" + fname + "\"";
return false;
}
wxInt32 integer32;
wxInt32* integer32p = &integer32;
wxFloat64 float64;
wxFloat64* float64p = &float64;
// First write out Main Header bytes 0 through 99
if (!writeHeader(out_file, main.header, err_msg)) return false;
// Write out Main Records, starting from byte 100
int total_records = main.records.size();
for (int i=0; i<total_records; i++) {
integer32 = main.records[i].header.record_number;
integer32 = myINT_SWAP_ON_LE(integer32);
out_file.write((char*) integer32p, 4);
integer32 = main.records[i].header.content_length;
integer32 = myINT_SWAP_ON_LE(integer32);
out_file.write((char*) integer32p, 4);
PointContents* pc = (PointContents*) main.records[i].contents_p;
integer32 = pc->shape_type;
integer32 = myINT_SWAP_ON_BE(integer32);
out_file.write((char*) integer32p, 4);
float64 = pc->x;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
float64 = pc->y;
float64 = myDOUBLE_SWAP_ON_BE(float64);
out_file.write((char*) float64p, 8);
}
out_file.close();
return true;
}
bool Shapefile::writePolygonIndexFile(const wxString& fname,
const Index& index, wxString& err_msg)
{
if (index.header.shape_type != Shapefile::POLYGON) {
err_msg += "Not a Polygon Shapefile";
return false;
}
std::ofstream out_file;
out_file.open(GET_ENCODED_FILENAME(fname),std::ios::out | std::ios::binary);
if (!(out_file.is_open() && out_file.good())) {
err_msg += "Problem opening \"" + fname + "\"";
return false;
}
wxInt32 integer32;
wxInt32* integer32p = &integer32;
wxFloat64 float64;
wxFloat64* float64p = &float64;
// First write out Header bytes 0 through 99
if (!writeHeader(out_file, index.header, err_msg)) return false;
// Write out Index, starting from byte 100
int total_index_records = calcNumIndexHeaderRecords(index.header);
for (int i=0; i<total_index_records; i++) {
integer32 = index.records[i].offset;
integer32 = myINT_SWAP_ON_LE(integer32);
out_file.write((char*) integer32p, 4);
integer32 = index.records[i].content_length;
integer32 = myINT_SWAP_ON_LE(integer32);
out_file.write((char*) integer32p, 4);
}
out_file.close();
return true;
}
std::string Shapefile::getIndentString(int indent)
{
std::string a("");
std::string b("");
for (int i=0; i<spaces_per_indent; i++) a += " ";
for (int i=0; i<indent; i++) b += a;
return b;
}
std::string Shapefile::pointToString(const Shapefile::Point& p)
{
std::ostringstream s;
s << "(" << p.x << ", " << p.y << ")";
return s.str();
}
std::string Shapefile::edgeToString(const Shapefile::Edge& e)
{
std::ostringstream s;
s << "{" << pointToString(e.a) << ", " << pointToString(e.b) << "}";
return s.str();
}
std::string Shapefile::boxToString(const std::vector<wxFloat64>& box)
{
if (box.size() < 4) return "";
std::ostringstream s;
s << "(" << box[0] << ", " << box[1] << ", " << box[2]
<< ", " << box[3] << ")";
return s.str();
}
/** The following could define a run-time and relatively robust endianess test */
//bool isBigEndian() {
// const int i = 1;
// return (*(char*)&i) == 0;
//}
/** Alternately, to avoid a function call, could use the following macro */
const int i = 1;
#define is_bigendian() ( (*(char*)&i) == 0 )
int Shapefile::myINT_SWAP_ON_BE( int x )
{
if is_bigendian() {
char* c = (char*) &x;
union {
wxInt32 y;
char c[4];
} data;
data.c[0] = c[3];
data.c[1] = c[2];
data.c[2] = c[1];
data.c[3] = c[0];
return data.y;
}
return x;
}
int Shapefile::myINT_SWAP_ON_LE( int x )
{
if is_bigendian() {
return x;
}
char* c = (char*) &x;
union {
wxInt32 y;
char c[4];
} data;
data.c[0] = c[3];
data.c[1] = c[2];
data.c[2] = c[1];
data.c[3] = c[0];
return data.y;
}
wxFloat64 Shapefile::myDOUBLE_SWAP_ON_BE( wxFloat64 x )
{
if is_bigendian() {
char* c = (char*) &x;
union {
wxFloat64 y;
char c[8];
} data;
data.c[0] = c[7];
data.c[1] = c[6];
data.c[2] = c[5];
data.c[3] = c[4];
data.c[4] = c[3];
data.c[5] = c[2];
data.c[6] = c[1];
data.c[7] = c[0];
return data.y;
}
return x;
}