PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ lwgeom_geohash_precision()

int lwgeom_geohash_precision ( GBOX  bbox,
GBOX bounds 
)

Definition at line 760 of file lwalgorithm.c.

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

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: