Show:
  1. #include "gdal_common.hpp"
  2. #include "gdal_geometry.hpp"
  3. #include "gdal_geometrycollection.hpp"
  4. #include "gdal_multipolygon.hpp"
  5. #include "gdal_polygon.hpp"
  6. #include "collections/geometry_collection_children.hpp"
  7.  
  8. #include <stdlib.h>
  9.  
  10. namespace node_gdal {
  11.  
  12. Nan::Persistent<FunctionTemplate> MultiPolygon::constructor;
  13.  
  14. void MultiPolygon::Initialize(Local<Object> target)
  15. {
  16. Nan::HandleScope scope;
  17.  
  18. Local<FunctionTemplate> lcons = Nan::New<FunctionTemplate>(MultiPolygon::New);
  19. lcons->Inherit(Nan::New(GeometryCollection::constructor));
  20. lcons->InstanceTemplate()->SetInternalFieldCount(1);
  21. lcons->SetClassName(Nan::New("MultiPolygon").ToLocalChecked());
  22.  
  23. Nan::SetPrototypeMethod(lcons, "toString", toString);
  24. Nan::SetPrototypeMethod(lcons, "unionCascaded", unionCascaded);
  25. Nan::SetPrototypeMethod(lcons, "getArea", getArea);
  26.  
  27. Nan::Set(target, Nan::New("MultiPolygon").ToLocalChecked(), Nan::GetFunction(lcons).ToLocalChecked());
  28.  
  29. constructor.Reset(lcons);
  30. }
  31.  
  32. MultiPolygon::MultiPolygon(OGRMultiPolygon *geom)
  33. : Nan::ObjectWrap(),
  34. this_(geom),
  35. owned_(true),
  36. size_(0)
  37. {
  38. LOG("Created MultiPolygon [%p]", geom);
  39. }
  40.  
  41. MultiPolygon::MultiPolygon()
  42. : Nan::ObjectWrap(),
  43. this_(NULL),
  44. owned_(true),
  45. size_(0)
  46. {
  47. }
  48.  
  49. MultiPolygon::~MultiPolygon()
  50. {
  51. if(this_) {
  52. LOG("Disposing MultiPolygon [%p] (%s)", this_, owned_ ? "owned" : "unowned");
  53. if (owned_) {
  54. OGRGeometryFactory::destroyGeometry(this_);
  55. Nan::AdjustExternalMemory(-size_);
  56. }
  57. LOG("Disposed MultiPolygon [%p]", this_);
  58. this_ = NULL;
  59. }
  60. }
  61.  
  62. /**
  63. * @constructor
  64. * @class gdal.MultiPolygon
  65. * @extends gdal.GeometryCollection
  66. */
  67. NAN_METHOD(MultiPolygon::New)
  68. {
  69. Nan::HandleScope scope;
  70. MultiPolygon *f;
  71.  
  72. if (!info.IsConstructCall()) {
  73. Nan::ThrowError("Cannot call constructor as function, you need to use 'new' keyword");
  74. return;
  75. }
  76.  
  77. if (info[0]->IsExternal()) {
  78. Local<External> ext = info[0].As<External>();
  79. void* ptr = ext->Value();
  80. f = static_cast<MultiPolygon *>(ptr);
  81.  
  82. } else {
  83. if (info.Length() != 0) {
  84. Nan::ThrowError("MultiPolygon constructor doesn't take any arguments");
  85. return;
  86. }
  87. f = new MultiPolygon(new OGRMultiPolygon());
  88. }
  89.  
  90. Local<Value> children = GeometryCollectionChildren::New(info.This());
  91. Nan::SetPrivate(info.This(), Nan::New("children_").ToLocalChecked(), children);
  92.  
  93. f->Wrap(info.This());
  94. info.GetReturnValue().Set(info.This());
  95. }
  96.  
  97. Local<Value> MultiPolygon::New(OGRMultiPolygon *geom)
  98. {
  99. Nan::EscapableHandleScope scope;
  100. return scope.Escape(MultiPolygon::New(geom, true));
  101. }
  102.  
  103. Local<Value> MultiPolygon::New(OGRMultiPolygon *geom, bool owned)
  104. {
  105. Nan::EscapableHandleScope scope;
  106.  
  107. if (!geom) {
  108. return scope.Escape(Nan::Null());
  109. }
  110.  
  111. //make a copy of geometry owned by a feature
  112. // + no need to track when a feature is destroyed
  113. // + no need to throw errors when a method trys to modify an owned read-only geometry
  114. // - is slower
  115.  
  116. if (!owned) {
  117. geom = static_cast<OGRMultiPolygon*>(geom->clone());
  118. };
  119.  
  120. MultiPolygon *wrapped = new MultiPolygon(geom);
  121. wrapped->owned_ = true;
  122.  
  123. UPDATE_AMOUNT_OF_GEOMETRY_MEMORY(wrapped);
  124.  
  125. Local<Value> ext = Nan::New<External>(wrapped);
  126. Local<Object> obj = Nan::NewInstance(Nan::GetFunction(Nan::New(MultiPolygon::constructor)).ToLocalChecked(), 1, &ext).ToLocalChecked();
  127.  
  128. return scope.Escape(obj);
  129. }
  130.  
  131. NAN_METHOD(MultiPolygon::toString)
  132. {
  133. Nan::HandleScope scope;
  134. info.GetReturnValue().Set(Nan::New("MultiPolygon").ToLocalChecked());
  135. }
  136.  
  137. /**
  138. * Unions all the geometries and returns the result.
  139. *
  140. * @method unionCascaded
  141. * @return {gdal.Geometry}
  142. */
  143. NAN_METHOD(MultiPolygon::unionCascaded)
  144. {
  145. Nan::HandleScope scope;
  146.  
  147. MultiPolygon *geom = Nan::ObjectWrap::Unwrap<MultiPolygon>(info.This());
  148.  
  149. info.GetReturnValue().Set(Geometry::New(geom->this_->UnionCascaded()));
  150. }
  151.  
  152. /**
  153. * Computes the combined area of the collection.
  154. *
  155. * @method getArea
  156. * @return {Number}
  157. */
  158. NODE_WRAPPED_METHOD_WITH_RESULT(MultiPolygon, getArea, Number, get_Area);
  159.  
  160. } // namespace node_gdal
  161.