-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLSDShapeTools.cpp
More file actions
531 lines (420 loc) · 16.3 KB
/
LSDShapeTools.cpp
File metadata and controls
531 lines (420 loc) · 16.3 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
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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDShapeTools
// Land Surface Dynamics Shapefile tools
//
// A collection of routines for maipulating the binary ESRI shapefile format
// for use within the Edinburgh Land Surface Dynamics group topographic toolbox
//
// Developed by:
// Simon M. Mudd
// Martin D. Hurst
// David T. Milodowski
// Stuart W.D. Grieve
// Declan A. Valters
// Fiona Clubb
//
// Copyright (C) 2013 Simon M. Mudd 2013
//
// Developer can be contacted by simon.m.mudd _at_ ed.ac.uk
//
// Simon Mudd
// University of Edinburgh
// School of GeoSciences
// Drummond Street
// Edinburgh, EH8 9XP
// Scotland
// United Kingdom
//
// This program 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 2 of the License, or (at your option) any later version.
//
// This program 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, write to:
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301
// USA
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//-----------------------------------------------------------------
//DOCUMENTATION URL: http://www.geos.ed.ac.uk/~s0675405/LSD_Docs/
//-----------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include "LSDShapeTools.hpp"
using namespace std;
#ifndef ShapeTools_CPP
#define ShapeTools_CPP
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method to test the Byte order of the system.
// Returns a boolean value where true is little endian.
//
// SWDG 11/3/14
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
bool SystemEndiannessTest(){
int TestInt = 1; //this is stored as 4 bytes in memory
int ReconstructedTestInt;
int ReconstructedTestIntSwapped;
char * TestBytes = (char *) &TestInt; //convert each byte of the int into a char
memcpy(&ReconstructedTestInt, TestBytes, 4);
BYTE temp = TestBytes[0];
TestBytes[0] = TestBytes[3];
TestBytes[3] = temp;
temp = TestBytes[1];
TestBytes[1] = TestBytes[2];
TestBytes[2] = temp;
memcpy(&ReconstructedTestIntSwapped, TestBytes, 4);
if (ReconstructedTestInt == 1){
//cout << "Little Endian" << endl;
return true;
}
else if (ReconstructedTestIntSwapped == 1){
//cout << "Big Endian" << endl;
return false;
}
else{
cout << "Unable to determine endianness of system." << endl;
exit(EXIT_FAILURE);
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method to swap the byte order of a word in memory. Used if the system's byte order
// does not match the data in the shapefile.
//
// SWDG 11/3/14
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void ByteSwap(int length, void * ByteData){
BYTE temp;
for (int i = 0; i< length/2; ++i){
temp = ((BYTE *) ByteData)[i];
((BYTE *) ByteData)[i] = ((BYTE *) ByteData)[length-i-1];
((BYTE *) ByteData)[length-i-1] = temp;
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method to get the size of the binary file being loaded.
//
// Taken from http://www.dreamincode.net/forums/topic/170054-understanding-and-reading-binary-files-in-c/
//
// SWDG 10/3/14
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
long getFileSize(FILE *file){
long lCurPos, lEndPos;
lCurPos = ftell(file);
fseek(file, 0, 2);
lEndPos = ftell(file);
fseek(file, lCurPos, 0);
return lEndPos;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method to load an ESRI ShapeFile.
//
// Only works for X,Y point shapefiles at present and it's behaviour is totally undefined
// if you pass in any other type of file.
//
// In future this will be rebuilt into a full class that can support shapefiles of
// different types.
//
// Built in part from:
// http://www.dreamincode.net/forums/topic/170054-understanding-and-reading-binary-files-in-c/
//
// SWDG 13/3/14
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
PointData LoadShapefile(string Filename){
BYTE *ByteData; // Pointer to our buffered data
FILE *file = NULL; // File pointer
// Open the file in binary mode using the "rb" format string
// This also checks if the file exists and/or can be opened for reading correctly
if ((file = fopen(Filename.c_str(), "rb")) == NULL){
cout << "Could not open specified file" << endl;
exit(EXIT_FAILURE);
}
else{
cout << "File opened successfully" << endl;
}
// Get the size of the file in bytes
long fileSize = getFileSize(file);
// Allocate space in the buffer for the whole file
ByteData = new BYTE[fileSize];
// Read the file in to the buffer
fread(ByteData, fileSize, 1, file);
//Declare variables used in the method
int FileLength;
int ShapeType;
double Xmin;
double Ymin;
double Xmax;
double Ymax;
double Zmin;
double Zmax;
double Mmin;
double Mmax;
int RecordLength;
int NoOfRecords;
PointData Points;
double TempX;
double TempY;
bool LittleEndian = SystemEndiannessTest(); // Get byteorder of the system
// If system byte order is Little Endian
if (LittleEndian == true){
// Get the length of the file
ByteSwap(4, ByteData+24);
memcpy(&FileLength, ByteData+24, 4);
// Get type of shape in file (not currently used) see
memcpy(&ShapeType, ByteData+32, 4);
// Get Georeferencing data (not currently used)
memcpy(&Xmin, ByteData+36, 8);
memcpy(&Ymin, ByteData+44, 8);
memcpy(&Xmax, ByteData+52, 8);
memcpy(&Ymax, ByteData+60, 8);
memcpy(&Zmin, ByteData+68, 8);
memcpy(&Zmax, ByteData+76, 8);
memcpy(&Mmin, ByteData+84, 8);
memcpy(&Mmax, ByteData+92, 8);
// Get the length of the first record (All records are the same length for points)
ByteSwap(4, ByteData+104);
memcpy(&RecordLength, ByteData+104, 4);
//Calculate the number of records in the file
NoOfRecords = (FileLength-50)/(RecordLength+4); // FileLength - 50(the length in words of the header)
// divided by RecordLength+4 (4 is the length in words of the record header)
//Read all of the records into the Points structure
for (int q = 0; q < NoOfRecords; ++q){
memcpy(&TempX, ByteData+112+(q * ((RecordLength+4)*2)), 8); // RecordLength+4*2 == 28 (The total length of a record)
memcpy(&TempY, ByteData+120+(q * ((RecordLength+4)*2)), 8);
Points.X.push_back(TempX);
Points.Y.push_back(TempY);
}
}
// If system byte order is Big Endian
else{
// Get the length of the file
memcpy(&FileLength, ByteData+24, 4);
// Get type of shape in file (not currently used) see
ByteSwap(4, ByteData+32);
memcpy(&ShapeType, ByteData+32, 4);
// Get Georeferencing data (not currently used)
ByteSwap(8, ByteData+36);
memcpy(&Xmin, ByteData+36, 8);
ByteSwap(8, ByteData+44);
memcpy(&Ymin, ByteData+44, 8);
ByteSwap(8, ByteData+52);
memcpy(&Xmax, ByteData+52, 8);
ByteSwap(8, ByteData+60);
memcpy(&Ymax, ByteData+60, 8);
ByteSwap(8, ByteData+68);
memcpy(&Zmin, ByteData+68, 8);
ByteSwap(8, ByteData+76);
memcpy(&Zmax, ByteData+76, 8);
ByteSwap(8, ByteData+84);
memcpy(&Mmin, ByteData+84, 8);
ByteSwap(8, ByteData+92);
memcpy(&Mmax, ByteData+92, 8);
// Get the length of the first record (All records are the same length for points)
memcpy(&RecordLength, ByteData+104, 4);
//Calculate the number of records in the file
NoOfRecords = (FileLength-50)/(RecordLength+4); // FileLength - 50(the length in words of the header)
// divided by RecordLength+4 (4 is the length in words of the record header)
//Read all of the records into the Points structure
for (int q = 0; q < NoOfRecords; ++q){
ByteSwap(8, ByteData+112+(q * ((RecordLength+4)*2)));
memcpy(&TempX, ByteData+112+(q * ((RecordLength+4)*2)), 8); // RecordLength+4*2 == 28 (The total length of a record)
ByteSwap(8, ByteData+120+(q * ((RecordLength+4)*2)));
memcpy(&TempY, ByteData+120+(q * ((RecordLength+4)*2)), 8);
Points.X.push_back(TempX);
Points.Y.push_back(TempY);
}
}
// Close the file and return the point data
fclose(file);
return Points;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method to load an ESRI polyline Shapefile.
//
// Only works for polyline shapefiles at present and it's behaviour is totally undefined
// if you pass in any other type of file.
//
// In future this will be rebuilt into a full class that can support shapefiles of
// different types.
//
// Returns a vector of points. So that each item in the vector represents a single polyline.
//
// Built in part from:
// http://www.dreamincode.net/forums/topic/170054-understanding-and-reading-binary-files-in-c/
//
// SWDG 17/3/14
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
vector<PointData> LoadPolyline(string Filename){
BYTE *ByteData; // Pointer to our buffered data
FILE *file = NULL; // File pointer
// Open the file in binary mode using the "rb" format string
// This also checks if the file exists and/or can be opened for reading correctly
if ((file = fopen(Filename.c_str(), "rb")) == NULL){
cout << "Could not open specified file" << endl;
exit(EXIT_FAILURE);
}
else{
cout << "File opened successfully" << endl;
}
// Get the size of the file in bytes
long fileSize = getFileSize(file);
// Allocate space in the buffer for the whole file
ByteData = new BYTE[fileSize];
// Read the file in to the buffer
fread(ByteData, fileSize, 1, file);
//Declare variables used in the method
int FileLength;
int ShapeType;
double Xmin;
double Ymin;
double Xmax;
double Ymax;
double Zmin;
double Zmax;
double Mmin;
double Mmax;
int RecordLength;
PointData Points;
double TempX;
double TempY;
vector<PointData> Polylines;
int Skip;
int shapetype;
int numparts;
int numpoints;
bool LittleEndian = SystemEndiannessTest(); // Get byteorder of the system
// If system byte order is Little Endian
if (LittleEndian == true){
// Get the length of the file
ByteSwap(4, ByteData+24);
memcpy(&FileLength, ByteData+24, 4);
// Get type of shape in file (not currently used) see
memcpy(&ShapeType, ByteData+32, 4);
// Get Georeferencing data (not currently used)
memcpy(&Xmin, ByteData+36, 8);
memcpy(&Ymin, ByteData+44, 8);
memcpy(&Xmax, ByteData+52, 8);
memcpy(&Ymax, ByteData+60, 8);
memcpy(&Zmin, ByteData+68, 8);
memcpy(&Zmax, ByteData+76, 8);
memcpy(&Mmin, ByteData+84, 8);
memcpy(&Mmax, ByteData+92, 8);
// Get the length of the first record
ByteSwap(4, ByteData+104);
memcpy(&RecordLength, ByteData+104, 4);
Skip = RecordLength*2 + 8 + 100;
memcpy(&shapetype, ByteData+108, 4);
memcpy(&numparts, ByteData+144, 4);
memcpy(&numpoints, ByteData+148, 4); // number of XY pairs in the first polyline
for (int q = 0; q < numpoints; ++q){
memcpy(&TempX, ByteData+156+(q*16), 8);
memcpy(&TempY, ByteData+164+(q*16), 8);
Points.X.push_back(TempX);
Points.Y.push_back(TempY);
}
Polylines.push_back(Points);
while (Skip < (FileLength*2)){
ByteSwap(4, ByteData+Skip+4);
memcpy(&RecordLength, ByteData+Skip+4, 4);
memcpy(&shapetype, ByteData+Skip+8, 4);
memcpy(&numparts, ByteData+Skip+44, 4);
memcpy(&numpoints, ByteData+Skip+48, 4); // number of XY pairs in the polyline
PointData Points;
for (int w = 0; w < numpoints; ++w){
memcpy(&TempX, ByteData+Skip+56+(w*16), 8);
memcpy(&TempY, ByteData+Skip+64+(w*16), 8);
Points.X.push_back(TempX);
Points.Y.push_back(TempY);
}
Polylines.push_back(Points);
Skip += (RecordLength*2) +8;
}
}
// If system byte order is Big Endian
else{
// Get the length of the file
memcpy(&FileLength, ByteData+24, 4);
// Get type of shape in file (not currently used) see
ByteSwap(4, ByteData+32);
memcpy(&ShapeType, ByteData+32, 4);
// Get Georeferencing data (not currently used)
ByteSwap(8, ByteData+36);
memcpy(&Xmin, ByteData+36, 8);
ByteSwap(8, ByteData+44);
memcpy(&Ymin, ByteData+44, 8);
ByteSwap(8, ByteData+52);
memcpy(&Xmax, ByteData+52, 8);
ByteSwap(8, ByteData+60);
memcpy(&Ymax, ByteData+60, 8);
ByteSwap(8, ByteData+68);
memcpy(&Zmin, ByteData+68, 8);
ByteSwap(8, ByteData+76);
memcpy(&Zmax, ByteData+76, 8);
ByteSwap(8, ByteData+84);
memcpy(&Mmin, ByteData+84, 8);
ByteSwap(8, ByteData+92);
memcpy(&Mmax, ByteData+92, 8);
// Get the length of the first record
memcpy(&RecordLength, ByteData+104, 4);
Skip = RecordLength*2 + 8 + 100;
ByteSwap(8, ByteData+108);
memcpy(&shapetype, ByteData+108, 4);
ByteSwap(8, ByteData+144);
memcpy(&numparts, ByteData+144, 4);
ByteSwap(8, ByteData+148);
memcpy(&numpoints, ByteData+148, 4); // number of XY pairs in the first polyline
for (int q = 0; q < numpoints; ++q){
ByteSwap(8, ByteData+156+(q*16));
memcpy(&TempX, ByteData+156+(q*16), 8);
ByteSwap(8, ByteData+164+(q*16));
memcpy(&TempY, ByteData+164+(q*16), 8);
cout << TempX << " " << TempY << endl;
Points.X.push_back(TempX);
Points.Y.push_back(TempY);
}
Polylines.push_back(Points);
cout << "---------" << endl;
while (Skip < (FileLength*2)){
ByteSwap(8, ByteData+Skip+4);
memcpy(&RecordLength, ByteData+Skip+4, 4);
ByteSwap(8, ByteData+Skip+8);
memcpy(&shapetype, ByteData+Skip+8, 4);
ByteSwap(8, ByteData+Skip+44);
memcpy(&numparts, ByteData+Skip+44, 4);
ByteSwap(8, ByteData+Skip+48);
memcpy(&numpoints, ByteData+Skip+48, 4); // number of XY pairs in the polyline
PointData Points;
for (int w = 0; w < numpoints; ++w){
ByteSwap(8, ByteData+Skip+56+(w*16));
memcpy(&TempX, ByteData+Skip+56+(w*16), 8);
ByteSwap(8, ByteData+Skip+64+(w*16));
memcpy(&TempY, ByteData+Skip+64+(w*16), 8);
cout << TempX << " " << TempY << endl;
Points.X.push_back(TempX);
Points.Y.push_back(TempY);
}
Polylines.push_back(Points);
Skip += (RecordLength*2) +8;
cout << "------" << endl;
}
}
// Close the file and return the point data
fclose(file);
return Polylines;
}
#endif