geometrycollection.js 4.9 KB

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