PostGIS  2.5.0dev-r@@SVN_REVISION@@
int lwgeom_geohash_precision ( GBOX  bbox,
GBOX bounds 
)

Definition at line 742 of file lwalgorithm.c.

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

Referenced by lwgeom_geohash(), and test_geohash_precision().

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

Here is the caller graph for this function: