PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ geohash_point()

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

Definition at line 590 of file lwalgorithm.c.

References base32, and lwalloc().

Referenced by lwgeom_geohash(), and test_geohash_point().

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