@cloudbase/js-sdk
Version:
cloudbase javascript sdk
107 lines (106 loc) • 4.06 kB
JavaScript
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.");
};
import { SYMBOL_GEO_MULTI_POLYGON } from '../helper/symbol';
import { isArray, isNumber } from '../utils/type';
import { LineString } from './lineString';
var Polygon = (function () {
function Polygon(lines) {
if (!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)) {
throw new TypeError("\"lines\" must be of type LineString[]. Received type ".concat(typeof line, "[]"));
}
if (!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' || !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 (!isNumber(point[0]) || !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_GEO_MULTI_POLYGON;
},
enumerable: false,
configurable: true
});
return Polygon;
}());
export { Polygon };