UNPKG

@geoapify/un-locode

Version:

A Node.js library for querying United Nations Location Code (UN/LOCODE) data

158 lines (112 loc) 5.15 kB
# @geoapify/un-locode A lightweight Node.js helper for querying United Nations Location Code ([UN/LOCODE](https://unece.org/trade/cefact/unlocode-code-list-country-and-territory)) data locally. Look up a single location by country and place code and get back normalized metadata, functional designations, and coordinates (when available). ## Installation ```bash npm install @geoapify/un-locode ``` ## Usage `query` is synchronous and returns either a `UnlocodeItem` object or `null`. Country and location codes must be uppercase ISO 3166-1 alpha-2 and UN/LOCODE values respectively. CommonJS: ```javascript const { query } = require('@geoapify/un-locode'); const aberdeen = query('US', 'APG'); console.log(aberdeen?.locationName); // "Aberdeen" ``` ESM / TypeScript: ```ts import { query } from '@geoapify/un-locode'; import type { UnlocodeItem } from '@geoapify/un-locode'; const aberdeen: UnlocodeItem | null = query('US', 'APG'); ``` ### Parameters - `countryCode` (`string`): ISO 3166-1 alpha-2 code (`US`, `DE`, `SG`, ...). - `locationCode` (`string`): Three-character UN/LOCODE location code (`APG`, `HAM`, ...). ### Response Example return value: ```json { "country": "US", "location": "APG", "locationName": "Aberdeen", "subdivision": "MD", "status": "AI", "functionCodes": ["3", "4"], "coordinates": { "lat": 39.5120347, "lon": -76.1643289 } } ``` - `country`: ISO country code. - `location`: UN/LOCODE location code. - `locationName`: Name without diacritics. - `subdivision`: Optional state/province value. - `status`: Two-letter UN/LOCODE status code. - `functionCodes`: Array of functional designations (see below). - `coordinates`: Optional WGS84 latitude/longitude pair. > **Note**: Some rows in the original UN/LOCODE dump lack coordinates. We enrich those rows with the [Geoapify Geocoding API](https://www.geoapify.com/geocoding-api/) when generating the bundled dataset. ### Function Codes - **1** = Port (any waterborne transport) - **2** = Rail terminal - **3** = Road terminal - **4** = Airport - **5** = Postal exchange office - **6** = Inland Clearance Depot (ICD) / dry port - **7** = Fixed transport functions (pipelines, power lines, ropeways, etc.) - **B** = Border crossing - **0** = Function unknown / not specified ### Status Codes - **AA**: Approved by a competent national government agency - **AC**: Approved by Customs Authority - **AF**: Approved by a national facilitation body - **AI**: Adopted by an international organization (e.g., IATA or ECLAC) - **AM**: Approved by the UN/LOCODE Maintenance Agency - **AQ**: Entry approved, functions not verified - **AS**: Approved by a national standardization body - **RL**: Recognized location confirmed by a gazetteer or other reference - **RN**: Request from credible national sources for locations within their country - **RQ**: Request under consideration - **UR**: Entry included on user’s request; not officially approved - **RR**: Request rejected - **QQ**: Original entry not verified since the indicated date - **XX**: Entry to be removed in the next issue of UN/LOCODE ## Caching Behavior The library lazily loads CSV files per-country and keeps them in memory for 24 hours. Long-running processes benefit from repeated queries, while the cache automatically purges stale entries. If you need tighter control (for instance, to release memory after batch processing), restart the process or load only the countries you require. ## Data Source & Updates - The repository currently ships with the UNECE **2024-2** UN/LOCODE release (published January 2025) found in `data-source/2024-2 UNLOCODE CodeList.csv`. - Geoapify’s Geocoding API is used to supplement coordinates for rows that lack latitude/longitude in the official dump. - To regenerate the JSON/CSV assets for a newer dataset: 1. Place the new CSV/XLS file inside `data-source/`. 2. Add your `API_KEY` to `src/utility/utility.ts` (used during geocoding). 3. Run `npm run generate-files` (see `BUILD.md` for details). ## Requirements - Node.js **18.0.0** or newer (the generator scripts rely on modern ECMAScript features). - npm (comes with Node) for running the provided scripts. Install dependencies once via `npm install`, then use the commands below as needed. ## Development Workflow ### Running tests ```bash npm test ``` ### Building the library ```bash npm run build-all ``` This compiles the TypeScript sources and bundles the packaged JSON assets. ### Regenerating JSON data 1. Drop the latest CSV/XLS export into the root `data-source/` folder. 2. Add your Geoapify API key to `src/utility/utility.ts`. 3. Run: ```bash npm run generate-files ``` ### Running the demo 1. Build the project (`npm run build-all`). 2. Start the demo app: ```bash npm run start:demo ``` ## Error Handling `query` returns `null` when the provided country/location combination does not exist. Make sure to guard for this in your application. ## Contributing Contributions are welcome! Please fork the repository, make your changes, and submit a pull request. See `BUILD.md` for full data-generation instructions.