Skip to content

Commit

Permalink
Read embedded GeoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Jun 20, 2024
1 parent 14594b6 commit 0b37b80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
13 changes: 11 additions & 2 deletions OverpassVectorLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Vector as VectorLayer } from "ol/layer";
import { Vector as VectorSource } from "ol/source";
import { type Rule, evaluateStyle, parseMapCSS } from "./mapcss";
import { Geometry } from "ol/geom";
import GeoJSON from "ol/format/GeoJSON";
import OSMXML from "./OSMXML";
import { splitQuerySubpart } from "./overpass";

Expand All @@ -12,16 +13,24 @@ export default class OverpassVectorLayer extends VectorLayer<
const map = this.getMapInternal();
const features = await Promise.all(
splitQuerySubpart(query).map(({ query, subpart }) =>
this.executeQuery0(query, subpart),
/\/\/\/\s*@type geojson/dg.test(query)
? this.readGeoJSON(query.replace(/\/\/\/.*/dg, ""), subpart)
: this.executeQuery0(query, subpart),
),
);
const vectorSource = new VectorSource({
features: features.reduce((a, b) => [...a, ...b]),
features: features.flat(),
});
this.setSource(vectorSource);
map?.getView().fit(vectorSource.getExtent(), { padding: [24, 24, 24, 24] });
}

private readGeoJSON(query: string, subpart = "") {
const features = new GeoJSON().readFeatures(query);
features.forEach((feature) => feature.set("@subpart", subpart));
return features;
}

private async executeQuery0(query: string, subpart = "") {
const map = this.getMapInternal();
if (map) {
Expand Down
5 changes: 5 additions & 0 deletions default.mapcss
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ relation:active {
color: #f50;
fill-color: #f50;
}
node[place=town] {
font: "bold 11pt / 1.0 sans-serif";
text-color: black;
text: name;
}
12 changes: 12 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ const defaultQuery = `
relation(4740507);>;out geom;
/// @subpart background
//nwr[railway=rail]({{bbox}});out geom;
/// @subpart town
/// @type geojson
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": { "name": "Gars am Kamp", "place": "town" },
"geometry": { "type": "Point", "coordinates": [15.6594592, 48.5951053] }
}
]
}
`.trim();
const store = new (class Store {
get query(): string {
Expand Down

0 comments on commit 0b37b80

Please sign in to comment.