-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathmapnik_map_query_point.cpp
More file actions
290 lines (273 loc) · 9.82 KB
/
mapnik_map_query_point.cpp
File metadata and controls
290 lines (273 loc) · 9.82 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
#include "mapnik_map.hpp"
#include "mapnik_featureset.hpp"
#include <mapnik/map.hpp>
#include <mapnik/layer.hpp>
namespace detail {
struct AsyncQueryPoint : Napi::AsyncWorker
{
using Base = Napi::AsyncWorker;
AsyncQueryPoint(map_ptr const& map, double x, double y, int layer_idx, bool geo_coords, Napi::Function const& callback)
: Base(callback),
map_(map),
x_(x),
y_(y),
layer_idx_(layer_idx),
geo_coords_(geo_coords) {}
void Execute() override
{
try
{
std::vector<mapnik::layer> const& layers = map_->layers();
if (layer_idx_ >= 0)
{
mapnik::featureset_ptr fs;
if (geo_coords_)
{
fs = map_->query_point(layer_idx_, x_, y_);
}
else
{
fs = map_->query_map_point(layer_idx_, x_, y_);
}
mapnik::layer const& lyr = layers[layer_idx_];
featuresets_.insert(std::make_pair(lyr.name(), fs));
}
else
{
// query all layers
unsigned idx = 0;
for (mapnik::layer const& lyr : layers)
{
mapnik::featureset_ptr fs;
if (geo_coords_)
{
fs = map_->query_point(idx, x_, y_);
}
else
{
fs = map_->query_map_point(idx, x_, y_);
}
featuresets_.insert(std::make_pair(lyr.name(), fs));
++idx;
}
}
}
catch (std::exception const& ex)
{
SetError(ex.what());
}
}
std::vector<napi_value> GetResult(Napi::Env env) override
{
std::size_t num_result = featuresets_.size();
if (num_result >= 1)
{
Napi::Array arr = Napi::Array::New(env, num_result);
typedef std::map<std::string, mapnik::featureset_ptr> fs_itr;
fs_itr::iterator it = featuresets_.begin();
fs_itr::iterator end = featuresets_.end();
unsigned idx = 0;
for (; it != end; ++it)
{
Napi::Object obj = Napi::Object::New(env);
obj.Set("layer", Napi::String::New(env, it->first));
Napi::Value arg = Napi::External<mapnik::featureset_ptr>::New(env, &it->second);
obj.Set("featureset", Featureset::constructor.New({arg}));
arr.Set(idx, obj);
++idx;
}
featuresets_.clear();
return {env.Undefined(), arr};
}
return Base::GetResult(env);
}
private:
map_ptr map_;
double x_;
double y_;
int layer_idx_;
bool geo_coords_;
std::map<std::string, mapnik::featureset_ptr> featuresets_;
};
} // namespace detail
/**
* Query a `Mapnik#Map` object to retrieve layer and feature data based on an
* X and Y `Mapnik#Map` coordinates (use `Map#queryPoint` to query with geographic coordinates).
*
* @name queryMapPoint
* @memberof Map
* @instance
* @param {number} x - x coordinate
* @param {number} y - y coordinate
* @param {Object} [options]
* @param {String|number} [options.layer] - layer name (string) or index (positive integer, 0 index)
* to query. If left blank, will query all layers.
* @param {Function} callback
* @returns {Array} array - An array of `Featureset` objects and layer names, which each contain their own
* `Feature` objects.
* @example
* // iterate over the first layer returned and get all attribute information for each feature
* map.queryMapPoint(10, 10, {layer: 0}, function(err, results) {
* if (err) throw err;
* console.log(results); // => [{"layer":"layer_name","featureset":{}}]
* var featureset = results[0].featureset;
* var attributes = [];
* var feature;
* while ((feature = featureset.next())) {
* attributes.push(feature.attributes());
* }
* console.log(attributes); // => [{"attr_key": "attr_value"}, {...}, {...}]
* });
*
*/
Napi::Value Map::queryMapPoint(Napi::CallbackInfo const& info)
{
return query_point_impl(info, false);
}
/**
* Query a `Mapnik#Map` object to retrieve layer and feature data based on geographic
* coordinates of the source data (use `Map#queryMapPoint` to query with XY coordinates).
*
* @name queryPoint
* @memberof Map
* @instance
* @param {number} x - x geographic coordinate (CRS based on source data)
* @param {number} y - y geographic coordinate (CRS based on source data)
* @param {Object} [options]
* @param {String|number} [options.layer] - layer name (string) or index (positive integer, 0 index)
* to query. If left blank, will query all layers.
* @param {Function} callback
* @returns {Array} array - An array of `Featureset` objects and layer names, which each contain their own
* `Feature` objects.
* @example
* // query based on web mercator coordinates
* map.queryMapPoint(-12957605.0331, 5518141.9452, {layer: 0}, function(err, results) {
* if (err) throw err;
* console.log(results); // => [{"layer":"layer_name","featureset":{}}]
* var featureset = results[0].featureset;
* var attributes = [];
* var feature;
* while ((feature = featureset.next())) {
* attributes.push(feature.attributes());
* }
* console.log(attributes); // => [{"attr_key": "attr_value"}, {...}, {...}]
* });
*
*/
Napi::Value Map::queryPoint(Napi::CallbackInfo const& info)
{
return query_point_impl(info, true);
}
Napi::Value Map::query_point_impl(Napi::CallbackInfo const& info, bool geo_coords)
{
Napi::Env env = info.Env();
if (info.Length() < 3)
{
Napi::Error::New(env, "requires at least three arguments, a x,y query and a callback")
.ThrowAsJavaScriptException();
return env.Undefined();
}
double x;
double y;
if (!info[0].IsNumber() || !info[1].IsNumber())
{
Napi::TypeError::New(env, "x,y arguments must be numbers").ThrowAsJavaScriptException();
return env.Undefined();
}
else
{
x = info[0].As<Napi::Number>().DoubleValue();
y = info[1].As<Napi::Number>().DoubleValue();
}
Napi::Object options = Napi::Object::New(env);
int layer_idx = -1;
if (info.Length() > 3)
{
// options object
if (!info[2].IsObject())
{
Napi::TypeError::New(env, "optional third argument must be an options object").ThrowAsJavaScriptException();
return env.Undefined();
}
options = info[2].As<Napi::Object>();
if (options.Has("layer"))
{
std::vector<mapnik::layer> const& layers = map_->layers();
Napi::Value layer_id = options.Get("layer");
if (!(layer_id.IsString() || layer_id.IsNumber()))
{
Napi::TypeError::New(env, "'layer' option required for map query and must be either a layer name(string) or layer index (integer)").ThrowAsJavaScriptException();
return env.Undefined();
}
if (layer_id.IsString())
{
bool found = false;
unsigned int idx(0);
std::string layer_name = layer_id.As<Napi::String>();
for (mapnik::layer const& lyr : layers)
{
if (lyr.name() == layer_name)
{
found = true;
layer_idx = idx;
break;
}
++idx;
}
if (!found)
{
std::ostringstream s;
s << "Layer name '" << layer_name << "' not found";
Napi::TypeError::New(env, s.str().c_str()).ThrowAsJavaScriptException();
return env.Undefined();
}
}
else if (layer_id.IsNumber())
{
layer_idx = layer_id.As<Napi::Number>().Int32Value();
std::size_t layer_num = layers.size();
if (layer_idx < 0)
{
std::ostringstream s;
s << "Zero-based layer index '" << layer_idx << "' not valid"
<< " must be a positive integer, ";
if (layer_num > 0)
{
s << "only '" << layer_num << "' layers exist in map";
}
else
{
s << "no layers found in map";
}
Napi::TypeError::New(env, s.str()).ThrowAsJavaScriptException();
return env.Undefined();
}
else if (layer_idx >= static_cast<int>(layer_num))
{
std::ostringstream s;
s << "Zero-based layer index '" << layer_idx << "' not valid, ";
if (layer_num > 0)
{
s << "only '" << layer_num << "' layers exist in map";
}
else
{
s << "no layers found in map";
}
Napi::TypeError::New(env, s.str()).ThrowAsJavaScriptException();
return env.Undefined();
}
}
}
}
// ensure function callback
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();
}
auto* worker = new detail::AsyncQueryPoint(map_, x, y, layer_idx, geo_coords, callback.As<Napi::Function>());
worker->Queue();
return env.Undefined();
}