Show:
  1.  
  2. #include "gdal_common.hpp"
  3. #include "gdal_geometry.hpp"
  4. #include "gdal_linestring.hpp"
  5. #include "gdal_point.hpp"
  6. #include "collections/linestring_points.hpp"
  7.  
  8. #include <stdlib.h>
  9.  
  10. namespace node_gdal {
  11.  
  12. Nan::Persistent<FunctionTemplate> LineString::constructor;
  13.  
  14. void LineString::Initialize(Local<Object> target)
  15. {
  16. Nan::HandleScope scope;
  17.  
  18. Local<FunctionTemplate> lcons = Nan::New<FunctionTemplate>(LineString::New);
  19. lcons->Inherit(Nan::New(Geometry::constructor));
  20. lcons->InstanceTemplate()->SetInternalFieldCount(1);
  21. lcons->SetClassName(Nan::New("LineString").ToLocalChecked());
  22.  
  23. Nan::SetPrototypeMethod(lcons, "toString", toString);
  24. Nan::SetPrototypeMethod(lcons, "getLength", getLength);
  25. Nan::SetPrototypeMethod(lcons, "value", value);
  26. Nan::SetPrototypeMethod(lcons, "addSubLineString", addSubLineString);
  27.  
  28. ATTR(lcons, "points", pointsGetter, READ_ONLY_SETTER);
  29.  
  30. Nan::Set(target, Nan::New("LineString").ToLocalChecked(), Nan::GetFunction(lcons).ToLocalChecked());
  31.  
  32. constructor.Reset(lcons);
  33. }
  34.  
  35. LineString::LineString(OGRLineString *geom)
  36. : Nan::ObjectWrap(),
  37. this_(geom),
  38. owned_(true),
  39. size_(0)
  40. {
  41. LOG("Created LineString [%p]", geom);
  42. }
  43.  
  44. LineString::LineString()
  45. : Nan::ObjectWrap(),
  46. this_(NULL),
  47. owned_(true),
  48. size_(0)
  49. {
  50. }
  51.  
  52. LineString::~LineString()
  53. {
  54. if(this_) {
  55. LOG("Disposing LineString [%p] (%s)", this_, owned_ ? "owned" : "unowned");
  56. if (owned_) {
  57. OGRGeometryFactory::destroyGeometry(this_);
  58. Nan::AdjustExternalMemory(-size_);
  59. }
  60. LOG("Disposed LineString [%p]", this_);
  61. this_ = NULL;
  62. }
  63. }
  64.  
  65. /**
  66. * Concrete representation of a multi-vertex line.
  67. *
  68. * @example
  69. * ```
  70. * var lineString = new gdal.LineString();
  71. * lineString.points.add(new gdal.Point(0,0));
  72. * lineString.points.add(new gdal.Point(0,10));```
  73. *
  74. * @constructor
  75. * @class gdal.LineString
  76. * @extends gdal.Geometry
  77. */
  78. NAN_METHOD(LineString::New)
  79. {
  80. Nan::HandleScope scope;
  81. LineString *f;
  82.  
  83. if (!info.IsConstructCall()) {
  84. Nan::ThrowError("Cannot call constructor as function, you need to use 'new' keyword");
  85. return;
  86. }
  87.  
  88. if (info[0]->IsExternal()) {
  89. Local<External> ext = info[0].As<External>();
  90. void* ptr = ext->Value();
  91. f = static_cast<LineString *>(ptr);
  92.  
  93. } else {
  94. if (info.Length() != 0) {
  95. Nan::ThrowError("LineString constructor doesn't take any arguments");
  96. return;
  97. }
  98. f = new LineString(new OGRLineString());
  99. }
  100.  
  101. Local<Value> points = LineStringPoints::New(info.This());
  102. Nan::SetPrivate(info.This(), Nan::New("points_").ToLocalChecked(), points);
  103.  
  104. f->Wrap(info.This());
  105. info.GetReturnValue().Set(info.This());
  106. }
  107.  
  108. Local<Value> LineString::New(OGRLineString *geom)
  109. {
  110. Nan::EscapableHandleScope scope;
  111. return scope.Escape(LineString::New(geom, true));
  112. }
  113.  
  114. Local<Value> LineString::New(OGRLineString *geom, bool owned)
  115. {
  116. Nan::EscapableHandleScope scope;
  117.  
  118. if (!geom) {
  119. return scope.Escape(Nan::Null());
  120. }
  121.  
  122. //make a copy of geometry owned by a feature
  123. // + no need to track when a feature is destroyed
  124. // + no need to throw errors when a method trys to modify an owned read-only geometry
  125. // - is slower
  126.  
  127. if (!owned) {
  128. geom = static_cast<OGRLineString*>(geom->clone());
  129. };
  130.  
  131. LineString *wrapped = new LineString(geom);
  132. wrapped->owned_ = true;
  133.  
  134. UPDATE_AMOUNT_OF_GEOMETRY_MEMORY(wrapped);
  135.  
  136. Local<Value> ext = Nan::New<External>(wrapped);
  137. Local<Object> obj = Nan::NewInstance(Nan::GetFunction(Nan::New(LineString::constructor)).ToLocalChecked(), 1, &ext).ToLocalChecked();
  138.  
  139. return scope.Escape(obj);
  140. }
  141.  
  142. NAN_METHOD(LineString::toString)
  143. {
  144. Nan::HandleScope scope;
  145. info.GetReturnValue().Set(Nan::New("LineString").ToLocalChecked());
  146. }
  147.  
  148. /**
  149. * Computes the length of the line string.
  150. *
  151. * @method getLength
  152. * @return Number
  153. */
  154. NODE_WRAPPED_METHOD_WITH_RESULT(LineString, getLength, Number, get_Length);
  155.  
  156. /**
  157. * Returns the point at the specified distance along the line string.
  158. *
  159. * @method value
  160. * @param {Number} distance
  161. * @return {gdal.Point}
  162. */
  163. NAN_METHOD(LineString::value)
  164. {
  165. Nan::HandleScope scope;
  166.  
  167. LineString *geom = Nan::ObjectWrap::Unwrap<LineString>(info.This());
  168.  
  169. OGRPoint *pt = new OGRPoint();
  170. double dist;
  171.  
  172. NODE_ARG_DOUBLE(0, "distance", dist);
  173.  
  174. geom->this_->Value(dist, pt);
  175.  
  176. info.GetReturnValue().Set(Point::New(pt));
  177. }
  178.  
  179. /**
  180. * Add a segment of another linestring to this one.
  181. *
  182. * Adds the request range of vertices to the end of this line string in an efficient manner. If the start index is larger than the end index then the vertices will be reversed as they are copied.
  183. *
  184. * @method addSubLineString
  185. * @param {gdal.LineString} line the other linestring
  186. * @param {int} [start=0] the first vertex to copy, defaults to 0 to start with the first vertex in the other linestring
  187. * @param {int} [end=-1] the last vertex to copy, defaults to -1 indicating the last vertex of the other linestring
  188. * @return {void}
  189. */
  190. NAN_METHOD(LineString::addSubLineString)
  191. {
  192. Nan::HandleScope scope;
  193.  
  194. LineString *geom = Nan::ObjectWrap::Unwrap<LineString>(info.This());
  195. LineString *other;
  196. int start = 0;
  197. int end = -1;
  198. NODE_ARG_WRAPPED(0, "line", LineString, other);
  199. NODE_ARG_INT_OPT(1, "start", start);
  200. NODE_ARG_INT_OPT(2, "end", end);
  201.  
  202. int n = other->get()->getNumPoints();
  203.  
  204. if(start < 0 || end < -1 || start >= n || end >= n) {
  205. Nan::ThrowRangeError("Invalid start or end index for linestring");
  206. return;
  207. }
  208.  
  209. geom->this_->addSubLineString(other->get(), start, end);
  210.  
  211. UPDATE_AMOUNT_OF_GEOMETRY_MEMORY(geom);
  212.  
  213. return;
  214. }
  215.  
  216. /**
  217. * Points that make up the line string.
  218. *
  219. * @attribute points
  220. * @type {gdal.LineStringPoints}
  221. */
  222. NAN_GETTER(LineString::pointsGetter)
  223. {
  224. Nan::HandleScope scope;
  225. info.GetReturnValue().Set(Nan::GetPrivate(info.This(), Nan::New("points_").ToLocalChecked()).ToLocalChecked());
  226. }
  227.  
  228. } // namespace node_gdal
  229.