PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ lwgeom_geohash_precision()

int lwgeom_geohash_precision ( GBOX  bbox,
GBOX bounds 
)

Definition at line 765 of file lwalgorithm.c.

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