Created
July 25, 2024 12:39
-
-
Save missinglink/0abb5b436dffda6681bfd858f43953d0 to your computer and use it in GitHub Desktop.
Find the corresponding repo for a WhosOnFirst ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* download the ccmap file (updated every time the data updates): | |
* | |
* curl -OL https://data.geocode.earth/wof/dist/meta/whosonfirst-data-admin.ccmap.gz | |
* | |
* | |
* convert ccmap file to typescript (save stdout to `whosonfirst-data-admin.ccmap.ts`): | |
* | |
* import fs from 'node:fs' | |
* const data = fs.readFileSync('whosonfirst-data-admin.ccmap.gz', 'base64') | |
* process.stdout.write(`export default "${data}"`) | |
* | |
**/ | |
import pako from 'pako' | |
import ccmap from './whosonfirst-data-admin.ccmap' | |
// find is a slow-yet-memory-efficient method of | |
// finding the wofid in the delta compressed map | |
export const find = (wofid: number) => { | |
const gdata = Uint8Array.from(atob(ccmap), c => c.charCodeAt(0)) | |
const data = new TextDecoder().decode(pako.ungzip(gdata)) | |
let o = 0 | |
let prev = 0 | |
while (o < data.length) { | |
const cc = data.substring(o, o+2); o += 2 | |
let delta = data.substring(o, o+1); o += 1 | |
while (true) { | |
const next = data.substring(o, o+1) | |
if (isNaN(parseInt(next))) break | |
delta += next; o += 1 | |
} | |
prev = (prev + parseInt(delta)) | |
if (prev === wofid) return cc | |
} | |
return null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment