Skip to content

Commit f577741

Browse files
committed
Add download script
1 parent 8f06437 commit f577741

File tree

7 files changed

+109
-47
lines changed

7 files changed

+109
-47
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
npm-debug.log
33
cities1000.txt
4+
cities1000.zip

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cities.json

.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
# Cities of the World
2+
23
[![Creative Commons License](https://i.creativecommons.org/l/by/3.0/80x15.png)](https://creativecommons.org/licenses/by/3.0/)
34

4-
These cities comes from GeoNames Gazetteer:
5-
http://www.geonames.org
5+
These cities comes from [GeoNames Gazetteer](http://www.geonames.org).
6+
7+
> See https://www.geonames.org/datasources/ for the list of data sources.
68
79
Here is the description of the original dataset:
8-
> all cities with a population > 1000 or seats of adm div (ca 150.000) [...]
10+
11+
> *all cities with a population > 1000 or seats of adm div (ca 150.000) [...]*
912
1013
## Install
14+
1115
```
1216
npm install --save cities.json
1317
```
1418

1519
## Usage
20+
1621
Either on **node** or the **browser** (with `webpack`) it get as simple as this:
1722

1823
**ES5**
24+
1925
```
2026
const cities = require('cities.json');
2127
```
2228

2329
**ES6**
30+
2431
```
2532
import cities from 'cities.json';
2633
```
2734

2835
> Since webpack >= v2.0.0, importing of JSON files will work by default.
2936
3037
## Description
38+
3139
This Json version is an array of object of the following shape:
40+
3241
- ISO 3166-1 alpha-2 country code
3342
- name
3443
- Latitude
3544
- Longitude
45+
3646
```
3747
[
3848
{

convert.js

+47-42
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
11
var fs = require('fs');
2-
var jsonfile = require('jsonfile');
3-
var readline = require('readline');
2+
var jsonfile = require('jsonfile');
3+
var readline = require('readline');
44

55
var file = './cities.json';
6-
var cities = [], i = 0, city;
6+
var cities = [],
7+
i = 0,
8+
city;
79

8-
readline.createInterface({
9-
input: fs.createReadStream('./cities1000.txt'),
10-
output: process.stdout,
11-
terminal: false
12-
}).on('line', function(line) {
13-
city = line.split("\t");
14-
if (i !== 0) {
15-
// geonameid : integer id of record in geonames database
16-
// name : name of geographical point (utf8) varchar(200)
17-
// asciiname : name of geographical point in plain ascii characters, varchar(200)
18-
// alternatenames : alternatenames, comma separated, ascii names automatically transliterated, convenience attribute from alternatename table, varchar(10000)
19-
// latitude : latitude in decimal degrees (wgs84)
20-
// longitude : longitude in decimal degrees (wgs84)
21-
// feature class : see http://www.geonames.org/export/codes.html, char(1)
22-
// feature code : see http://www.geonames.org/export/codes.html, varchar(10)
23-
// country code : ISO-3166 2-letter country code, 2 characters
24-
// cc2 : alternate country codes, comma separated, ISO-3166 2-letter country code, 200 characters
25-
// admin1 code : fipscode (subject to change to iso code), see exceptions below, see file admin1Codes.txt for display names of this code; varchar(20)
26-
// admin2 code : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)
27-
// admin3 code : code for third level administrative division, varchar(20)
28-
// admin4 code : code for fourth level administrative division, varchar(20)
29-
// population : bigint (8 byte int)
30-
// elevation : in meters, integer
31-
// dem : digital elevation model, srtm3 or gtopo30, average elevation of 3''x3'' (ca 90mx90m) or 30''x30'' (ca 900mx900m) area in meters, integer. srtm processed by cgiar/ciat.
32-
// timezone : the iana timezone id (see file timeZone.txt) varchar(40)
33-
// modification date : date of last modification in yyyy-MM-dd format
34-
cities.push({
35-
country: city[8],
36-
name: city[1].replace('"', '').replace('"', ''),
37-
lat: city[4],
38-
lng: city[5]
39-
});
40-
}
41-
i++;
42-
}).on('close', function() {
43-
jsonfile.writeFile(file, cities, {spaces: 2}, function (err) {
44-
if (err) {
45-
console.error(err)
10+
readline
11+
.createInterface({
12+
input: fs.createReadStream('./cities1000.txt'),
13+
output: process.stdout,
14+
terminal: false,
15+
})
16+
.on('line', function (line) {
17+
city = line.split('\t');
18+
if (i !== 0) {
19+
// geonameid : integer id of record in geonames database
20+
// name : name of geographical point (utf8) varchar(200)
21+
// asciiname : name of geographical point in plain ascii characters, varchar(200)
22+
// alternatenames : alternatenames, comma separated, ascii names automatically transliterated, convenience attribute from alternatename table, varchar(10000)
23+
// latitude : latitude in decimal degrees (wgs84)
24+
// longitude : longitude in decimal degrees (wgs84)
25+
// feature class : see http://www.geonames.org/export/codes.html, char(1)
26+
// feature code : see http://www.geonames.org/export/codes.html, varchar(10)
27+
// country code : ISO-3166 2-letter country code, 2 characters
28+
// cc2 : alternate country codes, comma separated, ISO-3166 2-letter country code, 200 characters
29+
// admin1 code : fipscode (subject to change to iso code), see exceptions below, see file admin1Codes.txt for display names of this code; varchar(20)
30+
// admin2 code : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)
31+
// admin3 code : code for third level administrative division, varchar(20)
32+
// admin4 code : code for fourth level administrative division, varchar(20)
33+
// population : bigint (8 byte int)
34+
// elevation : in meters, integer
35+
// dem : digital elevation model, srtm3 or gtopo30, average elevation of 3''x3'' (ca 90mx90m) or 30''x30'' (ca 900mx900m) area in meters, integer. srtm processed by cgiar/ciat.
36+
// timezone : the iana timezone id (see file timeZone.txt) varchar(40)
37+
// modification date : date of last modification in yyyy-MM-dd format
38+
cities.push({
39+
country: city[8],
40+
name: city[1].replace('"', '').replace('"', ''),
41+
lat: city[4],
42+
lng: city[5],
43+
});
4644
}
45+
i++;
4746
})
48-
});
47+
.on('close', function () {
48+
jsonfile.writeFile(file, cities, { spaces: 2 }, function (err) {
49+
if (err) {
50+
console.error(err);
51+
}
52+
});
53+
});

download.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const http = require('https'); // or 'https' for https:// URLs
2+
const fs = require('fs');
3+
const yauzl = require('yauzl');
4+
5+
const txtFilename = 'cities1000.txt';
6+
const zipFilename = 'cities1000.zip';
7+
const zipFile = fs.createWriteStream(zipFilename);
8+
const request = http.get(
9+
`https://download.geonames.org/export/dump/${zipFilename}`,
10+
(response) => {
11+
response.pipe(zipFile);
12+
13+
zipFile.on('finish', () => {
14+
zipFile.close();
15+
console.log('Download Completed');
16+
17+
yauzl.open(zipFilename, { lazyEntries: true }, (err, zipfile) => {
18+
if (err) throw err;
19+
zipfile.readEntry();
20+
zipfile.on('entry', (entry) => {
21+
if (entry.fileName === txtFilename) {
22+
const txtFile = fs.createWriteStream(entry.fileName);
23+
zipfile.openReadStream(entry, (err, readStream) => {
24+
if (err) {
25+
throw err;
26+
}
27+
readStream.on('end', function () {
28+
zipfile.readEntry();
29+
});
30+
readStream.pipe(txtFile);
31+
});
32+
}
33+
});
34+
});
35+
});
36+
}
37+
);

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"main": "cities.json",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"convert": "node convert"
8+
"convert": "node convert",
9+
"download": "node download",
10+
"build": "npm run download && npm run convert",
11+
"prettier": "prettier -w -u ."
912
},
1013
"repository": {
1114
"type": "git",
@@ -23,6 +26,8 @@
2326
"homepage": "https://github.com/lutangar/cities.json#readme",
2427
"devDependencies": {
2528
"jsonfile": "^2.4.0",
26-
"readline": "^1.3.0"
29+
"prettier": "^2.7.1",
30+
"readline": "^1.3.0",
31+
"yauzl": "^2.10.0"
2732
}
2833
}

0 commit comments

Comments
 (0)