@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
75 lines • 2.19 kB
JavaScript
import { featureCollection, polygon, bbox } from "@turf/turf";
import { isFeature } from "./geo.js";
import { v4 as uuid } from "uuid";
/** Helper to convert a Feature or a FeatureCollection to a Feature array */
export function toFeatureArray(input) {
if (isFeature(input)) {
return [input];
}
else {
return input.features;
}
}
export function toFeaturePolygonArray(input) {
if (isFeature(input)) {
return [input];
}
else {
return input.features;
}
}
/**
* Returns a Feature with given features geometry and properties. Reasonable defaults are given for properties not provided
* Default geometry is a square from 0,0 to 1,1
*/
export const genFeature = (options = {}) => {
const { feature = polygon([
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
],
]), name = `feature-${uuid()}`, id = uuid(), } = options;
return {
...feature,
id,
properties: {
id,
name,
},
bbox: feature.bbox
? feature.bbox
: feature.geometry
? bbox(feature.geometry)
: undefined,
};
};
/**
* Given array of features, return a feature collection with given properties.
* Generates reasonable default values for any properties not passed in
* The geometry type of the returned collection will match the one passed in
* Properties of features are retained
*/
export const genFeatureCollection = (features, options = {}) => {
const fcId = options.id || uuid();
const { name = `featureCollection-${fcId}` } = options;
return {
type: "FeatureCollection",
features: features.map((feat, index) => {
const fId = uuid();
return {
...feat,
id: fId,
properties: {
...feat.properties,
id: fId,
name: feat.properties?.name || `${name}-${index}`,
},
};
}),
bbox: bbox(featureCollection(features)),
};
};
//# sourceMappingURL=feature.js.map