ydn.geohash
YDN GEO module. geohash
This module provides encoding and decoding two dimensional geometric coordinates into string hash while preserving locality of the data points. This is essentially a form of Z-order curve representation of two dimensions.
See Wikipedia Geohash article for more detail.
decode(hash) Dencode geohash into a 2D coordinate.
- Parameters:
- {string} hash
- Standard geohash string representation.
- Return:
- {!Array.number}
- Return four elements array representing
[latitude, longitude, error_latitude, error_longitude]
.
encode(coordinate) Encode a 2D coordinate into geohash.
Encode a pair of latitude and longitude into standard geohash. Geohash string uses 32 lower case char of 0123456789bcdefghjkmnpqrstuvwxyz
.
Latitude range from -90 to 90.
Longitude range from -180 to -180.
Precision is positive integer number of resulting hash string. Each char in hash string encode 4 bits of latitude and longitude pair. Hence, error is 1 / Math.pow(2, 4 * precision/2) * 180
. For default precision of 12 char, the error is ±0.0000107288 or ±1.190 meters in equator.
var h = ydn.geohash.encode([37.8324, 112.5584]); var cor = ydn.geohash.dencode(h);
- Parameters:
- {!Array.number} coordinate
- A coordinate pair with optional precision given as array of
[latitude, longitude, opt_precision]
. Default value ofopt_precision
is 12.
- Return:
- {string}
- Geohash representation of given coordinate.
neighbor(hash, dir) Return neighbor geohash of given position.
Unit rectangular vector have two elements representing the first one for longitude or EAST-WEST and the second one for latitude or NORTH-SOUTH.
{ NORTH: [1, 0], NORTH_EAST: [1, 1], NORTH_WEST: [1, -1], EAST: [0, 1], SOUTH: [-1, 0], SOUTH_EAST: [0, 1], SOUTH_WEST: [-1, -1], WEST: [0, -1] };
- Parameters:
- {string} hash
- Given position.
- {!Array.number} dir
- Unit rectangular vector representation direction.
- Return:
- {string}
- Neighbor geohash.