PostGIS  2.1.10dev-r@@SVN_REVISION@@
 char* lwgeom_geohash ( const LWGEOM * lwgeom, int precision )

Calculate the GeoHash (http://geohash.org) string for a geometry.

Caller must free.

Definition at line 828 of file lwalgorithm.c.

Referenced by ST_GeoHash(), and test_geohash().

829 {
830  GBOX gbox;
831  GBOX gbox_bounds;
832  double lat, lon;
833  int result;
834
835  gbox_init(&gbox);
836  gbox_init(&gbox_bounds);
837
838  result = lwgeom_calculate_gbox_cartesian(lwgeom, &gbox);
839  if ( result == LW_FAILURE ) return NULL;
840
841  /* Return error if we are being fed something outside our working bounds */
842  if ( gbox.xmin < -180 || gbox.ymin < -90 || gbox.xmax > 180 || gbox.ymax > 90 )
843  {
844  lwerror("Geohash requires inputs in decimal degrees.");
845  return NULL;
846  }
847
848  /* What is the center of our geometry bounds? We'll use that to
849  ** approximate location. */
850  lon = gbox.xmin + (gbox.xmax - gbox.xmin) / 2;
851  lat = gbox.ymin + (gbox.ymax - gbox.ymin) / 2;
852
853  if ( precision <= 0 )
854  {
855  precision = lwgeom_geohash_precision(gbox, &gbox_bounds);
856  }
857
858  /*
859  ** Return the geohash of the center, with a precision determined by the
860  ** extent of the bounds.
861  ** Possible change: return the point at the center of the precision bounds?
862  */
863  return geohash_point(lon, lat, precision);
864 }
double xmax
Definition: liblwgeom.h:249
int lwgeom_geohash_precision(GBOX bbox, GBOX *bounds)
Definition: lwalgorithm.c:743
char ** result
Definition: liblwgeom.h:218
#define LW_FAILURE
Definition: liblwgeom.h:54
char * geohash_point(double longitude, double latitude, int precision)
Definition: lwalgorithm.c:582
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
double ymin
Definition: liblwgeom.h:250
void gbox_init(GBOX *gbox)
Zero out all the entries in the GBOX.
Definition: g_box.c:34
double xmin
Definition: liblwgeom.h:248
double ymax
Definition: liblwgeom.h:251
int lwgeom_calculate_gbox_cartesian(const LWGEOM *lwgeom, GBOX *gbox)
Calculate the 2-4D bounding box of a geometry.
Definition: g_box.c:607

Here is the call graph for this function:

Here is the caller graph for this function: