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

cityjs

v1.0.0

Published

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

Downloads

33

Readme

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

  • [x] Build utilities for grabbing geoname data
  • [x] Implement basic geo-coordinate lookups using kd trees as seen in citipy
  • [x] Performance improvements
  • [x] Maybe implment proper distance comparison (might not matter)
  • [x] Create rollup configurations
  • [x] 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