-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovr2shp.h
410 lines (306 loc) · 12.5 KB
/
ovr2shp.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
404
405
406
407
408
409
410
#include <cmath>
#include <filesystem>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <vector>
#include "hfa_p.h"
#include "ogrsf_frmts.h" // GDAL vector drivers
#include "logging.h"
using namespace std;
namespace fs = std::filesystem;
extern const string HFA_POLYLINE_COORDS_ATTR_NAME;
extern const string HFA_ANNOTATION_XFORM_ATTR_NAME;
extern const string HFA_XFORM_COEF_ATTR_NAME;
extern const string HFA_XFORM_VECT_ATTR_NAME;
/*
* Prototypes
*
*/
HFAEntry *find(HFAEntry *node, string name);
vector<pair<double, double>> rotate(vector<pair<double, double>> pts,
double *center, double orientation);
bool extract_proj(HFAHandle hHFA, OGRSpatialReference &srs);
string to_polyWKT(vector<pair<double, double>> pts);
string to_linestrWKT(vector<pair<double, double>> pts);
/************************************************************************/
/* */
/* HFAGeom */
/* */
/* Interface for all geometry/shape objects in the HFA structure */
/* */
/************************************************************************/
class HFAGeom {
public:
virtual ~HFAGeom(){}; // for dynamic_cast
virtual vector<pair<double, double>> get_pts() const = 0;
virtual void write(ostream &) const = 0;
friend ostream &operator<<(ostream &os, const HFAGeom &hg) {
hg.write(os);
return os;
}
};
/************************************************************************/
/* */
/* HFAEllipse */
/* */
/* Base class for Eant_Ellipse */
/* */
/************************************************************************/
class HFAEllipse : public HFAGeom {
double *center;
double rotation;
double semiMajorAxis;
double semiMinorAxis;
vector<pair<double, double>> get_unorientated_pts() const;
public:
HFAEllipse(HFAEntry *);
double *get_center() { return center; };
double get_rotation() { return rotation; };
double get_majX() { return semiMajorAxis; };
double get_minX() { return semiMinorAxis; };
vector<pair<double, double>> get_pts() const {
return rotate(get_unorientated_pts(), center, rotation);
}
void write(ostream &os) const {
os << "center: " << center[0] << ", " << center[1] << endl;
os << "rotation: " << rotation << endl;
os << "majx: " << semiMajorAxis << endl;
os << "minx: " << semiMinorAxis << endl;
}
};
/************************************************************************/
/* */
/* HFARectangle */
/* */
/* Base class for Eant_Rectangle */
/* */
/************************************************************************/
class HFARectangle : public HFAGeom {
double *center;
double rotation;
double width;
double height;
vector<pair<double, double>> get_unorientated_pts() const;
public:
HFARectangle(HFAEntry *);
double *get_center() { return center; };
double get_rotation() { return rotation; };
double get_width() { return width; };
double get_height() { return height; };
vector<pair<double, double>> get_pts() const {
return rotate(get_unorientated_pts(), center, rotation);
}
void write(ostream &os) const {
os << "center: " << center[0] << ", " << center[1] << endl;
os << "rotation: " << rotation << endl;
os << "width: " << width << " " << endl;
os << "height: " << height << " " << endl;
}
};
/************************************************************************/
/* */
/* HFAPolyline */
/* */
/* Base class for Eant_Polyline */
/* */
/************************************************************************/
class HFAPolyline : public HFAGeom {
protected:
vector<pair<double, double>> pts;
public:
HFAPolyline(HFAEntry *node);
void write(ostream &os) const { return; }
vector<pair<double, double>> get_pts() const { return pts; }
};
/************************************************************************/
/* */
/* HFAPolygon (untested) */
/* */
/* Base class for Eant_Polygon */
/* */
/************************************************************************/
class HFAPolygon : public HFAPolyline {
public:
HFAPolygon(HFAEntry *node) : HFAPolyline(node) {
// enclose line to turn it into a polygon
pts.push_back(pts[0]);
};
};
/************************************************************************/
/* */
/* HFAText */
/* */
/* Base class for Eant_Text */
/* */
/************************************************************************/
class HFAText : public HFAGeom {
double *origin;
const char *text;
public:
HFAText(HFAEntry *node) {
origin = new double[2];
origin[0] = node->GetDoubleField("origin.x");
origin[1] = node->GetDoubleField("origin.y");
text = node->GetStringField("text.string");
}
double *get_origin() { return origin; };
const char *get_text() { return text; };
vector<pair<double, double>> get_pts() const {
vector<pair<double, double>> pts;
pts.push_back(make_pair(origin[0], origin[1]));
return pts;
}
void write(ostream &os) const {
os << "origin: " << origin[0] << ", " << origin[1] << endl;
os << "textval: " << text << endl;
}
};
/************************************************************************/
/* */
/* HFAAnnotation */
/* */
/* Base class for Element_Eant/Element_2_Eant */
/* Stores annotation (not shape) level metadata. */
/* */
/************************************************************************/
class HFAAnnotation {
int id;
const char *name;
const char *description;
const char *elmType;
int elmTypeId;
HFAGeom *geom;
/*
* coord_vect (1x3)
* {x, y, z}
*
* xform (3x3)
* { xform[0] xform[1], -- coef
* xform[2] xform[3], -- coef
* xform[4] xform[5] -- vect }
*
*/
double xform[6];
public:
HFAAnnotation(HFAEntry *);
int get_id() { return id; };
const char *get_name() { return name; };
const char *get_desc() { return description; };
const char *get_type() { return elmType; };
double *get_xform() { return xform; };
int get_typeId() { return elmTypeId; };
HFAGeom *get_geom() { return geom; };
void set_geom(HFAGeom *g) { geom = g; };
vector<pair<double, double>> get_pts() const;
string get_wkt() const;
friend ostream &operator<<(ostream &os, const HFAAnnotation &ha) {
os << "id: " << ha.id << endl;
os << "type: " << ha.elmType << " [" << ha.elmTypeId << "]" << endl;
os << "name: " << ((ha.name == NULL) ? "" : ha.name) << endl;
os << "description: "
<< ((ha.description == NULL) ? "" : ha.description) << endl;
os.precision(numeric_limits<long double>::digits10 + 1);
os << "xform: " << endl;
for (int i = 0; i < 6; i += 2) {
os << "\t" << ha.xform[i];
os << " " << ha.xform[i + 1];
os << endl;
}
os << endl;
os << *ha.geom;
os << ha.get_wkt();
os << endl;
return os;
}
};
/************************************************************************/
/* */
/* HFAAnnotationLayer */
/* */
/* Base class to store all annotations in a .ovr */
/* Stores file level metadata (projection/map info) */
/* */
/************************************************************************/
class HFAAnnotationLayer {
HFAHandle hHFA;
HFAEntry *root;
bool hasSRS = false;
OGRSpatialReference srs;
vector<HFAAnnotation *> annotations;
GDALDataset *gdalDs;
set<int> geomTypes;
void display_HFATree(HFAEntry *node, int nIdent);
bool write_to_shp(const char *, fs::path);
public:
HFAAnnotationLayer(HFAHandle);
OGRSpatialReference get_srs() { return srs; };
void set_srs(OGRSpatialReference new_srs) {
hasSRS = true;
srs = new_srs;
};
void set_srs(char *proj4srs) {
hasSRS = true;
srs.importFromProj4(proj4srs);
};
vector<HFAAnnotation *> get_annos() const { return annotations; }
bool is_empty() { return annotations.empty(); }
int get_num_annos() { return annotations.size(); }
set<int> get_geomTypes() { return geomTypes; }
void add_geomType(int nGeomType) { geomTypes.insert(nGeomType); }
bool to_shp(fs::path dst) {
const char *shpDriverName = "ESRI Shapefile";
return write_to_shp(shpDriverName, dst);
};
bool to_gjson(char *ofilename) {
// TODO @NotImplemented
const char *gjsonDriverName = "GeoJSON";
cerr << "GeoJSON is not supported yet!" << endl;
return false;
};
void printTree() { display_HFATree(root, 0); }
friend ostream &operator<<(ostream &, const HFAAnnotationLayer &);
};
/************************************************************************/
/* */
/* HFA Geometry Factory */
/* */
/************************************************************************/
/*
* HFAGeomFactory
*
* Abstract factory
*
*/
class HFAGeomFactory {
public:
typedef HFAGeom *(*Factory)(HFAEntry *);
bool registerFactory(int gTypeId, string name, Factory const &factory) {
return _map.insert(make_pair(gTypeId, factory)).second &&
_idMap.insert(make_pair(gTypeId, name)).second;
}
HFAGeom *build(int gTypeId, HFAEntry *node) {
map<int, Factory>::const_iterator fIt = _map.find(gTypeId);
if (fIt != _map.end()) {
Factory f = fIt->second;
return (*f)(node);
}
return NULL;
}
bool supports(int gTypeId) { return _map.find(gTypeId) != _map.end(); }
string gTypeIdToStr(int gTypeId) {
map<int, string>::const_iterator it = _idMap.find(gTypeId);
if (it != _idMap.end()) {
return it->second;
}
return NULL;
}
private:
map<int, Factory> _map;
map<int, string> _idMap;
};
template <typename DerivedGeom> HFAGeom *geomBuilder(HFAEntry *node) {
return new DerivedGeom(node);
}
static HFAGeomFactory geomFactory;