mapcomplete/scripts/generateStats.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.9 KiB
TypeScript
Raw Normal View History

import known_layers from "../src/assets/generated/known_layers.json"
import { LayerConfigJson } from "../src/Models/ThemeConfig/Json/LayerConfigJson"
import { TagUtils } from "../src/Logic/Tags/TagUtils"
import { Utils } from "../src/Utils"
2022-03-13 01:27:19 +01:00
import { writeFileSync } from "fs"
import ScriptUtils from "./ScriptUtils"
/* Downloads stats on osmSource-tags and keys from tagInfo */
async function main(includeTags = true) {
ScriptUtils.fixUtils()
const layers = <LayerConfigJson[]>known_layers.layers
2022-03-13 01:27:19 +01:00
const keysAndTags = new Map<string, Set<string>>()
for (const layer of layers) {
if (layer.source["geoJson"] !== undefined && !layer.source["isOsmCache"]) {
continue
}
2023-03-25 02:48:24 +01:00
if (layer.source == null || typeof layer.source === "string") {
2022-03-13 01:27:19 +01:00
continue
}
2023-06-20 22:49:58 +02:00
const sources = TagUtils.Tag(layer.source["osmTags"])
2022-03-13 01:27:19 +01:00
const allKeys = sources.usedKeys()
for (const key of allKeys) {
if (!keysAndTags.has(key)) {
keysAndTags.set(key, new Set<string>())
}
}
const allTags = includeTags ? sources.usedTags() : []
for (const tag of allTags) {
if (!keysAndTags.has(tag.key)) {
keysAndTags.set(tag.key, new Set<string>())
}
keysAndTags.get(tag.key).add(tag.value)
}
}
const keyTotal = new Map<string, number>()
const tagTotal = new Map<string, Map<string, number>>()
await Promise.all(
Array.from(keysAndTags.keys()).map(async (key) => {
const values = keysAndTags.get(key)
const data = await Utils.downloadJson(
`https://taginfo.openstreetmap.org/api/4/key/stats?key=${key}`
)
const count = data.data.find((item) => item.type === "all").count
keyTotal.set(key, count)
console.log(key, "-->", count)
if (values.size > 0) {
tagTotal.set(key, new Map<string, number>())
await Promise.all(
Array.from(values).map(async (value) => {
const tagData = await Utils.downloadJson(
`https://taginfo.openstreetmap.org/api/4/tag/stats?key=${key}&value=${value}`
2022-09-08 21:40:48 +02:00
)
2022-03-13 01:27:19 +01:00
const count = tagData.data.find((item) => item.type === "all").count
tagTotal.get(key).set(value, count)
console.log(key + "=" + value, "-->", count)
})
2022-09-08 21:40:48 +02:00
)
}
})
)
2022-03-13 01:27:19 +01:00
writeFileSync(
"./src/assets/key_totals.json",
2022-03-13 01:27:19 +01:00
JSON.stringify(
{
2022-06-24 16:47:00 +02:00
keys: Utils.MapToObj(keyTotal, (t) => t),
tags: Utils.MapToObj(tagTotal, (v) => Utils.MapToObj(v, (t) => t)),
2022-03-13 01:27:19 +01:00
},
null,
" "
)
)
}
main().then(() => console.log("All done"))