Geohash Area Utilities
The `geohash-area` library provides specialized utilities for working with geohashes, primarily focused on generating and querying geohashes for rectangular regions and arbitrary areas. It is designed to facilitate efficient spatial lookups in databases by producing a set of geohashes that cover a specific geographical extent. The package is currently at version 0.1.0, indicating an early development stage, and while a specific release cadence is not defined, its focused scope suggests a stable, yet evolving, set of core functionalities. Key differentiators include its unique `rect` function for radial geohash generation and an `area` function that supports recursively determining geohashes for complex, user-defined geographical boundaries via a callback, setting it apart from more generic geohash encoding/decoding libraries. It ships with comprehensive TypeScript type definitions, ensuring a robust development experience.
Common errors
-
TypeError: geohash.encode is not a function
cause Attempting to import individual functions as named exports (e.g., `import { encode } from 'geohash-area'`) instead of importing the entire module namespace or default export.fixUse a module namespace import: `import * as geohash from 'geohash-area';` and then access the function as `geohash.encode(...)`. -
RangeError: precision must be between 1 and 12
cause The `precision` parameter (geohash length) provided to functions like `encode`, `rect`, or `area` is outside the acceptable range (typically 1 to 12).fixEnsure the `precision` value passed to geohash functions is an integer between 1 and 12, inclusive. Refer to geohash standards for appropriate precision levels based on desired accuracy. -
TypeError: fn is not a function
cause The `fn` parameter for `geohash.area` was not provided or was not a callable function, leading to an attempt to invoke an undefined or non-function value.fixEnsure that a valid callback function is passed as the first argument to `geohash.area`. This function should accept a geohash string and return `0`, `1`, or `2` based on its relation to the area of interest.
Warnings
- breaking As a 0.1.0 version package, the API surface (`geohash.rect`, `geohash.area`, etc.) may not be stable and could introduce breaking changes in minor or patch releases without following strict semantic versioning practices.
- gotcha The `geohash.distance` function explicitly returns distance in *miles*. This can be a common source of error for developers expecting kilometers or needing a different unit without explicit conversion.
- gotcha The `geohash.area` function requires a recursive callback function (`fn`) to determine if a geohash is within the area of interest. Incorrectly implementing this `fn` (e.g., returning `1` for partial inclusion or `2` for full inclusion) can lead to inaccurate or incomplete area coverage.
- gotcha The `precision` parameter in `encode`, `rect`, and `area` functions directly impacts the granularity and number of geohashes generated. Very high precision can lead to a large number of geohashes, potentially causing performance issues or excessive database queries.
Install
-
npm install geohash-area -
yarn add geohash-area -
pnpm add geohash-area
Imports
- geohash
const geohash = require('geohash-area');import * as geohash from 'geohash-area';
- rect
import { rect } from 'geohash-area';import * as geohash from 'geohash-area'; const geohashes = geohash.rect(34.0522, -118.2437, 10, 6);
- GeohashString
import { GeohashString } from 'geohash-area';import * as geohash from 'geohash-area'; function processGeohash(hash: string): void { /* ... */ }
Quickstart
import * as geohash from 'geohash-area';
// 1. Encode a point to a geohash
const lat = 34.0522;
const lng = -118.2437;
const precision = 6;
const encodedHash = geohash.encode(lat, lng, precision);
console.log(`Encoded Geohash for (${lat}, ${lng}) with precision ${precision}: ${encodedHash}`);
// 2. Decode a geohash to its bounding box and midpoint
const decodedInfo = geohash.decode(encodedHash);
console.log('Decoded Geohash info:', decodedInfo);
// 3. Generate geohashes for a rectangular area around a point
const distanceMiles = 5;
const areaHashes = geohash.rect(lat, lng, distanceMiles, precision);
console.log(`Geohashes covering a ${distanceMiles}-mile radius around (${lat}, ${lng}):`, areaHashes.slice(0, 5), `... (${areaHashes.length} total)`);
// 4. Calculate distance between two points (in miles)
const point1 = { lat: 34.0522, lng: -118.2437 };
const point2 = { lat: 34.0000, lng: -118.2000 };
const dist = geohash.distance(point1, point2);
console.log(`Distance between (${point1.lat}, ${point1.lng}) and (${point2.lat}, ${point2.lng}): ${dist.toFixed(2)} miles`);