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

47 lines (44 loc) 1.3 kB
import { fetchIfHTTP } from "./util.js"; import csv2geojsonPkg from "csv2geojson"; const { csv2geojson } = csv2geojsonPkg; import { geoJsonLayers as layers } from "./common.js"; const detect = async (query) => { if (query.startsWith("https://") && query.endsWith(".tsv")) { return true; } if (query.startsWith("https://") && query.endsWith(".csv")) { return true; } if (query.startsWith("https://docs.google.com/spreadsheets/")) { return true; } }; export default { source: async function (query) { if (query.startsWith("https://docs.google.com/spreadsheets/")) { const u = new URL(query); if ( u.pathname.endsWith("/edit") && u.pathname.startsWith("/spreadsheets/") ) { const sp = new URLSearchParams(u.search); sp.set("tqx", "out:csv"); query = u.origin + u.pathname.replace(/edit$/, "gviz/tq") + "?" + sp.toString(); } } query = await fetchIfHTTP(query); const data = await new Promise((resolve, reject) => csv2geojson(query, { delimiter: "auto" }, (error, result) => error ? reject(error.message) : resolve(result), ), ); return { type: "geojson", data, generateId: true }; }, detect, layers, fitBounds: true, };