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

Definition at line 743 of file lwalgorithm.c.

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

Referenced by lwgeom_geohash(), and test_geohash_precision().

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

Here is the caller graph for this function: