PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ lwgeom_geohash_precision()

int lwgeom_geohash_precision ( GBOX  bbox,
GBOX bounds 
)

Definition at line 728 of file lwalgorithm.c.

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

Referenced by lwgeom_geohash(), and test_geohash_precision().

729 {
730  double minx, miny, maxx, maxy;
731  double latmax, latmin, lonmax, lonmin;
732  double lonwidth, latwidth;
733  double latmaxadjust, lonmaxadjust, latminadjust, lonminadjust;
734  int precision = 0;
735 
736  /* Get the bounding box, return error if things don't work out. */
737  minx = bbox.xmin;
738  miny = bbox.ymin;
739  maxx = bbox.xmax;
740  maxy = bbox.ymax;
741 
742  if ( minx == maxx && miny == maxy )
743  {
744  /* It's a point. Doubles have 51 bits of precision.
745  ** 2 * 51 / 5 == 20 */
746  return 20;
747  }
748 
749  lonmin = -180.0;
750  latmin = -90.0;
751  lonmax = 180.0;
752  latmax = 90.0;
753 
754  /* Shrink a world bounding box until one of the edges interferes with the
755  ** bounds of our rectangle. */
756  while ( 1 )
757  {
758  lonwidth = lonmax - lonmin;
759  latwidth = latmax - latmin;
760  latmaxadjust = lonmaxadjust = latminadjust = lonminadjust = 0.0;
761 
762  if ( minx > lonmin + lonwidth / 2.0 )
763  {
764  lonminadjust = lonwidth / 2.0;
765  }
766  else if ( maxx < lonmax - lonwidth / 2.0 )
767  {
768  lonmaxadjust = -1 * lonwidth / 2.0;
769  }
770  if ( miny > latmin + latwidth / 2.0 )
771  {
772  latminadjust = latwidth / 2.0;
773  }
774  else if (maxy < latmax - latwidth / 2.0 )
775  {
776  latmaxadjust = -1 * latwidth / 2.0;
777  }
778  /* Only adjust if adjustments are legal (we haven't crossed any edges). */
779  if ( (lonminadjust || lonmaxadjust) && (latminadjust || latmaxadjust ) )
780  {
781  latmin += latminadjust;
782  lonmin += lonminadjust;
783  latmax += latmaxadjust;
784  lonmax += lonmaxadjust;
785  /* Each adjustment cycle corresponds to 2 bits of storage in the
786  ** geohash. */
787  precision += 2;
788  }
789  else
790  {
791  break;
792  }
793  }
794 
795  /* Save the edges of our bounds, in case someone cares later. */
796  bounds->xmin = lonmin;
797  bounds->xmax = lonmax;
798  bounds->ymin = latmin;
799  bounds->ymax = latmax;
800 
801  /* Each geohash character (base32) can contain 5 bits of information.
802  ** We are returning the precision in characters, so here we divide. */
803  return precision / 5;
804 }
double xmax
Definition: liblwgeom.h:277
double ymin
Definition: liblwgeom.h:278
double xmin
Definition: liblwgeom.h:276
uint8_t precision
Definition: cu_in_twkb.c:25
double ymax
Definition: liblwgeom.h:279
Here is the caller graph for this function: