Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 285 lines (246 sloc) 8.939 kB
a344edc @springmeyer adapt to mapnik 2.2.0-pre code upstream as per https://github.com/map…
springmeyer authored
1 #include <mapnik/version.hpp>
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
2 #include <mapnik/datasource_cache.hpp>
3
4 #include "mapnik_datasource.hpp"
7ba3154 @springmeyer add a mapnik.Featureset class that can be accessed from a datasource …
springmeyer authored
5 #include "mapnik_featureset.hpp"
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
6 #include "utils.hpp"
c53b35c @springmeyer shift headers to fix unused function warning
springmeyer authored
7 #include "ds_emitter.hpp"
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
8
6f47ff9 @springmeyer work harder to catch ds exceptions
springmeyer authored
9 // stl
10 #include <exception>
11
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
12 Persistent<FunctionTemplate> Datasource::constructor;
13
14 void Datasource::Initialize(Handle<Object> target) {
15
16 HandleScope scope;
62b010d @kkaefer kill trailing whitespace
kkaefer authored
17
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
18 constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(Datasource::New));
19 constructor->InstanceTemplate()->SetInternalFieldCount(1);
20 constructor->SetClassName(String::NewSymbol("Datasource"));
21
22 // methods
23 NODE_SET_PROTOTYPE_METHOD(constructor, "parameters", parameters);
24 NODE_SET_PROTOTYPE_METHOD(constructor, "describe", describe);
25 NODE_SET_PROTOTYPE_METHOD(constructor, "features", features);
fd2481b @springmeyer get_featureset -> featureset (avoiding underbars when unneeded)
springmeyer authored
26 NODE_SET_PROTOTYPE_METHOD(constructor, "featureset", featureset);
9c6dba3 @springmeyer move extent to method on datasource
springmeyer authored
27 NODE_SET_PROTOTYPE_METHOD(constructor, "extent", extent);
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
28
29 target->Set(String::NewSymbol("Datasource"),constructor->GetFunction());
30 }
31
32 Datasource::Datasource() :
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
33 ObjectWrap(),
34 datasource_() {}
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
35
36 Datasource::~Datasource()
37 {
38 }
39
40 Handle<Value> Datasource::New(const Arguments& args)
41 {
42 HandleScope scope;
43
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
44 if (!args.IsConstructCall())
45 return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword"));
46
62b010d @kkaefer kill trailing whitespace
kkaefer authored
47 if (args[0]->IsExternal())
48 {
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
49 //std::clog << "external!\n";
50 Local<External> ext = Local<External>::Cast(args[0]);
51 void* ptr = ext->Value();
52 Datasource* d = static_cast<Datasource*>(ptr);
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing …
springmeyer authored
53 if (d->datasource_->type() == mapnik::datasource::Raster)
54 {
55 args.This()->Set(String::NewSymbol("type"),
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
56 String::NewSymbol("raster"),
57 static_cast<v8::PropertyAttribute>(v8::ReadOnly|v8::DontDelete));
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing …
springmeyer authored
58 }
59 else
60 {
61 args.This()->Set(String::NewSymbol("type"),
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
62 String::NewSymbol("vector"),
63 static_cast<v8::PropertyAttribute>(v8::ReadOnly|v8::DontDelete));
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing …
springmeyer authored
64 }
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
65 d->Wrap(args.This());
66 return args.This();
62b010d @kkaefer kill trailing whitespace
kkaefer authored
67 }
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
68 if (!args.Length() == 1){
69 return ThrowException(Exception::TypeError(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
70 String::New("accepts only one argument, an object of key:value datasource options")));
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
71 }
62b010d @kkaefer kill trailing whitespace
kkaefer authored
72
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
73 if (!args[0]->IsObject())
74 return ThrowException(Exception::TypeError(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
75 String::New("Must provide an object, eg {type: 'shape', file : 'world.shp'}")));
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
76
77 Local<Object> options = args[0]->ToObject();
62b010d @kkaefer kill trailing whitespace
kkaefer authored
78
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
79 // TODO - maybe validate in js?
80
81 bool bind=true;
82 if (options->Has(String::New("bind")))
83 {
84 Local<Value> bind_opt = options->Get(String::New("bind"));
85 if (!bind_opt->IsBoolean())
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
86 return ThrowException(Exception::TypeError(
87 String::New("'bind' must be a Boolean")));
62b010d @kkaefer kill trailing whitespace
kkaefer authored
88
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
89 bind = bind_opt->BooleanValue();
90 }
91
92 mapnik::parameters params;
93 Local<Array> names = options->GetPropertyNames();
94 uint32_t i = 0;
95 uint32_t a_length = names->Length();
96 while (i < a_length) {
97 Local<Value> name = names->Get(i)->ToString();
98 Local<Value> value = options->Get(name);
f847435 @springmeyer allow getting and setting the arbitrary map parameters - requires lat…
springmeyer authored
99 // TODO - don't treat everything as strings
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
100 params[TOSTR(name)] = TOSTR(value);
101 i++;
102 }
103
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
104 mapnik::datasource_ptr ds;
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
105 try
106 {
c2a7b29 @springmeyer Adapt to mapnik 2.2-pre singleton changes
springmeyer authored
107 #if MAPNIK_VERSION >= 200200
108 ds = mapnik::datasource_cache::instance().create(params, bind);
109 #else
a344edc @springmeyer adapt to mapnik 2.2.0-pre code upstream as per https://github.com/map…
springmeyer authored
110 ds = mapnik::datasource_cache::instance()->create(params, bind);
c2a7b29 @springmeyer Adapt to mapnik 2.2-pre singleton changes
springmeyer authored
111 #endif
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
112 }
38dd2ab @springmeyer better c++ style as per mapnik conventions
springmeyer authored
113 catch (std::exception const& ex)
6f47ff9 @springmeyer work harder to catch ds exceptions
springmeyer authored
114 {
115 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
116 String::New(ex.what())));
6f47ff9 @springmeyer work harder to catch ds exceptions
springmeyer authored
117 }
118 catch (...)
119 {
120 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
121 String::New("unknown exception happened, please file bug")));
6f47ff9 @springmeyer work harder to catch ds exceptions
springmeyer authored
122 }
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
123 if (ds)
124 {
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing …
springmeyer authored
125 if (ds->type() == mapnik::datasource::Raster)
126 {
127 args.This()->Set(String::NewSymbol("type"),
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
128 String::NewSymbol("raster"),
129 static_cast<v8::PropertyAttribute>(v8::ReadOnly|v8::DontDelete));
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing …
springmeyer authored
130 }
131 else
132 {
133 args.This()->Set(String::NewSymbol("type"),
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
134 String::NewSymbol("vector"),
135 static_cast<v8::PropertyAttribute>(v8::ReadOnly|v8::DontDelete));
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing …
springmeyer authored
136 }
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
137 Datasource* d = new Datasource();
138 d->Wrap(args.This());
139 d->datasource_ = ds;
140 return args.This();
141 }
142 return Undefined();
143 }
144
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
145 Handle<Value> Datasource::New(mapnik::datasource_ptr ds_ptr) {
146 HandleScope scope;
147 Datasource* d = new Datasource();
148 d->datasource_ = ds_ptr;
149 Handle<Value> ext = External::New(d);
150 Handle<Object> obj = constructor->GetFunction()->NewInstance(1, &ext);
151 return scope.Close(obj);
152 }
153
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
154 Handle<Value> Datasource::parameters(const Arguments& args)
155 {
156 HandleScope scope;
157 Datasource* d = ObjectWrap::Unwrap<Datasource>(args.This());
158 Local<Object> ds = Object::New();
159 mapnik::parameters::const_iterator it = d->datasource_->params().begin();
160 mapnik::parameters::const_iterator end = d->datasource_->params().end();
161 for (; it != end; ++it)
162 {
db7e164 @springmeyer new render() function accepting mapnik.Image and supporting changed f…
springmeyer authored
163 node_mapnik::params_to_object serializer( ds , it->first);
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
164 boost::apply_visitor( serializer, it->second );
165 }
166 return scope.Close(ds);
167 }
168
9c6dba3 @springmeyer move extent to method on datasource
springmeyer authored
169 Handle<Value> Datasource::extent(const Arguments& args)
170 {
171 HandleScope scope;
172 Datasource* d = ObjectWrap::Unwrap<Datasource>(args.This());
173 Local<Array> a = Array::New(4);
174 mapnik::box2d<double> const& e = d->datasource_->envelope();
175 a->Set(0, Number::New(e.minx()));
176 a->Set(1, Number::New(e.miny()));
177 a->Set(2, Number::New(e.maxx()));
178 a->Set(3, Number::New(e.maxy()));
179 return scope.Close(a);
180 }
181
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
182 Handle<Value> Datasource::describe(const Arguments& args)
183 {
184 HandleScope scope;
185 Datasource* d = ObjectWrap::Unwrap<Datasource>(args.This());
186 Local<Object> description = Object::New();
db7e164 @springmeyer new render() function accepting mapnik.Image and supporting changed f…
springmeyer authored
187 try
188 {
189 node_mapnik::describe_datasource(description,d->datasource_);
817ebfc @springmeyer catch exception when describing a datasource
springmeyer authored
190 }
38dd2ab @springmeyer better c++ style as per mapnik conventions
springmeyer authored
191 catch (std::exception const& ex )
817ebfc @springmeyer catch exception when describing a datasource
springmeyer authored
192 {
193 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
194 String::New(ex.what())));
817ebfc @springmeyer catch exception when describing a datasource
springmeyer authored
195 }
d829f2e @springmeyer more aggressively catch possible exceptions around datasources and fe…
springmeyer authored
196 catch (...)
197 {
198 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
199 String::New("unknown exception happened describing datasource, please file bug")));
d829f2e @springmeyer more aggressively catch possible exceptions around datasources and fe…
springmeyer authored
200 }
817ebfc @springmeyer catch exception when describing a datasource
springmeyer authored
201
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
202 return scope.Close(description);
203 }
204
205 Handle<Value> Datasource::features(const Arguments& args)
206 {
207
208 HandleScope scope;
2c6759a @springmeyer allow slicing of features - closes #9
springmeyer authored
209
210 unsigned first = 0;
211 unsigned last = 0;
212 // we are slicing
213 if (args.Length() == 2)
214 {
215 if (!args[0]->IsNumber() || !args[1]->IsNumber())
216 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
217 String::New("Index of 'first' and 'last' feature must be an integer")));
2c6759a @springmeyer allow slicing of features - closes #9
springmeyer authored
218 first = args[0]->IntegerValue();
219 last = args[1]->IntegerValue();
220 }
62b010d @kkaefer kill trailing whitespace
kkaefer authored
221
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
222 Datasource* d = ObjectWrap::Unwrap<Datasource>(args.This());
62b010d @kkaefer kill trailing whitespace
kkaefer authored
223
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
224 // TODO - we don't know features.length at this point
225 Local<Array> a = Array::New(0);
b8c7de3 @springmeyer clean up exception handling
springmeyer authored
226 try
227 {
228 node_mapnik::datasource_features(a,d->datasource_,first,last);
229 }
38dd2ab @springmeyer better c++ style as per mapnik conventions
springmeyer authored
230 catch (std::exception const& ex )
b8c7de3 @springmeyer clean up exception handling
springmeyer authored
231 {
232 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
233 String::New(ex.what())));
b8c7de3 @springmeyer clean up exception handling
springmeyer authored
234 }
d829f2e @springmeyer more aggressively catch possible exceptions around datasources and fe…
springmeyer authored
235 catch (...)
236 {
237 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
238 String::New("unknown exception happened slicing datasource, please file bug")));
d829f2e @springmeyer more aggressively catch possible exceptions around datasources and fe…
springmeyer authored
239 }
3b4bdba @springmeyer whoops, actually add the new c++ files for mapnik.Datasource()
springmeyer authored
240
241 return scope.Close(a);
7ba3154 @springmeyer add a mapnik.Featureset class that can be accessed from a datasource …
springmeyer authored
242 }
243
fd2481b @springmeyer get_featureset -> featureset (avoiding underbars when unneeded)
springmeyer authored
244 Handle<Value> Datasource::featureset(const Arguments& args)
7ba3154 @springmeyer add a mapnik.Featureset class that can be accessed from a datasource …
springmeyer authored
245 {
246
247 HandleScope scope;
248
249 Datasource* ds = ObjectWrap::Unwrap<Datasource>(args.This());
250
91a39af @springmeyer catch exceptions when querying featureset
springmeyer authored
251 mapnik::featureset_ptr fs;
252 try
253 {
d829f2e @springmeyer more aggressively catch possible exceptions around datasources and fe…
springmeyer authored
254 mapnik::query q(ds->datasource_->envelope());
255 mapnik::layer_descriptor ld = ds->datasource_->get_descriptor();
256 std::vector<mapnik::attribute_descriptor> const& desc = ld.get_descriptors();
257 std::vector<mapnik::attribute_descriptor>::const_iterator itr = desc.begin();
258 std::vector<mapnik::attribute_descriptor>::const_iterator end = desc.end();
259 while (itr != end)
260 {
261 q.add_property_name(itr->get_name());
262 ++itr;
263 }
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
264
91a39af @springmeyer catch exceptions when querying featureset
springmeyer authored
265 fs = ds->datasource_->features(q);
266 }
38dd2ab @springmeyer better c++ style as per mapnik conventions
springmeyer authored
267 catch (std::exception const& ex)
91a39af @springmeyer catch exceptions when querying featureset
springmeyer authored
268 {
269 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
270 String::New(ex.what())));
91a39af @springmeyer catch exceptions when querying featureset
springmeyer authored
271 }
272 catch (...)
273 {
274 return ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
275 String::New("unknown exception happened getting featureset, please file bug")));
91a39af @springmeyer catch exceptions when querying featureset
springmeyer authored
276 }
277
7ba3154 @springmeyer add a mapnik.Featureset class that can be accessed from a datasource …
springmeyer authored
278 if (fs)
279 {
280 return scope.Close(Featureset::New(fs));
281 }
282
283 return Undefined();
80daf4c @springmeyer avoid compiler warnings
springmeyer authored
284 }
Something went wrong with that request. Please try again.