forked from MapComplete/MapComplete
Add possibility to add external geojson
This commit is contained in:
parent
d7c1f38d26
commit
f0765df5ed
10 changed files with 98 additions and 11 deletions
|
@ -11,6 +11,7 @@ import LayerConfig from "../../Customizations/JSON/LayerConfig";
|
|||
import LocalStorageSource from "./LocalStorageSource";
|
||||
import LayoutConfig from "../../Customizations/JSON/LayoutConfig";
|
||||
import Loc from "../../Models/Loc";
|
||||
import GeoJsonSource from "./GeoJsonSource";
|
||||
|
||||
export default class FeaturePipeline implements FeatureSource {
|
||||
|
||||
|
@ -30,6 +31,15 @@ export default class FeaturePipeline implements FeatureSource {
|
|||
)
|
||||
);
|
||||
|
||||
const geojsonSources: GeoJsonSource [] = []
|
||||
for (const flayer of flayers.data) {
|
||||
const sourceUrl = flayer.layerDef.source.geojsonSource
|
||||
if (sourceUrl !== undefined) {
|
||||
geojsonSources.push(new WayHandlingApplyingFeatureSource(flayers,
|
||||
new GeoJsonSource(flayer.layerDef.id, sourceUrl)))
|
||||
}
|
||||
}
|
||||
|
||||
const amendedLocalStorageSource =
|
||||
new RememberingSource(
|
||||
new WayHandlingApplyingFeatureSource(flayers,
|
||||
|
@ -37,11 +47,12 @@ export default class FeaturePipeline implements FeatureSource {
|
|||
));
|
||||
|
||||
newPoints = new FeatureDuplicatorPerLayer(flayers, newPoints);
|
||||
|
||||
|
||||
const merged = new FeatureSourceMerger([
|
||||
amendedOverpassSource,
|
||||
amendedLocalStorageSource,
|
||||
newPoints
|
||||
newPoints,
|
||||
...geojsonSources
|
||||
]);
|
||||
|
||||
const source =
|
||||
|
|
|
@ -22,7 +22,6 @@ export default class FeatureSourceMerger implements FeatureSource {
|
|||
let all = {}; // Mapping 'id' -> {feature, freshness}
|
||||
for (const source of this._sources) {
|
||||
if(source?.features?.data === undefined){
|
||||
console.log("Not defined");
|
||||
continue;
|
||||
}
|
||||
for (const f of source.features.data) {
|
||||
|
|
52
Logic/FeatureSource/GeoJsonSource.ts
Normal file
52
Logic/FeatureSource/GeoJsonSource.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import FeatureSource from "./FeatureSource";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import * as $ from "jquery";
|
||||
import {Layer} from "leaflet";
|
||||
|
||||
/**
|
||||
* Fetches a geojson file somewhere and passes it along
|
||||
*/
|
||||
export default class GeoJsonSource implements FeatureSource {
|
||||
features: UIEventSource<{ feature: any; freshness: Date }[]>;
|
||||
|
||||
constructor(layerId: string, url: string, onFail: ((errorMsg: any) => void) = undefined) {
|
||||
if (onFail === undefined) {
|
||||
onFail = errorMsg => {
|
||||
console.warn(`Could not load geojson layer from`, url, "due to", errorMsg)
|
||||
}
|
||||
}
|
||||
this.features = new UIEventSource<{ feature: any; freshness: Date }[]>(undefined)
|
||||
const eventSource = this.features;
|
||||
$.getJSON(url, function (json, status) {
|
||||
if (status !== "success") {
|
||||
console.log("Fetching geojson failed failed")
|
||||
onFail(status);
|
||||
return;
|
||||
}
|
||||
|
||||
if (json.elements === [] && json.remarks.indexOf("runtime error") > 0) {
|
||||
console.log("Timeout or other runtime error");
|
||||
onFail("Runtime error (timeout)")
|
||||
return;
|
||||
}
|
||||
const time = new Date();
|
||||
const features: { feature: any, freshness: Date } [] = []
|
||||
let i = 0;
|
||||
for (const feature of json.features) {
|
||||
if (feature.properties.id === undefined) {
|
||||
feature.properties.id = url + "/" + i;
|
||||
feature.id = url + "/" + i;
|
||||
i++;
|
||||
}
|
||||
feature._matching_layer_id = layerId;
|
||||
features.push({feature: feature, freshness: time})
|
||||
}
|
||||
console.log("Loaded features are", features)
|
||||
eventSource.setData(features)
|
||||
|
||||
}).fail(onFail)
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue