UNPKG

@cloudbase/js-sdk

Version:
110 lines (109 loc) 4.22 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Polygon = void 0; var symbol_1 = require("../helper/symbol"); var type_1 = require("../utils/type"); var lineString_1 = require("./lineString"); var Polygon = (function () { function Polygon(lines) { if (!(0, type_1.isArray)(lines)) { throw new TypeError("\"lines\" must be of type LineString[]. Received type ".concat(typeof lines)); } if (lines.length === 0) { throw new Error('Polygon must contain 1 linestring at least'); } lines.forEach(function (line) { if (!(line instanceof lineString_1.LineString)) { throw new TypeError("\"lines\" must be of type LineString[]. Received type ".concat(typeof line, "[]")); } if (!lineString_1.LineString.isClosed(line)) { throw new Error("LineString ".concat(line.points.map(function (p) { return p.toReadableString(); }), " is not a closed cycle")); } }); this.lines = lines; } Polygon.prototype.parse = function (key) { var _a; return _a = {}, _a[key] = { type: 'Polygon', coordinates: this.lines.map(function (line) { return line.points.map(function (point) { return [point.longitude, point.latitude]; }); }) }, _a; }; Polygon.prototype.toJSON = function () { return { type: 'Polygon', coordinates: this.lines.map(function (line) { return line.points.map(function (point) { return [point.longitude, point.latitude]; }); }) }; }; Polygon.validate = function (polygon) { var e_1, _a, e_2, _b; if (polygon.type !== 'Polygon' || !(0, type_1.isArray)(polygon.coordinates)) { return false; } try { for (var _c = __values(polygon.coordinates), _d = _c.next(); !_d.done; _d = _c.next()) { var line = _d.value; if (!this.isCloseLineString(line)) { return false; } try { for (var line_1 = (e_2 = void 0, __values(line)), line_1_1 = line_1.next(); !line_1_1.done; line_1_1 = line_1.next()) { var point = line_1_1.value; if (!(0, type_1.isNumber)(point[0]) || !(0, type_1.isNumber)(point[1])) { return false; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (line_1_1 && !line_1_1.done && (_b = line_1.return)) _b.call(line_1); } finally { if (e_2) throw e_2.error; } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_d && !_d.done && (_a = _c.return)) _a.call(_c); } finally { if (e_1) throw e_1.error; } } return true; }; Polygon.isCloseLineString = function (lineString) { var firstPoint = lineString[0]; var lastPoint = lineString[lineString.length - 1]; if (firstPoint[0] !== lastPoint[0] || firstPoint[1] !== lastPoint[1]) { return false; } return true; }; Object.defineProperty(Polygon.prototype, "_internalType", { get: function () { return symbol_1.SYMBOL_GEO_MULTI_POLYGON; }, enumerable: false, configurable: true }); return Polygon; }()); exports.Polygon = Polygon;