multipoint.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. module.exports = MultiPoint;
  2. var util = require('util');
  3. var Types = require('./types');
  4. var Geometry = require('./geometry');
  5. var Point = require('./point');
  6. var BinaryWriter = require('./binarywriter');
  7. function MultiPoint(points, srid) {
  8. Geometry.call(this);
  9. this.points = points || [];
  10. this.srid = srid;
  11. if (this.points.length > 0) {
  12. this.hasZ = this.points[0].hasZ;
  13. this.hasM = this.points[0].hasM;
  14. }
  15. }
  16. util.inherits(MultiPoint, Geometry);
  17. MultiPoint.Z = function (points, srid) {
  18. var multiPoint = new MultiPoint(points, srid);
  19. multiPoint.hasZ = true;
  20. return multiPoint;
  21. };
  22. MultiPoint.M = function (points, srid) {
  23. var multiPoint = new MultiPoint(points, srid);
  24. multiPoint.hasM = true;
  25. return multiPoint;
  26. };
  27. MultiPoint.ZM = function (points, srid) {
  28. var multiPoint = new MultiPoint(points, srid);
  29. multiPoint.hasZ = true;
  30. multiPoint.hasM = true;
  31. return multiPoint;
  32. };
  33. MultiPoint._parseWkt = function (value, options) {
  34. var multiPoint = new MultiPoint();
  35. multiPoint.srid = options.srid;
  36. multiPoint.hasZ = options.hasZ;
  37. multiPoint.hasM = options.hasM;
  38. if (value.isMatch(['EMPTY']))
  39. return multiPoint;
  40. value.expectGroupStart();
  41. multiPoint.points.push.apply(multiPoint.points, value.matchCoordinates(options));
  42. value.expectGroupEnd();
  43. return multiPoint;
  44. };
  45. MultiPoint._parseWkb = function (value, options) {
  46. var multiPoint = new MultiPoint();
  47. multiPoint.srid = options.srid;
  48. multiPoint.hasZ = options.hasZ;
  49. multiPoint.hasM = options.hasM;
  50. var pointCount = value.readUInt32();
  51. for (var i = 0; i < pointCount; i++)
  52. multiPoint.points.push(Geometry.parse(value, options));
  53. return multiPoint;
  54. };
  55. MultiPoint._parseTwkb = function (value, options) {
  56. var multiPoint = new MultiPoint();
  57. multiPoint.hasZ = options.hasZ;
  58. multiPoint.hasM = options.hasM;
  59. if (options.isEmpty)
  60. return multiPoint;
  61. var previousPoint = new Point(0, 0, options.hasZ ? 0 : undefined, options.hasM ? 0 : undefined);
  62. var pointCount = value.readVarInt();
  63. for (var i = 0; i < pointCount; i++)
  64. multiPoint.points.push(Point._readTwkbPoint(value, options, previousPoint));
  65. return multiPoint;
  66. };
  67. MultiPoint._parseGeoJSON = function (value) {
  68. var multiPoint = new MultiPoint();
  69. if (value.coordinates.length > 0)
  70. multiPoint.hasZ = value.coordinates[0].length > 2;
  71. for (var i = 0; i < value.coordinates.length; i++)
  72. multiPoint.points.push(Point._parseGeoJSON({ coordinates: value.coordinates[i] }));
  73. return multiPoint;
  74. };
  75. MultiPoint.prototype.toWkt = function () {
  76. if (this.points.length === 0)
  77. return this._getWktType(Types.wkt.MultiPoint, true);
  78. var wkt = this._getWktType(Types.wkt.MultiPoint, false) + '(';
  79. for (var i = 0; i < this.points.length; i++)
  80. wkt += this._getWktCoordinate(this.points[i]) + ',';
  81. wkt = wkt.slice(0, -1);
  82. wkt += ')';
  83. return wkt;
  84. };
  85. MultiPoint.prototype.toWkb = function () {
  86. var wkb = new BinaryWriter(this._getWkbSize());
  87. wkb.writeInt8(1);
  88. this._writeWkbType(wkb, Types.wkb.MultiPoint);
  89. wkb.writeUInt32LE(this.points.length);
  90. for (var i = 0; i < this.points.length; i++)
  91. wkb.writeBuffer(this.points[i].toWkb({ srid: this.srid }));
  92. return wkb.buffer;
  93. };
  94. MultiPoint.prototype.toTwkb = function () {
  95. var twkb = new BinaryWriter(0, true);
  96. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  97. var isEmpty = this.points.length === 0;
  98. this._writeTwkbHeader(twkb, Types.wkb.MultiPoint, precision, isEmpty);
  99. if (this.points.length > 0) {
  100. twkb.writeVarInt(this.points.length);
  101. var previousPoint = new Point(0, 0, 0, 0);
  102. for (var i = 0; i < this.points.length; i++)
  103. this.points[i]._writeTwkbPoint(twkb, precision, previousPoint);
  104. }
  105. return twkb.buffer;
  106. };
  107. MultiPoint.prototype._getWkbSize = function () {
  108. var coordinateSize = 16;
  109. if (this.hasZ)
  110. coordinateSize += 8;
  111. if (this.hasM)
  112. coordinateSize += 8;
  113. coordinateSize += 5;
  114. return 1 + 4 + 4 + (this.points.length * coordinateSize);
  115. };
  116. MultiPoint.prototype.toGeoJSON = function (options) {
  117. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  118. geoJSON.type = Types.geoJSON.MultiPoint;
  119. geoJSON.coordinates = [];
  120. for (var i = 0; i < this.points.length; i++)
  121. geoJSON.coordinates.push(this.points[i].toGeoJSON().coordinates);
  122. return geoJSON;
  123. };