Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 291 lines (252 sloc) 8.887 kb
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
1
2 #include "utils.hpp"
3 #include "mapnik_layer.hpp"
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
4 #include "mapnik_datasource.hpp"
7f6fe7d @springmeyer disable the JSDatasource since its not a viable idea given the way that ...
springmeyer authored
5 //#include "mapnik_js_datasource.hpp"
4b87f68 @springmeyer memory::datasource impementation - can be used in thread pool for async ...
springmeyer authored
6 #include "mapnik_memory_datasource.hpp"
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
7
0299e77 @springmeyer use boost::make_shared to allocate shared pointers - faster allocation a...
springmeyer authored
8 // boost
9 #include <boost/make_shared.hpp>
10
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
11 // mapnik
8e5a8d3 @springmeyer allow building against Mapnik 2.0.0 (10 of 81 tests will fail)
springmeyer authored
12 #include <mapnik/version.hpp>
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
13 #include <mapnik/layer.hpp>
14
8e5a8d3 @springmeyer allow building against Mapnik 2.0.0 (10 of 81 tests will fail)
springmeyer authored
15 #if MAPNIK_VERSION <= 200000
16 #define active isActive
17 #define min_zoom getMinZoom
18 #define max_zoom getMaxZoom
19 #define queryable isQueryable
20 #define active isActive
21 #define visible isVisible
22 #endif
23
24
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
25 Persistent<FunctionTemplate> Layer::constructor;
26
27 void Layer::Initialize(Handle<Object> target) {
28
29 HandleScope scope;
62b010d kill trailing whitespace
Konstantin Käfer authored
30
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
31 constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(Layer::New));
32 constructor->InstanceTemplate()->SetInternalFieldCount(1);
33 constructor->SetClassName(String::NewSymbol("Layer"));
34
35 // methods
36 NODE_SET_PROTOTYPE_METHOD(constructor, "describe", describe);
37
38 // properties
1e83986 @springmeyer workaround #74 by allowing multiple symbols - tests pass for getters/set...
springmeyer authored
39 ATTR(constructor, "name", get_prop, set_prop);
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
40 ATTR(constructor, "srs", get_prop, set_prop);
41 ATTR(constructor, "styles", get_prop, set_prop);
42 ATTR(constructor, "datasource", get_prop, set_prop);
43
44 target->Set(String::NewSymbol("Layer"),constructor->GetFunction());
45 }
46
47 Layer::Layer(std::string const& name) :
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
48 ObjectWrap(),
49 layer_(boost::make_shared<mapnik::layer>(name)) {}
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
50
51 Layer::Layer(std::string const& name, std::string const& srs) :
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
52 ObjectWrap(),
53 layer_(boost::make_shared<mapnik::layer>(name,srs)) {}
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
54
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
55 Layer::Layer() :
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
56 ObjectWrap(),
57 layer_() {}
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
58
59
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
60 Layer::~Layer() {}
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
61
62 Handle<Value> Layer::New(const Arguments& args)
63 {
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
64 HandleScope scope;
62b010d kill trailing whitespace
Konstantin Käfer authored
65
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
66 if (!args.IsConstructCall())
67 return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword"));
62b010d kill trailing whitespace
Konstantin Käfer authored
68
69 if (args[0]->IsExternal())
70 {
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
71 Local<External> ext = Local<External>::Cast(args[0]);
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
72 void* ptr = ext->Value();
73 Layer* l = static_cast<Layer*>(ptr);
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
74 l->Wrap(args.This());
75 return args.This();
62b010d kill trailing whitespace
Konstantin Käfer authored
76 }
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
77
78 if (args.Length() == 1)
79 {
80 if (!args[0]->IsString())
81 {
82 return ThrowException(Exception::TypeError(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
83 String::New("'name' must be a string")));
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
84 }
85 Layer* l = new Layer(TOSTR(args[0]));
86 l->Wrap(args.This());
87 return args.This();
88 }
89 else if (args.Length() == 2)
90 {
91 if (!args[0]->IsString() || !args[1]->IsString())
92 return ThrowException(Exception::TypeError(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
93 String::New("'name' and 'srs' must be a strings")));
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
94 Layer* l = new Layer(TOSTR(args[0]),TOSTR(args[1]));
95 l->Wrap(args.This());
96 return args.This();
97 }
98 else
99 {
100 return ThrowException(Exception::TypeError(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
101 String::New("please provide Layer name and optional srs")));
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
102 }
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
103 return args.This();
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
104 }
105
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
106 Handle<Value> Layer::New(mapnik::layer const& lay_ref) {
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
107 HandleScope scope;
108 Layer* l = new Layer();
109 // copy new mapnik::layer into the shared_ptr
0299e77 @springmeyer use boost::make_shared to allocate shared pointers - faster allocation a...
springmeyer authored
110 l->layer_ = boost::make_shared<mapnik::layer>(lay_ref);
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
111 Handle<Value> ext = External::New(l);
112 Handle<Object> obj = constructor->GetFunction()->NewInstance(1, &ext);
113 return scope.Close(obj);
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
114 }
115
116 Handle<Value> Layer::get_prop(Local<String> property,
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
117 const AccessorInfo& info)
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
118 {
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
119 HandleScope scope;
120 Layer* l = ObjectWrap::Unwrap<Layer>(info.This());
121 std::string a = TOSTR(property);
122 if (a == "name")
123 return scope.Close(String::New(l->layer_->name().c_str()));
124 else if (a == "srs")
125 return scope.Close(String::New(l->layer_->srs().c_str()));
126 else if (a == "styles") {
127 std::vector<std::string> const& style_names = l->layer_->styles();
128 Local<Array> s = Array::New(style_names.size());
129 for (unsigned i = 0; i < style_names.size(); ++i)
130 {
131 s->Set(i, String::New(style_names[i].c_str()) );
132 }
62b010d kill trailing whitespace
Konstantin Käfer authored
133 return scope.Close(s);
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
134 }
135 else if (a == "datasource") {
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
136 mapnik::datasource_ptr ds = l->layer_->datasource();
137 if (ds)
138 {
dd58c9c @springmeyer close the local scope when returning class instances from functions
springmeyer authored
139 return scope.Close(Datasource::New(ds));
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
140 }
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
141 }
142 return Undefined();
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
143 }
144
145 void Layer::set_prop(Local<String> property,
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
146 Local<Value> value,
147 const AccessorInfo& info)
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
148 {
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
149 HandleScope scope;
51980ae @springmeyer v8 accessors appear to be broken - LNK2005 errors
springmeyer authored
150 Layer* l = ObjectWrap::Unwrap<Layer>(info.This());
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
151 std::string a = TOSTR(property);
152 if (a == "name")
153 {
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
154 if (!value->IsString()) {
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
155 ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
156 String::New("'name' must be a string")));
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
157 } else {
158 l->layer_->set_name(TOSTR(value));
159 }
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
160 }
161 else if (a == "srs")
162 {
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
163 if (!value->IsString()) {
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
164 ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
165 String::New("'srs' must be a string")));
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
166 } else {
167 l->layer_->set_srs(TOSTR(value));
168 }
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
169 }
170 else if (a == "styles")
171 {
172 if (!value->IsArray())
173 ThrowException(Exception::Error(
3a02935 @springmeyer cpp coding style cleanups
springmeyer authored
174 String::New("Must provide an array of style names")));
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
175 else {
176 Local<Array> a = Local<Array>::Cast(value->ToObject());
177 // todo - how to check if cast worked?
178 uint32_t i = 0;
179 uint32_t a_length = a->Length();
180 while (i < a_length) {
181 l->layer_->add_style(TOSTR(a->Get(i)));
182 i++;
183 }
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
184 }
185 }
186 else if (a == "datasource")
187 {
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
188 Local<Object> obj = value->ToObject();
0c8e85e @springmeyer add ability to dynamically add a JSDatasource to a layer
springmeyer authored
189 if (value->IsNull() || value->IsUndefined()) {
7f6fe7d @springmeyer disable the JSDatasource since its not a viable idea given the way that ...
springmeyer authored
190 ThrowException(Exception::TypeError(String::New("mapnik.Datasource, or mapnik.MemoryDatasource instance expected")));
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
191 } else {
0c8e85e @springmeyer add ability to dynamically add a JSDatasource to a layer
springmeyer authored
192 if (Datasource::constructor->HasInstance(obj)) {
e2d1c5f @springmeyer bugfix: unwrap to correct type
springmeyer authored
193 Datasource *d = ObjectWrap::Unwrap<Datasource>(obj);
0c8e85e @springmeyer add ability to dynamically add a JSDatasource to a layer
springmeyer authored
194 l->layer_->set_datasource(d->get());
195 }
7f6fe7d @springmeyer disable the JSDatasource since its not a viable idea given the way that ...
springmeyer authored
196 /*else if (JSDatasource::constructor->HasInstance(obj))
0c8e85e @springmeyer add ability to dynamically add a JSDatasource to a layer
springmeyer authored
197 {
198 JSDatasource *d = ObjectWrap::Unwrap<JSDatasource>(obj);
199 l->layer_->set_datasource(d->get());
7f6fe7d @springmeyer disable the JSDatasource since its not a viable idea given the way that ...
springmeyer authored
200 }*/
4b87f68 @springmeyer memory::datasource impementation - can be used in thread pool for async ...
springmeyer authored
201 else if (MemoryDatasource::constructor->HasInstance(obj))
202 {
203 MemoryDatasource *d = ObjectWrap::Unwrap<MemoryDatasource>(obj);
204 l->layer_->set_datasource(d->get());
205 }
0c8e85e @springmeyer add ability to dynamically add a JSDatasource to a layer
springmeyer authored
206 else
207 {
4b87f68 @springmeyer memory::datasource impementation - can be used in thread pool for async ...
springmeyer authored
208 ThrowException(Exception::TypeError(String::New("mapnik.Datasource, mapnik.JSDatasource, or mapnik.MemoryDatasource instance expected")));
0c8e85e @springmeyer add ability to dynamically add a JSDatasource to a layer
springmeyer authored
209 }
ad2a484 @springmeyer expose ability to get/set mapnik.Datasource from a mapnik.Layer
springmeyer authored
210 }
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
211 }
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
212 }
213
214 Handle<Value> Layer::describe(const Arguments& args)
215 {
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
216 HandleScope scope;
62b010d kill trailing whitespace
Konstantin Käfer authored
217
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
218 Layer* l = ObjectWrap::Unwrap<Layer>(args.This());
62b010d kill trailing whitespace
Konstantin Käfer authored
219
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
220 Local<Object> description = Object::New();
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
221 mapnik::layer const& layer = *l->layer_;
222 if ( layer.name() != "" )
223 {
224 description->Set(String::NewSymbol("name"), String::New(layer.name().c_str()));
225 }
62b010d kill trailing whitespace
Konstantin Käfer authored
226
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
227 if ( layer.srs() != "" )
228 {
229 description->Set(String::NewSymbol("srs"), String::New(layer.srs().c_str()));
230 }
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
231
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
232 if ( !layer.active())
4bdd9e1 @springmeyer expose mapnik.Datasource - a generic contructor for all types of mapnik ...
springmeyer authored
233 {
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
234 description->Set(String::NewSymbol("status"), Boolean::New(layer.active()));
4bdd9e1 @springmeyer expose mapnik.Datasource - a generic contructor for all types of mapnik ...
springmeyer authored
235 }
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
236
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
237 if ( layer.clear_label_cache())
238 {
239 description->Set(String::NewSymbol("clear_label_cache"), Boolean::New(layer.clear_label_cache()));
240 }
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
241
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
242 if ( layer.min_zoom() > 0)
243 {
244 description->Set(String::NewSymbol("minzoom"), Number::New(layer.min_zoom()));
245 }
2c6759a @springmeyer allow slicing of features - closes #9
springmeyer authored
246
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
247 if ( layer.max_zoom() != std::numeric_limits<double>::max() )
2c6759a @springmeyer allow slicing of features - closes #9
springmeyer authored
248 {
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
249 description->Set(String::NewSymbol("maxzoom"), Number::New(layer.max_zoom()));
2c6759a @springmeyer allow slicing of features - closes #9
springmeyer authored
250 }
251
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
252 if ( layer.queryable())
253 {
254 description->Set(String::NewSymbol("queryable"), Boolean::New(layer.queryable()));
255 }
62b010d kill trailing whitespace
Konstantin Käfer authored
256
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
257 std::vector<std::string> const& style_names = layer.styles();
258 Local<Array> s = Array::New(style_names.size());
259 for (unsigned i = 0; i < style_names.size(); ++i)
260 {
261 s->Set(i, String::New(style_names[i].c_str()) );
262 }
62b010d kill trailing whitespace
Konstantin Käfer authored
263
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
264 description->Set(String::NewSymbol("styles"), s );
265
266 mapnik::datasource_ptr datasource = layer.datasource();
267 Local<v8::Object> ds = Object::New();
268 description->Set(String::NewSymbol("datasource"), ds );
269 if ( datasource )
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
270 {
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
271 mapnik::parameters::const_iterator it = datasource->params().begin();
272 mapnik::parameters::const_iterator end = datasource->params().end();
273 for (; it != end; ++it)
274 {
275 node_mapnik::params_to_object serializer( ds , it->first);
276 boost::apply_visitor( serializer, it->second );
277 }
4c5aba8 @springmeyer changes to get mapnik.Layer working fully
springmeyer authored
278 }
62b010d kill trailing whitespace
Konstantin Käfer authored
279
81ca3b5 @springmeyer clean up cruft removing describe_data from map and layer, and fixing up ...
springmeyer authored
280 return scope.Close(description);
a6a2d09 @springmeyer working implementation of mapnik.Layer class along with ability to add a...
springmeyer authored
281 }
8e5a8d3 @springmeyer allow building against Mapnik 2.0.0 (10 of 81 tests will fail)
springmeyer authored
282
283 #if MAPNIK_VERSION <= 200000
284 #undef active
285 #undef min_zoom
286 #undef max_zoom
287 #undef queryable
288 #undef active
289 #undef visible
290 #endif
Something went wrong with that request. Please try again.