static void test_geohash_point(void) { char *geohash; geohash = geohash_point(0, 0, 16); //printf("\ngeohash %s\n",geohash); CU_ASSERT_STRING_EQUAL(geohash, "s000000000000000"); lwfree(geohash); geohash = geohash_point(90, 0, 16); //printf("\ngeohash %s\n",geohash); CU_ASSERT_STRING_EQUAL(geohash, "w000000000000000"); lwfree(geohash); geohash = geohash_point(20.012345, -20.012345, 15); //printf("\ngeohash %s\n",geohash); CU_ASSERT_STRING_EQUAL(geohash, "kkqnpkue9ktbpe5"); lwfree(geohash); }
/* ** Return a geohash string for the geometry. <http://geohash.org> ** Where the precision is non-positive, calculate a precision based on the ** bounds of the feature. Big features have loose precision. ** Small features have tight precision. */ char *lwgeom_geohash(const LWGEOM *lwgeom, int precision) { GBOX gbox; GBOX gbox_bounds; double lat, lon; int result; gbox_init(&gbox); gbox_init(&gbox_bounds); result = lwgeom_calculate_gbox(lwgeom, &gbox); if ( result == LW_FAILURE ) return NULL; /* Return error if we are being fed something outside our working bounds */ if ( gbox.xmin < -180 || gbox.ymin < -90 || gbox.xmax > 180 || gbox.ymax > 90 ) { lwerror("Geohash requires inputs in decimal degrees."); return NULL; } /* What is the center of our geometry bounds? We'll use that to ** approximate location. */ lon = gbox.xmin + (gbox.xmax - gbox.xmin) / 2; lat = gbox.ymin + (gbox.ymax - gbox.ymin) / 2; if ( precision <= 0 ) { precision = lwgeom_geohash_precision(gbox, &gbox_bounds); } /* ** Return the geohash of the center, with a precision determined by the ** extent of the bounds. ** Possible change: return the point at the center of the precision bounds? */ return geohash_point(lon, lat, precision); }