-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathmapnik_vector_tile_image.cpp
More file actions
486 lines (452 loc) · 16 KB
/
mapnik_vector_tile_image.cpp
File metadata and controls
486 lines (452 loc) · 16 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include "mapnik_vector_tile.hpp"
#include "mapnik_image.hpp"
//mapnik
#include <mapnik/image_any.hpp>
#include <mapnik/image_scaling.hpp>
#include <mapnik/map.hpp>
#include <mapnik/layer.hpp>
#include <mapnik/feature_factory.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/geometry/box2d.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/featureset.hpp>
#include <mapnik/memory_datasource.hpp>
#include <mapnik/raster.hpp>
// mapnik-vector-tile
#include "vector_tile_processor.hpp"
#include "vector_tile_load_tile.hpp"
/**
* Add a {@link Image} as a tile layer (synchronous)
*
* @memberof VectorTile
* @instance
* @name addImageSync
* @param {mapnik.Image} image
* @param {string} name of the layer to be added
* @param {Object} options
* @param {string} [options.image_scaling=bilinear] can be any
* of the <mapnik.imageScaling> methods
* @param {string} [options.image_format=webp] or `jpeg`, `png`, `tiff`
* @example
* var vt = new mapnik.VectorTile(1, 0, 0, {
* tile_size:256
* });
* var im = new mapnik.Image(256, 256);
* vt.addImageSync(im, 'layer-name', {
* image_format: 'jpeg',
* image_scaling: 'gaussian'
* });
*/
Napi::Value VectorTile::addImageSync(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
Napi::EscapableHandleScope scope(env);
if (info.Length() < 1 || !info[0].IsObject())
{
Napi::Error::New(env, "first argument must be an Image object").ThrowAsJavaScriptException();
return env.Undefined();
}
if (info.Length() < 2 || !info[1].IsString())
{
Napi::Error::New(env, "second argument must be a layer name (string)").ThrowAsJavaScriptException();
return env.Undefined();
}
std::string layer_name = info[1].As<Napi::String>();
Napi::Object obj = info[0].As<Napi::Object>();
if (!obj.InstanceOf(Image::constructor.Value()))
{
Napi::Error::New(env, "first argument must be an Image object").ThrowAsJavaScriptException();
return env.Undefined();
}
Image* im = Napi::ObjectWrap<Image>::Unwrap(obj);
if (im->impl()->width() <= 0 || im->impl()->height() <= 0)
{
Napi::Error::New(env, "Image width and height must be greater than zero").ThrowAsJavaScriptException();
return env.Undefined();
}
std::string image_format = "webp";
mapnik::scaling_method_e scaling_method = mapnik::SCALING_BILINEAR;
if (info.Length() > 2)
{
// options object
if (!info[2].IsObject())
{
Napi::Error::New(env, "optional third argument must be an options object").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object options = info[2].As<Napi::Object>();
if (options.Has("image_scaling"))
{
Napi::Value param_val = options.Get("image_scaling");
if (!param_val.IsString())
{
Napi::TypeError::New(env, "option 'image_scaling' must be a string").ThrowAsJavaScriptException();
return env.Undefined();
}
std::string image_scaling = param_val.As<Napi::String>();
auto method = mapnik::scaling_method_from_string(image_scaling);
if (!method)
{
Napi::TypeError::New(env, "option 'image_scaling' must be a string and a valid scaling method (e.g 'bilinear')")
.ThrowAsJavaScriptException();
return env.Undefined();
}
scaling_method = *method;
}
if (options.Has("image_format"))
{
Napi::Value param_val = options.Get("image_format");
if (!param_val.IsString())
{
Napi::TypeError::New(env, "option 'image_format' must be a string").ThrowAsJavaScriptException();
return env.Undefined();
}
image_format = param_val.As<Napi::String>();
}
}
mapnik::image_any im_copy = *im->impl();
std::shared_ptr<mapnik::memory_datasource> ds = std::make_shared<mapnik::memory_datasource>(mapnik::parameters());
mapnik::raster_ptr ras = std::make_shared<mapnik::raster>(tile_->extent(), im_copy, 1.0);
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, 1));
feature->set_raster(ras);
ds->push(feature);
ds->envelope(); // can be removed later, currently doesn't work with out this.
ds->set_envelope(tile_->extent());
try
{
// create map object
mapnik::Map map(tile_->size(), tile_->size(), "epsg:3857");
mapnik::layer lyr(layer_name, "epsg:3857");
lyr.set_datasource(ds);
map.add_layer(lyr);
mapnik::vector_tile_impl::processor ren(map);
ren.set_scaling_method(scaling_method);
ren.set_image_format(image_format);
ren.update_tile(*tile_);
return scope.Escape(Napi::Boolean::New(env, true));
}
catch (std::exception const& ex)
{
Napi::Error::New(env, ex.what()).ThrowAsJavaScriptException();
}
return env.Undefined();
}
namespace {
struct AsyncAddImage : Napi::AsyncWorker
{
AsyncAddImage(mapnik::vector_tile_impl::merc_tile_ptr const& tile,
image_ptr const& image,
std::string const& layer_name,
std::string const& image_format,
mapnik::scaling_method_e scaling_method,
Napi::Function const& callback)
: Napi::AsyncWorker(callback),
tile_(tile),
image_(image),
layer_name_(layer_name),
image_format_(image_format),
scaling_method_(scaling_method)
{
}
void Execute() override
{
try
{
mapnik::image_any im_copy = *image_;
std::shared_ptr<mapnik::memory_datasource> ds = std::make_shared<mapnik::memory_datasource>(mapnik::parameters());
mapnik::raster_ptr ras = std::make_shared<mapnik::raster>(tile_->extent(), im_copy, 1.0);
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, 1));
feature->set_raster(ras);
ds->push(feature);
ds->envelope(); // can be removed later, currently doesn't work with out this.
ds->set_envelope(tile_->extent());
// create map object
mapnik::Map map(tile_->size(), tile_->size(), "epsg:3857");
mapnik::layer lyr(layer_name_, "epsg:3857");
lyr.set_datasource(ds);
map.add_layer(lyr);
mapnik::vector_tile_impl::processor ren(map);
ren.set_scaling_method(scaling_method_);
ren.set_image_format(image_format_);
ren.update_tile(*tile_);
}
catch (std::exception const& ex)
{
SetError(ex.what());
}
}
private:
mapnik::vector_tile_impl::merc_tile_ptr tile_;
image_ptr image_;
std::string layer_name_;
std::string image_format_;
mapnik::scaling_method_e scaling_method_;
};
} // namespace
/**
* Add a <mapnik.Image> as a tile layer (asynchronous)
*
* @memberof VectorTile
* @instance
* @name addImage
* @param {mapnik.Image} image
* @param {string} name of the layer to be added
* @param {Object} [options]
* @param {string} [options.image_scaling=bilinear] can be any
* of the <mapnik.imageScaling> methods
* @param {string} [options.image_format=webp] other options include `jpeg`, `png`, `tiff`
* @example
* var vt = new mapnik.VectorTile(1, 0, 0, {
* tile_size:256
* });
* var im = new mapnik.Image(256, 256);
* vt.addImage(im, 'layer-name', {
* image_format: 'jpeg',
* image_scaling: 'gaussian'
* }, function(err) {
* if (err) throw err;
* // your custom code using `vt`
* });
*/
Napi::Value VectorTile::addImage(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
// If last param is not a function assume sync
if (info.Length() < 2)
{
Napi::Error::New(env, "addImage requires at least two parameters: an Image and a layer name").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Value callback = info[info.Length() - 1];
if (!callback.IsFunction())
{
return addImageSync(info);
}
if (!info[0].IsObject())
{
Napi::Error::New(env, "first argument must be an Image object").ThrowAsJavaScriptException();
return env.Undefined();
}
if (!info[1].IsString())
{
Napi::Error::New(env, "second argument must be a layer name (string)").ThrowAsJavaScriptException();
return env.Undefined();
}
std::string layer_name = info[1].As<Napi::String>();
Napi::Object obj = info[0].As<Napi::Object>();
if (!obj.InstanceOf(Image::constructor.Value()))
{
Napi::Error::New(env, "first argument must be an Image object").ThrowAsJavaScriptException();
return env.Undefined();
}
Image* im = Napi::ObjectWrap<Image>::Unwrap(obj);
if (im->impl()->width() <= 0 || im->impl()->height() <= 0)
{
Napi::Error::New(env, "Image width and height must be greater than zero").ThrowAsJavaScriptException();
return env.Undefined();
}
std::string image_format = "webp";
mapnik::scaling_method_e scaling_method = mapnik::SCALING_BILINEAR;
if (info.Length() > 3)
{
// options object
if (!info[2].IsObject())
{
Napi::Error::New(env, "optional third argument must be an options object").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object options = info[2].As<Napi::Object>();
if (options.Has("image_scaling"))
{
Napi::Value param_val = options.Get("image_scaling");
if (!param_val.IsString())
{
Napi::TypeError::New(env, "option 'image_scaling' must be a string").ThrowAsJavaScriptException();
return env.Undefined();
}
std::string image_scaling = param_val.As<Napi::String>();
auto method = mapnik::scaling_method_from_string(image_scaling);
if (!method)
{
Napi::TypeError::New(env, "option 'image_scaling' must be a string and a valid scaling method (e.g 'bilinear')")
.ThrowAsJavaScriptException();
return env.Undefined();
}
scaling_method = *method;
}
if (options.Has("image_format"))
{
Napi::Value param_val = options.Get("image_format");
if (!param_val.IsString())
{
Napi::TypeError::New(env, "option 'image_format' must be a string").ThrowAsJavaScriptException();
return env.Undefined();
}
image_format = param_val.As<Napi::String>();
}
}
auto* worker = new AsyncAddImage{tile_, im->impl(), layer_name, image_format,
scaling_method, callback.As<Napi::Function>()};
worker->Queue();
return env.Undefined();
}
/**
* Add raw image buffer as a new tile layer (synchronous)
*
* @memberof VectorTile
* @instance
* @name addImageBufferSync
* @param {Buffer} buffer - raw data
* @param {string} name - name of the layer to be added
* @example
* var vt = new mapnik.VectorTile(1, 0, 0, {
* tile_size: 256
* });
* var image_buffer = fs.readFileSync('./path/to/image.jpg');
* vt.addImageBufferSync(image_buffer, 'layer-name');
*/
Napi::Value VectorTile::addImageBufferSync(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
if (info.Length() < 1 || !info[0].IsObject())
{
Napi::TypeError::New(env, "first argument must be a buffer object").ThrowAsJavaScriptException();
return env.Undefined();
}
if (info.Length() < 2 || !info[1].IsString())
{
Napi::Error::New(env, "second argument must be a layer name (string)").ThrowAsJavaScriptException();
return env.Undefined();
}
std::string layer_name = info[1].As<Napi::String>();
Napi::Object obj = info[0].As<Napi::Object>();
if (!obj.IsBuffer())
{
Napi::TypeError::New(env, "first arg must be a buffer object").ThrowAsJavaScriptException();
return env.Undefined();
}
std::size_t buffer_size = obj.As<Napi::Buffer<char>>().Length();
if (buffer_size <= 0)
{
Napi::Error::New(env, "cannot accept empty buffer as protobuf").ThrowAsJavaScriptException();
return env.Undefined();
}
try
{
add_image_buffer_as_tile_layer(*tile_, layer_name, obj.As<Napi::Buffer<char>>().Data(), buffer_size);
}
catch (std::exception const& ex)
{
// no obvious way to get this to throw in JS under obvious conditions
// but keep the standard exeption cache in C++
// LCOV_EXCL_START
Napi::Error::New(env, ex.what()).ThrowAsJavaScriptException();
return env.Undefined();
// LCOV_EXCL_STOP
}
return env.Undefined();
}
/*
typedef struct
{
uv_work_t request;
VectorTile* d;
const char *data;
size_t dataLength;
std::string layer_name;
bool error;
std::string error_name;
Napi::FunctionReference cb;
Napi::Persistent<v8::Object> buffer;
} vector_tile_addimagebuffer_baton_t;
*/
namespace {
struct AsyncAddImageBuffer : Napi::AsyncWorker
{
AsyncAddImageBuffer(mapnik::vector_tile_impl::merc_tile_ptr const& tile,
Napi::Buffer<char> const& buffer,
std::string const& layer_name,
Napi::Function const& callback)
: Napi::AsyncWorker(callback),
tile_(tile),
buffer_ref{Napi::Persistent(buffer)},
data_{buffer.Data()},
dataLength_{buffer.Length()},
layer_name_(layer_name)
{
}
void Execute() override
{
try
{
add_image_buffer_as_tile_layer(*tile_, layer_name_, data_, dataLength_);
}
catch (std::exception const& ex)
{
SetError(ex.what());
}
}
private:
mapnik::vector_tile_impl::merc_tile_ptr tile_;
Napi::Reference<Napi::Buffer<char>> buffer_ref;
char const* data_;
std::size_t dataLength_;
std::string layer_name_;
};
} // namespace
/**
* Add an encoded image buffer as a layer
*
* @memberof VectorTile
* @instance
* @name addImageBuffer
* @param {Buffer} buffer - raw image data
* @param {string} name - name of the layer to be added
* @param {Function} callback
* @example
* var vt = new mapnik.VectorTile(1, 0, 0, {
* tile_size: 256
* });
* var image_buffer = fs.readFileSync('./path/to/image.jpg'); // returns a buffer
* vt.addImageBufferSync(image_buffer, 'layer-name', function(err) {
* if (err) throw err;
* // your custom code
* });
*/
Napi::Value VectorTile::addImageBuffer(Napi::CallbackInfo const& info)
{
if (info.Length() < 3)
{
return addImageBufferSync(info);
}
Napi::Env env = info.Env();
// ensure callback is a function
Napi::Value callback = info[info.Length() - 1];
if (!callback.IsFunction())
{
Napi::TypeError::New(env, "last argument must be a callback function").ThrowAsJavaScriptException();
return env.Undefined();
}
if (info.Length() < 1 || !info[0].IsObject())
{
Napi::TypeError::New(env, "first argument must be a buffer object").ThrowAsJavaScriptException();
return env.Undefined();
}
if (info.Length() < 2 || !info[1].IsString())
{
Napi::Error::New(env, "second argument must be a layer name (string)").ThrowAsJavaScriptException();
return env.Undefined();
}
std::string layer_name = info[1].As<Napi::String>();
Napi::Object obj = info[0].As<Napi::Object>();
if (!obj.IsBuffer())
{
Napi::TypeError::New(env, "first arg must be a buffer object").ThrowAsJavaScriptException();
return env.Undefined();
}
auto* worker = new AsyncAddImageBuffer{tile_, obj.As<Napi::Buffer<char>>(), layer_name, callback.As<Napi::Function>()};
worker->Queue();
return env.Undefined();
}