UNPKG

geospatialdraw

Version:
114 lines 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var turf_1 = require("@turf/turf"); var GeoJSON_1 = require("ol/format/GeoJSON"); /** * Detects shapes of GeometryJSON objects by evaluating their geometric contents */ var ShapeDetector = /** @class */ (function () { /** * Constructs an instance of the ShapeDetector */ function ShapeDetector() { this.geoFormat = new GeoJSON_1.default(); } /** * Gets the shape of GeometryJSON object * @param geoJSON - GeometryJSON object * @returns Shape of geometry */ ShapeDetector.prototype.shapeFromGeoJSON = function (geoJSON) { var feature = this.geoFormat.readFeature(geoJSON); return this.shapeFromFeature(feature); }; /** * Gets the shape of an Open Layers feature * @param feature - Open Layers feature * @returns Shape of geometry */ ShapeDetector.prototype.shapeFromFeature = function (feature) { if (this.isLineFeature(feature)) { return 'Line'; } else if (this.isBoundingBoxFeature(feature)) { return 'Bounding Box'; } else if (this.isPointFeature(feature)) { return 'Point'; } else if (this.isPointRadiusFeature(feature)) { return 'Point Radius'; } else { return 'Polygon'; } }; /** * Checks if feature matches shape * @param feature - Open Layers feature * @returns true if geometry is a line */ ShapeDetector.prototype.isLineFeature = function (feature) { var _a; return ((_a = feature.getGeometry()) === null || _a === void 0 ? void 0 : _a.getType()) === 'LineString'; }; /** * Checks if feature matches shape * @param feature - Open Layers feature * @returns true if geometry is a point */ ShapeDetector.prototype.isPointFeature = function (feature) { var _a; return (((_a = feature.getGeometry()) === null || _a === void 0 ? void 0 : _a.getType()) === 'Point' && (feature.get('buffer') === undefined || feature.get('buffer') <= 0)); }; /** * Checks if feature matches shape * @param feature - Open Layers feature * @returns true if geometry is a bounding box */ ShapeDetector.prototype.isBoundingBoxFeature = function (feature) { var _a; if (!this.isPolygonFeature(feature)) { return false; } else { var coordinates_1 = feature.getGeometry().getCoordinates()[0]; var extent = (_a = feature.getGeometry()) === null || _a === void 0 ? void 0 : _a.getExtent(); if (!extent) { return false; } var expectedCoordinates = (0, turf_1.bboxPolygon)(extent).geometry .coordinates[0]; return (coordinates_1.length === 5 && expectedCoordinates.every(function (expectedPoint) { return coordinates_1.some(function (point) { return point[0] === expectedPoint[0] && point[1] === expectedPoint[1]; }); })); } }; /** * Checks if feature matches shape * @param feature - Open Layers feature * @returns true if geometry is a point radius */ ShapeDetector.prototype.isPointRadiusFeature = function (feature) { var _a; return (((_a = feature.getGeometry()) === null || _a === void 0 ? void 0 : _a.getType()) === 'Point' && feature.get('buffer') && feature.get('buffer') > 0); }; /** * Checks if feature matches shape * @param feature - Open Layers feature * @returns true if geometry is a polygon */ ShapeDetector.prototype.isPolygonFeature = function (feature) { var _a; return ((_a = feature.getGeometry()) === null || _a === void 0 ? void 0 : _a.getType()) === 'Polygon'; }; return ShapeDetector; }()); exports.default = ShapeDetector; //# sourceMappingURL=shape-detector.js.map