PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ lwgeom_geohash()

lwvarlena_t* lwgeom_geohash ( const LWGEOM lwgeom,
int  precision 
)

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

Caller must free.

Definition at line 868 of file lwalgorithm.c.

869 {
870  GBOX gbox = {0};
871  GBOX gbox_bounds = {0};
872  double lat, lon;
873  int result;
874 
875  gbox_init(&gbox);
876  gbox_init(&gbox_bounds);
877 
878  result = lwgeom_calculate_gbox_cartesian(lwgeom, &gbox);
879  if ( result == LW_FAILURE ) return NULL;
880 
881  /* Return error if we are being fed something outside our working bounds */
882  if ( gbox.xmin < -180 || gbox.ymin < -90 || gbox.xmax > 180 || gbox.ymax > 90 )
883  {
884  lwerror("Geohash requires inputs in decimal degrees, got (%g %g, %g %g).",
885  gbox.xmin, gbox.ymin,
886  gbox.xmax, gbox.ymax);
887  return NULL;
888  }
889 
890  /* What is the center of our geometry bounds? We'll use that to
891  ** approximate location. */
892  lon = gbox.xmin + (gbox.xmax - gbox.xmin) / 2;
893  lat = gbox.ymin + (gbox.ymax - gbox.ymin) / 2;
894 
895  if ( precision <= 0 )
896  {
897  precision = lwgeom_geohash_precision(gbox, &gbox_bounds);
898  }
899 
900  /*
901  ** Return the geohash of the center, with a precision determined by the
902  ** extent of the bounds.
903  ** Possible change: return the point at the center of the precision bounds?
904  */
905  return geohash_point(lon, lat, precision);
906 }
static uint8_t precision
Definition: cu_in_twkb.c:25
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
int lwgeom_calculate_gbox_cartesian(const LWGEOM *lwgeom, GBOX *gbox)
Calculate the 2-4D bounding box of a geometry.
Definition: gbox.c:752
void gbox_init(GBOX *gbox)
Zero out all the entries in the GBOX.
Definition: gbox.c:40
#define LW_FAILURE
Definition: liblwgeom.h:96
int lwgeom_geohash_precision(GBOX bbox, GBOX *bounds)
Definition: lwalgorithm.c:771
lwvarlena_t * geohash_point(double longitude, double latitude, int precision)
Definition: lwalgorithm.c:603
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354

References gbox_init(), geohash_point(), LW_FAILURE, lwerror(), lwgeom_calculate_gbox_cartesian(), lwgeom_geohash_precision(), precision, result, 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: