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

Definition at line 567 of file lwalgorithm.c.

References base32, and lwalloc().

Referenced by lwgeom_geohash(), and test_geohash_point().

568 {
569  int is_even=1, i=0;
570  double lat[2], lon[2], mid;
571  char bits[] = {16,8,4,2,1};
572  int bit=0, ch=0;
573  char *geohash = NULL;
574 
575  geohash = lwalloc(precision + 1);
576 
577  lat[0] = -90.0;
578  lat[1] = 90.0;
579  lon[0] = -180.0;
580  lon[1] = 180.0;
581 
582  while (i < precision)
583  {
584  if (is_even)
585  {
586  mid = (lon[0] + lon[1]) / 2;
587  if (longitude >= mid)
588  {
589  ch |= bits[bit];
590  lon[0] = mid;
591  }
592  else
593  {
594  lon[1] = mid;
595  }
596  }
597  else
598  {
599  mid = (lat[0] + lat[1]) / 2;
600  if (latitude >= mid)
601  {
602  ch |= bits[bit];
603  lat[0] = mid;
604  }
605  else
606  {
607  lat[1] = mid;
608  }
609  }
610 
611  is_even = !is_even;
612  if (bit < 4)
613  {
614  bit++;
615  }
616  else
617  {
618  geohash[i++] = base32[ch];
619  bit = 0;
620  ch = 0;
621  }
622  }
623  geohash[i] = 0;
624  return geohash;
625 }
uint8_t precision
Definition: cu_in_twkb.c:25
static char * base32
Definition: lwalgorithm.c:560
void * lwalloc(size_t size)
Definition: lwutil.c:199

Here is the call graph for this function:

Here is the caller graph for this function: