PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geohash_point()

char* geohash_point ( double  longitude,
double  latitude,
int  precision 
)

Definition at line 581 of file lwalgorithm.c.

References base32, and lwalloc().

Referenced by lwgeom_geohash(), and test_geohash_point().

582 {
583  int is_even=1, i=0;
584  double lat[2], lon[2], mid;
585  char bits[] = {16,8,4,2,1};
586  int bit=0, ch=0;
587  char *geohash = NULL;
588 
589  geohash = lwalloc(precision + 1);
590 
591  lat[0] = -90.0;
592  lat[1] = 90.0;
593  lon[0] = -180.0;
594  lon[1] = 180.0;
595 
596  while (i < precision)
597  {
598  if (is_even)
599  {
600  mid = (lon[0] + lon[1]) / 2;
601  if (longitude >= mid)
602  {
603  ch |= bits[bit];
604  lon[0] = mid;
605  }
606  else
607  {
608  lon[1] = mid;
609  }
610  }
611  else
612  {
613  mid = (lat[0] + lat[1]) / 2;
614  if (latitude >= mid)
615  {
616  ch |= bits[bit];
617  lat[0] = mid;
618  }
619  else
620  {
621  lat[1] = mid;
622  }
623  }
624 
625  is_even = !is_even;
626  if (bit < 4)
627  {
628  bit++;
629  }
630  else
631  {
632  geohash[i++] = base32[ch];
633  bit = 0;
634  ch = 0;
635  }
636  }
637  geohash[i] = 0;
638  return geohash;
639 }
uint8_t precision
Definition: cu_in_twkb.c:25
static char * base32
Definition: lwalgorithm.c:574
void * lwalloc(size_t size)
Definition: lwutil.c:229
Here is the call graph for this function:
Here is the caller graph for this function: