UNPKG

@trailstash/ultra

Version:

A web based tool for making MapLibre GL maps with data from sources such as Overpass, GeoJSON, GPX, KML, TCX, etc

63 lines (59 loc) 1.85 kB
import { setQueryBounds } from "../bounds.js"; import { layers, popupTemplate, popupContextBuilder } from "./osm.js"; const flattenPropertiesAndInferIdAndType = (geoJSON) => { // prety major assumption basked into this as Postpass executes arbitrary SQL so the returned // data could be anything. This assumes that the query returns un-renamed columns. geoJSON.features = geoJSON.features.map((f) => { const properties = {}; if (f.properties.osm_id) { properties["@id"] = f.properties.osm_id; properties["@type"] = f.properties.osm_type; } if (f.properties.tags) { for (const [k, v] of Object.entries(f.properties.tags)) { properties[k] = v; } } for (const [k, v] of Object.entries(f.properties)) { properties[k] = v; } return { ...f, properties, }; }); return geoJSON; }; const postpass = { source: async function (query, controller, { server, bounds }) { if (!server) { server = "https://postpass.geofabrik.de/api/0.2/interpreter"; } const resp = await fetch(server, { method: "POST", body: new URLSearchParams([["data", query]]), signal: controller.signal, headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json", }, }); if (!resp.ok) { throw new Error( `Postpass API returned ${resp.status}:\n${await resp.text()}`, ); } const data = await resp.json(); return { type: "geojson", data: flattenPropertiesAndInferIdAndType(data), attribution: '\u003Ca href="https://www.openstreetmap.org/copyright" target="_blank"\u003E© OpenStreetMap contributors\u003C/a\u003E', generateId: true, }; }, layers, popupTemplate, popupContextBuilder, }; export default postpass;