-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
vapour.cpp
365 lines (308 loc) · 9.85 KB
/
vapour.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
#include <limits>
#include "ogrsf_frmts.h"
#include "ogr_api.h"
#include <Rcpp.h>
#include "CollectorList.h"
using namespace Rcpp;
constexpr int MAX_INT = std::numeric_limits<int>::max ();
// [[Rcpp::export]]
Rcpp::CharacterVector vapour_layer_names_cpp(Rcpp::CharacterVector dsource,
Rcpp::CharacterVector sql = "")
{
GDALAllRegister();
GDALDataset *poDS;
poDS = (GDALDataset*) GDALOpenEx(dsource[0], GDAL_OF_VECTOR, NULL, NULL, NULL );
if( poDS == NULL )
{
Rcpp::stop("Open failed.\n");
}
int nlayer = poDS->GetLayerCount();
Rcpp::CharacterVector lnames = Rcpp::CharacterVector(nlayer);
OGRLayer *poLayer;
for (int ilayer = 0; ilayer < nlayer; ilayer++) {
poLayer = poDS->GetLayer(ilayer);
lnames[ilayer] = poLayer->GetName();
}
GDALClose(poDS);
return(lnames);
}
// copied from Edzer Pebesma, https://github.com/r-spatial/sf/blob/master/src/gdal_read.cpp
Rcpp::List allocate_attribute(OGRFeatureDefn *poFDefn, int n_features, bool int64_as_string) {
int n = poFDefn->GetFieldCount(); //+ poFDefn->GetGeomFieldCount();
Rcpp::List out(n);
Rcpp::CharacterVector names(n);
for (int i = 0; i < poFDefn->GetFieldCount(); i++) {
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(i);
switch (poFieldDefn->GetType()) {
case OFTInteger:
out[i] = Rcpp::IntegerVector(n_features);
break;
case OFTDate: {
Rcpp::NumericVector ret(n_features);
ret.attr("class") = "Date";
out[i] = ret;
} break;
case OFTDateTime: {
Rcpp::NumericVector ret(n_features);
Rcpp::CharacterVector cls(2);
cls(0) = "POSIXct";
cls(1) = "POSIXt";
ret.attr("class") = cls;
out[i] = ret;
} break;
case OFTInteger64: // fall through: converts Int64 -> double
if (int64_as_string)
out[i] = Rcpp::CharacterVector(n_features);
else
out[i] = Rcpp::NumericVector(n_features);
break;
case OFTReal:
out[i] = Rcpp::NumericVector(n_features);
break;
case OFTString:
out[i] = Rcpp::CharacterVector(n_features);
break;
case OFTStringList:
case OFTRealList:
case OFTIntegerList:
out[i] = Rcpp::List(n_features);
break;
default:
Rcpp::stop("Unrecognized field type\n"); // #nocov
break;
}
names[i] = poFieldDefn->GetNameRef();
}
out.attr("names") = names;
return out;
}
// [[Rcpp::export]]
List vapour_read_attributes_cpp(Rcpp::CharacterVector dsource,
Rcpp::IntegerVector layer = 0,
Rcpp::CharacterVector sql = "")
{
GDALAllRegister();
GDALDataset *poDS;
poDS = (GDALDataset*) GDALOpenEx(dsource[0], GDAL_OF_VECTOR, NULL, NULL, NULL );
if( poDS == NULL )
{
Rcpp::stop("Open failed.\n");
}
OGRLayer *poLayer;
Rcpp::CharacterVector empty = " ";
if (sql[0] != "") {
poLayer = poDS->ExecuteSQL(sql[0],
NULL,
empty[0] );
if (poLayer == NULL) {
Rcpp::stop("SQL execution failed.\n");
}
} else {
poLayer = poDS->GetLayer(layer[0]);
}
if (poLayer == NULL) {
Rcpp::stop("Layer open failed.\n");
}
// poLayer = poDS->GetLayer(layer[0]);
OGRFeature *poFeature;
poLayer->ResetReading();
// poFeature = poLayer->GetNextFeature();
long long nFeature_long = poLayer->GetFeatureCount();
if (nFeature_long > static_cast<long long>(MAX_INT))
Rcpp::stop("Number of features exceeds maximal number able to be read");
//int nFeature = poLayer->GetFeatureCount();
int nFeature = static_cast<int>(nFeature_long);
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
bool int64_as_string = true;
// for now we do this for attributes, need to figure out the
// ListCollector logic here ...
if (nFeature < 0) {
nFeature = 0;
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{
nFeature++;
}
}
poLayer->ResetReading();
Rcpp::List out = allocate_attribute(poFDefn, nFeature, int64_as_string);
int iFeature = 0;
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
int iField;
for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
{
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
if( poFieldDefn->GetType() == OFTInteger ) {
Rcpp::IntegerVector nv;
nv = out[iField];
nv[iFeature] = poFeature->GetFieldAsInteger( iField );
}
if( poFieldDefn->GetType() == OFTReal ) {
Rcpp::NumericVector nv;
nv = out[iField];
nv[iFeature] = poFeature->GetFieldAsDouble( iField );
}
if( poFieldDefn->GetType() == OFTString ) {
Rcpp::CharacterVector nv;
nv = out[iField];
nv[iFeature] = poFeature->GetFieldAsString( iField );
}
}
iFeature = iFeature + 1;
}
GDALClose( poDS );
return(out);
}
// [[Rcpp::export]]
List vapour_read_geometry_cpp(Rcpp::CharacterVector dsource,
Rcpp::IntegerVector layer = 0,
Rcpp::CharacterVector sql = "",
Rcpp::CharacterVector what = "geometry",
Rcpp::CharacterVector textformat = "json")
{
GDALAllRegister();
GDALDataset *poDS;
poDS = (GDALDataset*) GDALOpenEx(dsource[0], GDAL_OF_VECTOR, NULL, NULL, NULL );
if( poDS == NULL )
{
Rcpp::stop("Open failed.\n");
}
OGRLayer *poLayer;
Rcpp::CharacterVector empty = " ";
if (sql[0] != "") {
poLayer = poDS->ExecuteSQL(sql[0],
NULL,
empty[0] );
if (poLayer == NULL) {
Rcpp::stop("SQL execution failed.\n");
}
} else {
poLayer = poDS->GetLayer(layer[0]);
}
if (poLayer == NULL) {
Rcpp::stop("Layer open failed.\n");
}
OGRFeature *poFeature;
poLayer->ResetReading();
//OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
CollectorList feature_xx;
int iFeature = 0;
int warncount = 0;
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{
OGRGeometry *poGeometry;
poGeometry = poFeature->GetGeometryRef();
if (poGeometry == NULL) {
warncount++;
feature_xx.push_back(R_NilValue);
if (warncount == 1) Rcpp::warning("at least one geometry is NULL, perhaps the 'sql' argument excludes the native geometry?\n(use 'SELECT * FROM ..') ");
} else {
// GEOMETRY
// geometry native binary
// text various text forms
// extent simple bbox
if (what[0] == "geometry") {
//https://github.com/r-spatial/sf/blob/798068d3044a65797c52bf3b42bc4a5d83b45e9a/src/gdal.cpp#L207
Rcpp::RawVector raw(poGeometry->WkbSize());
//todo we probably need better err handling see sf handle_error
poGeometry->exportToWkb(wkbNDR, &(raw[0]), wkbVariantIso);
feature_xx.push_back(raw);
iFeature = iFeature + 1;
}
if (what[0] == "text") {
Rcpp::CharacterVector txt(1);
if (textformat[0] == "json") {
txt[0] = poGeometry->exportToJson();
}
if (textformat[0] == "gml") {
txt[0] = poGeometry->exportToGML();
}
if (textformat[0] == "kml") {
txt[0] = poGeometry->exportToKML();
}
if (textformat[0] == "wkt") {
// // see buffer handling for SRS here which is where
// // I got inspiration from : http://www.gdal.org/gdal_tutorial.html
char *pszGEOM_WKT = NULL;
// // see here for the constants for the format variants
// // http://www.gdal.org/ogr__core_8h.html#a6716bd3399c31e7bc8b0fd94fd7d9ba6a7459e8d11fa69e89271771c8d0f265d8
poGeometry->exportToWkt(&pszGEOM_WKT, wkbVariantIso );
txt[0] = pszGEOM_WKT;
CPLFree( pszGEOM_WKT );
}
feature_xx.push_back(txt);
iFeature = iFeature + 1;
}
if (what[0] == "extent") {
OGREnvelope env;
OGR_G_GetEnvelope(poGeometry, &env);
Rcpp::NumericVector extent = NumericVector::create(env.MinX, env.MaxX, env.MinY, env.MaxY);
feature_xx.push_back(extent);
iFeature = iFeature + 1;
}
}
OGRFeature::DestroyFeature( poFeature );
}
GDALClose( poDS );
return(feature_xx.vector());
}
// [[Rcpp::export]]
List vapour_projection_info_cpp(Rcpp::CharacterVector dsource,
Rcpp::IntegerVector layer = 0,
Rcpp::CharacterVector sql = "")
{
GDALAllRegister();
GDALDataset *poDS;
poDS = (GDALDataset*) GDALOpenEx(dsource[0], GDAL_OF_VECTOR, NULL, NULL, NULL );
if( poDS == NULL )
{
Rcpp::stop("Open failed.\n");
}
OGRLayer *poLayer;
Rcpp::CharacterVector empty = " ";
if (sql[0] != "") {
poLayer = poDS->ExecuteSQL(sql[0],
NULL,
empty[0] );
if (poLayer == NULL) {
Rcpp::stop("SQL execution failed.\n");
}
} else {
poLayer = poDS->GetLayer(layer[0]);
}
if (poLayer == NULL) {
Rcpp::stop("Layer open failed.\n");
}
OGRSpatialReference *SRS = poLayer->GetSpatialRef();
char *proj;
List info_out(6);
CharacterVector outproj(1);
CharacterVector outnames(6);
SRS->exportToProj4(&proj);
outproj[0] = proj;
info_out[0] = Rcpp::clone( outproj);
outnames[0] = "Proj4";
SRS->exportToMICoordSys(&proj);
outproj[0] = proj;
info_out[1] = Rcpp::clone( outproj);
outnames[1] = "MICoordSys";
SRS->exportToPrettyWkt(&proj, false); //bSimplify = false
outproj[0] = proj;
info_out[2] = Rcpp::clone( outproj);
outnames[2] = "PrettyWkt";
SRS->exportToWkt(&proj);
outproj[0] = proj;
info_out[3] = Rcpp::clone( outproj);
outnames[3] = "Wkt";
int epsg = SRS->GetEPSGGeogCS();
info_out[4] = epsg;
outnames[4] = "EPSG";
// SRS->exportToXML();
info_out[5] = "<not implemented>";
outnames[5] = "XML";
CPLFree(proj);
//info_out[0] = outproj;
info_out.attr("names") = outnames;
return info_out;
}