-
Notifications
You must be signed in to change notification settings - Fork 6
/
encode.cpp
364 lines (292 loc) · 9.67 KB
/
encode.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
#include <Rcpp.h>
using namespace Rcpp;
#include "googlePolylines.h"
template <int RTYPE>
Rcpp::CharacterVector sfClass(Vector<RTYPE> v) {
return v.attr("class");
}
Rcpp::CharacterVector getSfClass(SEXP sf) {
switch( TYPEOF(sf) ) {
case REALSXP:
return sfClass<REALSXP>(sf);
case VECSXP:
return sfClass<VECSXP>(sf);
case INTSXP:
return sfClass<INTSXP>(sf);
default: Rcpp::stop("unknown sf type");
}
return "";
}
template<typename Out>
void split(const std::string &s, char delim, Out result) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
void split(const std::string &s, char delim) {
global_vars::elems.clear();
split(s, delim, std::back_inserter(global_vars::elems));
}
void write_data(std::ostringstream& os, std::ostringstream& oszm, Rcpp::CharacterVector& sfg_dim, int dim_divisor,
SEXP sfc, const char *cls, int srid);
void write_matrix_list(std::ostringstream& os, std::ostringstream& oszm, Rcpp::List lst, Rcpp::CharacterVector& sfg_dim, int dim_divisor);
void make_dim_divisor(const char *cls, int *d) {
int divisor = 2;
if (strcmp(cls, "XY") == 0)
divisor = XY;
else if (strcmp(cls, "XYZ") == 0)
divisor = XYZ;
else if (strcmp(cls, "XYM") == 0)
divisor = XYM;
else if (strcmp(cls, "XYZM") == 0)
divisor = XYZM;
else
Rcpp::stop("Unknown dimension attribute");
*d = divisor;
}
void make_type(const char *cls, int *tp = NULL,
int srid = 0) {
int type = 0;
if (strstr(cls, "sfc_") == cls)
cls += 4;
if (strcmp(cls, "POINT") == 0)
type = SF_Point;
else if (strcmp(cls, "MULTIPOINT") == 0)
type = SF_MultiPoint;
else if (strcmp(cls, "LINESTRING") == 0)
type = SF_LineString;
else if (strcmp(cls, "POLYGON") == 0)
type = SF_Polygon;
else if (strcmp(cls, "MULTILINESTRING") == 0)
type = SF_MultiLineString;
else if (strcmp(cls, "MULTIPOLYGON") == 0)
type = SF_MultiPolygon;
else if (strcmp(cls, "GEOMETRY") == 0)
type = SF_Geometry;
else if (strcmp(cls, "GEOMETRYCOLLECTION") == 0)
type = SF_GeometryCollection;
else
type = SF_Unknown;
if (tp != NULL)
*tp = type;
// Rcpp::Rcout << "type: " << type << std::endl;
//return type;
}
void write_multipolygon(std::ostringstream& os, std::ostringstream& oszm, Rcpp::List lst, Rcpp::CharacterVector& sfg_dim, int dim_divisor) {
for (int i = 0; i < lst.length(); i++) {
write_matrix_list(os, oszm, lst[i], sfg_dim, dim_divisor);
}
}
void addToStream(std::ostringstream& os) {
os << global_vars::encodedString << ' ';
}
void encode_point( std::ostringstream& os, std::ostringstream& oszm, Rcpp::NumericVector point, Rcpp::CharacterVector& sfg_dim, int dim_divisor) {
global_vars::lons.clear();
global_vars::lats.clear();
global_vars::lons.push_back(point[0]);
global_vars::lats.push_back(point[1]);
global_vars::encodedString = encode_polyline();
addToStream(os);
// if ( dim_divisor > 2 ) {
// Rcpp::NumericVector elev(1);
// Rcpp::NumericVector meas(1);
//
// elev[0] = point[2];
// if (dim_divisor == 4 ) {
// meas[0] = point[3];
// }
//
// encodedString = encode_polyline(elev, meas);
// addToStream(oszm, encodedString);
// }
}
void encode_points( std::ostringstream& os, std::ostringstream& oszm, Rcpp::NumericMatrix point,
Rcpp::CharacterVector& sfg_dim, int dim_divisor) {
int n = point.size() / dim_divisor;
global_vars::lons.clear();
global_vars::lons.resize(1);
global_vars::lats.clear();
global_vars::lats.resize(1);
//Rcpp::NumericVector elev(1);
//Rcpp::NumericVector meas(1);
for (int i = 0; i < n; i++){
global_vars::lons[0] = point(i, 0);
global_vars::lats[0] = point(i, 1);
global_vars::encodedString = encode_polyline();
addToStream(os);
// if ( dim_divisor > 2 ) {
// elev[0] = point(i, 2);
// meas[0] = dim_divisor == 4 ? point(i, 3) : 0;
//
// encodedString = encode_polyline( elev, meas );
// addToStream(oszm, encodedString);
// }
}
}
void encode_vector( std::ostringstream& os, std::ostringstream& oszm, Rcpp::List vec, Rcpp::CharacterVector& sfg_dim,
int dim_divisor) {
// - XY == [0][1]
// - XYZ == [0][1][2]{3}
// - XYM == [0][1][2]{3}
// - XYZM == [0][1][2][3]
int n = vec.size() / dim_divisor;
global_vars::lats.clear();
global_vars::lons.clear();
for (int i = 0; i < n; i++) {
global_vars::lons.push_back(vec[i]);
global_vars::lats.push_back(vec[(i + n)]);
}
global_vars::encodedString = encode_polyline();
addToStream(os);
// if (dim_divisor > 2) {
// // there are Z and M attributes to encode
// // constructor sets them to 0
// Rcpp::NumericVector elev(n);
// Rcpp::NumericVector meas(n);
//
// for (int i = 0; i < n; i++) {
// elev[i] = vec[(i + n + n)];
// meas[i] = dim_divisor == 4 ? vec[(i + n + n + n)] : 0;
// }
// encodedString = encode_polyline(elev, meas);
// addToStream(oszm, encodedString);
// }
}
void encode_vectors( std::ostringstream& os, std::ostringstream& oszm, Rcpp::List sfc, Rcpp::CharacterVector& sfg_dim,
int dim_divisor){
size_t n = sfc.size();
for (size_t i = 0; i < n; i++) {
encode_vector(os, oszm, sfc[i], sfg_dim, dim_divisor);
}
}
void encode_matrix(std::ostringstream& os, std::ostringstream& oszm, Rcpp::NumericMatrix mat,
Rcpp::CharacterVector& sfg_dim, int dim_divisor ) {
global_vars::lons.clear();
global_vars::lats.clear();
int nrow = mat.nrow();
for (int i = 0; i < nrow; ++i) {
global_vars::lats.push_back(mat(i, 1));
global_vars::lons.push_back(mat(i, 0));
}
global_vars::encodedString = encode_polyline();
addToStream(os);
// if (dim_divisor > 2 ) {
// int n = mat.size() / dim_divisor;
// Rcpp::NumericVector elev(n);
// Rcpp::NumericVector meas(n);
// if( dim_divisor == 3 ) {
// elev = mat(_, 2);
// } else if ( dim_divisor == 4 ) {
// elev = mat(_, 2);
// meas = mat(_, 3);
// }
// encodedString = encode_polyline(elev, meas);
// addToStream(oszm, encodedString);
// }
}
void write_matrix_list(std::ostringstream& os, std::ostringstream& oszm, Rcpp::List lst,
Rcpp::CharacterVector& sfg_dim, int dim_divisor ) {
size_t len = lst.length();
for (size_t j = 0; j < len; j++){
encode_matrix(os, oszm, lst[j], sfg_dim, dim_divisor);
}
global_vars::encodedString = SPLIT_CHAR;
addToStream(os);
// if (dim_divisor > 2) {
// addToStream(oszm, SPLIT_CHAR);
// }
}
void write_geometry(std::ostringstream& os, std::ostringstream& oszm, SEXP s,
Rcpp::CharacterVector& sfg_dim, int dim_divisor) {
Rcpp::CharacterVector cls_attr = getSfClass(s);
write_data(os, oszm, sfg_dim, dim_divisor, s, cls_attr[1], 0);
}
void write_data(std::ostringstream& os, std::ostringstream& oszm, Rcpp::CharacterVector& sfg_dim, int dim_divisor,
SEXP sfc, const char *cls = NULL, int srid = 0) {
int tp;
make_type(cls, &tp, srid);
switch(tp) {
case SF_Point:
encode_point(os, oszm, sfc, sfg_dim, dim_divisor);
break;
case SF_MultiPoint:
encode_points(os, oszm, sfc, sfg_dim, dim_divisor);
break;
case SF_LineString:
encode_vector(os, oszm, sfc, sfg_dim, dim_divisor);
break;
case SF_MultiLineString:
encode_vectors(os, oszm, sfc, sfg_dim, dim_divisor);
break;
case SF_Polygon:
write_matrix_list(os, oszm, sfc, sfg_dim, dim_divisor);
break;
case SF_MultiPolygon:
write_multipolygon(os, oszm, sfc, sfg_dim, dim_divisor);
break;
case SF_Geometry:
write_geometry(os, oszm, sfc, sfg_dim, dim_divisor);
break;
// case SF_GeometryCollection:
// write_geometrycollection(os, sfc);
// break;
default: {
// Rcpp::Rcout << "type is " << sf_type << "\n";
Rcpp::stop("encoding this sf type is currently not supported");
}
}
}
// [[Rcpp::export]]
Rcpp::List rcpp_encodeSfGeometry(Rcpp::List sfc, bool strip){
Rcpp::CharacterVector cls_attr = sfc.attr("class");
Rcpp::CharacterVector sfg_dim;
int dim_divisor;
Rcpp::List output(sfc.size());
//Rcpp::List output_zm(sfc.size());
int lastItem;
Rcpp::List thisSfc;
std::string str;
Rcpp::CharacterVector sv;
// TODO(empty geometries should not enter this list and return something?)
for (int i = 0; i < sfc.size(); i++){
std::ostringstream os;
std::ostringstream oszm;
Rcpp::checkUserInterrupt();
sfg_dim = getSfClass(sfc[i]);
thisSfc = sfc[i];
if (thisSfc.size() > 0 ) {
make_dim_divisor(sfg_dim[0], &dim_divisor);
write_data(os, oszm, sfg_dim, dim_divisor, sfc[i], cls_attr[0], 0);
}
str = os.str();
// std::string zmstr = oszm.str();
split(str, ' ');
// std::vector< std::string > zmstrs = split(zmstr, ' ');
// MULTI* objects
lastItem = global_vars::elems.size() - 1;
if (lastItem >= 0) {
if (global_vars::elems[lastItem] == "-") {
global_vars::elems.erase(global_vars::elems.end() - 1);
// if (dim_divisor > 2 ) {
// zmstrs.erase(zmstrs.end() - 1);
// }
}
}
sv = wrap( global_vars::elems );
// Rcpp::CharacterVector zmsv = wrap( zmstrs );
if(strip == FALSE) {
sv.attr("sfc") = sfg_dim;
// zmsv.attr("zm") = as< Rcpp::CharacterVector >( sfg_dim[0] );
}
output[i] = sv;
// output_zm[i] = zmsv;
}
// TODO(only return zm stream IFF there are dim attributes?)
// return Rcpp::List::create(
// _["XY"] = output
// _["ZM"] = output_zm
// );
return output;
}