linestring.js 4.8 KB

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