binding/v8/jsi/geometry/GeometryCollection.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 GeometryCollection.cpp
22 
23  \brief Utility functions to register the GeometryCollection class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../geometry/GeometryCollection.h"
28 #include "../../common/Utils.h"
29 #include "Geometry.h"
30 
31 // Boost
32 #include <boost/cstdint.hpp>
33 
34 ::v8::Handle<::v8::Value> GeometryCollection_GetNumGeometries(const ::v8::Arguments& args)
35 {
36  ::v8::HandleScope hs;
37 
38  if(args.Holder().IsEmpty())
39  return ::v8::ThrowException(::v8::String::New("In order to use getNumGeometries method you must use object notation: \"ng = obj.getNumGeometries();\""));
40 
41  te::gm::GeometryCollection* g = te::v8::common::Unwrap<te::gm::GeometryCollection>(args.Holder());
42 
43  if(g == 0)
44  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getNumGeometries method!"));
45 
46  std::size_t n = g->getNumGeometries();
47 
48  ::v8::Handle<::v8::Integer> jn = ::v8::Integer::New(static_cast<boost::int32_t>(n));
49 
50  return hs.Close(jn);
51 }
52 
53 ::v8::Handle<::v8::Value> GeometryCollection_SetNumGeometries(const ::v8::Arguments& args)
54 {
55  ::v8::HandleScope hs;
56 
57  if(args.Holder().IsEmpty())
58  return ::v8::ThrowException(::v8::String::New("In order to use setNumGeometries method you must use object notation: \"obj.setNumGeometries(2);\""));
59 
60  if((args.Length() != 1) || args[0].IsEmpty() || !args[0]->IsInt32())
61  return ::v8::ThrowException(::v8::String::New("In order to use setNumGeometries method you must use object notation: \"obj.setNumGeometries(2);\""));
62 
63  te::gm::GeometryCollection* g = te::v8::common::Unwrap<te::gm::GeometryCollection>(args.Holder());
64 
65  if(g == 0)
66  return ::v8::ThrowException(::v8::String::New("Invalid geometry in setNumGeometries method!"));
67 
68  int n = args[0]->ToInt32()->Value();
69 
70  g->setNumGeometries(n);
71 
72  return hs.Close(::v8::Undefined());
73 }
74 
75 ::v8::Handle<::v8::Value> GeometryCollection_GetGeometryN(const ::v8::Arguments& args)
76 {
77  ::v8::HandleScope hs;
78 
79  if(args.Holder().IsEmpty())
80  return ::v8::ThrowException(::v8::String::New("In order to use getGeometryN method you must use object notation: \"gn = obj.getGeometryN(2);\""));
81 
82  if((args.Length() != 1) || args[0].IsEmpty() || !args[0]->IsInt32())
83  return ::v8::ThrowException(::v8::String::New("In order to use getGeometryN method you must use object notation: \"gn = obj.getGeometryN(2);\""));
84 
85  te::gm::GeometryCollection* thisGeom = te::v8::common::Unwrap<te::gm::GeometryCollection>(args.Holder());
86 
87  if(thisGeom == 0)
88  return ::v8::ThrowException(::v8::String::New("Invalid geometry in getGeometryN method!"));
89 
90  int i = args[0]->ToInt32()->Value();
91 
92  te::gm::Geometry* g = thisGeom->getGeometryN(i);
93 
94  ::v8::Local<::v8::Object> jg = te::v8::jsi::Geometry_Make(g, false);
95 
96  return hs.Close(jg);
97 }
98 
99 ::v8::Handle<::v8::Value> GeometryCollection_SetGeometryN(const ::v8::Arguments& args)
100 {
101  ::v8::HandleScope hs;
102 
103  if(args.Holder().IsEmpty())
104  return ::v8::ThrowException(::v8::String::New("In order to use setGeometryN method you must use object notation: \"obj.setGeometryN(i, new_geom);\""));
105 
106  if((args.Length() != 2) || args[0].IsEmpty() || !args[0]->IsInt32() || args[1].IsEmpty() || !args[1]->IsObject())
107  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong parameter type in setGeometryN method!"));
108 
109  te::gm::GeometryCollection* thisGeom = te::v8::common::Unwrap<te::gm::GeometryCollection>(args.Holder());
110 
111  if(thisGeom == 0)
112  return ::v8::ThrowException(::v8::String::New("Invalid geometry in setGeometryN method!"));
113 
114  int i = args[0]->ToInt32()->Value();
115  te::gm::Geometry* rhsGeom = te::v8::common::UnwrapAndLooseOwnership<te::gm::Geometry>(args[1]->ToObject());
116 
117  if(rhsGeom == 0)
118  return ::v8::ThrowException(::v8::String::New("Invalid geometry in setGeometryN method!"));
119 
120  thisGeom->setGeometryN(i, rhsGeom);
121 
122  return hs.Close(::v8::Undefined());
123 }
124 
125 ::v8::Handle<::v8::Value> GeometryCollection_RemoveGeometryN(const ::v8::Arguments& args)
126 {
127  ::v8::HandleScope hs;
128 
129  if(args.Holder().IsEmpty())
130  return ::v8::ThrowException(::v8::String::New("In order to use removeGeometryN method you must use object notation: \"obj.removeGeometryN(2);\""));
131 
132  if((args.Length() != 1) || args[0].IsEmpty() || !args[0]->IsInt32())
133  return ::v8::ThrowException(::v8::String::New("In order to use removeGeometryN method you must use object notation: \"obj.removeGeometryN(2);\""));
134 
135  te::gm::GeometryCollection* g = te::v8::common::Unwrap<te::gm::GeometryCollection>(args.Holder());
136 
137  if(g == 0)
138  return ::v8::ThrowException(::v8::String::New("Invalid geometry in removeGeometryN method!"));
139 
140  int n = args[0]->ToInt32()->Value();
141 
142  g->removeGeometryN(n);
143 
144  return hs.Close(::v8::Undefined());
145 }
146 
147 ::v8::Handle<::v8::Value> GeometryCollection_Add(const ::v8::Arguments& args)
148 {
149  ::v8::HandleScope hs;
150 
151  if(args.Holder().IsEmpty())
152  return ::v8::ThrowException(::v8::String::New("In order to use add method you must use object notation: \"obj.add(new_geom);\""));
153 
154  if((args.Length() != 1) || args[0].IsEmpty() || !args[0]->IsObject())
155  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong parameter type in add method!"));
156 
157  te::gm::GeometryCollection* thisGeom = te::v8::common::Unwrap<te::gm::GeometryCollection>(args.Holder());
158 
159  if(thisGeom == 0)
160  return ::v8::ThrowException(::v8::String::New("Invalid geometry in add method!"));
161 
162  te::gm::Geometry* rhsGeom = te::v8::common::UnwrapAndLooseOwnership<te::gm::Geometry>(args[0]->ToObject());
163 
164  if(rhsGeom == 0)
165  return ::v8::ThrowException(::v8::String::New("Invalid geometry in add method!"));
166 
167  thisGeom->add(rhsGeom);
168 
169  return hs.Close(::v8::Undefined());
170 }
171 
172 ::v8::Handle<::v8::Value> GeometryCollection_Clear(const ::v8::Arguments& args)
173 {
174  ::v8::HandleScope hs;
175 
176  if(args.Holder().IsEmpty())
177  return ::v8::ThrowException(::v8::String::New("In order to use clear method you must use object notation: \"obj.clear();\""));
178 
179  te::gm::GeometryCollection* g = te::v8::common::Unwrap<te::gm::GeometryCollection>(args.Holder());
180 
181  if(g == 0)
182  return ::v8::ThrowException(::v8::String::New("Invalid geometry in clear method!"));
183 
184  g->clear();
185 
186  return hs.Close(::v8::Undefined());
187 }
188 
189 ::v8::Handle<::v8::Value> GeometryCollection_Constructor(const ::v8::Arguments& args)
190 {
191  ::v8::HandleScope hs;
192 
193  if(!args.IsConstructCall())
194  return ::v8::ThrowException(::v8::String::New("In order to create a GeometryCollection you need to call its constructor like: var mygc = new TeGeometryCollection(2, TE_OGC_GEOMETRYCOLLECTION, 4326)."));
195 
196  if(args.Holder().IsEmpty())
197  return ::v8::ThrowException(::v8::String::New("GeometryCollection constructor must use object notation!"));
198 
199  if((args.Length() != 3) ||
200  args[0].IsEmpty() || !args[0]->IsInt32() ||
201  args[1].IsEmpty() || !args[1]->IsInt32() ||
202  args[2].IsEmpty() || !args[2]->IsInt32())
203  return ::v8::ThrowException(::v8::String::New("Missing parameter or wrong parameter type in GeometryCollection constructor method!!"));
204 
205  int ngeom = args[0]->Int32Value();
206  int geomType = args[1]->Int32Value();
207  int srid = args[2]->ToInt32()->Int32Value();
208 
209  std::auto_ptr<te::gm::GeometryCollection> g(new te::gm::GeometryCollection(ngeom, static_cast<te::gm::GeomType>(geomType), srid));
210 
211  ::v8::Local<::v8::Object> jg = te::v8::common::Make(g.get(), te::v8::jsi::GetGeometryCollectionTemplate, true);
212 
213  g.release();
214 
215  return hs.Close(jg);
216 }
217 
218 void te::v8::jsi::RegisterGeometryCollection(::v8::Local<::v8::Object>& global)
219 {
220  ::v8::HandleScope hs;
221 
222  ::v8::Local<::v8::FunctionTemplate> jsgc = ::v8::FunctionTemplate::New(GeometryCollection_Constructor);
223 
224  global->Set(::v8::String::New("TeGeometryCollection"), jsgc->GetFunction());
225 }
226 
227 static ::v8::Persistent<::v8::FunctionTemplate> sg_gc_template;
228 
229 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetGeometryCollectionTemplate()
230 {
231  if(sg_gc_template.IsEmpty())
232  {
233  ::v8::Persistent<::v8::FunctionTemplate>& gTpl = GetGeometryTemplate();
234  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
235  result->Inherit(gTpl);
236 
237  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
238 
239  prototype->Set(::v8::String::NewSymbol("getNumGeometries"), ::v8::FunctionTemplate::New(GeometryCollection_GetNumGeometries));
240  prototype->Set(::v8::String::NewSymbol("setNumGeometries"), ::v8::FunctionTemplate::New(GeometryCollection_SetNumGeometries));
241  prototype->Set(::v8::String::NewSymbol("getGeometryN"), ::v8::FunctionTemplate::New(GeometryCollection_GetGeometryN));
242  prototype->Set(::v8::String::NewSymbol("setGeometryN"), ::v8::FunctionTemplate::New(GeometryCollection_SetGeometryN));
243  prototype->Set(::v8::String::NewSymbol("removeGeometryN"), ::v8::FunctionTemplate::New(GeometryCollection_RemoveGeometryN));
244  prototype->Set(::v8::String::NewSymbol("add"), ::v8::FunctionTemplate::New(GeometryCollection_Add));
245  prototype->Set(::v8::String::NewSymbol("clear"), ::v8::FunctionTemplate::New(GeometryCollection_Clear));
246 
247  sg_gc_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
248  }
249 
250  return sg_gc_template;
251 }
252 
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
::v8::Handle<::v8::Value > GeometryCollection_GetNumGeometries(const ::v8::Arguments &args)
static::v8::Persistent<::v8::FunctionTemplate > sg_gc_template
::v8::Handle<::v8::Value > GeometryCollection_Constructor(const ::v8::Arguments &args)
void setNumGeometries(std::size_t size)
It sets the number of geometries in this GeometryCollection.
void removeGeometryN(std::size_t i)
It removes the n-th geometry in this geometry collection.
::v8::Handle<::v8::Value > GeometryCollection_GetGeometryN(const ::v8::Arguments &args)
void clear()
It deletes all the elements of the collection.
::v8::Handle<::v8::Value > GeometryCollection_Clear(const ::v8::Arguments &args)
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
::v8::Handle<::v8::Value > GeometryCollection_RemoveGeometryN(const ::v8::Arguments &args)
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
::v8::Persistent<::v8::FunctionTemplate > & GetGeometryTemplate()
It returns a reference to the persistent template of a Geometry object.
void add(Geometry *g)
It adds the geometry into the collection.
::v8::Local<::v8::Object > Geometry_Make(te::gm::Geometry *g, const bool isOwner)
Given a C++ geometry this function creates a new JavaScript geometry cast to the right geometry subty...
void setGeometryN(std::size_t i, Geometry *g)
It sets the n-th geometry in this geometry collection.
::v8::Handle<::v8::Value > GeometryCollection_SetGeometryN(const ::v8::Arguments &args)
It is a collection of other geometric objects.
::v8::Handle<::v8::Value > GeometryCollection_SetNumGeometries(const ::v8::Arguments &args)
::v8::Handle<::v8::Value > GeometryCollection_Add(const ::v8::Arguments &args)
::v8::Persistent<::v8::FunctionTemplate > & GetGeometryCollectionTemplate()
It returns a reference to the persistent template of a GeometryCollection object. ...
void RegisterGeometryCollection(::v8::Local<::v8::Object > &global)
It register the GeometryCollection class.