country-coder
📍 ➡️ 🇩🇰 Convert longitude-latitude pairs to ISO 3166-1 codes quickly and locally
What is it?
country-coder is a lightweight package that looks up region identifiers for geographic points without calling a server. It can code and convert between several common IDs:
Results can optionally include non-country ISO 3166-1 features, such as Puerto Rico (PR) or the Isle of Man (IM). Some unofficial yet exceptionally-reserved or user-assigned ISO codes are also supported, such as the European Union (EU) and Kosovo (XK).
Advantages
Client-side coding has a number of benefits over server-side solutions:
- ✅ 🚅 Performance: get fast, reliable results at scale
- ✅ ✌️ Ease of Use: forget async callbacks, network errors, API keys, and rate limits
- ✅ 🕶 Privacy: keep your location data on-device
- ✅ 📴 Offline Workflows: deploy to connection-challenged environments
Caveats
country-coder prioritizes package size and lookup speed over precision. Thus, it's not suitable for some situations and use cases:
- 🚫 🛂 Disputed Borders: only one country is coded per point, roughly the "de facto controlling country"
- 🚫 🚢 Maritime Borders: only points on land are supported; borders over water are highly generalized
- 🚫 🖋 Complex Borders: land borders are of varying detail and may be imprecise at granular scales
- 🚫 🧩 Country Subdivisions: provinces and similar features under ISO 3166-2 cannot be coded
- 🚫 📇 Naming: feature names are omitted; get them via another package or the Wikidata API
- 🚫 📐 Spatial Operations: a feature's calculated area, bounding box, etc. will likely be inaccurate
- 🚫 🗺 Mapmaking: the border data is not intended for rendering
Installing
npm install country-coder
This library is available in both ES5/CommonJS and ES6 module formats.
const CountryCoder = require('country-coder').CountryCoder;
import { CountryCoder } from 'country-coder';
Quick Start
Simply pass in a [longitude, latitude] to iso1A2Code to get the country code.
const coder = new CountryCoder();
coder.iso1A2Code([-4.5, 54.2]);
To include non-country regions, pass in region for the level option.
const coder = new CountryCoder();
coder.iso1A2Code([-4.5, 54.2], { level: 'region' });
The same method can convert from other identifiers.
const coder = new CountryCoder();
coder.iso1A2Code('Q145');
Read the full API reference to see everything country-coder can do.
Contributing
This package is kept intentionally minimal. However, if you find a bug or have an interesting idea for an enhancement, feel free to open an Issue and/or Pull Request.
API Reference
Methods
- new CountryCoder() constructor
- feature(arg: string | Location, opts?: CodingOptions): RegionFeature?
- iso1A2Code(arg: string | Location, opts?: CodingOptions): string?
- iso1A3Code(arg: string | Location, opts?: CodingOptions): string?
- iso1N3Code(arg: string | Location, opts?: CodingOptions): string?
- wikidataQID(arg: string | Location, opts?: CodingOptions): string?
- emojiFlag(arg: string | Location, opts?: CodingOptions): string?
- features(loc: Location): [RegionFeature]
- iso1A2Codes(loc: Location): [string]
- isInEuropeanUnion(arg: string | Location): boolean
Properties
- borders: RegionFeatureCollection - the base GeoJSON containing all features
Types
Methods
# new CountryCoder()
<>
Constructs a new CountryCoder.
const coder = new CountryCoder();
# feature(arg: string | Location, opts?: CodingOptions): RegionFeature?
<>
Returns the GeoJSON feature from borders for the given location or identifier and options, if found.
const coder = new CountryCoder();
coder.feature([-4.5, 54.2]);
coder.feature([-4.5, 54.2], { level: 'region' });
coder.feature([0, 90]);
coder.feature('GB');
coder.feature('GBR');
coder.feature('826');
coder.feature('Q145');
coder.feature('🇬🇧');
coder.feature('UK');
coder.feature('IM');
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [-4.5, 54.2] } };
coder.feature(pointGeoJSON);
coder.feature(pointGeoJSON.geometry);
# iso1A2Code(arg: string | Location, opts?: CodingOptions): string?
<>
Returns the ISO 3166-1 alpha-2 code for the given location or identifier and options, if found.
const coder = new CountryCoder();
coder.iso1A2Code([-4.5, 54.2]);
coder.iso1A2Code([-4.5, 54.2], { level: 'region' });
coder.iso1A2Code([0, 90]);
coder.iso1A2Code('GBR');
coder.iso1A2Code('826');
coder.iso1A2Code('Q145');
coder.iso1A2Code('🇬🇧');
coder.iso1A2Code('UK');
coder.iso1A2Code('IMN');
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [-4.5, 54.2] } };
coder.iso1A2Code(pointGeoJSON);
coder.iso1A2Code(pointGeoJSON.geometry);
# iso1A3Code(arg: string | Location, opts?: CodingOptions): string?
<>
Returns the ISO 3166-1 alpha-3 code for the given location or identifier and options, if found.
const coder = new CountryCoder();
coder.iso1A3Code([-4.5, 54.2]);
coder.iso1A3Code([-4.5, 54.2], { level: 'region' });
coder.iso1A3Code([0, 90]);
coder.iso1A3Code('GB');
coder.iso1A3Code('826');
coder.iso1A3Code('Q145');
coder.iso1A3Code('🇬🇧');
coder.iso1A3Code('UK');
coder.iso1A3Code('IM');
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [-4.5, 54.2] } };
coder.iso1A3Code(pointGeoJSON);
coder.iso1A3Code(pointGeoJSON.geometry);
# iso1N3Code(arg: string | Location, opts?: CodingOptions): string?
<>
Returns the ISO 3166-1 numeric-3 code for the given location or identifier and options, if found.
const coder = new CountryCoder();
coder.iso1N3Code([-4.5, 54.2]);
coder.iso1N3Code([-4.5, 54.2], { level: 'region' });
coder.iso1N3Code([0, 90]);
coder.iso1N3Code('GB');
coder.iso1N3Code('GBR');
coder.iso1N3Code('Q145');
coder.iso1N3Code('🇬🇧');
coder.iso1N3Code('UK');
coder.iso1N3Code('IM');
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [-4.5, 54.2] } };
coder.iso1N3Code(pointGeoJSON);
coder.iso1N3Code(pointGeoJSON.geometry);
# wikidataQID(arg: string | Location, opts?: CodingOptions): string?
<>
Returns the Wikidata QID for the given location or identifier and options, if found.
const coder = new CountryCoder();
coder.wikidataQID([-4.5, 54.2]);
coder.wikidataQID([-4.5, 54.2], { level: 'region' });
coder.wikidataQID([0, 90]);
coder.wikidataQID('GB');
coder.wikidataQID('GBR');
coder.wikidataQID('826');
coder.wikidataQID('🇬🇧');
coder.wikidataQID('UK');
coder.wikidataQID('IM');
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [-4.5, 54.2] } };
coder.wikidataQID(pointGeoJSON);
coder.wikidataQID(pointGeoJSON.geometry);
# emojiFlag(arg: string | Location, opts?: CodingOptions): string?
<>
Returns the emoji flag sequence for the given location or identifier and options, if found.
const coder = new CountryCoder();
coder.emojiFlag([-4.5, 54.2]);
coder.emojiFlag([-4.5, 54.2], { level: 'region' });
coder.emojiFlag([0, 90]);
coder.emojiFlag('GB');
coder.emojiFlag('GBR');
coder.emojiFlag('826');
coder.emojiFlag('Q145');
coder.emojiFlag('UK');
coder.emojiFlag('IM');
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [-4.5, 54.2] } };
coder.emojiFlag(pointGeoJSON);
coder.emojiFlag(pointGeoJSON.geometry);
# features(loc: Location): [RegionFeature]
<>
Returns all the the features containing the given location.
const coder = new CountryCoder();
coder.features([-4.5, 54.2]);
coder.features([0, 51.5]);
coder.features([6.1, 46.2]);
coder.features([0, 90]);
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [6.1, 46.2] } };
coder.features(pointGeoJSON);
coder.features(pointGeoJSON.geometry);
# iso1A2Codes(loc: Location): [string]
<>
Returns the ISO 3166-1 alpha-2 codes for all the the features containing the given location.
const coder = new CountryCoder();
coder.iso1A2Codes([-4.5, 54.2]);
coder.iso1A2Codes([0, 51.5]);
coder.iso1A2Codes([6.1, 46.2]);
coder.iso1A2Codes([0, 90]);
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [-4.5, 54.2] } };
coder.iso1A2Codes(pointGeoJSON);
coder.iso1A2Codes(pointGeoJSON.geometry);
# isInEuropeanUnion(arg: string | Location): boolean
<>
Returns true if the feature with the given location or identifier is found to be part of the European Union.
const coder = new CountryCoder();
coder.isInEuropeanUnion([0, 51.5]);
coder.isInEuropeanUnion([-4.5, 54.2]);
coder.isInEuropeanUnion([6.1, 46.2]);
coder.isInEuropeanUnion([0, 90]);
coder.isInEuropeanUnion('EU');
coder.isInEuropeanUnion('GB');
coder.isInEuropeanUnion('GBR');
coder.isInEuropeanUnion('826');
coder.isInEuropeanUnion('Q145');
coder.isInEuropeanUnion('🇬🇧');
coder.isInEuropeanUnion('UK');
coder.isInEuropeanUnion('IM');
coder.isInEuropeanUnion('CH');
let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [0, 51.5] } };
coder.isInEuropeanUnion(pointGeoJSON);
coder.isInEuropeanUnion(pointGeoJSON.geometry);
Properties
# borders: RegionFeatureCollection
<>
The base GeoJSON feature collection used for feature lookup. While this property is public, modifying it is not recommended and may have unintended effects.
Types
# Vec2
An array of two numbers as [longitude, latitude] referenced to the WGS 84 datum.
[number, number]
# PointGeometry
GeoJSON Point geometry as specified by RFC 7946.
# PointFeature
A GeoJSON Feature with Point geometry as specified by RFC 7946.
# Location
A geographic location in one of the supported formats.
Vec2 | PointGeometry | PointFeature
# CodingOptions
An object containing options used for geocoding.
level: string, for overlapping features, the division level of the one to get
country (default): the "sovereign state" feature
region: the lowest-level feature with an official or user-assigned ISO code
# RegionFeature
A GeoJSON feature representing a codable geographic area.
# RegionFeatureProperties
An object containing the attributes of a RegionFeature object.
iso1A2: string, ISO 3166-1 alpha-2 code
iso1A3: string, ISO 3166-1 alpha-3 code
iso1N3: string, ISO 3166-1 numeric-3 code
wikidata: string, Wikidata QID
emojiFlag: string, the emoji flag sequence derived from this feature's ISO 3166-1 alpha-2 code
aliases: [string], additional identifiers which can be used to look up this feature
country: string, for features entirely within a country, the ISO 3166-1 alpha-2 code for that country
groups: [string], the ISO 3166-1 alpha-2 codes of other features this feature is entirely within, other than its country
isoStatus: string, the status of this feature's ISO 3166-1 code(s) if they are not officially-assigned
excRes: exceptionally-reserved
usrAssn: user-assigned
# RegionFeatureCollection
A GeoJSON feature collection containing RegionFeature objects.