MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / geohashBoundingBox

Function geohashBoundingBox

src/geohash_helper.cpp:95–113  ·  view source on GitHub ↗

Return the bounding box of the search area by shape (see geohash.h GeoShape) * bounds[0] - bounds[2] is the minimum and maximum longitude * while bounds[1] - bounds[3] is the minimum and maximum latitude. * since the higher the latitude, the shorter the arc length, the box shape is as follows * (left and right edges are actually bent), as shown in the following diagram: * * \-------------

Source from the content-addressed store, hash-verified

93 * Northern Hemisphere Southern Hemisphere Around the equator
94 */
95int geohashBoundingBox(GeoShape *shape, double *bounds) {
96 if (!bounds) return 0;
97 double longitude = shape->xy[0];
98 double latitude = shape->xy[1];
99 double height = shape->conversion * (shape->type == CIRCULAR_TYPE ? shape->t.radius : shape->t.r.height/2);
100 double width = shape->conversion * (shape->type == CIRCULAR_TYPE ? shape->t.radius : shape->t.r.width/2);
101
102 const double lat_delta = rad_deg(height/EARTH_RADIUS_IN_METERS);
103 const double long_delta_top = rad_deg(width/EARTH_RADIUS_IN_METERS/cos(deg_rad(latitude+lat_delta)));
104 const double long_delta_bottom = rad_deg(width/EARTH_RADIUS_IN_METERS/cos(deg_rad(latitude-lat_delta)));
105 /* The directions of the northern and southern hemispheres
106 * are opposite, so we choice different points as min/max long/lat */
107 int southern_hemisphere = latitude < 0 ? 1 : 0;
108 bounds[0] = southern_hemisphere ? longitude-long_delta_bottom : longitude-long_delta_top;
109 bounds[2] = southern_hemisphere ? longitude+long_delta_bottom : longitude+long_delta_top;
110 bounds[1] = latitude - lat_delta;
111 bounds[3] = latitude + lat_delta;
112 return 1;
113}
114
115/* Calculate a set of areas (center + 8) that are able to cover a range query
116 * for the specified position and shape (see geohash.h GeoShape).

Callers 1

Calls 2

rad_degFunction · 0.85
deg_radFunction · 0.85

Tested by

no test coverage detected