UNPKG

@seasketch/geoprocessing

Version:

Geoprocessing and reporting framework for SeaSketch 2.0

204 lines • 9.1 kB
import fs from "fs-extra"; import path from "node:path"; import { isSketch, isSketchCollection, isPolygonFeature, isMultiPolygonFeature, isPolygonAnyFeature, isLineStringFeature, isPointFeature, isPolygonSketchCollection, isMultiPolygonSketchCollection, isPolygonAllSketchCollection, isLineStringSketchCollection, isPointSketchCollection, isFeature, } from "../../src/helpers/index.js"; /** * Reads all files from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName */ export async function getExampleSketchAll(partialName) { const sketches = []; if (fs.existsSync("examples/sketches")) { const filenames = await fs.readdir("examples/sketches"); await Promise.all(filenames .filter((fname) => /\.json/.test(fname)) .map(async (f) => { const sketch = await fs.readJSON(`examples/sketches/${f}`); sketches.push(sketch); })); } const filtered = sketches.filter((f) => partialName ? f.properties?.name.includes(partialName) : f); return filtered; } /** * Reads all Polygon sketch and sketch collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExamplePolygonSketchAll(partialName) { return (await getExampleSketchAll(partialName)).filter((s) => isPolygonFeature(s) || isPolygonSketchCollection(s)); } /** * Reads all MultiPolygon sketch and sketch collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExampleMultiPolygonSketchAll(partialName) { return (await getExampleSketchAll(partialName)).filter((s) => isMultiPolygonFeature(s) || isMultiPolygonSketchCollection(s)); } /** * Reads all polygon type (Polygon or MultiPolygon) sketch and sketch * collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExamplePolygonAllSketchAll(partialName) { return (await getExampleSketchAll(partialName)).filter((s) => isPolygonAnyFeature(s) || isPolygonAllSketchCollection(s)); } /** * Reads all LineString sketch and sketch collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExampleLineStringSketchAll(partialName) { return (await getExampleSketchAll(partialName)).filter((s) => isLineStringFeature(s) || isLineStringSketchCollection(s)); } /** * Reads all Point sketch and sketch collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExamplePointSketchAll(partialName) { return (await getExampleSketchAll(partialName)).filter((s) => isPointFeature(s) || isPointSketchCollection(s)); } /** * Reads all sketches from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName */ export async function getExampleSketches(partialName) { return (await getExampleSketchAll(partialName)).filter(isSketch); } /** * Reads all Polygon sketches from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExamplePolygonSketches(partialName) { return (await getExampleSketches(partialName)).filter(isPolygonFeature); } /** * Reads all MultiPolygon sketches from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExampleMultiPolygonSketches(partialName) { return (await getExampleSketches(partialName)).filter(isMultiPolygonFeature); } /** * Reads all Polygon or MultiPolygon sketches from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExamplePolygonAllSketches(partialName) { return (await getExampleSketches(partialName)).filter(isPolygonAnyFeature); } /** * Reads all Linestring sketches from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExampleLineStringSketches(partialName) { return (await getExampleSketches(partialName)).filter(isLineStringFeature); } /** * Reads all Point sketches from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExamplePointSketches(partialName) { return (await getExampleSketches(partialName)).filter(isPointFeature); } /** * Reads all sketche collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName */ export async function getExampleSketchCollections(partialName) { return (await getExampleSketchAll(partialName)).filter(isSketchCollection); } /** * Reads all Polygon sketch collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExamplePolygonSketchCollections(partialName) { return (await getExampleSketchCollections(partialName)).filter(isPolygonSketchCollection); } /** * Reads all Linestring sketch collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExampleLineStringSketchCollections(partialName) { return (await getExampleSketchCollections(partialName)).filter(isLineStringSketchCollection); } /** * Reads all Point sketch collections from examples/sketches for testing. Run from project root * Optionally filters out those that don't match partialName * TODO: remove cast if possible */ export async function getExamplePointSketchCollections(partialName) { return (await getExampleSketchCollections(partialName)).filter(isPointSketchCollection); } /** * Convenience function returns object with sketches keyed by name * Optionally filters out those that don't match partialName * @deprecated use partialName support in getExample*Sketches(partialName) functions */ export async function getExampleSketchesByName(partialName) { const sketches = await getExampleSketches(partialName); return sketches.reduce((sketchObject, s) => { return { ...sketchObject, [s.properties.name]: s, }; }, {}); } /** * Reads features and featurecollections from examples/features for testing. Run from project root * Optionally filters out those that don't match partialName */ export async function getExampleFeaturesAll(partialName) { const features = []; if (fs.existsSync("examples/features")) { const filenames = await fs.readdir("examples/features"); await Promise.all(filenames .filter((fname) => /\.json/.test(fname)) .map(async (f) => { const feature = await fs.readJSON(`examples/features/${f}`); feature.properties = feature.properties || {}; feature.properties.name = feature.properties.name || path.basename(f); features.push(feature); })); } const filtered = features.filter((f) => partialName ? f.properties?.name.includes(partialName) : f); return filtered; } /** * Reads features of all types from examples/features for testing. Run from project root * Optionally filters out those that don't match partialName */ export async function getExampleFeatures(partialName) { return (await getExampleFeaturesAll(partialName)).filter(isFeature); } /** * Reads features of all types from examples/features for testing. Run from project root * Optionally filters out those that don't match partialName */ export async function getExamplePolygonFeatures(partialName) { return (await getExampleFeaturesAll(partialName)).filter(isPolygonFeature); } /** * Convenience function returns object with features keyed by their filename. Features without a name will not be included * Optionally filters out those that don't match partialName */ export async function getExampleFeaturesByName(partialName) { const features = await getExampleFeatures(partialName); return features.reduce((featureMap, f) => { return f.properties?.name ? { ...featureMap, [f.properties.name]: f, } : featureMap; }, {}); } //# sourceMappingURL=getExamples.js.map