npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

worldcities

v0.1.8

Published

Convenience library for looking up city and country data using GPS coordinates and by name.

Downloads

436

Readme

World Cities

Coverage Status

24,343 world cities and 252 countries.

A convenient wrapper for the GeoNames database of world cities data.

The data from the GeoNames tab seperated values files are parsed and normalized into a JSON object for quick lookup and distance calculations. The data for each city's country is also normalized, indexed by ISO_3166-1 country code, paired with the country's flag's SVG, GeoJSON of its boundaries, and relative URL to Wikipedia page.

Usage

Query for a city by GPS

const WorldCities = require('worldcities');
const city = WorldCities.getNearestCity(5.42282298420212, 100.33753796627954);

// City {
//   latitude: 5.41123,
//   longitude: 100.33543,
//   name: 'George Town',
//   population: 300000,
//   timezone: 'Asia/Kuala_Lumpur',
//   country: Country {
//     areaSquareMeters: 329750,
//     callingCode: '60',
//     capital: 'Kuala Lumpur',
//     continent: 'AS',
//     countryCode: 'MY',
//     countryCode3: 'MYS',
//     currencyCode: 'MYR',
//     currencyName: 'Ringgit',
//     languages: [
//       'ms-MY', 'en',
//       'zh',    'ta',
//       'te',    'ml',
//       'pa',    'th'
//     ],
//     name: 'Malaysia',
//     neighbors: [ 'BN', 'TH', 'ID' ],
//     population: 31528585,
//     postalCodeRegExp: '^(\\d{5})$',
//     tld: '.my',
//     wikipedia: 'Malaysia',
//     geoJSON: undefined,
//     flagSVG: undefined
//   }
// }

Query for a large city by GPS

const WorldCities = require('worldcities');
const city = WorldCities.getNearestLargeCity(17.076480407330514, -101.3674415353851);

Query for a single city by name

const city = WorldCities.getByName('Tokyo');

Query all cities matching a name

const cities = WorldCities.getByName('nEw yOrK');
assert.equal(cities.length, 3);

Get a Country by Country Code

Retrieve the country data by querying for it by the 2 letter country code. The second argument returns the Country with full data, including geoJSON and flag SVG.

const country = WorldCities.getCountry('NZ', true);
fs.writeFileSync('nz.flag.svg', country.flagSVG);

Timezones

The city data includes the IANA timezone ID in string format; ie. "America/Los_Angeles". To use these to localize dates and times to the city, it's highly recommended to use the Moment Timezone library.

For example, to convert a UNIX timestamp to a localized date time string:

const moment = require('moment-timezone');

moment(Math.floor(Date.now() / 1000), 'X')
  .tz('America/Los_Angeles')
  .format('dddd, MMMM Do YYYY, h:mm:ss a z');

// "Friday, May 1st 2020, 5:52:11 am PDT"

GeoJSON

GeoJSON objects can be visualized nicely in the http://geojson.io/ UI.

Google Maps also supports using GeoJSON as a "Data Layer" via the API.

SQLite

All of the data is also available in a compact SQLite file. It is in the source repository, but not included in the package to keep size reasonable.

[https://raw.githubusercontent.com/OpenDataFormats/worldcities/master/data/worldcities.sqlite]

Data Sources

City and country data:

GeoNames

Flag SVG data

flag-icon-css