mapcomplete/Logic/Osm/Overpass.ts

56 lines
1.9 KiB
TypeScript
Raw Normal View History

import {TagsFilter} from "../Tags";
2020-10-11 23:25:27 +00:00
import * as $ from "jquery"
2020-07-29 22:59:08 +00:00
import * as OsmToGeoJson from "osmtogeojson";
2021-01-02 23:19:42 +00:00
import Bounds from "../../Models/Bounds";
2020-07-29 22:59:08 +00:00
/**
* Interfaces overpass to get all the latest data
*/
2020-06-23 22:35:19 +00:00
export class Overpass {
private _filter: TagsFilter
public static testUrl: string = null
2020-06-23 22:35:19 +00:00
constructor(filter: TagsFilter) {
this._filter = filter
2020-06-23 22:35:19 +00:00
}
2020-07-29 22:59:08 +00:00
private buildQuery(bbox: string): string {
const filters = this._filter.asOverpass()
let filter = ""
2020-06-23 22:35:19 +00:00
for (const filterOr of filters) {
filter += 'nwr' + filterOr + ';'
2020-06-23 22:35:19 +00:00
}
const query =
'[out:json][timeout:25]' + bbox + ';(' + filter + ');out body;>;out skel qt;'
return "https://overpass-api.de/api/interpreter?data=" + encodeURIComponent(query)
2020-06-23 22:35:19 +00:00
}
2020-07-29 22:59:08 +00:00
2021-01-02 23:19:42 +00:00
queryGeoJson(bounds: Bounds, continuation: ((any, date: Date) => void), onFail: ((reason) => void)): void {
2020-07-20 18:15:21 +00:00
2020-10-11 23:25:27 +00:00
let query = this.buildQuery("[bbox:" + bounds.south + "," + bounds.west + "," + bounds.north + "," + bounds.east + "]")
if (Overpass.testUrl !== null) {
2020-06-23 22:35:19 +00:00
console.log("Using testing URL")
query = Overpass.testUrl;
}
$.getJSON(query,
function (json, status) {
if (status !== "success") {
console.log("Query failed")
onFail(status);
}
2020-07-20 18:15:21 +00:00
2020-10-11 23:25:27 +00:00
if (json.elements === [] && json.remarks.indexOf("runtime error") > 0) {
2020-07-01 19:21:29 +00:00
console.log("Timeout or other runtime error");
onFail("Runtime error (timeout)")
2020-07-01 19:21:29 +00:00
return;
}
2020-06-23 22:35:19 +00:00
// @ts-ignore
const geojson = OsmToGeoJson.default(json);
2021-01-02 23:19:42 +00:00
const osmTime = new Date(json.osm3s.timestamp_osm_base);
continuation(geojson, osmTime);
2020-06-23 22:35:19 +00:00
}).fail(onFail)
}
}