-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSDShapeTools.hpp
376 lines (331 loc) · 13.2 KB
/
LSDShapeTools.hpp
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
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// 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/
//-----------------------------------------------------------------
using namespace std;
#include <vector>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <cmath>
#ifndef ShapeTools_H
#define ShapeTools_H
// An unsigned char can store 1 Byte (8bits) of data
typedef unsigned char BYTE;
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method to test the Byte order of the system.
// Returns a boolean value where true is little endian.
//
// SWDG 11/3/14
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
bool SystemEndiannessTest();
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Structure to store X and Y point data.
//
// SWDG 12/3/14
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
struct PointData
{
vector<double> X;
vector<double> Y;
};
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// 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);
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method to load an ESRI ShapeFile.
//
// Only works for X,Y point shapefiles at present and it's behavious 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);
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// 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);
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method to convert an IndexChannelTree to a PointData object.
//
// Returns a vector of points.
//
// multistem_option -> 0 = mainstem only, 1 = all tributaries, 2 specify tributary number (DAV 09/04/2015)
// DTM 11/07/2014
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
PointData LoadChannelTree(string Filename, int multistem_option = 0, int trib_number = 0);
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// 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);
/// @brief Ellipsoid class for converting coordinates between UTM and lat long
class LSDEllipsoid
{
public:
/// @detail the default constructor
LSDEllipsoid() {};
/// @detail assigment constructor for the ellipsiod class
/// @param id a reference into the ellipsoid
/// @param name the name of the ellipsoid
/// @param radius the radius of the equator in km
/// @param fr not sure what this is
LSDEllipsoid(int id, char* name, double radius, double fr)
{ Name=name; EquatorialRadius=radius; eccSquared=2/fr-1/(fr*fr);}
/// name of the ellipsoid
char* Name;
/// equatorial radius in km
double EquatorialRadius;
/// square of the equatorial radius
double eccSquared;
};
/// @brief Datum class for converting coordinates between UTM and lat long
class LSDDatum
{
public:
LSDDatum(){};
LSDDatum(int id, char* name, int eid, double dx, double dy, double dz)
{ Name=name; eId=eid; dX=dx; dY=dy; dZ=dz;}
/// name of the datum
char* Name;
/// the ellipsoid id
int eId;
double dX;
double dY;
double dZ;
};
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// A class for converting datums and coordinates
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
class LSDCoordinateConverterLLandUTM
{
public:
// default constructor. This sets up the data elements.
LSDCoordinateConverterLLandUTM() { create(); }
/// @brief converts LatLong to UTM coords
/// 3/22/95: by ChuckGantz chuck.gantz@globalstar.com, from USGS Bulletin 1532.
/// @param eID the ellipsoid ID. Options are:
/// 0 = "Airy1830";
/// 1 = "AiryModified";
/// 2 = "AustralianNational";
/// 3 = "Bessel1841Namibia";
/// 4 = "Bessel1841";
/// 5 = "Clarke1866";
/// 6 = "Clarke1880";
/// 7 = "EverestIndia1830";
/// 8 = "EverestSabahSarawak";
/// 9 = "EverestIndia1956";
/// 10 = "EverestMalaysia1969";
/// 11 = "EverestMalay_Sing";
/// 12 = "EverestPakistan";
/// 13 = "Fischer1960Modified";
/// 14 = "Helmert1906";
/// 15 = "Hough1960";
/// 16 = "Indonesian1974";
/// 17 = "International1924";
/// 18 = "Krassovsky1940";
/// 19 = "GRS80";
/// 20 = "SouthAmerican1969";
/// 21 = "WGS72";
/// 22 = "WGS84";
/// @param Lat the latitude in decimal degrees
/// @param Long the longitude in decimal degrees
/// @param Northing in metres. This argument is replaced by the function
/// @param Easting in metres. This argument is replaced by the function
/// @param Zone the UTM zone. This argument is replaced by the function
/// @author SMM, modified from Chuck Gantz
/// @date 07/12/2014
void LLtoUTM(int eId, double Lat, double Long,
double& Northing, double& Easting, int& Zone);
/// @brief converts LatLong to UTM coords, but forces the data to a specific zone
/// 3/22/95: by ChuckGantz chuck.gantz@globalstar.com, from USGS Bulletin 1532.
/// Updated by Simon M Mudd
/// @param eID the ellipsoid ID. Options are:
/// 0 = "Airy1830";
/// 1 = "AiryModified";
/// 2 = "AustralianNational";
/// 3 = "Bessel1841Namibia";
/// 4 = "Bessel1841";
/// 5 = "Clarke1866";
/// 6 = "Clarke1880";
/// 7 = "EverestIndia1830";
/// 8 = "EverestSabahSarawak";
/// 9 = "EverestIndia1956";
/// 10 = "EverestMalaysia1969";
/// 11 = "EverestMalay_Sing";
/// 12 = "EverestPakistan";
/// 13 = "Fischer1960Modified";
/// 14 = "Helmert1906";
/// 15 = "Hough1960";
/// 16 = "Indonesian1974";
/// 17 = "International1924";
/// 18 = "Krassovsky1940";
/// 19 = "GRS80";
/// 20 = "SouthAmerican1969";
/// 21 = "WGS72";
/// 22 = "WGS84";
/// @param Lat the latitude in decimal degrees
/// @param Long the longitude in decimal degrees
/// @param Northing in metres. This argument is replaced by the function
/// @param Easting in metres. This argument is replaced by the function
/// @param Zone the UTM zone. This argument is replaced by the function
/// @author SMM, modified from Chuck Gantz
/// @date 11/04/2016
void LLtoUTM_ForceZone(int eId, double Lat, double Long,
double& Northing, double& Easting, int Zone);
/// @brief converts LatLong to UTM coords
/// 3/22/95: by ChuckGantz chuck.gantz@globalstar.com, from USGS Bulletin 1532.
/// @param eID the ellipsoid ID. Options are:
/// 0 = "Airy1830";
/// 1 = "AiryModified";
/// 2 = "AustralianNational";
/// 3 = "Bessel1841Namibia";
/// 4 = "Bessel1841";
/// 5 = "Clarke1866";
/// 6 = "Clarke1880";
/// 7 = "EverestIndia1830";
/// 8 = "EverestSabahSarawak";
/// 9 = "EverestIndia1956";
/// 10 = "EverestMalaysia1969";
/// 11 = "EverestMalay_Sing";
/// 12 = "EverestPakistan";
/// 13 = "Fischer1960Modified";
/// 14 = "Helmert1906";
/// 15 = "Hough1960";
/// 16 = "Indonesian1974";
/// 17 = "International1924";
/// 18 = "Krassovsky1940";
/// 19 = "GRS80";
/// 20 = "SouthAmerican1969";
/// 21 = "WGS72";
/// 22 = "WGS84";
/// @param Northing in metres.
/// @param Easting in metres.
/// @param Zone the UTM zone.
/// @param isNorth is a boolean that states if the map is in the northern hemisphere
/// @param Lat the latitude in decimal degrees.
/// This argument is replaced by the function
/// @param Long the longitude in decimal degrees
/// This argument is replaced by the function
/// @author SMM, modified from Chuck Gantz
/// @date 07/12/2014
void UTMtoLL(int eId, double Northing, double Easting, int Zone, bool isNorth,
double& Lat, double& Long);
/// @brief converts LatLongHt in datum dIn, to LatLongHt in datum dTo;
/// @detail 2002dec: by Eugene Reimer, from PeterDana equations.
/// Lat and Long params are in degrees;
/// North latitudes and East longitudes are positive; Height is in meters;
/// ==This approach to Datum-conversion is a waste of time;
/// to get acceptable accuracy a large table is needed -- see NADCON, NTv2...
void DatumConvert(int dIn, double LatIn, double LongIn, double HtIn,
int dTo, double& LatTo, double& LongTo, double& HtTo);
protected:
/// @brief a vector holding the ellipsoids
vector<LSDEllipsoid> Ellipsoids;
/// @brief a vectro holding the datums
vector<LSDDatum> Datums;
double RADIANS_PER_DEGREE;
double DEGREES_PER_RADIAN;
/** Useful constants **/
double TWOPI;
double HALFPI;
// Grid granularity for rounding UTM coordinates to generate MapXY.
double grid_size; // 100 km grid
// WGS84 Parameters
double WGS84_A; // major axis
double WGS84_B; // minor axis
double WGS84_F; // ellipsoid flattening
double WGS84_E; // first eccentricity
double WGS84_EP; // second eccentricity
// UTM Parameters
double UTM_K0; // scale factor
double UTM_FE; // false easting
double UTM_FN_N; // false northing, northern hemisphere
double UTM_FN_S; // false northing, southern hemisphere
double UTM_E2; // e^2
double UTM_E4; // e^4
double UTM_E6; // e^6
double UTM_EP2; // e'^2
private:
/// @brief This create function sets up the data membeers that hold the
/// ellipsoid and datum data
void create();
};
#endif