-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerate.js
67 lines (55 loc) · 2.26 KB
/
generate.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
const { readFile, writeFile } = require('fs').promises
const { feature } = require('topojson-client')
const parseCSV = require('neat-csv')
async function generateIndonesiaProvinces() {
const datasets = [
'./dataset/indonesia-provinsi.json',
'./dataset/indonesia-topo-no-code.json'
]
const [indonesiaProvinces, oldTopoData] = await Promise.all(datasets.map(path => (
readFile(path).then(JSON.parse)
)))
const data = feature(oldTopoData, oldTopoData.objects.provinces)
.features
.reduce((accumulator, current) => {
const province = indonesiaProvinces.find(({ provinsi }) => provinsi === current.properties.provinsi)
if (province) {
const properties = { ...current.properties }
delete current.properties
accumulator.push({
...current,
...properties,
kodeProvi: province.kode
})
}
return accumulator
}, [])
writeFile('public/indonesia-provinces.json', JSON.stringify(data))
}
async function generateCountryData() {
const [world, countries] = await Promise.all([
readFile('dataset/world-110m.json', 'utf-8').then(JSON.parse),
readFile('dataset/world-country.csv', 'utf-8').then(parseCSV)
])
const data = feature(world, world.objects.countries)
.features
.reduce((accumulator, current) => {
const assignedCountry = countries.some(country => current.id === country.id && (
current.name = country.name,
current.iso2 = country['alpha-2'],
current.iso3 = country['alpha-3'],
current.region = country.region,
current.regionCode = country['region-code'],
current.subRegion = country['sub-region'],
current.subRegionCode = country['sub-region-code']
))
if (assignedCountry) {
delete current.properties
accumulator.push(current)
}
return accumulator
}, [])
writeFile('public/world-countries-110m.json', JSON.stringify(data))
}
generateIndonesiaProvinces()
generateCountryData()