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

latlon2country

v1.2.6

Published

Convert coordinates into the containing country

Downloads

77

Readme

LatLon2Country

LatLon2Country is a reverse geocoder, whose goal is to convert a location (latlon) into the country code in which the location lies in the browser.

It is specifically designed to work with opening_hours.js and MapComplete and borrows ideas from codegrid.

Usage

Install with npm:

npm install latlon2country

Run the code:

const coder = new CountryCoder("https://pietervdvn.github.io/latlon2country/") coder.CountryCodeFor(lon, lat)

If you want to selfhost the tiles (which is highly recommended), download tiles.zip, extract the zip to your server and point the constructor above to the correct path.

Base architecture

The client side downloads tiles with information to determine the country of a point. These tiles are statically generated by the 'generate'-scripts and are used to determine the country. Note that no country might be returned (e.g. the international waters), one country might be returned (most of the cases) or multiple countries might be returned ( contested territory). In the latter case, be very careful which one to show!

The code is specifically designed to handle multiple calls efficiently (e.g.: if a 100 points close to each other are requested at the same moment, these should only cause the correct tiles to downloaded once).

Tile format

The requested tiles all have the extension '.json'. THey however break down into two types of tiles:

  • Leaf tiles
  • 'Go Deeper'-tiles

Leaf tiles

A 'leaf'-tile is a tile with which the actual country can be determined without downloading more tiles.

If the result is uniform accross the tile (most commonly: the tile is completely within a single country), then the tile will consist of ["country_code"]

If a border goes trhough the tile, the tile will contain the actual geometries and the leaftile is a standard geojson file.

The client library will enumerate all the polygons and determine in which polygons the requested points lie. Multiple countries can be returned.

Note that the geometry-leaf-tiles have a fixed upper bound in size, in order to make processing fast (that is the entire point of this library)

Go-Deeper-tiles

THe other are tiles which contain an overview of the next step to take, e.g.

z.x.y.json [ "be", z + 1, z + 5, 0 ]

This tile is a single array, which contains the next actions to take for the upper left, upper right, bottom left and bottom right subtile respectively.

The upper left tile falls completely within belgium (so no more action is needed). To know the country of upper right one, one should download the appropriate tile at zoomlevel (z + 1). The bottom left tile is split multiple times, and the biggest subtiles there are way deeper, at 'z + 5' - so we skip all the intermediate layers and fetch the deeper tile immediately. For the bottom right tile, nothing (0) is defined, meaning international waters.

Note that in some edge cases, multiple country codes are given, e.g.:

[ "RU;UA", ... ]

This would indicate disputed territory, e.g. Crimea which is disputed and claimed by both Russia and Ukraine. The client returns those in alphabetical order. It is the responsibility of the program using this code to determine the "correct" country.