PostGIS  2.2.7dev-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 813 of file lwalgorithm.c.

References gbox_init(), geohash_point(), LW_FAILURE, lwerror(), lwgeom_calculate_gbox_cartesian(), lwgeom_geohash_precision(), GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Referenced by ST_GeoHash(), and test_geohash().

814 {
815  GBOX gbox;
816  GBOX gbox_bounds;
817  double lat, lon;
818  int result;
819 
820  gbox_init(&gbox);
821  gbox_init(&gbox_bounds);
822 
823  result = lwgeom_calculate_gbox_cartesian(lwgeom, &gbox);
824  if ( result == LW_FAILURE ) return NULL;
825 
826  /* Return error if we are being fed something outside our working bounds */
827  if ( gbox.xmin < -180 || gbox.ymin < -90 || gbox.xmax > 180 || gbox.ymax > 90 )
828  {
829  lwerror("Geohash requires inputs in decimal degrees, got (%g %g, %g %g).",
830  gbox.xmin, gbox.ymin,
831  gbox.xmax, gbox.ymax);
832  return NULL;
833  }
834 
835  /* What is the center of our geometry bounds? We'll use that to
836  ** approximate location. */
837  lon = gbox.xmin + (gbox.xmax - gbox.xmin) / 2;
838  lat = gbox.ymin + (gbox.ymax - gbox.ymin) / 2;
839 
840  if ( precision <= 0 )
841  {
842  precision = lwgeom_geohash_precision(gbox, &gbox_bounds);
843  }
844 
845  /*
846  ** Return the geohash of the center, with a precision determined by the
847  ** extent of the bounds.
848  ** Possible change: return the point at the center of the precision bounds?
849  */
850  return geohash_point(lon, lat, precision);
851 }
double xmax
Definition: liblwgeom.h:277
int lwgeom_geohash_precision(GBOX bbox, GBOX *bounds)
Definition: lwalgorithm.c:728
#define LW_FAILURE
Definition: liblwgeom.h:64
char * geohash_point(double longitude, double latitude, int precision)
Definition: lwalgorithm.c:567
double ymin
Definition: liblwgeom.h:278
void gbox_init(GBOX *gbox)
Zero out all the entries in the GBOX.
Definition: g_box.c:36
double xmin
Definition: liblwgeom.h:276
uint8_t precision
Definition: cu_in_twkb.c:25
double ymax
Definition: liblwgeom.h:279
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
int lwgeom_calculate_gbox_cartesian(const LWGEOM *lwgeom, GBOX *gbox)
Calculate the 2-4D bounding box of a geometry.
Definition: g_box.c:648

Here is the call graph for this function:

Here is the caller graph for this function: