Leaflet OSM utilities

Hooks and pure helpers for querying OpenStreetMap data and performing geometric operations. Used by <OsmFeatureSubtract>, <OsmFeatureAdd>, and <LineStringInput snapToRoads>, and available for custom workflows.

useOverpass

React Query hook around the Overpass API.

import { useOverpass } from "@/components/leaflet";
 
const { data } = useOverpass(
  '[out:json];way["natural"="water"](48.85,2.3,48.9,2.4);out geom;',
);
OptionTypeDefaultDescription
endpointstringhttps://overpass-api.de/api/interpreterOverpass endpoint URL.
timeoutMsnumber30000Request timeout in ms.
signalAbortSignal-Abort signal forwarded to fetch.
enabledbooleantrueReact Query enabled flag.
staleTimenumber3600000 (1 h)React Query staleTime.

useOsmFeatures + OSM_PRESETS

Wraps useOverpass with a typed-preset query builder. Pass a bbox and a list of preset names; returns a parsed GeoJSON FeatureCollection of polygons (line features are buffered when the preset declares bufferLinesMeters).

import { useOsmFeatures } from "@/components/leaflet";
 
const { data } = useOsmFeatures([2.3, 48.85, 2.4, 48.9], ["water", "roads"]);

Bundled presets (OSM_PRESETS)

PresetOSM tags matchedNotes
waternatural=water,bay,strait and any waterwayPolygons (lakes, bays) + waterways.
buildingsbuilding=*All building footprints.
forestnatural=wood,forest and landuse=forestWooded patches.
roadshighway=motorway,trunk,primary,secondaryBuffered to 15 m polygons before set ops.
ParamTypeDescription
bboxGeoJSON.BBox | null[w, s, e, n]. Pass null to disable the query.
presetsReadonlyArray<OsmPresetName>One or more preset names from OSM_PRESETS.
opts{ endpoint?, enabled?, staleTime? }Optional overrides forwarded to useOverpass.

snapToRoadsOnce

Async helper that calls the public OSRM match endpoint to snap a LineString to OSM roads. Returns null if OSRM returns no match.

import { snapToRoadsOnce } from "@/components/leaflet";
 
const snapped = await snapToRoadsOnce(line, { profile: "driving" });
OptionTypeDefaultDescription
endpointstringhttps://router.project-osrm.orgOSRM base URL.
profile"driving" | "walking" | "cycling""driving"OSRM routing profile.

Geometry helpers (geometry-ops)

Pure turf wrappers exported from @/components/leaflet.

FunctionSignatureDescription
subtract(input, mask)(Polygon | MultiPolygon, FeatureCollection) => Polygon | MultiPolygon | nullSubtract a union-of-masks polygon collection from input.
unionAll(fc)(FeatureCollection<Polygon>) => Polygon | MultiPolygon | nullUnion every feature in a polygon FeatureCollection.
bboxOf(geom)(Geometry) => BBoxCompute the bbox [w, s, e, n] of any GeoJSON geometry.
areaM2(geom)(Polygon | MultiPolygon) => numberArea in square meters via turf's geodesic algorithm.