-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
elements.js
104 lines (90 loc) · 2.28 KB
/
elements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Build statistics from elements.json.
Download last elements by elements.sh.
Tags calculated from nodes only.
*/
const fs = require('fs')
const file = fs.readFileSync('./BTCMap/elements.json')
const elements = JSON.parse(file)
const keys = new Map()
let types = new Map()
let tags = new Map()
const grabbedTags = {}
const tagsToGrab = [
'amenity',
'shop',
'cuisine',
'tourism',
'sport',
'company',
'building',
'healthcare',
'craft',
'leisure',
'office',
'place'
]
function fillKeys(obj, keys) {
for (const key in obj) {
if (typeof obj[key] === 'object' && key !== 'tags') {
if (!keys.has(key)) {
keys.set(key, new Map())
}
if (Array.isArray(obj[key])) {
for (const item of obj[key]) {
fillKeys(item, keys.get(key))
}
} else {
fillKeys(obj[key], keys.get(key))
}
} else {
keys.set(key, undefined)
}
}
}
for (const element of elements) {
fillKeys(element, keys)
const type = element.osm_json.type
types.set(type, (types.get(type) ?? 0) + 1)
if (type === 'node') {
for (const tag in element.osm_json.tags) {
tags.set(tag, (tags.get(tag) ?? 0) + 1)
if (tagsToGrab.includes(tag)) {
if (!grabbedTags[tag]) grabbedTags[tag] = new Map()
const value = element.osm_json.tags[tag]
grabbedTags[tag].set(value, (grabbedTags[tag].get(value) ?? 0) + 1)
}
}
}
}
const total = elements.length
console.log('Total elements:', total)
types = Array.from(types.entries()).sort((a, b) => b[1] - a[1])
for (const type of types) {
console.log(` ${type[0]}:`, type[1], `${(type[1] / total).toFixed(2)}%`)
}
function logKeys(keys, indent) {
for (const key of keys.keys()) {
console.log(`${indent}${key}`)
if (keys.get(key)) {
logKeys(keys.get(key), indent + ' ')
}
}
}
console.log()
console.log('Element structure:')
logKeys(keys, ' ')
console.log()
console.log('Tags:')
tags = Array.from(tags.entries()).sort((a, b) => b[1] - a[1])
for (const tag of tags) {
console.log(` ${tag[0]}:`, tag[1])
}
for (const tag in grabbedTags) {
console.log()
console.log(`Tag '${tag}':`)
const values = Array.from(grabbedTags[tag].entries()).sort((a, b) => b[1] - a[1])
for (const value of values) {
console.log(` ${value[0]}:`, value[1])
}
}