A modern ES6 TypeScript library to convert geographic coordinates into memorable four-word combinations for the OpenStateStack ecosystem.
- Convert geographic coordinates (latitude/longitude) to four words
- Convert Plus Codes to four words
- Decode four words back to coordinates or Plus Codes
- Support for multiple languages (planned)
- TypeScript support with full type definitions
- Modern ES6 module format
npm install @openstatestack/geo-token-code-sdk
import { GeoTokenCode } from '@openstatestack/geo-token-code-sdk';
// Create a new instance
const geoTokenCode = new GeoTokenCode();
// Encode coordinates to words
const encoded = geoTokenCode.encodeFromCoordinates(47.36618749999998, 8.523671875000009);
console.log(encoded.words); // ["redhead", "differs", "tablets", "report"]
// Encode Plus Code to words
const encodedFromPlusCode = geoTokenCode.encodeFromPlusCode('8FVC9G8F+FFF');
console.log(encodedFromPlusCode.words); // ["redhead", "differs", "tablets", "report"]
// Decode words back to coordinates
const decoded = geoTokenCode.decodeToCoordinates(["redhead", "differs", "tablets", "report"]);
console.log(decoded.coordinates); // { latitude: 47.36618749999998, longitude: 8.523671875000009 }
console.log(decoded.plusCode); // '9C3W9QCJ+2V'
You can configure the GeoTokenCode instance with options:
const geoTokenCode = new GeoTokenCode({
language: 'en', // Language for word dictionary (default: 'en')
precision: 1, // Precision level for coordinates (default: 10)
useShortCode: false // Whether to use short code format (default: false)
});
The main class for encoding and decoding geographic coordinates.
constructor(options?: GeoTokenCodeOptions)
Options:
-
language
: Language for word dictionary (default: 'en') -
precision
: Precision level for coordinates (default: 11) -
useShortCode
: Whether to use short code format (default: false)
Encode geographic coordinates to four words.
Encode a Plus Code to four words.
Decode four words back to geographic coordinates.
interface EncodingResult {
words: string[]; // Array of four words representing the location
plusCode: string; // The original plus code
confidence: number; // Confidence score (1.0 = perfect match)
}
interface DecodingResult {
plusCode: string; // The plus code representing the location
coordinates: {
latitude: number;
longitude: number;
};
confidence: number; // Confidence score (1.0 = perfect match)
}
The GeoTokenCode library uses a deterministic algorithm to convert geographic coordinates (or Plus Codes) into four memorable words:
- First, coordinates are converted to Plus Codes using Google's Open Location Code library.
- The Plus Code is then converted to a numeric value.
- This numeric value is used to select four words from a dictionary using a consistent mapping.
- The process is reversible, allowing the words to be converted back to coordinates.
This package uses ES6 modules. When importing in your project, make sure your environment supports ES modules. For Node.js, you may need to:
- Use Node.js version 14 or higher
- Add
"type": "module"
to your package.json
npm run build
npm test
OpenStateStack OSRUL-1.0
Contributions are welcome! Please feel free to submit a Pull Request.