forked from MapComplete/MapComplete
Merge files from develop branch
This commit is contained in:
parent
2a03b699d5
commit
3f29632cf4
16 changed files with 484 additions and 135 deletions
91
Logic/FeatureSource/OsmApiFeatureSource.ts
Normal file
91
Logic/FeatureSource/OsmApiFeatureSource.ts
Normal file
|
@ -0,0 +1,91 @@
|
|||
import FeatureSource from "./FeatureSource";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {OsmObject} from "../Osm/OsmObject";
|
||||
import State from "../../State";
|
||||
import {Utils} from "../../Utils";
|
||||
import Loc from "../../Models/Loc";
|
||||
|
||||
|
||||
export default class OsmApiFeatureSource implements FeatureSource {
|
||||
public readonly features: UIEventSource<{ feature: any; freshness: Date }[]> = new UIEventSource<{ feature: any; freshness: Date }[]>([]);
|
||||
public readonly name: string = "OsmApiFeatureSource";
|
||||
private readonly loadedTiles: Set<string> = new Set<string>();
|
||||
|
||||
constructor(location: UIEventSource<Loc>) {
|
||||
/* const self = this
|
||||
location.addCallback(_ => {
|
||||
self.loadArea()
|
||||
})
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
public load(id: string) {
|
||||
OsmObject.DownloadObject(id, (element, meta) => {
|
||||
const geojson = element.asGeoJson();
|
||||
geojson.id = geojson.properties.id;
|
||||
this.features.setData([{feature: geojson, freshness: meta["_last_edit:timestamp"]}])
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the current inview-area
|
||||
*/
|
||||
public loadArea(z: number = 16): boolean {
|
||||
const layers = State.state.filteredLayers.data;
|
||||
|
||||
const disabledLayers = layers.filter(layer => layer.layerDef.source.overpassScript !== undefined || layer.layerDef.source.geojsonSource !== undefined)
|
||||
if (disabledLayers.length > 0) {
|
||||
return false;
|
||||
}
|
||||
const loc = State.state.locationControl.data;
|
||||
if (loc.zoom < 16) {
|
||||
return false;
|
||||
}
|
||||
if (State.state.leafletMap.data === undefined) {
|
||||
return false; // Not yet inited
|
||||
}
|
||||
const bounds = State.state.leafletMap.data.getBounds()
|
||||
const tileRange = Utils.TileRangeBetween(z, bounds.getNorth(), bounds.getEast(), bounds.getSouth(), bounds.getWest())
|
||||
const self = this;
|
||||
Utils.MapRange(tileRange, (x, y) => {
|
||||
const key = x + "/" + y;
|
||||
if (self.loadedTiles.has(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.loadedTiles.add(key);
|
||||
|
||||
const bounds = Utils.tile_bounds(z, x, y);
|
||||
console.log("Loading OSM data tile", z, x, y, " with bounds", bounds)
|
||||
OsmObject.LoadArea(bounds, objects => {
|
||||
const keptGeoJson: {feature:any, freshness: Date}[] = []
|
||||
// Which layer does the object match?
|
||||
for (const object of objects) {
|
||||
|
||||
for (const flayer of layers) {
|
||||
const layer = flayer.layerDef;
|
||||
const tags = object.tags
|
||||
const doesMatch = layer.source.osmTags.matchesProperties(tags);
|
||||
if (doesMatch) {
|
||||
const geoJson = object.asGeoJson();
|
||||
geoJson._matching_layer_id = layer.id
|
||||
keptGeoJson.push({feature: geoJson, freshness: object.timestamp})
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
self.features.setData(keptGeoJson)
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue