Chore: update name suggestion index

This commit is contained in:
Pieter Vander Vennet 2025-01-08 14:21:07 +01:00
parent 9f80e60ed6
commit c7e34f1a86
356 changed files with 46221 additions and 38409 deletions

View file

@ -1,6 +1,5 @@
import * as nsiFeatures from "../../../node_modules/name-suggestion-index/dist/featureCollection.json"
import { LocationConflation } from "@rapideditor/location-conflation"
import type { Feature, MultiPolygon } from "geojson"
import type { Feature, FeatureCollection, MultiPolygon } from "geojson"
import { Utils } from "../../Utils"
import * as turf from "@turf/turf"
import { Mapping } from "../../Models/ThemeConfig/TagRenderingConfig"
@ -64,7 +63,7 @@ export default class NameSuggestionIndex {
>
>
private static loco = new LocationConflation(nsiFeatures) // Some additional boundaries
private loco: LocationConflation // Some additional boundaries
private _supportedTypes: string[]
@ -77,10 +76,12 @@ export default class NameSuggestionIndex {
logos: { wikidata?: string; facebook?: string }
}
>
>
>,
features: Readonly<FeatureCollection>
) {
this.nsiFile = nsiFile
this.nsiWdFile = nsiWdFile
this.loco = new LocationConflation(features)
}
private static inited: NameSuggestionIndex = undefined
@ -89,12 +90,12 @@ export default class NameSuggestionIndex {
if (NameSuggestionIndex.inited) {
return NameSuggestionIndex.inited
}
const [nsi, nsiWd] = await Promise.all(
["assets/data/nsi/nsi.json", "assets/data/nsi/wikidata.min.json"].map((url) =>
const [nsi, nsiWd, features] = await Promise.all(
["./assets/data/nsi/nsi.min.json", "./assets/data/nsi/wikidata.min.json", "./assets/data/nsi/featureCollection.min.json"].map((url) =>
Utils.downloadJsonCached(url, 1000 * 60 * 60 * 24 * 30)
)
)
NameSuggestionIndex.inited = new NameSuggestionIndex(<any>nsi, <any>nsiWd["wikidata"])
NameSuggestionIndex.inited = new NameSuggestionIndex(<any>nsi, <any>nsiWd["wikidata"], <any> features)
return NameSuggestionIndex.inited
}
@ -351,7 +352,7 @@ export default class NameSuggestionIndex {
const key = i.locationSet.include?.join(";") + "-" + i.locationSet.exclude?.join(";")
const fromCache = NameSuggestionIndex.resolvedSets[key]
const resolvedSet =
fromCache ?? NameSuggestionIndex.loco.resolveLocationSet(i.locationSet)
fromCache ?? this.loco.resolveLocationSet(i.locationSet)
if (!fromCache) {
NameSuggestionIndex.resolvedSets[key] = resolvedSet
}

View file

@ -1,5 +1,6 @@
import { Utils } from "../../Utils"
import type { FeatureCollection } from "geojson"
import ScriptUtils from "../../../scripts/ScriptUtils"
export interface TagInfoStats {
/**
@ -39,12 +40,12 @@ export default class TagInfo {
let url: string
if (value) {
url = `${this._backend}api/4/tag/stats?key=${encodeURIComponent(
key
key,
)}&value=${encodeURIComponent(value)}`
} else {
url = `${this._backend}api/4/key/stats?key=${encodeURIComponent(key)}`
}
return await Utils.downloadJsonCached<TagInfoStats>(url, 1000 * 60 * 60)
return await Utils.downloadJsonCached<TagInfoStats>(url, 1000 * 60 * 60 * 24)
}
/**
@ -69,10 +70,10 @@ export default class TagInfo {
}
const countriesFC: FeatureCollection = await Utils.downloadJsonCached<FeatureCollection>(
"https://download.geofabrik.de/index-v1-nogeom.json",
24 * 1000 * 60 * 60
24 * 1000 * 60 * 60,
)
TagInfo._geofabrikCountries = countriesFC.features.map(
(f) => <GeofabrikCountryProperties>f.properties
(f) => <GeofabrikCountryProperties>f.properties,
)
return TagInfo._geofabrikCountries
}
@ -98,7 +99,7 @@ export default class TagInfo {
private static async getDistributionsFor(
countryCode: string,
key: string,
value?: string
value?: string,
): Promise<TagInfoStats> {
if (!countryCode) {
return undefined
@ -110,30 +111,43 @@ export default class TagInfo {
try {
return await ti.getStats(key, value)
} catch (e) {
console.warn("Could not fetch info for", countryCode, key, value, "due to", e)
console.warn("Could not fetch info from taginfo for", countryCode, key, value, "due to", e, "Taginfo country specific instance is ", ti._backend)
return undefined
}
}
private static readonly blacklist = ["VI", "GF", "PR"]
public static async getGlobalDistributionsFor(
/**
* Get a taginfo object for every supportedCountry. This statistic is handled by 'f' and written into the passed in object
* @param writeInto
* @param f
* @param key
* @param value
*/
public static async getGlobalDistributionsFor<T>(
writeInto: Record<string, T>,
f: ((stats: TagInfoStats) => T),
key: string,
value?: string
): Promise<Record<string, TagInfoStats>> {
value?: string,
): Promise<number> {
const countriesAll = await this.geofabrikCountries()
const countries = countriesAll
.map((c) => c["iso3166-1:alpha2"]?.[0])
.filter((c) => !!c && TagInfo.blacklist.indexOf(c) < 0)
const perCountry: Record<string, TagInfoStats> = {}
const results = await Promise.all(
countries.map((country) => TagInfo.getDistributionsFor(country, key, value))
)
for (let i = 0; i < countries.length; i++) {
const countryCode = countries[i]
if (results[i]) {
perCountry[countryCode] = results[i]
let downloaded = 0
for (const country of countries) {
if(writeInto[country] !== undefined){
continue
}
const r = await TagInfo.getDistributionsFor(country, key, value)
if(r === undefined){
continue
}
downloaded ++
writeInto[country] = f(r)
}
return perCountry
return downloaded
}
}