PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ geohash_point()

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

Definition at line 597 of file lwalgorithm.c.

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

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: