From f5d1b47bda5c54dd7c25d9624ff14068ff7bccb5 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Mon, 13 May 2024 17:14:59 +0200 Subject: [PATCH] Fetch taginfo: add code to get all countries, imrove docs --- src/Logic/Web/TagInfo.ts | 78 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/src/Logic/Web/TagInfo.ts b/src/Logic/Web/TagInfo.ts index dfc6fb957..213a81ac7 100644 --- a/src/Logic/Web/TagInfo.ts +++ b/src/Logic/Web/TagInfo.ts @@ -1,35 +1,48 @@ import { Utils } from "../../Utils" +import type { FeatureCollection } from "geojson" export interface TagInfoStats { /** * The total number of entries in the data array, **not** the total number of objects known in OSM! * * Use `data.find(item => item.type==="all").count` for this + * @deprecated: you probably want to use data.find(item => item.type==="all").count */ total: number data: { type: "all" | "nodes" | "ways" | "relations" + // Absolute number count: number + // Relative, percentage count_fraction: number }[] } +interface GeofabrikCountryProperties { + id: string, + parent: string | "europe" | "asia", + urls: string[], + name: string, + "iso3166-1:alpha2": string[] +} + export default class TagInfo { public static readonly global = new TagInfo() private readonly _backend: string + private static _geofabrikCountries = undefined constructor(backend = "https://taginfo.openstreetmap.org/") { this._backend = backend } - public getStats(key: string, value?: string): Promise { + public async getStats(key: string, value?: string): Promise { let url: string if (value) { url = `${this._backend}api/4/tag/stats?key=${key}&value=${value}` } else { url = `${this._backend}api/4/key/stats?key=${key}` } - return Utils.downloadJsonCached(url, 1000 * 60 * 60) + return await Utils.downloadJsonCached(url, 1000 * 60 * 60) } /** @@ -44,4 +57,65 @@ export default class TagInfo { return `${this._backend}/keys/${k}#overview` } } + + /** + * Does not work in the browser due to some resources not being CORS-accessible + */ + public static async geofabrikCountries(): Promise { + if (TagInfo._geofabrikCountries) { + return TagInfo._geofabrikCountries + } + const countriesFC: FeatureCollection = await Utils.downloadJsonCached("https://download.geofabrik.de/index-v1-nogeom.json", 24 * 1000 * 60 * 60) + TagInfo._geofabrikCountries = countriesFC.features.map(f => f.properties) + return TagInfo._geofabrikCountries + } + + /** + * Creates a TagInfo-api-object for geofabrik for the given country. + * Returns undefined if geofabrik does not have such a country + * + * Does not work in the browser due to some resources not being CORS-accessible + * @param countryCode: an iso3166-1:alpha2 code + */ + public static async getInstanceFor(countryCode: string) { + const countries = await this.geofabrikCountries() + countryCode = countryCode.toUpperCase() + const country = countries.find(c => c["iso3166-1:alpha2"]?.indexOf(countryCode) >= 0) + if (!country || !country?.parent || !country?.id) { + return undefined + } + const url = `https://taginfo.geofabrik.de/${country.parent}:${country.id}/` + return new TagInfo(url) + } + + private static async getDistributionsFor(countryCode: string, key: string, value?: string): Promise{ + if (!countryCode) { + return undefined + } + const ti = await TagInfo.getInstanceFor(countryCode) + if (!ti) { + return undefined + } + try { + return await ti.getStats(key, value) + } catch (e) { + console.warn("Could not fetch info for", countryCode,key,value, "due to", e) + return undefined + } + } + + public static async getGlobalDistributionsFor(key: string, value?: string): Promise> { + const countries = await this.geofabrikCountries() + const perCountry: Record = {} + const results = await Promise.all(countries.map(country => TagInfo.getDistributionsFor(country?.["iso3166-1:alpha2"]?.[0], key, value))) + for (let i = 0; i < countries.length; i++){ + const country = countries[i] + const countryCode = country["iso3166-1:alpha2"]?.[0] + if(results[i]){ + perCountry[countryCode] = results[i] + } + } + return perCountry + } + }