Skip to content

Commit

Permalink
feat(update): add update script.
Browse files Browse the repository at this point in the history
  • Loading branch information
crhallberg committed Feb 3, 2023
1 parent d480420 commit 54fcf20
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# GeoNames data
US.txt
readme.txt
allCountries.txt
allCountries.zip

# Logs
logs
*.log
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ Returns the closest place object to the specified coordinates.

### Why?

I saw a lot of zip database modules on npm, but I was displeased with three aspects
I saw a lot of zip database modules on npm, but I was displeased with three aspects:

* Old data (zips uses 2010 US Census data from [geonames.org](http://www.geonames.org/), the most up-to-date I could find).
* Huge, raw data (zips' data is pre-processed so it can just load and go).
* List searching (zips organizes its data into trees instead of going down a list until a match is found or a large hash table).
- Old data (zips uses US Postcode data from [geonames.org](http://www.geonames.org/), the most up-to-date I could find).
- Huge, raw data (zips' data is pre-processed so it can just load and go).
- List searching (zips organizes its data into trees instead of going down a list until a match is found or a large hash table).

I hope you're happy too.

### Update

1. Download `US.zip` from [GeoNames Postcodes Download Server](https://download.geonames.org/export/zip/)
1. Extract `US.txt` to root.
1. `npm run update`.

*Last update: 2023-02-03*
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
{
"name": "zips",
"version": "1.1.2",
"version": "1.1.2.2023-02-03",
"description": "Light, fast, tree-based way to get cities by zipcode and location.",
"main": "index.js",
"scripts": {
"test": "mocha"
"author": "Chris Hallberg",
"homepage": "https://github.com/crhallberg/zips#readme",
"license": "MIT",
"bugs": {
"url": "https://github.com/crhallberg/zips/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/crhallberg/zips.git"
},
"main": "index.js",
"scripts": {
"update": "node ./update.js",
"test": "mocha"
},
"devDependencies": {
"mocha": "^3.5.3"
},
"keywords": [
"zipcodes",
"zip",
Expand All @@ -18,14 +28,5 @@
"zipcode",
"postal",
"code"
],
"author": "Chris Hallberg",
"license": "MIT",
"bugs": {
"url": "https://github.com/crhallberg/zips/issues"
},
"homepage": "https://github.com/crhallberg/zips#readme",
"dev-dependencies": {
"mocha": "^3.2.0"
}
]
}
54 changes: 54 additions & 0 deletions update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const fs = require('node:fs');
const readline = require('node:readline');

function parseRow(rowStr) {
const parts = rowStr.split("\t").map((str) => str.trim());
return {
country: parts[0],
zip: parts[1],
city: parts[2],
state: parts[4],
lat: parseFloat(parts[9]),
long: parseFloat(parts[10]),
};
}

const locTree = {
index: [],
zones: {},
};

const rl = readline.createInterface({ input: fs.createReadStream("./US.txt") });

rl.on("line", (line) => {
const place = parseRow(line);

const index = locTree.index.length;
locTree.index.push(place);

function addIndex(latIndex, longIndex, index) {
if (typeof locTree.zones[latIndex] == "undefined") {
locTree.zones[latIndex] = {};
}

if (typeof locTree.zones[latIndex][longIndex] == "undefined") {
locTree.zones[latIndex][longIndex] = [];
}

locTree.zones[latIndex][longIndex].push(index);
}

let latIndex = Math.floor(Math.abs(parseFloat(place.lat, 10)));
let longIndex = Math.floor(Math.abs(parseFloat(place.long, 10)));
addIndex(latIndex, longIndex, index);

latIndex = Math.ceil(Math.abs(parseFloat(place.lat, 10)));
longIndex = Math.ceil(Math.abs(parseFloat(place.long, 10)));
addIndex(latIndex, longIndex, index);
});

rl.on("close", () => {
fs.writeFileSync("data/loc-tree.json", JSON.stringify(locTree), "UTF8");

console.log("DONE");
});

0 comments on commit 54fcf20

Please sign in to comment.