point.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. module.exports = Point;
  2. var util = require('util');
  3. var Geometry = require('./geometry');
  4. var Types = require('./types');
  5. var BinaryWriter = require('./binarywriter');
  6. var ZigZag = require('./zigzag.js');
  7. function Point(x, y, z, m, srid) {
  8. Geometry.call(this);
  9. this.x = x;
  10. this.y = y;
  11. this.z = z;
  12. this.m = m;
  13. this.srid = srid;
  14. this.hasZ = typeof this.z !== 'undefined';
  15. this.hasM = typeof this.m !== 'undefined';
  16. }
  17. util.inherits(Point, Geometry);
  18. Point.Z = function (x, y, z, srid) {
  19. var point = new Point(x, y, z, undefined, srid);
  20. point.hasZ = true;
  21. return point;
  22. };
  23. Point.M = function (x, y, m, srid) {
  24. var point = new Point(x, y, undefined, m, srid);
  25. point.hasM = true;
  26. return point;
  27. };
  28. Point.ZM = function (x, y, z, m, srid) {
  29. var point = new Point(x, y, z, m, srid);
  30. point.hasZ = true;
  31. point.hasM = true;
  32. return point;
  33. };
  34. Point._parseWkt = function (value, options) {
  35. var point = new Point();
  36. point.srid = options.srid;
  37. point.hasZ = options.hasZ;
  38. point.hasM = options.hasM;
  39. if (value.isMatch(['EMPTY']))
  40. return point;
  41. value.expectGroupStart();
  42. var coordinate = value.matchCoordinate(options);
  43. point.x = coordinate.x;
  44. point.y = coordinate.y;
  45. point.z = coordinate.z;
  46. point.m = coordinate.m;
  47. value.expectGroupEnd();
  48. return point;
  49. };
  50. Point._parseWkb = function (value, options) {
  51. var point = Point._readWkbPoint(value, options);
  52. point.srid = options.srid;
  53. return point;
  54. };
  55. Point._readWkbPoint = function (value, options) {
  56. return new Point(value.readDouble(), value.readDouble(),
  57. options.hasZ ? value.readDouble() : undefined,
  58. options.hasM ? value.readDouble() : undefined);
  59. };
  60. Point._parseTwkb = function (value, options) {
  61. var point = new Point();
  62. point.hasZ = options.hasZ;
  63. point.hasM = options.hasM;
  64. if (options.isEmpty)
  65. return point;
  66. point.x = ZigZag.decode(value.readVarInt()) / options.precisionFactor;
  67. point.y = ZigZag.decode(value.readVarInt()) / options.precisionFactor;
  68. point.z = options.hasZ ? ZigZag.decode(value.readVarInt()) / options.zPrecisionFactor : undefined;
  69. point.m = options.hasM ? ZigZag.decode(value.readVarInt()) / options.mPrecisionFactor : undefined;
  70. return point;
  71. };
  72. Point._readTwkbPoint = function (value, options, previousPoint) {
  73. previousPoint.x += ZigZag.decode(value.readVarInt()) / options.precisionFactor;
  74. previousPoint.y += ZigZag.decode(value.readVarInt()) / options.precisionFactor;
  75. if (options.hasZ)
  76. previousPoint.z += ZigZag.decode(value.readVarInt()) / options.zPrecisionFactor;
  77. if (options.hasM)
  78. previousPoint.m += ZigZag.decode(value.readVarInt()) / options.mPrecisionFactor;
  79. return new Point(previousPoint.x, previousPoint.y, previousPoint.z, previousPoint.m);
  80. };
  81. Point._parseGeoJSON = function (value) {
  82. return Point._readGeoJSONPoint(value.coordinates);
  83. };
  84. Point._readGeoJSONPoint = function (coordinates) {
  85. if (coordinates.length === 0)
  86. return new Point();
  87. if (coordinates.length > 2)
  88. return new Point(coordinates[0], coordinates[1], coordinates[2]);
  89. return new Point(coordinates[0], coordinates[1]);
  90. };
  91. Point.prototype.toWkt = function () {
  92. if (typeof this.x === 'undefined' && typeof this.y === 'undefined' &&
  93. typeof this.z === 'undefined' && typeof this.m === 'undefined')
  94. return this._getWktType(Types.wkt.Point, true);
  95. return this._getWktType(Types.wkt.Point, false) + '(' + this._getWktCoordinate(this) + ')';
  96. };
  97. Point.prototype.toWkb = function (parentOptions) {
  98. var wkb = new BinaryWriter(this._getWkbSize());
  99. wkb.writeInt8(1);
  100. this._writeWkbType(wkb, Types.wkb.Point, parentOptions);
  101. if (typeof this.x === 'undefined' && typeof this.y === 'undefined') {
  102. wkb.writeDoubleLE(NaN);
  103. wkb.writeDoubleLE(NaN);
  104. if (this.hasZ)
  105. wkb.writeDoubleLE(NaN);
  106. if (this.hasM)
  107. wkb.writeDoubleLE(NaN);
  108. }
  109. else {
  110. this._writeWkbPoint(wkb);
  111. }
  112. return wkb.buffer;
  113. };
  114. Point.prototype._writeWkbPoint = function (wkb) {
  115. wkb.writeDoubleLE(this.x);
  116. wkb.writeDoubleLE(this.y);
  117. if (this.hasZ)
  118. wkb.writeDoubleLE(this.z);
  119. if (this.hasM)
  120. wkb.writeDoubleLE(this.m);
  121. };
  122. Point.prototype.toTwkb = function () {
  123. var twkb = new BinaryWriter(0, true);
  124. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  125. var isEmpty = typeof this.x === 'undefined' && typeof this.y === 'undefined';
  126. this._writeTwkbHeader(twkb, Types.wkb.Point, precision, isEmpty);
  127. if (!isEmpty)
  128. this._writeTwkbPoint(twkb, precision, new Point(0, 0, 0, 0));
  129. return twkb.buffer;
  130. };
  131. Point.prototype._writeTwkbPoint = function (twkb, precision, previousPoint) {
  132. var x = this.x * precision.xyFactor;
  133. var y = this.y * precision.xyFactor;
  134. var z = this.z * precision.zFactor;
  135. var m = this.m * precision.mFactor;
  136. twkb.writeVarInt(ZigZag.encode(x - previousPoint.x));
  137. twkb.writeVarInt(ZigZag.encode(y - previousPoint.y));
  138. if (this.hasZ)
  139. twkb.writeVarInt(ZigZag.encode(z - previousPoint.z));
  140. if (this.hasM)
  141. twkb.writeVarInt(ZigZag.encode(m - previousPoint.m));
  142. previousPoint.x = x;
  143. previousPoint.y = y;
  144. previousPoint.z = z;
  145. previousPoint.m = m;
  146. };
  147. Point.prototype._getWkbSize = function () {
  148. var size = 1 + 4 + 8 + 8;
  149. if (this.hasZ)
  150. size += 8;
  151. if (this.hasM)
  152. size += 8;
  153. return size;
  154. };
  155. Point.prototype.toGeoJSON = function (options) {
  156. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  157. geoJSON.type = Types.geoJSON.Point;
  158. if (typeof this.x === 'undefined' && typeof this.y === 'undefined')
  159. geoJSON.coordinates = [];
  160. else if (typeof this.z !== 'undefined')
  161. geoJSON.coordinates = [this.x, this.y, this.z];
  162. else
  163. geoJSON.coordinates = [this.x, this.y];
  164. return geoJSON;
  165. };