Skip to content

Commit

Permalink
ElasticSearch: decode geohash values in a geo_point field
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.osgeo.org/gdal/trunk@29776 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information
rouault committed Aug 24, 2015
1 parent 0c46834 commit 2f4087c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
13 changes: 13 additions & 0 deletions autotest/ogr/ogr_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,12 @@ def ogr_elasticsearch_5():
"_source": {
"another_geopoint": "49.2,2.2"
}
},
{
"_source": {""" +
# "this is the geohash format",
""" "another_geopoint": "u09qv80meqh16ve02equ"
}
}]
}
}""")
Expand Down Expand Up @@ -1020,6 +1026,13 @@ def ogr_elasticsearch_5():
gdaltest.post_reason('fail')
f.DumpReadable()
return 'fail'

# Test geohash
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f['another_geopoint'], 'POINT (2 49)') != 0:
gdaltest.post_reason('fail')
f.DumpReadable()
return 'fail'

f = None
lyr.CreateField(ogr.FieldDefn('superobject.subfield2', ogr.OFTString))
Expand Down
40 changes: 39 additions & 1 deletion gdal/ogr/ogrsf_frmts/elastic/ogrelasticlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,38 @@ OGRFeature *OGRElasticLayer::GetNextRawFeature()
return NULL;
}

/************************************************************************/
/* decode_geohash_bbox() */
/************************************************************************/

/* Derived from routine from https://github.com/davetroy/geohash/blob/master/ext/geohash_native.c */
/* (c) 2008-2010 David Troy, [email protected], (The MIT License) */

static const char BASE32[] = "0123456789bcdefghjkmnpqrstuvwxyz";

static void decode_geohash_bbox(const char *geohash, double lat[2], double lon[2])
{
int i, j, hashlen;
char c, cd, mask, is_even=1;
static const char bits[] = {16,8,4,2,1};
lat[0] = -90.0; lat[1] = 90.0;
lon[0] = -180.0; lon[1] = 180.0;
hashlen = strlen(geohash);
for (i=0; i<hashlen; i++) {
c = tolower(geohash[i]);
cd = strchr(BASE32, c)-BASE32;
for (j=0; j<5; j++) {
mask = bits[j];
if (is_even) {
lon[!(cd&mask)] = (lon[0] + lon[1])/2;
} else {
lat[!(cd&mask)] = (lat[0] + lat[1])/2;
}
is_even = !is_even;
}
}
}

/************************************************************************/
/* BuildFeature() */
/************************************************************************/
Expand Down Expand Up @@ -1102,7 +1134,13 @@ void OGRElasticLayer::BuildFeature(OGRFeature* poFeature, json_object* poSource,
poGeom = new OGRPoint( CPLAtof(papszTokens[1]),
CPLAtof(papszTokens[0]) );
}
// TODO decode if stored as a geohash string ?
else
{
double lat[2], lon[2];
decode_geohash_bbox(pszLatLon, lat, lon);
poGeom = new OGRPoint( (lon[0] + lon[1]) / 2,
(lat[0] + lat[1]) / 2 );
}

CSLDestroy(papszTokens);
}
Expand Down

0 comments on commit 2f4087c

Please sign in to comment.