Public
Edited
Apr 5, 2023
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cityRows = FileAttachment("2021-2 UNLOCODE CodeList.txt").text().then(r => r.split('\r\n'))
Insert cell
Insert cell
cityFileSpec = ({
change: {start: 0, length: 2},
iso2: {start: 3, length: 2},
locode: {start: 6, length: 3},
nameWithDiacritics: {start: 10, length: 35},
nameWithoutDiacritics: {start: 46, length: 35},
iso2Subdivision: {start: 82, length: 3},
functions: {start: 86, length: 8},
status: {start: 95, length: 2},
unknown1: {start: 98, length: 4},
unknown2: {start: 103, length: 3},
latitude: {start: 108, length: 5},
longitude: {start: 114, length: 6},
notes: {start: 121, length: 50}
})
Insert cell
Insert cell
citiesUnfiltered = cityRows
.map(r => {
return Object.fromEntries(
// construct a new object by
Object.entries(cityFileSpec)
// iterating through each key in the specification and
.map(([key, value]) => {
// extracting and trimming the resulting value
const extract = r.slice(value.start, value.start + value.length).trim()
// return the key/value pair if it is a non-empty string
if (extract.length > 0) { return [key, extract] }
// return the key/undefined pair otherwise
else { return [key, undefined] }
})
// and finally filtering out the undefined values
.filter(([key, value]) => value)
)
})
Insert cell
Insert cell
cities = citiesUnfiltered
.filter(r => !r.nameWithDiacritics?.startsWith('.'))
.filter(r => !r.change?.includes('='))
Insert cell
Insert cell
bcaiCities = cities.map(r => ({
id: ['C', r.iso2, r.iso2Subdivision, r.locode].join('-'),
...r,
nameUpperCase: r.nameWithoutDiacritics?.toUpperCase()
}))
Insert cell
Insert cell
countryCodes = bcaiCities
.reduce((codes, city) => {
if (city.iso2 && !codes.includes(city.iso2)) { codes.push(city.iso2)}
return codes
}, [])
.sort()
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more