Skip to content

Commit 0b37b80

Browse files
committed
Read embedded GeoJSON
1 parent 14594b6 commit 0b37b80

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

OverpassVectorLayer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Vector as VectorLayer } from "ol/layer";
22
import { Vector as VectorSource } from "ol/source";
33
import { type Rule, evaluateStyle, parseMapCSS } from "./mapcss";
44
import { Geometry } from "ol/geom";
5+
import GeoJSON from "ol/format/GeoJSON";
56
import OSMXML from "./OSMXML";
67
import { splitQuerySubpart } from "./overpass";
78

@@ -12,16 +13,24 @@ export default class OverpassVectorLayer extends VectorLayer<
1213
const map = this.getMapInternal();
1314
const features = await Promise.all(
1415
splitQuerySubpart(query).map(({ query, subpart }) =>
15-
this.executeQuery0(query, subpart),
16+
/\/\/\/\s*@type geojson/dg.test(query)
17+
? this.readGeoJSON(query.replace(/\/\/\/.*/dg, ""), subpart)
18+
: this.executeQuery0(query, subpart),
1619
),
1720
);
1821
const vectorSource = new VectorSource({
19-
features: features.reduce((a, b) => [...a, ...b]),
22+
features: features.flat(),
2023
});
2124
this.setSource(vectorSource);
2225
map?.getView().fit(vectorSource.getExtent(), { padding: [24, 24, 24, 24] });
2326
}
2427

28+
private readGeoJSON(query: string, subpart = "") {
29+
const features = new GeoJSON().readFeatures(query);
30+
features.forEach((feature) => feature.set("@subpart", subpart));
31+
return features;
32+
}
33+
2534
private async executeQuery0(query: string, subpart = "") {
2635
const map = this.getMapInternal();
2736
if (map) {

default.mapcss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ relation:active {
5050
color: #f50;
5151
fill-color: #f50;
5252
}
53+
node[place=town] {
54+
font: "bold 11pt / 1.0 sans-serif";
55+
text-color: black;
56+
text: name;
57+
}

main.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ const defaultQuery = `
2323
relation(4740507);>;out geom;
2424
/// @subpart background
2525
//nwr[railway=rail]({{bbox}});out geom;
26+
/// @subpart town
27+
/// @type geojson
28+
{
29+
"type": "FeatureCollection",
30+
"features": [
31+
{
32+
"type": "Feature",
33+
"properties": { "name": "Gars am Kamp", "place": "town" },
34+
"geometry": { "type": "Point", "coordinates": [15.6594592, 48.5951053] }
35+
}
36+
]
37+
}
2638
`.trim();
2739
const store = new (class Store {
2840
get query(): string {

0 commit comments

Comments
 (0)