-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESRIFiles.h
403 lines (366 loc) · 15.4 KB
/
ESRIFiles.h
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
/**
BSD 2-Clause License
Copyright (c) 2020, Andrey Kudryavtsev (andrewkoudr@hotmail.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <vector>
#include <string>
#include "Vector.h"
#include "LatLon.h"
using namespace std;
#pragma pack(push,1)
//===== ESRI shapefiles ========================================================
// https://dds.cr.usgs.gov/srtm/version2_1/SWBD/
// used to represent water body data. Each SWBD zip file contains three files :
// .shp, .shx and .dbf. This file decribes a cell as HxxxVyyc, where Hxxx is
// longtude (H is E or W), Vyy - latitude (V is N or S) and c (continental delivery)
// can be
// a - Australia
// e - Eurasia
// f - Africa
// i - Islands
// n - North America
// s - South America
//
// All integers in ESRI file are 32-bit, all floats are doubles (8 byte)
// continental delivery
enum {
CD_AUSTRALIA,
CD_EURASIA,
CD_AFRICA,
CD_ISLANDS,
CD_NAMERICA,
CD_SAMERICA,
CD_TOTAL
};
// characters in the end of ESRI file name
extern const char ESRIContDeliveryChars[CD_TOTAL];
// parse SWBD file name, like e054n39e.zip
bool ESRIFilenameToLatLon(const string &filename, double *lat, double *lon,
int *contdelivery, string &errorString);
// convert ESRI filename like e054n39e.zip to lat/lon min/max
bool ESRIFilenameToRect(const string &filename, SLatLonRect *rect, string &errorString);
// convert lat/lon (lower left corner) to ESRI file name
// without extension, like e054n39e.zip
string LatLonToESRIFilename(double lat, double lon, int contdelivery);
// change endianness for an array of size 32-bit integers
void BSwap32(size_t size, void *addr);
// data types in ESRI file
typedef int ESRIint;
typedef double ESRIfloat;
// ESRI file
class ESRIFile : public LatLonRect {
protected:
// extract SHP data
bool ExtractSHP(char *data, int size);
// extract SHX data
bool ExtractSHX(char *data, int size);
//NB extract DBF data; DBF header contains only one
// field descriptor, which is NOT usually the case
bool ExtractDBF(char *data, int size);
public:
// record type, taken from DBF
enum {
SWBD_OCEAN,
SWBD_LAKE,
SWBD_RIVER,
SWBD_TOTAL
};
// ESRI shape types
enum {
ESRI_NULLSHAPE = 0,
ESRI_POINT = 1,
ESRI_POLYLINE = 3,
// filled part is to the right of moving
// along points
ESRI_POLYGON = 5,
ESRI_MULTIPOINT = 8,
ESRI_POINTZ = 11,
ESRI_POLYLINEZ = 13,
ESRI_POLYGONZ = 15,
ESRI_MULTIPOINTZ = 18,
ESRI_POINTM = 21,
ESRI_POLYLINEM = 23,
ESRI_POLYGONM = 25,
ESRI_MULTIPOINTM = 28,
ESRI_MULTIPATCH = 31,
ESRI_SHAPETOTAL
};
// 100-byte header of SHP file
typedef struct SSHPHeader {
// must be 9994 (bigendian)
ESRIint filecode;
// 5 unused ints
ESRIint unused[5];
// data length in 16-bit words, including 50 words of header, bigendian
ESRIint filelength;
// must be 1000 little endian
ESRIint version;
// all the rest are little endian
ESRIint shapetype;
// bounding box (little endian)
ESRIfloat xmin;
ESRIfloat ymin;
ESRIfloat xmax;
ESRIfloat ymax;
ESRIfloat zmin;
ESRIfloat zmax;
ESRIfloat mmin;
ESRIfloat mmax;
// cast big endian into little endian
void CorrectEndianness()
{
BSwap32(7,this);
};
// shape type OK?
bool ShapeTypeOK()
{
return (shapetype == 0 || shapetype == 1 || shapetype == 3 || shapetype == 5 ||
shapetype == 8 || shapetype == 11 || shapetype == 13 || shapetype == 15 ||
shapetype == 18 || shapetype == 21 || shapetype == 23 || shapetype == 25 ||
shapetype == 28 || shapetype == 31);
};
} SSHPHeader;
// SHP file record header (8 bytes)
typedef struct SSHPRecHeader {
// record number, first record is 1 (big endian)
ESRIint recnumber;
// content length in 16-bit words (big)
ESRIint contlength;
// cast big endian into little endian
void CorrectEndianness()
{
BSwap32(2,this);
};
} SSHPRecHeader;
// SHX file record header (8 bytes)
typedef struct SSHXRecHeader {
// record number, first record is 1 (big endian)
ESRIint offset;
// content length in 16-bit words (big)
ESRIint contlength;
// cast big endian into little endian
void CorrectEndianness()
{
BSwap32(2,this);
};
} SSHXRecHeader;
// DBF field descriptor
typedef struct {
// field name
char fieldname[11];
// field type
char fieldtype;
// field data address
unsigned int fielddataaddress;
// field length
unsigned char fieldlength;
// field decimal count
unsigned char fielddeccount;
// reserved
char reserved0[2];
// work area ID
unsigned char workareaID;
// reserved
char reserved1[2];
// SETFIELDS flag
unsigned char setfieldsflag;
// reserved
char reserved2[8];
} SDBFFieldDescriptor;
// DBF file header
typedef struct {
// first 2 bits - version number
unsigned char version;
// update time
char updatetime[3];
// # records in the table
int numrecords;
// header size
unsigned short headersize;
// record size
unsigned short recordsize;
// reserved
char reserved0[3];
// reserved
char reserved1[13];
// reserved
char reserved2[4];
// field descriptor array
SDBFFieldDescriptor fielddescriptor;
// field terminator
char fieldterminator;
} SDBFHeader;
// record contents to handle and draw
class CESRIRecord {
public:
// shape type
ESRIint shapetype;
// SWBD_... type (ocean, lake or river)
ESRIint SWBDtype;
// offset in bytes from the start of main SHP file
// (this value is loaded from index .SHX file)
ESRIint offset;
// record length in bytes
// (this value is loaded from index .SHX file)
ESRIint length;
// min/max
TVector<ESRIfloat> min;
TVector<ESRIfloat> max;
// 0-based indices of parts, meaningful for
// ESRI_POLYLINE, ESRI_POLYGON
std::vector<ESRIint> parts;
// points of shape type
std::vector<TVector<ESRIfloat> > points;
// constructors
CESRIRecord();
CESRIRecord(ESRIint pshapetype);
// copy constructor and assignment operator
CESRIRecord(const CESRIRecord &other);
CESRIRecord &operator=(const CESRIRecord &other);
// destructor
~CESRIRecord();
// read XY point, fill Z with "no data"
TVector<ESRIfloat> ReadXYPoint(char *data, int &offset);
// read single float
ESRIfloat ReadFloat(char *data, int &offset);
// read integer
ESRIint ReadNumPoints(char *data, int &offset);
// returns signed area in lat/lon units for a part;
// returns 0.0 in failure
ESRIfloat Area(int partno, ESRIfloat accuracymetres);
// returns -1 if this is NOT water point or
// PART number if this IS water point;
// accuracy in metres is very approximate
ESRIint PointInside(ESRIfloat lat, ESRIfloat lon, ESRIfloat accuracymetres);
};
// anything less than -1e-38 means "no data"
static const double ESRInodata;
// file is OK, it contains only POLYGONZ records
// and all data have been read successfully
// from SHP,SHX and DBF files and placed into records;
// this is int, as there is something unexplainable
// happens with struct memory alignment here
int OK;
// data can be bad, e.g. first part of record has negative
// area (land), like N05E100)
int dataOK;
// continental delivery (see CD_... enumeration)
int contdelivery;
// error
string errorString;
// SHP header
SSHPHeader SHPheader;
// records
std::vector<CESRIRecord> records;
// default constructor
ESRIFile();
// from file
ESRIFile(const char *filename);
// destructor
~ESRIFile();
// returns -1 if this is NOT water point or
// RECORD (not record part) number if this IS water point;
// accuracy in metres is very approximate
ESRIint PointInside(ESRIfloat lat, ESRIfloat lon, ESRIfloat accuracymetres);
// get all coast line points with Z = 0 as lon and lat (!)
// in X,Y vector coordinate by parts of max partsize
// points in each part; parts[i] marks beginning of new part;
// if distance between prev and next points exceeds maxdist
// (deg), a new part is started, Zlevel is normally 0 (waterlevel)
template <class T>
void GetZ0Points(int partsize, T maxdist, T Zlevel, std::vector<TVector<T> > &points,
std::vector<int> &parts, T tolerance) const
{
points.clear();
parts.clear();
for (int j = 0; j < static_cast<int>(records.size()); j++)
{
for (int l = 0; l < records[j].parts.size(); l++)
{
int i0 = records[j].parts[l];
int i1 = (l < records[j].parts.size() - 1) ?
records[j].parts[l + 1] - 1 :
static_cast<int>(records[j].points.size()) - 1;
int numpoints = i1 - i0 + 1;
parts.push_back(static_cast<int>(points.size()));
int numparts = numpoints / partsize;
if (records[j].points.size() % partsize) numparts++;
int maxcount = (numparts > 0) ? (numpoints / numparts) : numpoints;
int count = 0;
for (int m = i0; m <= i1; m++)
{
TVector<T> point(static_cast<T>(records[j].points[m].X),
static_cast<T>(records[j].points[m].Y),
static_cast<T>(records[j].points[m].Z));
// static_cast<T>(records[j].points[m].Z - Zlevel)); //? maybe better to make shores steeper
// TVector<T> point(records[j].points[m].X,records[j].points[m].Y,Zlevel);
// throw outside points
if (point.X <= lonmin)
continue;
if (point.X >= lonmax)
continue;
if (point.Y <= latmin)
continue;
if (point.Y >= latmax)
continue;
// check if point is not on boundary
if (std::abs(point.X - lonmin) < tolerance)
continue;
if (std::abs(point.X - lonmax) < tolerance)
continue;
if (std::abs(point.Y - latmin) < tolerance)
continue;
if (std::abs(point.Y - latmax) < tolerance)
continue;
// otherwise, add this point
points.push_back(point);
count++;
// distance between successive points is too big
//TVector<T> testpoint(-1.0013888,50.784027,0.00000000);
//T d = !(point - testpoint);
//if (d < 0.00001)
//{
// int gsgsgsg = 0;
//}
// remove last point and start new count
if (count > 1 && points.size() > 1)
{
T dist = !(points[points.size() - 1] - points[points.size() - 2]);
if (dist > maxdist)
{
points.erase(points.end() - 1);
parts.push_back(static_cast<int>(points.size()));
count = 0;
}
}
// max part size reached
if (count > maxcount && count > 0)
{
parts.push_back(static_cast<int>(points.size()));
count = 0;
}
}
}
}
}
};
#pragma pack(pop)