multiLineString.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { SYMBOL_GEO_MULTI_LINE_STRING } from '../helper/symbol';
  2. import { isArray, isNumber } from '../utils/type';
  3. import { LineString } from './lineString';
  4. export class MultiLineString {
  5. constructor(lines) {
  6. if (!isArray(lines)) {
  7. throw new TypeError(`"lines" must be of type LineString[]. Received type ${typeof lines}`);
  8. }
  9. if (lines.length === 0) {
  10. throw new Error('Polygon must contain 1 linestring at least');
  11. }
  12. lines.forEach(line => {
  13. if (!(line instanceof LineString)) {
  14. throw new TypeError(`"lines" must be of type LineString[]. Received type ${typeof line}[]`);
  15. }
  16. });
  17. this.lines = lines;
  18. }
  19. parse(key) {
  20. return {
  21. [key]: {
  22. type: 'MultiLineString',
  23. coordinates: this.lines.map(line => {
  24. return line.points.map(point => [point.longitude, point.latitude]);
  25. })
  26. }
  27. };
  28. }
  29. toJSON() {
  30. return {
  31. type: 'MultiLineString',
  32. coordinates: this.lines.map(line => {
  33. return line.points.map(point => [point.longitude, point.latitude]);
  34. })
  35. };
  36. }
  37. static validate(multiLineString) {
  38. if (multiLineString.type !== 'MultiLineString' || !isArray(multiLineString.coordinates)) {
  39. return false;
  40. }
  41. for (let line of multiLineString.coordinates) {
  42. for (let point of line) {
  43. if (!isNumber(point[0]) || !isNumber(point[1])) {
  44. return false;
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. get _internalType() {
  51. return SYMBOL_GEO_MULTI_LINE_STRING;
  52. }
  53. }