PostGIS  2.1.10dev-r@@SVN_REVISION@@
char* geohash_point ( double  longitude,
double  latitude,
int  precision 
)

Definition at line 582 of file lwalgorithm.c.

References base32, and lwalloc().

Referenced by lwgeom_geohash(), and test_geohash_point().

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

Here is the call graph for this function:

Here is the caller graph for this function: