@trailstash/ultra
Version:
A web based tool for making MapLibre GL maps with data from sources such as Overpass, GeoJSON, GPX, KML, TCX, etc
97 lines (87 loc) • 2.06 kB
JavaScript
import EsriDump from "esri-dump";
import YAML from "yaml";
const layers = (source) => [
{
id: `${source}-geojson-fill`,
type: "fill",
source,
filter: ["==", ["geometry-type"], "Polygon"],
paint: {
"fill-color": "#555",
"fill-opacity": 0.3,
},
},
{
id: `${source}-geojson-fill-outline`,
type: "line",
source,
filter: ["==", ["geometry-type"], "Polygon"],
layout: { "line-cap": "round" },
paint: {
"line-color": "#555",
"line-width": 2,
"line-opacity": 1,
},
},
{
id: `${source}-geojson-line-2`,
type: "line",
source,
filter: ["==", ["geometry-type"], "LineString"],
layout: { "line-cap": "round" },
paint: {
"line-color": "#555",
"line-width": 2,
"line-opacity": 1,
},
},
{
id: `${source}-geojson-point`,
type: "circle",
source,
filter: ["==", ["geometry-type"], "Point"],
paint: {
"circle-stroke-width": 2,
"circle-stroke-color": "#555",
"circle-color": "rgba(85, 85, 85, 0.3)",
},
},
];
async function detect(query) {
const url = new URL(query);
return !!url.pathname.match(/\/(Map|Feature)Server\/\d+$/);
}
export default {
source: function geojson(query, controller, { server }) {
return new Promise((resolve, reject) => {
let esri;
if (server) {
const params = YAML.parse(query);
esri = new EsriDump(server, { params });
} else {
esri = new EsriDump(query);
}
const data = {
type: "FeatureCollection",
features: [],
};
esri.fetch();
esri.on("type", (type) => {
//Emitted before any data events
//emits one of
// - `MapServer'
// - `FeatureServer'
});
esri.on("feature", (feature) => {
data.features.push(feature);
});
esri.on("done", () => {
resolve({ type: "geojson", data, generateId: true });
});
esri.on("error", reject);
});
},
fitBounds: true,
layers,
detect,
};