UNPKG

terriajs

Version:

Geospatial data visualization platform.

105 lines 3.65 kB
import { feature, featureCollection } from "@turf/helpers"; import { isJsonObject, isJsonString } from "./Json"; // Note: these type checks are not that rigorous, we are assuming we are getting valid GeoJson objects export function isFeatureCollection(json) { return (isJsonObject(json, false) && json.type === "FeatureCollection" && Array.isArray(json.features)); } export function isFeature(json) { return (isJsonObject(json, false) && json.type === "Feature" && !!json.geometry); } export function isPoint(json) { return (isJsonObject(json, false) && json.type === "Feature" && isJsonObject(json.geometry, false) && json.geometry.type === "Point"); } export function isMultiPoint(json) { return (isJsonObject(json, false) && json.type === "Feature" && isJsonObject(json.geometry, false) && json.geometry.type === "MultiPoint"); } export function isLine(json) { return (isJsonObject(json, false) && json.type === "Feature" && isJsonObject(json.geometry, false) && json.geometry.type === "LineString"); } export function isMultiLineString(json) { return (isJsonObject(json, false) && json.type === "Feature" && isJsonObject(json.geometry, false) && json.geometry.type === "MultiLineString"); } export function isPolygon(json) { return (isJsonObject(json, false) && json.type === "Feature" && isJsonObject(json.geometry, false) && json.geometry.type === "Polygon"); } export function isMultiPolygon(json) { return (isJsonObject(json, false) && json.type === "Feature" && isJsonObject(json.geometry, false) && json.geometry.type === "MultiPolygon"); } export function isGeometryCollection(json) { return (isJsonObject(json, false) && json.type === "Feature" && isJsonObject(json.geometry, false) && json.geometry.type === "GeometryCollection"); } export function isGeometries(json) { return (isJsonObject(json, false) && isJsonString(json.type) && [ "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon" ].includes(json.type) && Array.isArray(json.coordinates)); } /** * Returns the points in a MultiPoint as separate Point features. */ export function explodeMultiPoint(feature) { return feature.geometry?.type === "MultiPoint" ? feature.geometry.coordinates.map((coordinates) => ({ type: "Feature", geometry: { type: "Point", coordinates }, properties: feature.properties })) : []; } export function toFeatureCollection(json) { if (isFeatureCollection(json)) return json; // It's already a feature collection, do nothing if (isFeature(json)) { // Move CRS data from Feature to FeatureCollection if ("crs" in json && isJsonObject(json.crs)) { const crs = json.crs; delete json.crs; const fc = featureCollection([json]); fc.crs = crs; return fc; } return featureCollection([json]); } if (isGeometries(json)) return featureCollection([feature(json)]); if (Array.isArray(json) && json.every((item) => isFeature(item))) { return featureCollection(json); } if (Array.isArray(json) && json.every((item) => isGeometries(item))) { return featureCollection(json.map((item) => feature(item))); } } //# sourceMappingURL=GeoJson.js.map