PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ lwgeom_geohash_precision()

int lwgeom_geohash_precision ( GBOX  bbox,
GBOX bounds 
)

Definition at line 771 of file lwalgorithm.c.

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