Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 197 lines (168 sloc) 7.707 kb
76c7dd7 Dane Springmeyer reflect mapnik.ProjTransform for robust projection transformations - clo...
springmeyer authored
1 #include "utils.hpp"
2 #include "mapnik_proj_transform.hpp"
3 #include "mapnik_projection.hpp"
4
5 #include <boost/make_shared.hpp>
6
7 Persistent<FunctionTemplate> ProjTransform::constructor;
8
9 void ProjTransform::Initialize(Handle<Object> target) {
10
11 HandleScope scope;
12
13 constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(ProjTransform::New));
14 constructor->InstanceTemplate()->SetInternalFieldCount(1);
15 constructor->SetClassName(String::NewSymbol("ProjTransform"));
16
17 NODE_SET_PROTOTYPE_METHOD(constructor, "forward", forward);
18 NODE_SET_PROTOTYPE_METHOD(constructor, "backward", backward);
19
20 target->Set(String::NewSymbol("ProjTransform"),constructor->GetFunction());
21 }
22
23 ProjTransform::ProjTransform(mapnik::projection const& src,
24 mapnik::projection const& dest) :
25 ObjectWrap(),
26 this_(boost::make_shared<mapnik::proj_transform>(src,dest)) {}
27
28 ProjTransform::~ProjTransform()
29 {
30 }
31
32 Handle<Value> ProjTransform::New(const Arguments& args)
33 {
34 HandleScope scope;
35 if (!args.IsConstructCall())
36 return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword"));
37
da53440 Dane Springmeyer thanks to new clang warning (comparison of constant with expression of t...
springmeyer authored
38 if (args.Length() != 2 || !args[0]->IsObject() || !args[1]->IsObject()) {
76c7dd7 Dane Springmeyer reflect mapnik.ProjTransform for robust projection transformations - clo...
springmeyer authored
39 return ThrowException(Exception::TypeError(
40 String::New("please provide two arguments: a pair of mapnik.Projection objects")));
41 }
42
43 Local<Object> src_obj = args[0]->ToObject();
44 if (src_obj->IsNull() || src_obj->IsUndefined() || !Projection::HasInstance(src_obj))
45 return ThrowException(Exception::TypeError(String::New("mapnik.Projection expected for first argument")));
46
47 Local<Object> dest_obj = args[1]->ToObject();
48 if (dest_obj->IsNull() || dest_obj->IsUndefined() || !Projection::HasInstance(dest_obj))
49 return ThrowException(Exception::TypeError(String::New("mapnik.Projection expected for second argument")));
50
51 Projection *p1 = ObjectWrap::Unwrap<Projection>(src_obj);
52 Projection *p2 = ObjectWrap::Unwrap<Projection>(dest_obj);
53
54 try
55 {
56 ProjTransform* p = new ProjTransform(*p1->get(),*p2->get());
57 p->Wrap(args.This());
58 return args.This();
59 }
60 catch (std::exception const& ex)
61 {
62 return ThrowException(Exception::Error(
63 String::New(ex.what())));
64 }
65 }
66
67 Handle<Value> ProjTransform::forward(const Arguments& args)
68 {
69 HandleScope scope;
70 ProjTransform* p = ObjectWrap::Unwrap<ProjTransform>(args.This());
71
72 if (!args.Length() == 1)
73 return ThrowException(Exception::Error(
74 String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]")));
75 else
76 {
77 if (!args[0]->IsArray())
78 return ThrowException(Exception::Error(
79 String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]")));
80
81 Local<Array> a = Local<Array>::Cast(args[0]);
82 uint32_t array_length = a->Length();
83 if (array_length == 2)
84 {
85 double x = a->Get(0)->NumberValue();
86 double y = a->Get(1)->NumberValue();
87 double z = 0;
88 if (!p->this_->forward(x,y,z))
89 {
90 std::ostringstream s;
91 s << "Failed to forward project "
92 << a->Get(0)->NumberValue() << "," << a->Get(1)->NumberValue() << " from " << p->this_->source().params() << " to " << p->this_->dest().params();
93 return ThrowException(Exception::Error(
94 String::New(s.str().c_str())));
95
96 }
97 Local<Array> a = Array::New(2);
98 a->Set(0, Number::New(x));
99 a->Set(1, Number::New(y));
100 return scope.Close(a);
101 }
102 else if (array_length == 4)
103 {
104 mapnik::box2d<double> box(a->Get(0)->NumberValue(),
105 a->Get(1)->NumberValue(),
106 a->Get(2)->NumberValue(),
107 a->Get(3)->NumberValue());
108 if (!p->this_->forward(box))
109 {
110 std::ostringstream s;
111 s << "Failed to forward project "
112 << box << " from " << p->this_->source().params() << " to " << p->this_->dest().params();
113 return ThrowException(Exception::Error(
114 String::New(s.str().c_str())));
115
116 }
117 Local<Array> a = Array::New(4);
118 a->Set(0, Number::New(box.minx()));
119 a->Set(1, Number::New(box.miny()));
120 a->Set(2, Number::New(box.maxx()));
121 a->Set(3, Number::New(box.maxy()));
122 return scope.Close(a);
123 }
124 else
125 return ThrowException(Exception::Error(
126 String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]")));
127
128 }
129 }
130
131 Handle<Value> ProjTransform::backward(const Arguments& args)
132 {
133 HandleScope scope;
134 ProjTransform* p = ObjectWrap::Unwrap<ProjTransform>(args.This());
135
136 if (!args.Length() == 1)
137 return ThrowException(Exception::Error(
138 String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]")));
139 else
140 {
141 if (!args[0]->IsArray())
142 {
143 return ThrowException(Exception::Error(
144 String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]")));
145 }
146
147 Local<Array> a = Local<Array>::Cast(args[0]);
148 uint32_t array_length = a->Length();
149 if (array_length == 2)
150 {
151 double x = a->Get(0)->NumberValue();
152 double y = a->Get(1)->NumberValue();
153 double z = 0;
154 if (!p->this_->backward(x,y,z))
155 {
156 std::ostringstream s;
157 s << "Failed to back project "
158 << a->Get(0)->NumberValue() << "," << a->Get(1)->NumberValue() << " from " << p->this_->dest().params() << " to: " << p->this_->source().params();
159 return ThrowException(Exception::Error(
160 String::New(s.str().c_str())));
161
162 }
163 Local<Array> a = Array::New(2);
164 a->Set(0, Number::New(x));
165 a->Set(1, Number::New(y));
166 return scope.Close(a);
167 }
168 else if (array_length == 4)
169 {
170 mapnik::box2d<double> box(a->Get(0)->NumberValue(),
171 a->Get(1)->NumberValue(),
172 a->Get(2)->NumberValue(),
173 a->Get(3)->NumberValue());
174 if (!p->this_->backward(box))
175 {
176 std::ostringstream s;
177 s << "Failed to back project "
178 << box << " from " << p->this_->source().params() << " to " << p->this_->dest().params();
179 return ThrowException(Exception::Error(
180 String::New(s.str().c_str())));
181
182 }
183 Local<Array> a = Array::New(4);
184 a->Set(0, Number::New(box.minx()));
185 a->Set(1, Number::New(box.miny()));
186 a->Set(2, Number::New(box.maxx()));
187 a->Set(3, Number::New(box.maxy()));
188 return scope.Close(a);
189 }
190 else
191 return ThrowException(Exception::Error(
192 String::New("Must provide an array of either [x,y] or [minx,miny,maxx,maxy]")));
193
194 }
195 }
196
Something went wrong with that request. Please try again.