@geoapify/un-locode

中文文档
1.0.3发布于 2025/11/14MITTypeScriptESM + CJS近 30 天下载 1.3万

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

原始使用文档
文档目录16 节
  1. Installation
  2. Usage
  3. Parameters
  4. Response
  5. Function Codes
  6. Status Codes
  7. Caching Behavior
  8. Data Source & Updates
  9. Requirements
  10. Development Workflow
  11. Running tests
  12. Building the library
  13. Regenerating JSON data
  14. Running the demo
  15. Error Handling
  16. Contributing

@geoapify/un-locode

A lightweight Node.js helper for querying United Nations Location Code (UN/LOCODE) data locally. Look up a single location by country and place code and get back normalized metadata, functional designations, and coordinates (when available).

Installation

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:

const { query } = require('@geoapify/un-locode');

const aberdeen = query('US', 'APG');
console.log(aberdeen?.locationName); // "Aberdeen"

ESM / TypeScript:

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:

{
  "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 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

npm test

Building the library

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:

    npm run generate-files
    

Running the demo

  1. Build the project (npm run build-all).

  2. Start the demo app:

    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.

版本与发布记录

@geoapify/un-locode 共发布 4 个版本,首次发布于 2024/10/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.3latest2025/11/141 年前
1.0.22024/10/302 年前
1.0.12024/10/302 年前
1.0.02024/10/282 年前

关于 @geoapify/un-locode 的常见问题

@geoapify/un-locode 是什么?

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

如何安装 @geoapify/un-locode?

在项目目录运行 npm install @geoapify/un-locode,或使用 pnpm add / yarn add / bun add。

@geoapify/un-locode 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@geoapify/un-locode 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@geoapify/un-locode 可以商用吗?

@geoapify/un-locode 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@geoapify/un-locode 的下载量有多少?

根据 npm 官方下载统计,@geoapify/un-locode 在 2026/10/16 – 2026/11/14 这 30 天内被下载 13,134 次,日均约 438 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@geoapify/un-locode 还在维护吗?

最新版本 1.0.3 发布于 2025/11/14(1 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/10/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。