/** * Convert an array of items to GeoJSON FeatureCollection */ export function toGeoJSON( items: T[], getLngLat: (item: T) => [number, number], getProperties?: (item: T) => Record ): GeoJSON.FeatureCollection { return { type: 'FeatureCollection', features: items.map((item, index) => { const [lng, lat] = getLngLat(item) return { type: 'Feature', id: index, geometry: { type: 'Point', coordinates: [lng, lat], }, properties: getProperties ? getProperties(item) : {}, } }), } } /** * Convert GeoJSON FeatureCollection to array of items */ export function fromGeoJSON( fc: GeoJSON.FeatureCollection, transform: (feature: GeoJSON.Feature) => T ): T[] { return fc.features.map(transform) } /** * Create a GeoJSON Point */ export function createPoint( coordinates: [number, number], properties: Record = {} ): GeoJSON.Feature { return { type: 'Feature', geometry: { type: 'Point', coordinates, }, properties, } } /** * Create a GeoJSON Polygon */ export function createPolygon( coordinates: [number, number][], properties: Record = {} ): GeoJSON.Feature { // Ensure the polygon is closed const closed = [...coordinates] if ( coordinates.length > 0 && (coordinates[0][0] !== coordinates[coordinates.length - 1][0] || coordinates[0][1] !== coordinates[coordinates.length - 1][1]) ) { closed.push(coordinates[0]) } return { type: 'Feature', geometry: { type: 'Polygon', coordinates: [closed], }, properties, } } /** * Create a GeoJSON LineString */ export function createLineString( coordinates: [number, number][], properties: Record = {} ): GeoJSON.Feature { return { type: 'Feature', geometry: { type: 'LineString', coordinates, }, properties, } } /** * Create an empty FeatureCollection */ export function createFeatureCollection( features: GeoJSON.Feature[] = [] ): GeoJSON.FeatureCollection { return { type: 'FeatureCollection', features, } }