12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const symbol_1 = require("../helper/symbol");
- const type_1 = require("../utils/type");
- const lineString_1 = require("./lineString");
- class Polygon {
- constructor(lines) {
- if (!type_1.isArray(lines)) {
- throw new TypeError(`"lines" must be of type LineString[]. Received type ${typeof lines}`);
- }
- if (lines.length === 0) {
- throw new Error('Polygon must contain 1 linestring at least');
- }
- lines.forEach(line => {
- if (!(line instanceof lineString_1.LineString)) {
- throw new TypeError(`"lines" must be of type LineString[]. Received type ${typeof line}[]`);
- }
- if (!lineString_1.LineString.isClosed(line)) {
- throw new Error(`LineString ${line.points.map(p => p.toReadableString())} is not a closed cycle`);
- }
- });
- this.lines = lines;
- }
- parse(key) {
- return {
- [key]: {
- type: 'Polygon',
- coordinates: this.lines.map(line => {
- return line.points.map(point => [point.longitude, point.latitude]);
- })
- }
- };
- }
- toJSON() {
- return {
- type: 'Polygon',
- coordinates: this.lines.map(line => {
- return line.points.map(point => [point.longitude, point.latitude]);
- })
- };
- }
- static validate(polygon) {
- if (polygon.type !== 'Polygon' || !type_1.isArray(polygon.coordinates)) {
- return false;
- }
- for (let line of polygon.coordinates) {
- if (!this.isCloseLineString(line)) {
- return false;
- }
- for (let point of line) {
- if (!type_1.isNumber(point[0]) || !type_1.isNumber(point[1])) {
- return false;
- }
- }
- }
- return true;
- }
- static isCloseLineString(lineString) {
- const firstPoint = lineString[0];
- const lastPoint = lineString[lineString.length - 1];
- if (firstPoint[0] !== lastPoint[0] || firstPoint[1] !== lastPoint[1]) {
- return false;
- }
- return true;
- }
- get _internalType() {
- return symbol_1.SYMBOL_GEO_POLYGON;
- }
- }
- exports.Polygon = Polygon;
|