PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ lwgeom_geohash_precision()

int lwgeom_geohash_precision ( GBOX  bbox,
GBOX bounds 
)

Definition at line 766 of file lwalgorithm.c.

767 {
768  double minx, miny, maxx, maxy;
769  double latmax, latmin, lonmax, lonmin;
770  double lonwidth, latwidth;
771  double latmaxadjust, lonmaxadjust, latminadjust, lonminadjust;
772  int precision = 0;
773 
774  /* Get the bounding box, return error if things don't work out. */
775  minx = bbox.xmin;
776  miny = bbox.ymin;
777  maxx = bbox.xmax;
778  maxy = bbox.ymax;
779 
780  if ( minx == maxx && miny == maxy )
781  {
782  /* It's a point. Doubles have 51 bits of precision.
783  ** 2 * 51 / 5 == 20 */
784  return 20;
785  }
786 
787  lonmin = -180.0;
788  latmin = -90.0;
789  lonmax = 180.0;
790  latmax = 90.0;
791 
792  /* Shrink a world bounding box until one of the edges interferes with the
793  ** bounds of our rectangle. */
794  while ( 1 )
795  {
796  lonwidth = lonmax - lonmin;
797  latwidth = latmax - latmin;
798  latmaxadjust = lonmaxadjust = latminadjust = lonminadjust = 0.0;
799 
800  if ( minx > lonmin + lonwidth / 2.0 )
801  {
802  lonminadjust = lonwidth / 2.0;
803  }
804  else if ( maxx < lonmax - lonwidth / 2.0 )
805  {
806  lonmaxadjust = -1 * lonwidth / 2.0;
807  }
808  if ( lonminadjust || lonmaxadjust )
809  {
810  lonmin += lonminadjust;
811  lonmax += lonmaxadjust;
812  /* Each adjustment cycle corresponds to 2 bits of storage in the
813  ** geohash. */
814  precision++;
815  }
816  else
817  {
818  break;
819  }
820 
821  if ( miny > latmin + latwidth / 2.0 )
822  {
823  latminadjust = latwidth / 2.0;
824  }
825  else if (maxy < latmax - latwidth / 2.0 )
826  {
827  latmaxadjust = -1 * latwidth / 2.0;
828  }
829  /* Only adjust if adjustments are legal (we haven't crossed any edges). */
830  if ( latminadjust || latmaxadjust )
831  {
832  latmin += latminadjust;
833  latmax += latmaxadjust;
834  /* Each adjustment cycle corresponds to 2 bits of storage in the
835  ** geohash. */
836  precision++;
837  }
838  else
839  {
840  break;
841  }
842  }
843 
844  /* Save the edges of our bounds, in case someone cares later. */
845  bounds->xmin = lonmin;
846  bounds->xmax = lonmax;
847  bounds->ymin = latmin;
848  bounds->ymax = latmax;
849 
850  /* Each geohash character (base32) can contain 5 bits of information.
851  ** We are returning the precision in characters, so here we divide. */
852  return precision / 5;
853 }
static uint8_t precision
Definition: cu_in_twkb.c:25
double ymax
Definition: liblwgeom.h:371
double xmax
Definition: liblwgeom.h:369
double ymin
Definition: liblwgeom.h:370
double xmin
Definition: liblwgeom.h:368

References precision, GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Referenced by lwgeom_geohash(), and test_geohash_precision().

Here is the caller graph for this function: