multilinestring.js 5.6 KB

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