PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ lwgeom_geohash()

char* lwgeom_geohash ( const LWGEOM lwgeom,
int  precision 
)

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

Caller must free.

Definition at line 861 of file lwalgorithm.c.

862 {
863  GBOX gbox;
864  GBOX gbox_bounds;
865  double lat, lon;
866  int result;
867 
868  gbox_init(&gbox);
869  gbox_init(&gbox_bounds);
870 
871  result = lwgeom_calculate_gbox_cartesian(lwgeom, &gbox);
872  if ( result == LW_FAILURE ) return NULL;
873 
874  /* Return error if we are being fed something outside our working bounds */
875  if ( gbox.xmin < -180 || gbox.ymin < -90 || gbox.xmax > 180 || gbox.ymax > 90 )
876  {
877  lwerror("Geohash requires inputs in decimal degrees, got (%g %g, %g %g).",
878  gbox.xmin, gbox.ymin,
879  gbox.xmax, gbox.ymax);
880  return NULL;
881  }
882 
883  /* What is the center of our geometry bounds? We'll use that to
884  ** approximate location. */
885  lon = gbox.xmin + (gbox.xmax - gbox.xmin) / 2;
886  lat = gbox.ymin + (gbox.ymax - gbox.ymin) / 2;
887 
888  if ( precision <= 0 )
889  {
890  precision = lwgeom_geohash_precision(gbox, &gbox_bounds);
891  }
892 
893  /*
894  ** Return the geohash of the center, with a precision determined by the
895  ** extent of the bounds.
896  ** Possible change: return the point at the center of the precision bounds?
897  */
898  return geohash_point(lon, lat, precision);
899 }
static uint8_t precision
Definition: cu_in_twkb.c:25
int lwgeom_calculate_gbox_cartesian(const LWGEOM *lwgeom, GBOX *gbox)
Calculate the 2-4D bounding box of a geometry.
Definition: gbox.c:740
void gbox_init(GBOX *gbox)
Zero out all the entries in the GBOX.
Definition: gbox.c:40
#define LW_FAILURE
Definition: liblwgeom.h:110
int lwgeom_geohash_precision(GBOX bbox, GBOX *bounds)
Definition: lwalgorithm.c:765
char * geohash_point(double longitude, double latitude, int precision)
Definition: lwalgorithm.c:597
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
double ymax
Definition: liblwgeom.h:343
double xmax
Definition: liblwgeom.h:341
double ymin
Definition: liblwgeom.h:342
double xmin
Definition: liblwgeom.h:340

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

Referenced by ST_GeoHash(), and test_geohash().

Here is the call graph for this function:
Here is the caller graph for this function: