-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathmapnik_layer.cpp
More file actions
312 lines (288 loc) · 9.78 KB
/
mapnik_layer.cpp
File metadata and controls
312 lines (288 loc) · 9.78 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
#include "mapnik_layer.hpp"
#include "mapnik_datasource.hpp"
#include "utils.hpp"
// mapnik
#include <mapnik/datasource.hpp> // for datasource_ptr, datasource
#include <mapnik/memory_datasource.hpp> // for memory_datasource
#include <mapnik/layer.hpp> // for layer
#include <mapnik/params.hpp> // for parameters
// stl
#include <limits>
Napi::FunctionReference Layer::constructor;
Napi::Object Layer::Initialize(Napi::Env env, Napi::Object exports, napi_property_attributes prop_attr)
{
// clang-format off
Napi::Function func = DefineClass(env, "Layer", {
InstanceMethod<&Layer::describe>("describe", prop_attr),
InstanceAccessor<&Layer::name, &Layer::name>("name", prop_attr),
InstanceAccessor<&Layer::active, &Layer::active>("active", prop_attr),
InstanceAccessor<&Layer::srs, &Layer::srs>("srs", prop_attr),
InstanceAccessor<&Layer::styles, &Layer::styles>("styles", prop_attr),
InstanceAccessor<&Layer::datasource, &Layer::datasource>("datasource", prop_attr),
InstanceAccessor<&Layer::minimum_scale_denominator, &Layer::minimum_scale_denominator>("minimum_scale_denominator", prop_attr),
InstanceAccessor<&Layer::maximum_scale_denominator, &Layer::maximum_scale_denominator>("maximum_scale_denominator", prop_attr),
InstanceAccessor<&Layer::queryable, &Layer::queryable>("queryable", prop_attr),
InstanceAccessor<&Layer::clear_label_cache, &Layer::clear_label_cache>("clear_label_cache", prop_attr)
});
// clang-format on
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("Layer", func);
return exports;
}
Layer::Layer(Napi::CallbackInfo const& info)
: Napi::ObjectWrap<Layer>(info)
{
Napi::Env env = info.Env();
if (info.Length() == 1 && info[0].IsExternal())
{
auto ext = info[0].As<Napi::External<layer_ptr>>();
if (ext) layer_ = *ext.Data();
return;
}
if (info.Length() == 1)
{
if (!info[0].IsString())
{
Napi::TypeError::New(env, "'name' must be a string").ThrowAsJavaScriptException();
return;
}
layer_ = std::make_shared<mapnik::layer>(info[0].As<Napi::String>());
}
else if (info.Length() == 2)
{
if (!info[0].IsString() || !info[1].IsString())
{
Napi::TypeError::New(env, "'name' and 'srs' must be a strings").ThrowAsJavaScriptException();
return;
}
layer_ = std::make_shared<mapnik::layer>(info[0].As<Napi::String>(), info[1].As<Napi::String>());
}
else
{
Napi::TypeError::New(env, "please provide Layer name and optional srs").ThrowAsJavaScriptException();
}
}
// accessors
// name
Napi::Value Layer::name(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
return Napi::String::New(env, layer_->name());
}
void Layer::name(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsString())
{
Napi::TypeError::New(env, "'name' must be a string").ThrowAsJavaScriptException();
return;
}
layer_->set_name(value.As<Napi::String>());
}
// srs
Napi::Value Layer::srs(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
return Napi::String::New(env, layer_->srs());
}
void Layer::srs(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsString())
{
Napi::TypeError::New(env, "'srs' must be a string").ThrowAsJavaScriptException();
return;
}
layer_->set_srs(value.As<Napi::String>());
}
// styles
Napi::Value Layer::styles(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
Napi::EscapableHandleScope scope(env);
std::vector<std::string> const& style_names = layer_->styles();
std::size_t size = style_names.size();
Napi::Array arr = Napi::Array::New(env, size);
for (std::size_t index = 0; index < size; ++index)
{
arr.Set(index, Napi::String::New(env, style_names[index]));
}
return scope.Escape(arr);
}
void Layer::styles(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsArray())
{
Napi::TypeError::New(env, "Must provide an array of style names").ThrowAsJavaScriptException();
}
else
{
Napi::Array arr = value.As<Napi::Array>();
std::size_t arr_size = arr.Length();
for (std::size_t index = 0; index < arr_size; ++index)
{
layer_->add_style(arr.Get(index).As<Napi::String>());
}
}
}
// datasource
Napi::Value Layer::datasource(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
Napi::EscapableHandleScope scope(env);
mapnik::datasource_ptr ds = layer_->datasource();
if (ds)
{
Napi::Value arg = Napi::External<mapnik::datasource_ptr>::New(env, &ds);
Napi::Object obj = Datasource::constructor.New({arg});
return scope.Escape(obj);
}
return env.Null();
}
void Layer::datasource(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsObject())
{
Napi::TypeError::New(env, "mapnik.Datasource instance expected").ThrowAsJavaScriptException();
return;
}
else
{
Napi::Object obj = value.As<Napi::Object>();
if (!obj.InstanceOf(Datasource::constructor.Value()))
{
Napi::TypeError::New(env, "mapnik.Datasource instance expected").ThrowAsJavaScriptException();
}
else
{
Datasource* ds = Napi::ObjectWrap<Datasource>::Unwrap(obj);
if (ds) layer_->set_datasource(ds->impl());
}
}
}
// minimum_scale_denominator
Napi::Value Layer::minimum_scale_denominator(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
return Napi::Number::New(env, layer_->minimum_scale_denominator());
}
void Layer::minimum_scale_denominator(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsNumber())
{
Napi::TypeError::New(env, "Must provide a number").ThrowAsJavaScriptException();
}
else
{
layer_->set_minimum_scale_denominator(value.As<Napi::Number>().DoubleValue());
}
}
// maximum_scale_denominator
Napi::Value Layer::maximum_scale_denominator(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
return Napi::Number::New(env, layer_->maximum_scale_denominator());
}
void Layer::maximum_scale_denominator(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsNumber())
{
Napi::TypeError::New(env, "Must provide a number").ThrowAsJavaScriptException();
}
else
{
layer_->set_maximum_scale_denominator(value.As<Napi::Number>().DoubleValue());
}
}
// queryable
Napi::Value Layer::queryable(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
return Napi::Boolean::New(env, layer_->queryable());
}
void Layer::queryable(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsBoolean())
{
Napi::TypeError::New(env, "'name' must be a boolean").ThrowAsJavaScriptException();
}
else
{
layer_->set_queryable(value.As<Napi::Boolean>());
}
}
// active
// queryable
Napi::Value Layer::active(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
return Napi::Boolean::New(env, layer_->active());
}
void Layer::active(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsBoolean())
{
Napi::TypeError::New(env, "'name' must be a boolean").ThrowAsJavaScriptException();
}
else
{
layer_->set_active(value.As<Napi::Boolean>());
}
}
// clear_label_cache
Napi::Value Layer::clear_label_cache(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
return Napi::Boolean::New(env, layer_->clear_label_cache());
}
void Layer::clear_label_cache(Napi::CallbackInfo const& info, Napi::Value const& value)
{
Napi::Env env = info.Env();
if (!value.IsBoolean())
{
Napi::TypeError::New(env, "'name' must be a boolean").ThrowAsJavaScriptException();
}
else
{
layer_->set_clear_label_cache(value.As<Napi::Boolean>());
}
}
Napi::Value Layer::describe(Napi::CallbackInfo const& info)
{
Napi::Env env = info.Env();
Napi::EscapableHandleScope scope(env);
Napi::Object description = Napi::Object::New(env);
description.Set("name", layer_->name());
description.Set("srs", layer_->srs());
description.Set("active", Napi::Boolean::New(env, layer_->active()));
description.Set("clear_label_cache", Napi::Boolean::New(env, layer_->clear_label_cache()));
description.Set("minimum_scale_denominator", Napi::Number::New(env, layer_->minimum_scale_denominator()));
description.Set("maximum_scale_denominator", Napi::Number::New(env, layer_->maximum_scale_denominator()));
description.Set("queryable", Napi::Boolean::New(env, layer_->queryable()));
std::vector<std::string> const& style_names = layer_->styles();
std::size_t size = style_names.size();
Napi::Array styles = Napi::Array::New(env, size);
for (std::size_t index = 0; index < size; ++index)
{
styles.Set(index, style_names[index]);
}
description.Set("styles", styles);
Napi::Object ds = Napi::Object::New(env);
description.Set(Napi::String::New(env, "datasource"), ds);
mapnik::datasource_ptr datasource = layer_->datasource();
if (datasource)
{
for (auto const& p : datasource->params())
{
node_mapnik::params_to_object(env, ds, std::get<0>(p), std::get<1>(p));
}
}
return scope.Escape(description);
}