lineString.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const symbol_1 = require("../helper/symbol");
  4. const point_1 = require("./point");
  5. const type_1 = require("../utils/type");
  6. class LineString {
  7. constructor(points) {
  8. if (!type_1.isArray(points)) {
  9. throw new TypeError(`"points" must be of type Point[]. Received type ${typeof points}`);
  10. }
  11. if (points.length < 2) {
  12. throw new Error('"points" must contain 2 points at least');
  13. }
  14. points.forEach(point => {
  15. if (!(point instanceof point_1.Point)) {
  16. throw new TypeError(`"points" must be of type Point[]. Received type ${typeof point}[]`);
  17. }
  18. });
  19. this.points = points;
  20. }
  21. parse(key) {
  22. return {
  23. [key]: {
  24. type: 'LineString',
  25. coordinates: this.points.map(point => point.toJSON().coordinates)
  26. }
  27. };
  28. }
  29. toJSON() {
  30. return {
  31. type: 'LineString',
  32. coordinates: this.points.map(point => point.toJSON().coordinates)
  33. };
  34. }
  35. static validate(lineString) {
  36. if (lineString.type !== 'LineString' || !type_1.isArray(lineString.coordinates)) {
  37. return false;
  38. }
  39. for (let point of lineString.coordinates) {
  40. if (!type_1.isNumber(point[0]) || !type_1.isNumber(point[1])) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. static isClosed(lineString) {
  47. const firstPoint = lineString.points[0];
  48. const lastPoint = lineString.points[lineString.points.length - 1];
  49. if (firstPoint.latitude === lastPoint.latitude && firstPoint.longitude === lastPoint.longitude) {
  50. return true;
  51. }
  52. }
  53. get _internalType() {
  54. return symbol_1.SYMBOL_GEO_LINE_STRING;
  55. }
  56. }
  57. exports.LineString = LineString;