PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ geohash_point()

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

Definition at line 591 of file lwalgorithm.c.

592 {
593  int is_even=1, i=0;
594  double lat[2], lon[2], mid;
595  char bits[] = {16,8,4,2,1};
596  int bit=0, ch=0;
597  char *geohash = NULL;
598 
599  geohash = lwalloc(precision + 1);
600 
601  lat[0] = -90.0;
602  lat[1] = 90.0;
603  lon[0] = -180.0;
604  lon[1] = 180.0;
605 
606  while (i < precision)
607  {
608  if (is_even)
609  {
610  mid = (lon[0] + lon[1]) / 2;
611  if (longitude >= mid)
612  {
613  ch |= bits[bit];
614  lon[0] = mid;
615  }
616  else
617  {
618  lon[1] = mid;
619  }
620  }
621  else
622  {
623  mid = (lat[0] + lat[1]) / 2;
624  if (latitude >= mid)
625  {
626  ch |= bits[bit];
627  lat[0] = mid;
628  }
629  else
630  {
631  lat[1] = mid;
632  }
633  }
634 
635  is_even = !is_even;
636  if (bit < 4)
637  {
638  bit++;
639  }
640  else
641  {
642  geohash[i++] = base32[ch];
643  bit = 0;
644  ch = 0;
645  }
646  }
647  geohash[i] = 0;
648  return geohash;
649 }
static uint8_t precision
Definition: cu_in_twkb.c:25
void * lwalloc(size_t size)
Definition: lwutil.c:229
static char * base32
Definition: lwalgorithm.c:584

References base32, lwalloc(), and precision.

Referenced by lwgeom_geohash(), and test_geohash_point().

Here is the call graph for this function:
Here is the caller graph for this function: