Search code examples
redisgeolocationgeometrygeocodinggeospatial

What's the difference between "area" and "BoundingBox" from Redis's source code


http://download.redis.io/redis-stable/deps/geohash-int/geohash_helper.c From the URL above, we know there are two conceptions , one is geohashBoundingBox ,and another is area,My questions is what's the difference between them and why we need them both? Also why sentence "geohashGetCoordRange(&long_range, &lat_range);" is called two times?

GeoHashRadius geohashGetAreasByRadius(double longitude, double latitude,    double radius_meters) {
GeoHashRange long_range, lat_range;
GeoHashRadius radius = { { 0 } };
GeoHashBits hash = { 0 };
GeoHashNeighbors neighbors = { { 0 } };
GeoHashArea area = { { 0 } };
double min_lon, max_lon, min_lat, max_lat;
double bounds[4];
int steps;

geohashBoundingBox(longitude, latitude, radius_meters, bounds);
min_lon = bounds[0];
min_lat = bounds[1];
max_lon = bounds[2];
max_lat = bounds[3];

steps = geohashEstimateStepsByRadius(radius_meters,latitude);

geohashGetCoordRange(&long_range, &lat_range);
geohashEncode(&long_range, &lat_range, longitude, latitude, steps, &hash);
geohashNeighbors(&hash, &neighbors);
geohashGetCoordRange(&long_range, &lat_range);
geohashDecode(long_range, lat_range, hash, &area);

if (area.latitude.min < min_lat) {
    GZERO(neighbors.south);
    GZERO(neighbors.south_west);
    GZERO(neighbors.south_east);
}
if (area.latitude.max > max_lat) {
    GZERO(neighbors.north);
    GZERO(neighbors.north_east);
    GZERO(neighbors.north_west);
}
if (area.longitude.min < min_lon) {
    GZERO(neighbors.west);
    GZERO(neighbors.south_west);
    GZERO(neighbors.north_west);
}
if (area.longitude.max > max_lon) {
    GZERO(neighbors.east);
    GZERO(neighbors.south_east);
    GZERO(neighbors.north_east);
}
radius.hash = hash;
radius.neighbors = neighbors;
radius.area = area;
return radius;

}


Solution

  • A bonding box in general is the smallest rectangular box that will contain an object. I can't speak to the exact function of GeoHashArea in redis, but since you imply that they have a similar purpose, if they both represent a geographic area then GeoHashArea will most certainly be a more detailed polygonal representation of an area than a simple rectangle like geohashBoundingBox.

    For your second question, presumably, since the variables long_range and lat_range are passed by reference, there's a chance that

    geohashEncode(&long_range, &lat_range, longitude, latitude, steps, &hash);
    

    modifies their value and so the function geohashGetCoordRange is called again on the different values.