unlocodes
unlocodes is a Ruby gem that exposes the UN/LOCODE dataset (United Nations Code for Trade and Transport Locations) as a queryable in-memory registry.
The dataset is sourced from the UNECE/UNCEFACT LOCODE vocabulary, version-tagged 2025-1: https://opensource.unicc.org/un/unece/uncefact/vocab-locode/-/tags/2025-1
The full 2025-1 dataset (115,928 LOCODEs across 249 countries) is bundled inside the gem as lib/unlocodes/data/locode.jsonld and loads lazily into a typed registry on first use.
Installation
Add to your Gemfile:
gem 'unlocodes'Usage
require 'unlocodes'
Unlocodes.find('CNSHA') # => #<Unlocodes::Entry code="CNSHA" ...>
Unlocodes.find('NLRTM').functions.map(&:code) # => ["B", "R", "T", "A", "P"]
Unlocodes.where(country: 'CN').count # => 1670
Unlocodes.where(function: 'B').count # => 17912 (sea ports)
Unlocodes.where(function: 'A').count # => 9009 (airports)
Unlocodes.find('CNPDG').coordinates # => #<Coordinates lat=31.2333 lon=121.5000>Function codes
The vocabulary publishes functions as unlcdf:1..unlcdf:9. The gem maps them to the UN/LOCODE manual’s letters:
| unlcdf: | Letter | Meaning |
|---|---|---|
1 |
B |
Port (sea) |
2 |
R |
Rail terminal |
3 |
T |
Road terminal |
4 |
A |
Airport |
5 |
P |
Postal exchange office |
6 |
I |
Inland water transport |
7 |
F |
Ferry port |
8 |
V |
Pipeline |
9 |
O |
Other / border crossing |
Use the letters in where(function: …):
Unlocodes.where(function: 'B').count # sea ports
Unlocodes.where(function: %w[B A]).count # entries that are both port and airportDistances
shanghai = Unlocodes.find('CNPDG').coordinates
rotterdam = Unlocodes.find('NLRTM').coordinates
shanghai.distance_to(rotterdam) # => 8922.0 (km, great-circle)Data refresh
To refresh the bundled dataset when a new UNCEFACT edition is released:
bundle exec rake unlocodes:fetch # default tag: 2025-1
UNLOCODE_TAG=2025-2 rake unlocodes:fetchIf the upstream tag layout changes, point UNLOCODE_PATH at the full JSON-LD URL:
UNLOCODE_PATH=https://example.org/path/unlocode.jsonld rake unlocodes:fetchLicense
BSD-2-Clause. See LICENSE.