@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
77 lines • 3.18 kB
JavaScript
import { hasOwnProperty } from "./native.js";
/**
* Check if object is a Feature. Any code inside a block guarded by a conditional call to this function will have type narrowed to Feature
*/
export function isGeometry(geometry) {
return (hasOwnProperty(geometry, "geometry") &&
hasOwnProperty(geometry, "properties") &&
hasOwnProperty(geometry, "type") &&
geometry.type !== "Feature");
}
/**
* Check if object is a Feature. Any code inside a block guarded by a conditional call to this function will have type narrowed
*/
export function isFeature(feature) {
return (hasOwnProperty(feature, "properties") &&
hasOwnProperty(feature, "type") &&
feature.type === "Feature");
}
/**
* Check if object is a Polygon feature. Any code inside a block guarded by a conditional call to this function will have type narrowed
*/
export function isPolygonFeature(feature) {
return isFeature(feature) && feature.geometry.type === "Polygon";
}
/**
* Check if object is an array of Polygon features. Any code inside a block guarded by a conditional call to this function will have type narrowed
*/
export function isPolygonFeatureArray(featureArray) {
return (Array.isArray(featureArray) &&
featureArray.reduce((last, feat) => {
return last && isFeature(feat);
}, true));
}
/**
* Check if object is a MultiPolygon. Any code inside a block guarded by a conditional call to this function will have type narrowed
*/
export function isMultiPolygonFeature(feature) {
return isFeature(feature) && feature.geometry.type === "MultiPolygon";
}
/**
* Check if object is a Polygon or MultiPolygon. Any code inside a block guarded by a conditional call to this function will have type narrowed
*/
export function isPolygonAnyFeature(feature) {
return (isFeature(feature) &&
(isPolygonFeature(feature) || isMultiPolygonFeature(feature)));
}
/**
* Check if object is a Linestring. Any code inside a block guarded by a conditional call to this function will have type narrowed
*/
export function isLineStringFeature(feature) {
return isFeature(feature) && feature.geometry.type === "LineString";
}
/**
* Check if object is a Point. Any code inside a block guarded by a conditional call to this function will have type narrowed
*/
export function isPointFeature(feature) {
return isFeature(feature) && feature.geometry.type === "Point";
}
/**
* Check if object is a Feature Collection. Any code inside a block guarded by a conditional call to this function will have type narrowed
*/
export function isFeatureCollection(feature) {
return (hasOwnProperty(feature, "type") &&
hasOwnProperty(feature, "features") &&
feature.type === "FeatureCollection");
}
// Verifies that features in collection are all of the specified type
export const collectionHasGeometry = (collection,
/** one or more geometry types */
g) => {
const gTypes = Array.isArray(g) ? g : [g];
return collection.features.reduce((acc, f) => acc &&
!!f.geometry &&
!!f.geometry.type &&
gTypes.includes(f.geometry.type), true);
};
//# sourceMappingURL=geo.js.map