geofire-common
Version:
Location-based querying and filtering using Firebase
125 lines (124 loc) • 5.72 kB
TypeScript
export declare const GEOHASH_PRECISION = 10;
export declare const BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz";
export declare const EARTH_MERI_CIRCUMFERENCE = 40007860;
export declare const METERS_PER_DEGREE_LATITUDE = 110574;
export declare const BITS_PER_CHAR = 5;
export declare const MAXIMUM_BITS_PRECISION: number;
export declare const EARTH_EQ_RADIUS = 6378137;
export declare const E2 = 0.00669447819799;
export declare const EPSILON = 1e-12;
export declare type Geopoint = [number, number];
export declare type Geohash = string;
export declare type GeohashRange = [Geohash, Geohash];
/**
* Validates the inputted key and throws an error if it is invalid.
*
* @param key The key to be verified.
*/
export declare function validateKey(key: string): void;
/**
* Validates the inputted location and throws an error if it is invalid.
*
* @param location The [latitude, longitude] pair to be verified.
*/
export declare function validateLocation(location: Geopoint): void;
/**
* Validates the inputted geohash and throws an error if it is invalid.
*
* @param geohash The geohash to be validated.
*/
export declare function validateGeohash(geohash: Geohash): void;
/**
* Converts degrees to radians.
*
* @param degrees The number of degrees to be converted to radians.
* @returns The number of radians equal to the inputted number of degrees.
*/
export declare function degreesToRadians(degrees: number): number;
/**
* Generates a geohash of the specified precision/string length from the [latitude, longitude]
* pair, specified as an array.
*
* @param location The [latitude, longitude] pair to encode into a geohash.
* @param precision The length of the geohash to create. If no precision is specified, the
* global default is used.
* @returns The geohash of the inputted location.
*/
export declare function geohashForLocation(location: Geopoint, precision?: number): Geohash;
/**
* Calculates the number of degrees a given distance is at a given latitude.
*
* @param distance The distance to convert.
* @param latitude The latitude at which to calculate.
* @returns The number of degrees the distance corresponds to.
*/
export declare function metersToLongitudeDegrees(distance: number, latitude: number): number;
/**
* Calculates the bits necessary to reach a given resolution, in meters, for the longitude at a
* given latitude.
*
* @param resolution The desired resolution.
* @param latitude The latitude used in the conversion.
* @return The bits necessary to reach a given resolution, in meters.
*/
export declare function longitudeBitsForResolution(resolution: number, latitude: number): number;
/**
* Calculates the bits necessary to reach a given resolution, in meters, for the latitude.
*
* @param resolution The bits necessary to reach a given resolution, in meters.
* @returns Bits necessary to reach a given resolution, in meters, for the latitude.
*/
export declare function latitudeBitsForResolution(resolution: number): number;
/**
* Wraps the longitude to [-180,180].
*
* @param longitude The longitude to wrap.
* @returns longitude The resulting longitude.
*/
export declare function wrapLongitude(longitude: number): number;
/**
* Calculates the maximum number of bits of a geohash to get a bounding box that is larger than a
* given size at the given coordinate.
*
* @param coordinate The coordinate as a [latitude, longitude] pair.
* @param size The size of the bounding box.
* @returns The number of bits necessary for the geohash.
*/
export declare function boundingBoxBits(coordinate: Geopoint, size: number): number;
/**
* Calculates eight points on the bounding box and the center of a given circle. At least one
* geohash of these nine coordinates, truncated to a precision of at most radius, are guaranteed
* to be prefixes of any geohash that lies within the circle.
*
* @param center The center given as [latitude, longitude].
* @param radius The radius of the circle in meters.
* @returns The center of the box, and the eight bounding box points.
*/
export declare function boundingBoxCoordinates(center: Geopoint, radius: number): [Geopoint, Geopoint, Geopoint, Geopoint, Geopoint, Geopoint, Geopoint, Geopoint, Geopoint];
/**
* Calculates the bounding box query for a geohash with x bits precision.
*
* @param geohash The geohash whose bounding box query to generate.
* @param bits The number of bits of precision.
* @returns A [start, end] pair of geohashes.
*/
export declare function geohashQuery(geohash: Geohash, bits: number): GeohashRange;
/**
* Calculates a set of query bounds to fully contain a given circle, each being a [start, end] pair
* where any geohash is guaranteed to be lexicographically larger than start and smaller than end.
*
* @param center The center given as [latitude, longitude] pair.
* @param radius The radius of the circle.
* @return An array of geohash query bounds, each containing a [start, end] pair.
*/
export declare function geohashQueryBounds(center: Geopoint, radius: number): GeohashRange[];
/**
* Method which calculates the distance, in kilometers, between two locations,
* via the Haversine formula. Note that this is approximate due to the fact that the
* Earth's radius varies between 6356.752 km and 6378.137 km.
*
* @param location1 The [latitude, longitude] pair of the first location.
* @param location2 The [latitude, longitude] pair of the second location.
* @returns The distance, in kilometers, between the inputted locations.
*/
export declare function distanceBetween(location1: Geopoint, location2: Geopoint): number;