PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ lwgeom_geohash_precision()

int lwgeom_geohash_precision ( GBOX  bbox,
GBOX bounds 
)

Definition at line 752 of file lwalgorithm.c.

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

Referenced by lwgeom_geohash(), and test_geohash_precision().

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