forked from NCAR/spatialdb
/
SpatiaLiteDB.cpp
390 lines (307 loc) · 9.84 KB
/
SpatiaLiteDB.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
#include "SpatiaLiteDB.h"
#include <iostream>
#include <algorithm>
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Point::Point(double x, double y, std::string label):
_x(x),
_y(y),
_label(label)
{
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Point::~Point() {
}
////////////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& lhs, SpatiaLiteDB::Point& rhs) {
lhs << "(" << rhs._x << "," << rhs._y << ")";
return lhs;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Linestring::Linestring(std::string label):
_label(label)
{
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Linestring::~Linestring() {
}
////////////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& lhs, SpatiaLiteDB::Linestring& rhs) {
for (SpatiaLiteDB::Linestring::iterator i = rhs.begin();
i != rhs.end();
i++) {
lhs << *i;
}
return lhs;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Ring::Ring():
_clockwise(0)
{
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Ring::Ring(int clockwise):
_clockwise(clockwise)
{
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Ring::~Ring() {
}
////////////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& lhs, SpatiaLiteDB::Ring& rhs) {
for (SpatiaLiteDB::Ring::iterator i = rhs.begin();
i != rhs.end();
i++) {
lhs << *i;
}
return lhs;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Polygon::Polygon(std::string label):
_label(label)
{
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Polygon::~Polygon() {
}
////////////////////////////////////////////////////////////////////
void SpatiaLiteDB::Polygon::setExtRing(Ring r) {
_extRing = r;
}
////////////////////////////////////////////////////////////////////
void SpatiaLiteDB::Polygon::addIntRing(Ring r) {
_intRings.push_back(r);
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::Ring SpatiaLiteDB::Polygon::extRing() {
return _extRing;
}
////////////////////////////////////////////////////////////////////
std::vector<SpatiaLiteDB::Ring> SpatiaLiteDB::Polygon::intRings() {
return _intRings;
}
////////////////////////////////////////////////////////////////////
std::ostream& operator<<(std::ostream& lhs, SpatiaLiteDB::Polygon& rhs) {
lhs << rhs._extRing;
for (int i = 0;
i < rhs._intRings.size();
i++) {
lhs << rhs._intRings[i];
}
return lhs;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::PointList::PointList()
{
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::PointList::~PointList() {
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::LinestringList::LinestringList()
{
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::LinestringList::~LinestringList() {
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::PolygonList::PolygonList(std::string label)
{
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::PolygonList::~PolygonList() {
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::SpatiaLiteDB(std::string dbPath) throw (std::string) :
SQLiteDB(dbPath)
{
// initialize spatiaLite.
_connection = spatialite_alloc_connection();
spatialite_init_ex(handle(), _connection, true);
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::~SpatiaLiteDB() {
spatialite_cleanup_ex(_connection);
}
////////////////////////////////////////////////////////////////////
std::string SpatiaLiteDB::version() {
std::stringstream s;
s << SQLiteDB::version() << ", ";
s << "SpatiaLite version: " << spatialite_version();
return s.str();
}
////////////////////////////////////////////////////////////////////
void SpatiaLiteDB::queryGeometry(
std::string table,
std::string geometry_col,
double left,
double bottom,
double right,
double top,
std::string label_col) {
_pointlist.clear();
_linestringlist.clear();
_polygonlist.clear();
bool getLabel = label_col.size() != 0;
// Search for geometry and name
// The query will be either
//
// SELECT Geometry FROM table WHERE MbrWithin(Column, BuildMbr(left, bottom, right, top));
// or
// SELECT Geometry,Label FROM table WHERE MbrWithin(Column, BuildMbr(left, bottom, right, top));
//
// where Geometry is the geometry column name and Label is the column containing a name or label.
std::string label;
if (getLabel) {
label = "," + label_col;
}
std::stringstream s;
s <<
"SELECT " <<
geometry_col <<
label <<
" FROM " <<
table <<
" WHERE MbrIntersects(" <<
geometry_col <<
", BuildMbr(" <<
left <<
"," <<
bottom <<
"," <<
right <<
"," <<
top <<
"));";
// prepare the query
prepare(s.str());
// fetch the next result row
while (step()) {
// get the geometry
gaiaGeomCollPtr geom = Geom(0);
// get the label, if we asked for it
std::string label;
if (getLabel) {
label = Text(1);
}
// the following will create lists for points, lines and polygons found in
// a single geometry. In practice it seems that only one of those types
// exists for a given geometry, but it's not clear if this is a requirement
// or just occurs in the sample databases we have been working with.
PointList points = point_list(geom, label);
for (PointList::iterator i = points.begin(); i != points.end(); i++) {
_pointlist.push_back(*i);
}
LinestringList lines = linestring_list(geom, label);
for (LinestringList::iterator i = lines.begin(); i != lines.end(); i++) {
_linestringlist.push_back(*i);
}
PolygonList polys = polygon_list(geom, label);
for (PolygonList::iterator i = polys.begin(); i != polys.end(); i++) {
_polygonlist.push_back(*i);
}
}
// finish with this query
finalize();
}
////////////////////////////////////////////////////////////////////
gaiaGeomCollPtr SpatiaLiteDB::Geom(int col) throw (std::string) {
// throw an exception if the requested column doesn't exist
checkColumn(SQLITE_BLOB, col);
const void* blob;
int blob_size;
gaiaGeomCollPtr geom;
blob = Blob(col, blob_size);
/// @todoDoes geom need to be freed at some time?
geom = gaiaFromSpatiaLiteBlobWkb((const unsigned char*) blob, blob_size);
return geom;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::PointList SpatiaLiteDB::point_list(gaiaGeomCollPtr geom, std::string label) {
SpatiaLiteDB::PointList pl;
gaiaPointPtr point = geom->FirstPoint;
while (point) {
pl.push_back(SpatiaLiteDB::Point(point->X, point->Y, label));
point = point->Next;
}
return pl;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::LinestringList SpatiaLiteDB::linestring_list(gaiaGeomCollPtr geom, std::string label) {
SpatiaLiteDB::LinestringList ll;
gaiaLinestringPtr linestring = geom->FirstLinestring;
while (linestring) {
Linestring ls(label);
for (int i = 0; i < linestring->Points; i++) {
double x;
double y;
gaiaGetPoint(linestring->Coords, i, &x, &y);
ls.push_back(SpatiaLiteDB::Point(x, y));
}
ll.push_back(ls);
linestring = linestring->Next;
}
return ll;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::PolygonList SpatiaLiteDB::polygon_list(gaiaGeomCollPtr geom, std::string label) {
double x;
double y;
SpatiaLiteDB::PolygonList polylist;
gaiaPolygonPtr polygon = geom->FirstPolygon;
while (polygon) {
Polygon p(label);
gaiaRingPtr rExt = polygon->Exterior;
Ring r(rExt->Clockwise);
for (int i = 0; i < rExt->Points; i++) {
gaiaGetPoint(rExt->Coords, i, &x, &y);
r.push_back(SpatiaLiteDB::Point(x, y));
}
p.setExtRing(r);
gaiaRingPtr rInt = polygon->Interiors;
while (rInt) {
Ring r(rInt->Clockwise);
for (int i = 0; i < rInt->Points; i++) {
gaiaGetPoint(rInt->Coords, i, &x, &y);
r.push_back(SpatiaLiteDB::Point(x, y));
}
p.addIntRing(r);
rInt = rInt->Next;
}
polylist.push_back(p);
polygon = polygon->Next;
}
return polylist;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::PointList SpatiaLiteDB::points() {
return _pointlist;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::LinestringList SpatiaLiteDB::linestrings() {
return _linestringlist;
}
////////////////////////////////////////////////////////////////////
SpatiaLiteDB::PolygonList SpatiaLiteDB::polygons() {
return _polygonlist;
}
////////////////////////////////////////////////////////////////////
std::vector<std::string> SpatiaLiteDB::geometryTables() {
std::vector<std::string> all_tables = table_names();
// find all tables containing a geometry column
std::vector<std::string> geometry_tables;
for (std::vector<std::string>::iterator table = all_tables.begin();
table != all_tables.end(); table++) {
std::vector<std::string> columns = column_names(*table);
for (std::vector<std::string>::iterator column = columns.begin();
column != columns.end(); column++) {
std::transform(column->begin(), column->end(), column->begin(),
(int(*)(int))std::tolower);
if (!column->compare("geometry")) {
geometry_tables.push_back(*table);
}
}
}
return geometry_tables;
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////