Add loading of live data

This commit is contained in:
Pieter Vander Vennet 2020-10-12 01:25:27 +02:00
parent feab5a19b3
commit 0d6412f824
14 changed files with 291 additions and 35 deletions

View file

@ -117,7 +117,7 @@ export class FilteredLayer {
feature.properties["_lon"] = "" + lat; // We expect a string here for lat/lon
feature.properties["_lat"] = "" + lon;
// But the codegrid SHOULD be a number!
CodeGrid.grid.getCode(lat, lon, (error, code) => {
CodeGrid.getCode(lat, lon, (error, code) => {
if (error === null) {
feature.properties["_country"] = code;
} else {

View file

@ -91,6 +91,14 @@ export class LayerUpdater {
self.retries.setData(0);
let newIds = 1;
for (const feature of geojson.features) {
if(feature.properties.id === undefined){
feature.properties.id = "ext/"+newIds;
newIds++;
}
}
function renderLayers(layers: FilteredLayer[]) {
if (layers.length === 0) {
self.runningQuery.setData(false);

View file

@ -1,6 +1,6 @@
import {Bounds} from "../Bounds";
import {TagsFilter} from "../Tags";
import $ from "jquery"
import * as $ from "jquery"
import * as OsmToGeoJson from "osmtogeojson";
/**
@ -27,14 +27,13 @@ export class Overpass {
}
queryGeoJson(bounds: Bounds, continuation: ((any) => void), onFail: ((reason) => void)): void {
let query = this.buildQuery( "[bbox:" + bounds.south + "," + bounds.west + "," + bounds.north + "," + bounds.east + "]")
if(Overpass.testUrl !== null){
let query = this.buildQuery("[bbox:" + bounds.south + "," + bounds.west + "," + bounds.north + "," + bounds.east + "]")
if (Overpass.testUrl !== null) {
console.log("Using testing URL")
query = Overpass.testUrl;
}
$.getJSON(query,
function (json, status) {
if (status !== "success") {
@ -42,7 +41,7 @@ export class Overpass {
onFail(status);
}
if(json.elements === [] && json.remarks.indexOf("runtime error") > 0){
if (json.elements === [] && json.remarks.indexOf("runtime error") > 0) {
console.log("Timeout or other runtime error");
onFail("Runtime error (timeout)")
return;

View file

@ -1,7 +1,12 @@
import codegrid from "codegrid-js";
export default class CodeGrid {
public static readonly grid = CodeGrid.InitGrid();
private static readonly grid = CodeGrid.InitGrid();
public static getCode(lat: any, lon: any, handle: (error, code) => void) {
CodeGrid.grid.getCode(lat, lon, handle);
}
private static InitGrid(): any {
const grid = codegrid.CodeGrid("./tiles/");
@ -15,4 +20,6 @@ export default class CodeGrid {
});
return grid;
}
}

View file

@ -0,0 +1,53 @@
/**
* Fetches data from random data sources
*/
import {UIEventSource} from "../UIEventSource";
import * as $ from "jquery"
export default class LiveQueryHandler {
private static cache = {} // url --> UIEventSource<actual data>
private static neededShorthands = {} // url -> (shorthand:paths)[]
public static FetchLiveData(url: string, shorthands: string[]): UIEventSource<any /* string -> string */> {
const shorthandsSet: string[] = LiveQueryHandler.neededShorthands[url] ?? []
for (const shorthand of shorthands) {
if (shorthandsSet.indexOf(shorthand) < 0) {
shorthandsSet.push(shorthand);
}
}
LiveQueryHandler.neededShorthands[url] = shorthandsSet;
if (LiveQueryHandler[url] === undefined) {
const source = new UIEventSource({});
LiveQueryHandler[url] = source;
console.log("Fetching live data from a third-party (unknown) API:",url)
$.getJSON(url, function (data) {
for (const shorthandDescription of shorthandsSet) {
const descr = shorthandDescription.trim().split(":");
const shorthand = descr[0];
const path = descr[1];
const parts = path.split(".");
let trail = data;
for (const part of parts) {
if (trail !== undefined) {
trail = trail[part];
}
}
source.data[shorthand] = trail;
}
source.ping();
})
}
return LiveQueryHandler[url];
}
}