Skip to content

Commit

Permalink
Merge pull request zeke#12 from nabilashraf/update-cities1000
Browse files Browse the repository at this point in the history
✨ updated cities1000, geoSpatial search
  • Loading branch information
zeke authored Nov 4, 2019
2 parents 61a8cda + 6b8c319 commit cb6fa56
Show file tree
Hide file tree
Showing 10 changed files with 23,201 additions and 161,607 deletions.
105 changes: 105 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Nabil Ashraf

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# all-the-cities

All the 138,398 cities of the world with a population of at least 1000 inhabitants, in a big JSON array.
All the 138,398 cities of the world with a population of at least 1000 inhabitants, in a big JSON array that is ready to be imported in MongoDB for `geoSpatialSearch`.

Derived from the [cities1000](https://www.npmjs.com/package/cities1000) npm package, which in turn came from [geonames.org data](http://download.geonames.org/export/dump/).
Derived from the [cities-with-1000](https://www.npmjs.com/package/cities-with-1000) npm package, which in turn came from [geonames.org data](http://download.geonames.org/export/dump/).

## Installation

Expand All @@ -22,6 +22,7 @@ cities.filter(city => {
})

// [{
// cityId: '5454711',
// name: 'Albuquerque',
// country: 'US',
// altCountry: '',
Expand All @@ -31,9 +32,12 @@ cities.filter(city => {
// featureCode: 'PPLA2',
// adminCode: 'NM',
// population: 545852,
// lat: 35.08449,
// lon: -106.65114
// loc: {
// type: 'Point',
// coordinates: [-106.65114, 35.084]
// }
// }, {
// cityId: '5476960',
// name: 'Los Ranchos de Albuquerque',
// country: 'US',
// altCountry: '',
Expand All @@ -43,12 +47,31 @@ cities.filter(city => {
// featureCode: 'PPL',
// adminCode: 'NM',
// population: 6024,
// lat: 35.16199,
// lon: -106.6428
// loc: {
// type: 'Point',
// coordinates: [-106.6428, 35.16199]
// }
// }]

```

## Fields available to import

```js

id - Id of the city (same in openWeatherMap)
name
altName
country
featureCode
adminCode
population
loc: { type: 'Point', coordinates: [0, 0] }

for **GEO JSON data**, a particular format is needed in MongoDB Schema as written in loc field above

```

## Tests

```sh
Expand All @@ -62,7 +85,7 @@ None

## Dev Dependencies

- [cities1000](https://github.com/substack/cities1000): lat/lon, names of cities with over 1000 people
- [cities-with-1000](https://github.com/nabilashraf/cities1000): lat/lon, names of cities with over 1000 people
- [tape](https://github.com/substack/tape): tap-producing test harness for node and browsers
- [split2](https://github.com/mcollina/split2): split a Text Stream into a Line Stream, using Stream 3
- [through2](https://github.com/rvagg/through2): A tiny wrapper around Node streams2 Transform to avoid explicit subclassing noise
Expand All @@ -72,4 +95,4 @@ None

MIT

_Generated by [package-json-to-readme](https://github.com/zeke/package-json-to-readme)_

88 changes: 45 additions & 43 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs')
const split = require('split2')
const through = require('through2')
const cities = require('cities1000')
const cities = require('cities-with-1000');
const Pbf = require('pbf')

var pbf = new Pbf()
Expand All @@ -12,54 +12,56 @@ var lastLat = 0
var lastLon = 0

var rowStream = through(function (line, enc, next) {
var row = line.toString().split('\t').reduce(function (acc, x, ix) {
var key = cities.fields[ix]
if (key === 'alternativeNames') x = x.split(',')
if (key === 'lat' || key === 'lon') x = parseFloat(x)
if (key === 'elevation') x = x ? parseInt(x, 10) : undefined
if (key === 'population') x = x ? parseInt(x, 10) : undefined

acc[key] = x
return acc
}, {})
if (!row.id) return

pbf.writeRawMessage(writeCity, row)

next()
var row = line.toString().split('\t').reduce(function (acc, x, ix) {
var key = cities.fields[ix]
if (key === 'alternativeNames') x = x.split(',')
if (key === 'lat' || key === 'lon') x = parseFloat(x)
if (key === 'elevation') x = x ? parseInt(x, 10) : undefined
if (key === 'population') x = x ? parseInt(x, 10) : undefined

acc[key] = x
return acc
}, {})
if (!row.id) return
pbf.writeRawMessage(writeCity, row)

next()
})

function writeCity(city, pbf) {
pbf.writeStringField(1, city.name)
pbf.writeStringField(2, city.country)

if (city.altCountry && city.altCountry !== city.country)
pbf.writeStringField(3, city.altCountry)

if (city.municipality)
pbf.writeStringField(4, city.municipality)

if (city.municipalitySubdivision)
pbf.writeStringField(5, city.municipalitySubdivision)

pbf.writeStringField(6, city.featureCode)
pbf.writeStringField(7, city.adminCode)

if (city.population)
pbf.writeVarintField(8, city.population)

const lat = Math.round(1e5 * city.lat)
const lon = Math.round(1e5 * city.lon)
pbf.writeSVarintField(9, lat - lastLat)
pbf.writeSVarintField(10, lon - lastLon)
lastLat = lat
lastLon = lon
pbf.writeSVarintField(1, city.id)
pbf.writeStringField(2, city.name)
pbf.writeStringField(3, city.country)

if (city.altCountry && city.altCountry !== city.country)
pbf.writeStringField(4, city.altCountry)

if (city.municipality)
pbf.writeStringField(5, city.municipality)

if (city.municipalitySubdivision)
pbf.writeStringField(6, city.municipalitySubdivision)

pbf.writeStringField(7, city.featureCode)
pbf.writeStringField(8, city.adminCode)

if (city.population)
pbf.writeVarintField(9, city.population)

const lat = Math.round(1e5 * city.lat)
const lon = Math.round(1e5 * city.lon)
pbf.writeSVarintField(10, lon - lastLon)
pbf.writeSVarintField(11, lat - lastLat)


lastLat = lat
lastLon = lon
}

fs.createReadStream(cities.file)
.pipe(split())
.pipe(rowStream)
.pipe(split())
.pipe(rowStream)

rowStream.on('finish', function () {
process.stdout.write(Buffer.from(pbf.finish()))
process.stdout.write(Buffer.from(pbf.finish()))
})
Loading

0 comments on commit cb6fa56

Please sign in to comment.