UNPKG

@polar/plugin-draw

Version:

Draw plugin for POLAR that adds draw interactions to the map, allowing users to place various shapes and texts.

79 lines (72 loc) 2.02 kB
import { Feature, FeatureCollection, GeoJsonTypes, Geometry, GeometryCollection, } from 'geojson' type GeometryType = Exclude<Geometry, GeometryCollection> const isMulti = (type: GeometryType['type']) => type.startsWith('Multi') const multi = (type: GeometryType['type']) => (isMulti(type) ? type : `Multi${type}`) as | 'MultiPoint' | 'MultiLineString' | 'MultiPolygon' const mergeBin = (features: Feature<GeometryType>[]): Feature<GeometryType>[] => !features.length ? [] : [ { ...features[0], geometry: { type: multi(features[0].geometry.type), coordinates: [ ...features .map(({ geometry }) => isMulti(geometry.type) ? geometry.coordinates : [geometry.coordinates] ) .flat(1), ], }, } as Feature<GeometryType>, ] const getGeometryBin = (type: GeoJsonTypes) => type.endsWith('Point') ? 'points' : type.endsWith('LineString') ? 'lineStrings' : type.endsWith('Polygon') ? 'polygons' : '' export const mergeToMultiGeometries = ( featureCollection: FeatureCollection<GeometryType> ): FeatureCollection<GeometryType> => { const bins = featureCollection.features.reduce( (accumulator, current) => { const bin = getGeometryBin(current.geometry.type) if (bin) { accumulator[bin].push(current) } else { console.warn( `@polar/plugin-draw: Unsupported geometry input "${current.geometry.type}" in multi geometry merge skipped.` ) } return accumulator }, { points: [], lineStrings: [], polygons: [], } as Record<'points' | 'lineStrings' | 'polygons', Feature<GeometryType>[]> ) return { ...featureCollection, features: [ ...mergeBin(bins.points), ...mergeBin(bins.lineStrings), ...mergeBin(bins.polygons), ], } }