Socket
Socket
Sign inDemoInstall

cityjs

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cityjs

Look for nearest city with geo coordinates - a js port of citipy


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
3.43 MB
Created
Weekly downloads
 

Readme

Source

cityjs

Simply finds the nearest city to lat/long position from a tiny database of about 50,000 cities. It's super fast and requires no remote api's (can be used offline).

Why?

Querying remote api's to find information about cities is not always an option. Maybe you have an offline raspberry pi with gps? Or maybe you have a reactive web app that needs to quickly lookup nearest cities without constantly pinging a remote api.

Data

This package uses data found here: http://download.geonames.org/export/dump By default the dataset of cities with populations > 5,000 is used. If you need to also lookup cities with smaller populations, please build cityjs from source.

This data set puts the cityjs package at around 1.8MB. This may not be ideal for some web apps, but it's small enough for most applications.

Usage

nearestCity({ latitude: number, longitude: number})

Returns a the nearest city to latitude, longitude. Provides city name, country code, lat, long, and distance from queried coordinates.

import { nearestCity } from 'cityjs'

const cityNearMe = nearestCity({ latitude: 44.0618643, longitude: -121.3188065 });

console.log(cityNearMe);

/**
 * Outputs:
 * {
 *   latitude:    44.05817
 *   longitude:   -121.31531
 *   name:        Bend
 *   countryCode: US
 *   distance:    0.00003898881741539725
 * }

nearestCities({ latitude: number, longitude: number}, k: number)

Similar to nearestCity, but returns an array of cities of length k sorted from nearest to farthest.

import { nearestCities } from 'cityjs'

const citiesNearMe = nearestCities({ latitude: 44.0618643, longitude: -121.3188065 }, 3);

console.log(citiesNearMe);

/**
 * Outputs:
 * [
 *   {
 *     latitude:    44.05817
 *     longitude:   -121.31531
 *     name:        Bend
 *     countryCode: US
 *     distance:    0.00003898881741539725
 *   },
 *   {
 *     latitude:    43.99151
 *     longitude:   -121.35836
 *     name:        Deschutes River Woods
 *     countryCode: US
 *     distance:    0.0006622218438156748
 *   },
 *   {
 *     latitude:    44.27262
 *     longitude:   -121.17392
 *     name:        Redmond
 *     countryCode: US
 *     distance:    0.0020506509923908602
 *   }
 * ]

Credit/Thanks

  • This package was heavily influence by the python package citipy.
  • Also credit to kd-tree-javascript module which is bundled with cityjs. This is where the magic happens.

TODO

  • Build utilities for grabbing geoname data
  • Implement basic geo-coordinate lookups using kd trees as seen in citipy
  • Performance improvements
  • Maybe implment proper distance comparison (might not matter)
  • Create rollup configurations
  • Implement extended functionality (eg get 5 nearest cities)
  • Create separate builds for different data sets (population > 500, 5000, etc...)
  • Add ability to pre-filter citiy list by country

FAQs

Last updated on 23 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc