PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ lwgeom_geohash_precision()

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 ( miny > latmin + latwidth / 2.0 )
785  {
786  latminadjust = latwidth / 2.0;
787  }
788  else if (maxy < latmax - latwidth / 2.0 )
789  {
790  latmaxadjust = -1 * latwidth / 2.0;
791  }
792  /* Only adjust if adjustments are legal (we haven't crossed any edges). */
793  if ( (lonminadjust || lonmaxadjust) && (latminadjust || latmaxadjust ) )
794  {
795  latmin += latminadjust;
796  lonmin += lonminadjust;
797  latmax += latmaxadjust;
798  lonmax += lonmaxadjust;
799  /* Each adjustment cycle corresponds to 2 bits of storage in the
800  ** geohash. */
801  precision += 2;
802  }
803  else
804  {
805  break;
806  }
807  }
808 
809  /* Save the edges of our bounds, in case someone cares later. */
810  bounds->xmin = lonmin;
811  bounds->xmax = lonmax;
812  bounds->ymin = latmin;
813  bounds->ymax = latmax;
814 
815  /* Each geohash character (base32) can contain 5 bits of information.
816  ** We are returning the precision in characters, so here we divide. */
817  return precision / 5;
818 }
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: