30 lines
984 B
TypeScript
30 lines
984 B
TypeScript
import type { FeatureCollection } from "geojson"
|
|
import type { Node } from "~/types"
|
|
|
|
/**
|
|
* Project located nodes onto a GeoJSON `FeatureCollection` for the map source.
|
|
*
|
|
* Pure (no map dependency) so it stays trivially testable. Nodes whose location
|
|
* isn't geocoded are dropped (the synthetic root has no location, so it never
|
|
* produces a pin).
|
|
*/
|
|
export function nodesToFeatureCollection(nodes: Node[]): FeatureCollection {
|
|
return {
|
|
type: "FeatureCollection",
|
|
features: nodes
|
|
.map((node) => ({ node, location: node.event.location }))
|
|
.filter(
|
|
({ location }) =>
|
|
location?.longitude != null && location?.latitude != null,
|
|
)
|
|
.map(({ node, location }) => ({
|
|
type: "Feature",
|
|
// GeoJSON order is [lng, lat].
|
|
geometry: {
|
|
type: "Point",
|
|
coordinates: [location!.longitude!, location!.latitude!],
|
|
},
|
|
properties: { id: node.id, title: node.event.title },
|
|
})),
|
|
}
|
|
}
|