binding/v8/jsi/geometry/Polygon.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Polygon.cpp
22 
23  \brief Utility functions to register the Polygon class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../geometry/LinearRing.h"
28 #include "../../../../geometry/Polygon.h"
29 #include "../../common/Utils.h"
30 #include "Geometry.h"
31 
32 ::v8::Handle<::v8::Value> Polygon_GetExteriorRing(const ::v8::Arguments& args)
33 {
34  ::v8::HandleScope hs;
35 
36  if(args.Holder().IsEmpty())
37  return ::v8::ThrowException(::v8::String::New("In order to use getExteriorRing method you must use object notation: \"ring = poly.getExteriorRing();\""));
38 
39  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
40 
41  if(g == 0)
42  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getExteriorRing method!"));
43 
45 
46  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(r, te::v8::jsi::GetLinearRingTemplate, false);
47 
48  return hs.Close(jsgeom);
49 }
50 
51 ::v8::Handle<::v8::Value> Polygon_GetNumInteriorRings(const ::v8::Arguments& args)
52 {
53  ::v8::HandleScope hs;
54 
55  if(args.Holder().IsEmpty())
56  return ::v8::ThrowException(::v8::String::New("In order to use getNumInteriorRings method you must use object notation: \"n = poly.getNumInteriorRings();\""));
57 
58  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
59 
60  if(g == 0)
61  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getNumInteriorRings method!"));
62 
63  std::size_t n = g->getNumInteriorRings();
64 
65  ::v8::Local<::v8::Integer> jn = ::v8::Integer::New(static_cast<int>(n));
66 
67  return hs.Close(jn);
68 }
69 
70 ::v8::Handle<::v8::Value> Polygon_GetNumRings(const ::v8::Arguments& args)
71 {
72  ::v8::HandleScope hs;
73 
74  if(args.Holder().IsEmpty())
75  return ::v8::ThrowException(::v8::String::New("In order to use getNumRings method you must use object notation: \"n = poly.getNumRings();\""));
76 
77  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
78 
79  if(g == 0)
80  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getNumRings method!"));
81 
82  std::size_t n = g->getNumRings();
83 
84  ::v8::Handle<::v8::Integer> jn = ::v8::Integer::New(static_cast<int>(n));
85 
86  return hs.Close(jn);
87 }
88 
89 ::v8::Handle<::v8::Value> Polygon_SetNumRings(const ::v8::Arguments& args)
90 {
91  ::v8::HandleScope hs;
92 
93  if(args.Holder().IsEmpty())
94  return ::v8::ThrowException(::v8::String::New("In order to use setNumRings method you must use object notation: \"poly.setNumRings(n);\""));
95 
96  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
97 
98  if(g == 0)
99  return ::v8::ThrowException(::v8::String::New("Invalid geometry in setNumRings method!"));
100 
101  if(args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsInt32())
102  return ::v8::ThrowException(::v8::String::New("Invalid parameter for setNumRings!"));
103 
104  int n = args[0]->ToInt32()->Value();
105 
106  g->setNumRings(n);
107 
108  return hs.Close(::v8::Undefined());
109 }
110 
111 ::v8::Handle<::v8::Value> Polygon_GetInteriorRingN(const ::v8::Arguments& args)
112 {
113  ::v8::HandleScope hs;
114 
115  if(args.Holder().IsEmpty())
116  return ::v8::ThrowException(::v8::String::New("In order to use getInteriorRingN method you must use object notation: \"r = poly.getInteriorRingN(i);\""));
117 
118  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
119 
120  if(g == 0)
121  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getInteriorRingN method!"));
122 
123  if(args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsInt32())
124  return ::v8::ThrowException(::v8::String::New("Invalid parameter for getInteriorRingN!"));
125 
126  int n = args[0]->ToInt32()->Value();
127 
128  te::gm::LinearRing* ring = g->getInteriorRingN(n);
129 
130  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(ring, te::v8::jsi::GetLinearRingTemplate, false);
131 
132  return hs.Close(jsgeom);
133 }
134 
135 ::v8::Handle<::v8::Value> Polygon_GetRingN(const ::v8::Arguments& args)
136 {
137  ::v8::HandleScope hs;
138 
139  if(args.Holder().IsEmpty())
140  return ::v8::ThrowException(::v8::String::New("In order to use getRingN method you must use object notation: \"r = poly.getRingN(i);\""));
141 
142  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
143 
144  if(g == 0)
145  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getRingN method!"));
146 
147  if(args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsInt32())
148  return ::v8::ThrowException(::v8::String::New("Invalid parameter for getRingN!"));
149 
150  int n = args[0]->ToInt32()->Value();
151 
152  te::gm::LinearRing* ring = g->getRingN(n);
153 
154  ::v8::Local<::v8::Object> jsgeom = te::v8::common::Make(ring, te::v8::jsi::GetLinearRingTemplate, false);
155 
156  return hs.Close(jsgeom);
157 }
158 
159 ::v8::Handle<::v8::Value> Polygon_SetRingN(const ::v8::Arguments& args)
160 {
161  ::v8::HandleScope hs;
162 
163  if(args.Holder().IsEmpty())
164  return ::v8::ThrowException(::v8::String::New("In order to use setRingN method you must use object notation: \"poly.setRingN(n, ring);\""));
165 
166  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
167 
168  if(g == 0)
169  return ::v8::ThrowException(::v8::String::New("Invalid geometry in setRingN method!"));
170 
171  if(args.Length() != 2 || args[0].IsEmpty() || !args[0]->IsInt32() || args[1].IsEmpty() || !args[1]->IsObject())
172  return ::v8::ThrowException(::v8::String::New("Invalid parameter for setNumRings!"));
173 
174  int n = args[0]->ToInt32()->Value();
175 
176  std::auto_ptr<te::gm::LinearRing> ring(te::v8::common::UnwrapAndLooseOwnership<te::gm::LinearRing>(args[1]->ToObject()));
177 
178  g->setRingN(n, ring.get());
179 
180  ring.release();
181 
182  return hs.Close(::v8::Undefined());
183 }
184 
185 ::v8::Handle<::v8::Value> Polygon_RemoveRingN(const ::v8::Arguments& args)
186 {
187  ::v8::HandleScope hs;
188 
189  if(args.Holder().IsEmpty())
190  return ::v8::ThrowException(::v8::String::New("In order to use removeRingN method you must use object notation: \"poly.removeRingN(i);\""));
191 
192  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
193 
194  if(g == 0)
195  return ::v8::ThrowException(::v8::String::New("Invalid geometry in removeRingN method!"));
196 
197  if(args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsInt32())
198  return ::v8::ThrowException(::v8::String::New("Invalid parameter for removeRingN!"));
199 
200  int n = args[0]->ToInt32()->Value();
201 
202  g->removeRingN(n);
203 
204  return hs.Close(::v8::Undefined());
205 }
206 
207 ::v8::Handle<::v8::Value> Polygon_Add(const ::v8::Arguments& args)
208 {
209  ::v8::HandleScope hs;
210 
211  if(args.Holder().IsEmpty())
212  return ::v8::ThrowException(::v8::String::New("In order to use add method you must use object notation: \"poly.add(ring);\""));
213 
214  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
215 
216  if(g == 0)
217  return ::v8::ThrowException(::v8::String::New("Invalid geometry in add method!"));
218 
219  if(args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsObject())
220  return ::v8::ThrowException(::v8::String::New("Invalid parameter for add!"));
221 
222  std::auto_ptr<te::gm::LinearRing> ring(te::v8::common::UnwrapAndLooseOwnership<te::gm::LinearRing>(args[0]->ToObject()));
223 
224  g->add(ring.get());
225 
226  ring.release();
227 
228  return hs.Close(::v8::Undefined());
229 }
230 
231 ::v8::Handle<::v8::Value> Polygon_Clear(const ::v8::Arguments& args)
232 {
233  ::v8::HandleScope hs;
234 
235  if(args.Holder().IsEmpty())
236  return ::v8::ThrowException(::v8::String::New("In order to use clear method you must use object notation: \"poly.clear();\""));
237 
238  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
239 
240  if(g == 0)
241  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getNumRings method!"));
242 
243  g->clear();
244 
245  return hs.Close(::v8::Undefined());
246 }
247 
248 ::v8::Handle<::v8::Value> Polygon_GetRings(const ::v8::Arguments& args)
249 {
250  ::v8::HandleScope hs;
251 
252  if(args.Holder().IsEmpty())
253  return ::v8::ThrowException(::v8::String::New("In order to use getRings method you must use object notation: \"rings = poly.getRings();\""));
254 
255  te::gm::Polygon* g = te::v8::common::Unwrap<te::gm::Polygon>(args.Holder());
256 
257  if(g == 0)
258  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getRings method!"));
259 
260  const std::vector<te::gm::LinearRing*>& rings = g->getRings();
261 
262  int len = static_cast<int>(rings.size());
263 
264  ::v8::Handle<::v8::Array> jrings = ::v8::Array::New(len);
265 
266  for(int i = 0; i < len; ++i)
267  jrings->Set(i, te::v8::common::Make(rings[i], te::v8::jsi::GetLinearRingTemplate, false));
268 
269  return hs.Close(jrings);
270 }
271 
272 ::v8::Handle<::v8::Value> Polygon_Constructor(const ::v8::Arguments& args)
273 {
274  ::v8::HandleScope hs;
275 
276  if(!args.IsConstructCall())
277  return ::v8::ThrowException(::v8::String::New("In order to create a Polygon you need to call its constructor like: var mygc = new TePolygon(3, TE_OGC_POLYGON, 4326)."));
278 
279  if(args.Holder().IsEmpty())
280  return ::v8::ThrowException(::v8::String::New("Polygon constructor must use object notation!"));
281 
282  if((args.Length() != 3) ||
283  args[0].IsEmpty() || !args[0]->IsInt32() ||
284  args[1].IsEmpty() || !args[1]->IsInt32() ||
285  args[2].IsEmpty() || !args[2]->IsInt32())
286  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong parameter type in Polygon constructor method!!"));
287 
288 
289  int nrings = args[0]->Int32Value();
290  int geomType = args[1]->Int32Value();
291  int srid = args[2]->ToInt32()->Int32Value();
292 
293  std::auto_ptr<te::gm::Polygon> g(new te::gm::Polygon(nrings, static_cast<te::gm::GeomType>(geomType), srid));
294 
295  ::v8::Local<::v8::Object> jg = te::v8::common::Make(g.get(), te::v8::jsi::GetPolygonTemplate, true);
296 
297  g.release();
298 
299  return hs.Close(jg);
300 }
301 
302 void te::v8::jsi::RegisterPolygon(::v8::Local<::v8::Object>& global)
303 {
304  ::v8::HandleScope hs;
305 
306  ::v8::Local<::v8::FunctionTemplate> jsgc = ::v8::FunctionTemplate::New(Polygon_Constructor);
307 
308  global->Set(::v8::String::New("TePolygon"), jsgc->GetFunction());
309 }
310 
311 static ::v8::Persistent<::v8::FunctionTemplate> sg_polygon_template;
312 
313 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetPolygonTemplate()
314 {
315  if(sg_polygon_template.IsEmpty())
316  {
317  ::v8::Persistent<::v8::FunctionTemplate>& surfaceTpl = GetSurfaceTemplate();
318  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
319  result->Inherit(surfaceTpl);
320 
321  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
322 
323  prototype->Set(::v8::String::NewSymbol("getExteriorRing"), ::v8::FunctionTemplate::New(Polygon_GetExteriorRing));
324  prototype->Set(::v8::String::NewSymbol("getNumInteriorRings"), ::v8::FunctionTemplate::New(Polygon_GetNumInteriorRings));
325  prototype->Set(::v8::String::NewSymbol("getNumRings"), ::v8::FunctionTemplate::New(Polygon_GetNumRings));
326  prototype->Set(::v8::String::NewSymbol("setNumRings"), ::v8::FunctionTemplate::New(Polygon_SetNumRings));
327  prototype->Set(::v8::String::NewSymbol("getInteriorRingN"), ::v8::FunctionTemplate::New(Polygon_GetInteriorRingN));
328  prototype->Set(::v8::String::NewSymbol("getRingN"), ::v8::FunctionTemplate::New(Polygon_GetRingN));
329  prototype->Set(::v8::String::NewSymbol("setRingN"), ::v8::FunctionTemplate::New(Polygon_SetRingN));
330  prototype->Set(::v8::String::NewSymbol("removeRingN"), ::v8::FunctionTemplate::New(Polygon_RemoveRingN));
331  prototype->Set(::v8::String::NewSymbol("add"), ::v8::FunctionTemplate::New(Polygon_Add));
332  prototype->Set(::v8::String::NewSymbol("push_back"), ::v8::FunctionTemplate::New(Polygon_Add));
333  prototype->Set(::v8::String::NewSymbol("clear"), ::v8::FunctionTemplate::New(Polygon_Clear));
334  prototype->Set(::v8::String::NewSymbol("getRings"), ::v8::FunctionTemplate::New(Polygon_GetRings));
335 
336  sg_polygon_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
337  }
338 
339  return sg_polygon_template;
340 }
341 
342 
std::size_t getNumRings() const
It returns the number of rings in this CurvePolygon.
Definition: CurvePolygon.h:153
Curve * getInteriorRingN(std::size_t i) const
It returns the n-th interior ring for this curve polygon as a curve.
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
void add(Curve *ring)
It adds the ring to the curve polygon.
std::vector< Curve * > & getRings()
It returns the polygon rings.
Definition: CurvePolygon.h:294
::v8::Persistent<::v8::FunctionTemplate > & GetLinearRingTemplate()
It returns a reference to the persistent template of a LinearRing object.
static::v8::Persistent<::v8::FunctionTemplate > sg_polygon_template
::v8::Handle<::v8::Value > Polygon_GetNumRings(const ::v8::Arguments &args)
std::size_t getNumInteriorRings() const
It returns the number of interior rings in this CurvePolygon.
Curve * getExteriorRing() const
It returns the exterior ring of this CurvePolygon.
void removeRingN(std::size_t i)
It removes the n-th ring in this CurvePolygon.
::v8::Handle<::v8::Value > Polygon_GetRings(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Polygon_GetExteriorRing(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Polygon_Constructor(const ::v8::Arguments &args)
void setNumRings(std::size_t size)
It sets the number of rings in this curve polygon.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
::v8::Handle<::v8::Value > Polygon_Clear(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Polygon_SetRingN(const ::v8::Arguments &args)
::v8::Persistent<::v8::FunctionTemplate > & GetPolygonTemplate()
It returns a reference to the persistent template of a Polygon object.
void RegisterPolygon(::v8::Local<::v8::Object > &global)
It register the Polygon class.
::v8::Handle<::v8::Value > Polygon_Add(const ::v8::Arguments &args)
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
::v8::Handle<::v8::Value > Polygon_GetInteriorRingN(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Polygon_GetRingN(const ::v8::Arguments &args)
void clear()
It deletes all the rings of the CurvePolygon and clear it.
::v8::Persistent<::v8::FunctionTemplate > & GetSurfaceTemplate()
It returns a reference to the persistent template of a Surface object.
::v8::Handle<::v8::Value > Polygon_GetNumInteriorRings(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Polygon_SetNumRings(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > Polygon_RemoveRingN(const ::v8::Arguments &args)
Curve * getRingN(std::size_t i) const
It returns the n-th ring for this curve polygon as a curve.
Definition: CurvePolygon.h:193
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.