-
Notifications
You must be signed in to change notification settings - Fork 163
/
mapnik_geometry.cpp
372 lines (346 loc) · 12.5 KB
/
mapnik_geometry.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
#include "utils.hpp"
#include "mapnik_geometry.hpp"
#include "mapnik_projection.hpp"
#include <mapnik/datasource.hpp>
#include <mapnik/geometry_reprojection.hpp>
#include <mapnik/geometry_is_valid.hpp>
#include <mapnik/util/geometry_to_geojson.hpp>
#include <mapnik/util/geometry_to_wkt.hpp>
#include <mapnik/util/geometry_to_wkb.hpp>
Nan::Persistent<v8::FunctionTemplate> Geometry::constructor;
/**
* Geometry: a representation of geographical features in terms of
* shape alone. This class provides many useful functions for conversion
* to and from formats.
*
* You'll never create a mapnik.Geometry instance manually: it is always
* part of a {@link mapnik.Feature} instance, which is often a part of
* a {@link mapnik.Featureset} instance.
*
* @name mapnik.Geometry
* @class
*/
void Geometry::Initialize(v8::Local<v8::Object> target) {
Nan::HandleScope scope;
v8::Local<v8::FunctionTemplate> lcons = Nan::New<v8::FunctionTemplate>(Geometry::New);
lcons->InstanceTemplate()->SetInternalFieldCount(1);
lcons->SetClassName(Nan::New("Geometry").ToLocalChecked());
Nan::SetPrototypeMethod(lcons, "extent", extent);
Nan::SetPrototypeMethod(lcons, "isValid", isValid);
Nan::SetPrototypeMethod(lcons, "toWKB", toWKB);
Nan::SetPrototypeMethod(lcons, "toWKT", toWKT);
Nan::SetPrototypeMethod(lcons, "toJSON", toJSON);
Nan::SetPrototypeMethod(lcons, "toJSONSync", toJSONSync);
NODE_MAPNIK_DEFINE_CONSTANT(lcons->GetFunction(),
"Point",mapnik::datasource_geometry_t::Point)
NODE_MAPNIK_DEFINE_CONSTANT(lcons->GetFunction(),
"LineString",mapnik::datasource_geometry_t::LineString)
NODE_MAPNIK_DEFINE_CONSTANT(lcons->GetFunction(),
"Polygon",mapnik::datasource_geometry_t::Polygon)
target->Set(Nan::New("Geometry").ToLocalChecked(), lcons->GetFunction());
constructor.Reset(lcons);
}
Geometry::Geometry(mapnik::feature_ptr f) :
Nan::ObjectWrap(),
feat_(f) {}
Geometry::~Geometry()
{
}
NAN_METHOD(Geometry::New)
{
if (info[0]->IsExternal())
{
v8::Local<v8::External> ext = info[0].As<v8::External>();
void* ptr = ext->Value();
Geometry* g = static_cast<Geometry*>(ptr);
g->Wrap(info.This());
info.GetReturnValue().Set(info.This());
return;
}
else
{
Nan::ThrowError("a mapnik.Geometry cannot be created directly - it is only available via a mapnik.Feature instance");
return;
}
info.GetReturnValue().Set(info.This());
}
v8::Local<v8::Value> Geometry::NewInstance(mapnik::feature_ptr f) {
Nan::EscapableHandleScope scope;
Geometry* g = new Geometry(f);
v8::Local<v8::Value> ext = Nan::New<v8::External>(g);
return scope.Escape(Nan::New(constructor)->GetFunction()->NewInstance(1, &ext));
}
/**
* Convert this geometry into a [GeoJSON](http://geojson.org/) representation,
* synchronously.
*
* @returns {string} GeoJSON, string-encoded representation of this geometry.
* @memberof mapnik.Geometry
* @instance
* @name toJSONSync
*/
NAN_METHOD(Geometry::toJSONSync)
{
info.GetReturnValue().Set(_toJSONSync(info));
}
bool to_geojson_projected(std::string & json,
mapnik::geometry::geometry<double> const& geom,
mapnik::proj_transform const& prj_trans)
{
unsigned int n_err = 0;
mapnik::geometry::geometry<double> projected_geom = mapnik::geometry::reproject_copy(geom,prj_trans,n_err);
if (n_err > 0) return false;
return mapnik::util::to_geojson(json,projected_geom);
}
v8::Local<v8::Value> Geometry::_toJSONSync(Nan::NAN_METHOD_ARGS_TYPE info) {
Nan::EscapableHandleScope scope;
Geometry* g = Nan::ObjectWrap::Unwrap<Geometry>(info.Holder());
std::string json;
if (info.Length() < 1)
{
if (!mapnik::util::to_geojson(json,g->feat_->get_geometry()))
{
// Fairly certain this situation can never be reached but
// leaving it none the less
/* LCOV_EXCL_START */
Nan::ThrowError("Failed to generate GeoJSON");
return scope.Escape(Nan::Undefined());
/* LCOV_EXCL_END */
}
}
else
{
if (!info[0]->IsObject()) {
Nan::ThrowTypeError("optional first arg must be an options object");
return scope.Escape(Nan::Undefined());
}
v8::Local<v8::Object> options = info[0]->ToObject();
if (options->Has(Nan::New("transform").ToLocalChecked()))
{
v8::Local<v8::Value> bound_opt = options->Get(Nan::New("transform").ToLocalChecked());
if (!bound_opt->IsObject()) {
Nan::ThrowTypeError("'transform' must be an object");
return scope.Escape(Nan::Undefined());
}
v8::Local<v8::Object> obj = bound_opt->ToObject();
if (obj->IsNull() || obj->IsUndefined() || !Nan::New(ProjTransform::constructor)->HasInstance(obj)) {
Nan::ThrowTypeError("mapnik.ProjTransform expected as first arg");
return scope.Escape(Nan::Undefined());
}
ProjTransform* tr = Nan::ObjectWrap::Unwrap<ProjTransform>(obj);
mapnik::proj_transform const& prj_trans = *tr->get();
mapnik::geometry::geometry<double> const& geom = g->feat_->get_geometry();
if (!to_geojson_projected(json,geom,prj_trans))
{
// Fairly certain this situation can never be reached but
// leaving it none the less
/* LCOV_EXCL_START */
Nan::ThrowError("Failed to generate GeoJSON");
return scope.Escape(Nan::Undefined());
/* LCOV_EXCL_END */
}
}
}
return scope.Escape(Nan::New<v8::String>(json).ToLocalChecked());
}
struct to_json_baton {
uv_work_t request;
Geometry* g;
ProjTransform* tr;
bool error;
std::string result;
Nan::Persistent<v8::Function> cb;
};
/**
* Convert this geometry into a [GeoJSON](http://geojson.org/) representation,
* asynchronously.
*
* @param {Object} [options={}]. The only supported object is `transform`,
* which should be a valid {@link mapnik.ProjTransform} object.
* @param {Function} callback called with (err, result)
* @memberof mapnik.Geometry
* @instance
* @name toJSON
*/
NAN_METHOD(Geometry::toJSON)
{
if ((info.Length() < 1) || !info[info.Length()-1]->IsFunction()) {
info.GetReturnValue().Set(_toJSONSync(info));
return;
}
to_json_baton *closure = new to_json_baton();
closure->request.data = closure;
closure->g = Nan::ObjectWrap::Unwrap<Geometry>(info.Holder());
closure->error = false;
closure->tr = nullptr;
if (info.Length() > 1)
{
if (!info[0]->IsObject()) {
Nan::ThrowTypeError("optional first arg must be an options object");
return;
}
v8::Local<v8::Object> options = info[0]->ToObject();
if (options->Has(Nan::New("transform").ToLocalChecked()))
{
v8::Local<v8::Value> bound_opt = options->Get(Nan::New("transform").ToLocalChecked());
if (!bound_opt->IsObject()) {
Nan::ThrowTypeError("'transform' must be an object");
return;
}
v8::Local<v8::Object> obj = bound_opt->ToObject();
if (obj->IsNull() || obj->IsUndefined() || !Nan::New(ProjTransform::constructor)->HasInstance(obj)) {
Nan::ThrowTypeError("mapnik.ProjTransform expected as first arg");
return;
}
closure->tr = Nan::ObjectWrap::Unwrap<ProjTransform>(obj);
closure->tr->_ref();
}
}
v8::Local<v8::Value> callback = info[info.Length()-1];
closure->cb.Reset(callback.As<v8::Function>());
uv_queue_work(uv_default_loop(), &closure->request, to_json, (uv_after_work_cb)after_to_json);
closure->g->Ref();
return;
}
void Geometry::to_json(uv_work_t* req)
{
to_json_baton *closure = static_cast<to_json_baton *>(req->data);
try
{
if (closure->tr)
{
mapnik::proj_transform const& prj_trans = *closure->tr->get();
mapnik::geometry::geometry<double> const& geom = closure->g->feat_->get_geometry();
if (!to_geojson_projected(closure->result,geom,prj_trans))
{
// Fairly certain this situation can never be reached but
// leaving it none the less
// LCOV_EXCL_START
closure->error = true;
closure->result = "Failed to generate GeoJSON";
// LCOV_EXCL_END
}
}
else
{
if (!mapnik::util::to_geojson(closure->result,closure->g->feat_->get_geometry()))
{
// Fairly certain this situation can never be reached but
// leaving it none the less
/* LCOV_EXCL_START */
closure->error = true;
closure->result = "Failed to generate GeoJSON";
/* LCOV_EXCL_END */
}
}
}
catch (std::exception const& ex)
{
// Fairly certain this situation can never be reached but
// leaving it none the less
/* LCOV_EXCL_START */
closure->error = true;
closure->result = ex.what();
/* LCOV_EXCL_END */
}
}
void Geometry::after_to_json(uv_work_t* req)
{
Nan::HandleScope scope;
to_json_baton *closure = static_cast<to_json_baton *>(req->data);
if (closure->error)
{
// Fairly certain this situation can never be reached but
// leaving it none the less
/* LCOV_EXCL_START */
v8::Local<v8::Value> argv[1] = { Nan::Error(closure->result.c_str()) };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(closure->cb), 1, argv);
/* LCOV_EXCL_END */
}
else
{
v8::Local<v8::Value> argv[2] = { Nan::Null(), Nan::New<v8::String>(closure->result).ToLocalChecked() };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(closure->cb), 2, argv);
}
closure->g->Unref();
if (closure->tr) {
closure->tr->_unref();
}
closure->cb.Reset();
delete closure;
}
/**
* Get the geometry's extent
*
* @name extent
* @memberof mapnik.Geometry
* @instance
* @returns {v8::Array<number>} extent [minx, miny, maxx, maxy] order geometry extent.
*/
NAN_METHOD(Geometry::extent)
{
Geometry* g = Nan::ObjectWrap::Unwrap<Geometry>(info.Holder());
v8::Local<v8::Array> a = Nan::New<v8::Array>(4);
mapnik::box2d<double> const& e = g->feat_->envelope();
a->Set(0, Nan::New<v8::Number>(e.minx()));
a->Set(1, Nan::New<v8::Number>(e.miny()));
a->Set(2, Nan::New<v8::Number>(e.maxx()));
a->Set(3, Nan::New<v8::Number>(e.maxy()));
info.GetReturnValue().Set(a);
}
NAN_METHOD(Geometry::isValid) {
info.GetReturnValue().Set(_isValid(info));
}
v8::Local<v8::Value> Geometry::_isValid(Nan::NAN_METHOD_ARGS_TYPE info) {
Nan::EscapableHandleScope scope;
Geometry* g = Nan::ObjectWrap::Unwrap<Geometry>(info.Holder());
//std::string message;
if (mapnik::geometry::is_valid(g->feat_->get_geometry())) {
return scope.Escape(Nan::True());
}
else {
return scope.Escape(Nan::False());
}
}
/**
* Get the geometry's representation as [Well-Known Text](http://en.wikipedia.org/wiki/Well-known_text)
*
* @name toWKT
* @memberof mapnik.Geometry
* @instance
* @returns {string} wkt representation of this geometry
*/
NAN_METHOD(Geometry::toWKT)
{
std::string wkt;
Geometry* g = Nan::ObjectWrap::Unwrap<Geometry>(info.Holder());
if (!mapnik::util::to_wkt(wkt, g->feat_->get_geometry()))
{
// Fairly certain this situation can never be reached but
// leaving it none the less
/* LCOV_EXCL_START */
Nan::ThrowError("Failed to generate WKT");
return;
/* LCOV_EXCL_END */
}
info.GetReturnValue().Set(Nan::New<v8::String>(wkt).ToLocalChecked());
}
/**
* Get the geometry's representation as Well-Known Binary
*
* @name toWKB
* @memberof mapnik.Geometry
* @instance
* @returns {string} wkb representation of this geometry
*/
NAN_METHOD(Geometry::toWKB)
{
Geometry* g = Nan::ObjectWrap::Unwrap<Geometry>(info.Holder());
mapnik::util::wkb_buffer_ptr wkb = mapnik::util::to_wkb(g->feat_->get_geometry(), mapnik::wkbNDR);
if (!wkb)
{
Nan::ThrowError("Failed to generate WKB - geometry likely null");
return;
}
info.GetReturnValue().Set(Nan::CopyBuffer(wkb->buffer(), wkb->size()).ToLocalChecked());
}