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:
- 🆎 ISO 3166-1 alpha-2 code (
ZA
) - 🔤 ISO 3166-1 alpha-3 code (
ZAF
) - 3️⃣ ISO 3166-1 numeric-3 code (
710
) - 3️⃣ United Nations M49 code (
710
) - 🌐 Wikidata QID (
Q258
) - 🇺🇳 Emoji flag (🇿🇦)
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
), as well as M49 regions like Africa (002
) or Polynesia (061
).
In addition to identifiers, country-coder
can provide basic regional information:
- ☎️ Telephone Calling Codes (+44)
- 🛣 Driving Side (right, left)
- 🚗 Traffic Speed Unit (km/h, mph)
- 🇪🇺 European Union Membership
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
- 🚫 📇 Multilingual Naming: only basic English names are included; get display names 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 = ; // CommonJS import allconst iso1A2Code = iso1A2Code; // CommonJS import named// or; // ES6 import all; // ES6 import named
Quick Start
Simply pass in a [longitude, latitude]
to iso1A2Code
to get the country code.
; // returns 'GB'
To include non-country territories, pass in territory
for the level
option.
; // returns 'IM'
The same method can convert from other identifiers.
; // returns 'GB'
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
- feature(query: Location | string | number, opts?: CodingOptions): RegionFeature?
- iso1A2Code(query: Location | string | number, opts?: CodingOptions): string?
- iso1A3Code(query: Location | string | number, opts?: CodingOptions): string?
- iso1N3Code(query: Location | string | number, opts?: CodingOptions): string?
- m49Code(query: Location | string | number, opts?: CodingOptions): string?
- wikidataQID(query: Location | string | number, opts?: CodingOptions): string?
- emojiFlag(query: Location | string | number, opts?: CodingOptions): string?
- featuresContaining(query: Location | string | number, strict: boolean): [RegionFeature]
- featuresIn(id: string | number, strict: boolean): [RegionFeature]
- aggregateFeature(id: string | number): [RegionFeature]
- isIn(query: Location | string | number, bounds: string | number): boolean
- isInEuropeanUnion(query: Location | string | number): boolean
- driveSide(query: Location | string | number): string?
- roadSpeedUnit(query: Location | string | number): string?
- callingCodes(query: Location | string | number): [string]
Properties
- borders: RegionFeatureCollection - the base GeoJSON containing all features
Types
- Vec2: [number, number]
- PointGeometry: a GeoJSON Point geometry object
- PointFeature: a GeoJSON feature object with a Point geometry type
- Location: Vec2 | PointGeometry | PointFeature
- CodingOptions
- RegionFeature
- RegionFeatureProperties
- RegionFeatureCollection
Methods
# feature(query: Location | string | number, opts?: CodingOptions): RegionFeature? <>
Returns the GeoJSON feature from borders
for the given location or identifier and options, if found. Note that the geometry
of the feature may not contain its full bounds (see aggregateFeature).
; // returns United Kingdom feature; // returns {Isle of Man}; // returns null; // returns {United Kingdom}; // returns {United Kingdom}; // returns {United Kingdom}; // returns {United Kingdom}; // returns {United Kingdom}; // returns {United Kingdom}; // returns {United Kingdom}; // returns {Isle of Man}; // returns {United Kingdom} let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: -45 542 ;; // returns {United Kingdom}; // returns {United Kingdom}
# iso1A2Code(query: Location | string | number, opts?: CodingOptions): string? <>
Returns the ISO 3166-1 alpha-2 code for the given location or identifier and options, if found.
; // returns 'GB'; // returns 'IM'; // returns null; // returns 'GB'; // returns 'GB'; // returns 'GB'; // returns 'GB'; // returns 'GB'; // returns 'GB'; // returns 'IM'; // returns 'GB' let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: -45 542 ;; // returns 'GB'; // returns 'GB'
# iso1A3Code(query: Location | string | number, opts?: CodingOptions): string? <>
Returns the ISO 3166-1 alpha-3 code for the given location or identifier and options, if found.
; // returns 'GBR'; // returns 'IMN'; // returns null; // returns 'GBR'; // returns 'GBR'; // returns 'GBR'; // returns 'GBR'; // returns 'GBR'; // returns 'GBR'; // returns 'IMN'; // returns 'GBR' let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: -45 542 ;; // returns 'GBR'; // returns 'GBR'
# iso1N3Code(query: Location | string | number, opts?: CodingOptions): string? <>
Returns the ISO 3166-1 numeric-3 code for the given location or identifier and options, if found. For more comprehensive coverage, see m49Code.
; // returns '826'; // returns '833'; // returns null; // returns '826'; // returns '826'; // returns '826'; // returns '826'; // returns '826'; // returns '833'; // returns null (Africa); // returns '826' let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: -45 542 ;; // returns '826'; // returns '826'
# m49Code(query: Location | string | number, opts?: CodingOptions): string? <>
Returns the United Nations M49 code for the given location or identifier and options, if found. These codes are a superset of ISO 3166-1 numeric-3 codes, adding a subdivision (Sark) and transnational regions (e.g. Asia, Central America, Polynesia).
; // returns '826'; // returns '833'; // returns null; // returns '826'; // returns '826'; // returns '826'; // returns '826'; // returns '826'; // returns '833'; // returns '002' (Africa); // returns '826' let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: -45 542 ;; // returns '826'; // returns '826'
# wikidataQID(query: Location | string | number, opts?: CodingOptions): string? <>
Returns the Wikidata QID for the given location or identifier and options, if found.
; // returns 'Q145'; // returns 'Q9676'; // returns null; // returns 'Q145'; // returns 'Q145'; // returns 'Q145'; // returns 'Q145'; // returns 'Q145'; // returns 'Q145'; // returns 'Q9676'; // returns 'Q145' let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: -45 542 ;; // returns 'Q145'; // returns 'Q145'
# emojiFlag(query: Location | string | number, opts?: CodingOptions): string? <>
Returns the emoji flag sequence for the given location or identifier and options, if found.
; // returns '🇬🇧'; // returns '🇮🇲'; // returns null; // returns '🇬🇧'; // returns '🇬🇧'; // returns '🇬🇧'; // returns '🇬🇧'; // returns '🇬🇧'; // returns '🇬🇧'; // returns '🇮🇲'; // returns '🇬🇧' let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: -45 542 ;; // returns '🇬🇧'; // returns '🇬🇧'
# featuresContaining(query: Location | string | number, strict: boolean): [RegionFeature] <>
Returns all the the features of any type that contain or match the given location or identifier, if any. If strict
is true
then only features that are strictly containing are returned.
; // returns [{Isle of Man}, {Northern Europe}, {Europe}, {United Kingdom}]; // returns [{United Kingdom}, {Northern Europe}, {Europe}, {European Union}]; // returns [{Switzerland}, {Western Europe}, {Europe}]; // returns []; // returns [{United Kingdom}, {Northern Europe}, {Europe}, {European Union}]; // returns [{United Kingdom}, {Northern Europe}, {Europe}, {European Union}]; // returns [{United Kingdom}, {Northern Europe}, {Europe}, {European Union}]; // returns [{United Kingdom}, {Northern Europe}, {Europe}, {European Union}]; // returns [{United Kingdom}, {Northern Europe}, {Europe}, {European Union}]; // returns [{United Kingdom}, {Northern Europe}, {Europe}, {European Union}]; // returns [{United Kingdom}, {Northern Europe}, {Europe}, {European Union}]; // returns [{Northern Europe}, {Europe}]; // returns [{Northern Europe}, {Europe}, {European Union}]; // returns [{Europe}] let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: 0 -90 ;; // returns [{Antarctica}]; // returns [{Antarctica}]
# featuresIn(id: string | number, strict: boolean): [RegionFeature] <>
Returns all the the features that match or are contained within the given identifier, if any. If strict
is true
then only features that are strictly contained are returned.
; // returns [{China}, {Hong Kong}, {Macau}]; // returns [{China}, {Hong Kong}, {Macau}]; // returns [{China}, {Hong Kong}, {Macau}]; // returns [{China}, {Hong Kong}, {Macau}]; // returns [{China}, {Hong Kong}, {Macau}]; // returns [{China}, {Hong Kong}, {Macau}]; // returns [{China}, {Hong Kong}, {Macau}]; // returns [{Hong Kong}, {Macau}]
# aggregateFeature(id: string | number): [RegionFeature] <>
Returns a new feature with the properties
of the feature matching id
and the combined geometry
of it and all its component features. This step is not necessary when only accessing a feature's properties.
; // returns China, Hong Kong, and Macau as one feature; // returns China, Hong Kong, and Macau as one feature; // returns China, Hong Kong, and Macau as one feature; // returns China, Hong Kong, and Macau as one feature; // returns China, Hong Kong, and Macau as one feature; // returns China, Hong Kong, and Macau as one feature; // returns China, Hong Kong, and Macau as one feature
# isIn(query: Location | string | number, bounds: string | number): boolean <>
Returns true
if the feature matching query
is, or is within, the feature matching bounds
.
; // returns true; // returns true; // returns true; // returns false; // returns false; // returns true; // returns false; // returns true; // returns true; // returns true; // returns true; // returns true; // returns false let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: 0 515 ;; // returns true; // returns true
# isInEuropeanUnion(query: Location | string | number): boolean <>
Returns true
if the feature with the given location or identifier is found to be part of the European Union. This is a convenience method for isIn(query, 'EU')
.
; // returns true (Britain); // returns false (Isle of Man); // returns false (Switzerland); // returns false (North Pole); // returns true; // returns true; // returns true; // returns true; // returns true; // returns true; // returns true; // returns true; // returns true; // returns false; // returns false let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: 0 515 ;; // returns true (Britain); // returns true (Britain)
# driveSide(query: Location | string | number): string? <>
Returns the side of the road on which traffic drives for the given location or identifier, if found.
; // returns 'left' (Britain); // returns 'right' (Switzerland); // returns null (North Pole); // returns null; // returns 'left'; // returns 'left'; // returns 'left'; // returns 'left'; // returns 'left'; // returns 'left'; // returns 'left'; // returns 'left'; // returns 'right' let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: 0 515 ;; // returns 'left' (Britain); // returns 'left' (Britain)
# roadSpeedUnit(query: Location | string | number): string? <>
Returns the unit of speed used on traffic signs for the given location or identifier, if found.
; // returns 'mph' (Britain); // returns 'km/h' (Switzerland); // returns null (North Pole); // returns null; // returns 'mph'; // returns 'mph'; // returns 'mph'; // returns 'mph'; // returns 'mph'; // returns 'mph'; // returns 'mph'; // returns 'mph'; // returns 'km/h' let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: 0 515 ;; // returns 'mph' (Britain); // returns 'mph' (Britain)
# callingCodes(query: Location | string | number): [string] <>
Returns the full international calling code prefix of phone numbers for the given location or identifier, if any. All prefixes have a country code, with some also including an area code separated by a space character. These are commonly formatted with a preceding plus sign (e.g. +1 242
).
; // returns ['44'] (Britain); // returns [] (North Pole); // returns []; // returns ['44']; // returns ['44']; // returns ['44']; // returns ['44']; // returns ['44']; // returns ['44']; // returns ['44']; // returns ['44']; // returns ['1 242']; // returns ['1 876', '1 658'] let pointGeoJSON = type: 'Feature' geometry: type: 'Point' coordinates: 0 515 ;; // returns ['44'] (Britain); // returns ['44'] (Britain)
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 preferred geographic classification of the one to code. If no feature exists at the specified level, the feature at the next-highest level is coded, if any. The possible values map directly to thelevel
property of RegionFeatureProperties objects.world
union
: European Unionregion
: Africa, Americas, Antarctica, Asia, Europe, Oceaniasubregion
: Sub-Saharan Africa, North America, Micronesia, etc.intermediateRegion
: Eastern Africa, South America, Channel Islands, etc.country
: Ethiopia, Brazil, United States, etc.territory
: Puerto Rico, Gurnsey, Hong Kong, etc.subterritory
: Sark, Ascension Island, Diego Garcia, etc.
# RegionFeature
A GeoJSON feature representing a codable geographic area.
# RegionFeatureProperties
An object containing the attributes of a RegionFeature object.
id
:string
, a unique ID for this feature specific to country-coderiso1A2
:string
, ISO 3166-1 alpha-2 codeiso1A3
:string
, ISO 3166-1 alpha-3 codeiso1N3
:string
, ISO 3166-1 numeric-3 codem49
:string
, UN M49 codewikidata
:string
, Wikidata QIDemojiFlag
:string
, the emoji flag sequence derived from this feature's ISO 3166-1 alpha-2 codenameEn
:string
, common name in Englishaliases
:[string]
, additional identifiers which can be used to look up this featurecountry
:string
, for features entirely within a country, the ISO 3166-1 alpha-2 code for that countrygroups
:[string]
, the ISO 3166-1 alpha-2 or M49 codes of other features this feature is entirely withinmembers
:[string]
, the ISO 3166-1 alpha-2 or M49 codes of other features this feature entirely contains, the inverse ofgroups
level
:string
, the rough geographic classification of this featureworld
union
: European Unionregion
: Africa, Americas, Antarctica, Asia, Europe, Oceaniasubregion
: Sub-Saharan Africa, North America, Micronesia, etc.intermediateRegion
: Eastern Africa, South America, Channel Islands, etc.country
: Ethiopia, Brazil, United States, etc.territory
: Puerto Rico, Gurnsey, Hong Kong, etc.subterritory
: Sark, Ascension Island, Diego Garcia, etc.
isoStatus
:string
, the status of this feature's ISO 3166-1 code(s), if anyofficial
: officially-assignedexcRes
: exceptionally-reservedusrAssn
: user-assigned
driveSide
:string
, the side of the road on which traffic drives within this featureright
left
roadSpeedUnit
:string
, the speed unit used on traffic signs in this featuremph
: miles per hourkm/h
: kilometers per hour
callingCodes
:[string]
, the international calling codes for this feature, sometimes including area codes
# RegionFeatureCollection
A GeoJSON feature collection containing RegionFeature objects.