PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ lwgeom_geohash_precision()

int lwgeom_geohash_precision ( GBOX  bbox,
GBOX bounds 
)

Definition at line 751 of file lwalgorithm.c.

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

Referenced by lwgeom_geohash(), and test_geohash_precision().

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