albarji
albarji

Reputation: 890

How to convert HEXEWKB to Latitude, Longitude (in python)?

I downloaded some Point of Interest data from OpenStreetMap and as it turn outs the locations are encoded in HEXEWKB format:

CSV fields
==============================================
1 : Node type;  N|W|R (in upper case), wheter it is a Node, Way or Relation in the openstreetmap model
2 : id; The openstreetmap id
3 : name;   The default name of the city
4 : countrycode;    The iso3166-2 country code (2 letters)
5 : alternatenames; the names of the POI in other languages
6 : location;   The middle location of the POI in HEXEWKB
7 : tags; the POI tags : amenity,aeroway,building,craft,historic,leisure,man_made,office,railway,tourism,shop,sport,landuse,highway separated by '___' 

I need to transform these into longitude/latitude values. This very same question was asked before for the Java language (How to convert HEXEWKB to Latitude, Longitude (in java)?), however I need a Python solution.

My attempts so far have been focused in trying to use GeoDjango's GEOS module (https://docs.djangoproject.com/en/1.8/ref/contrib/gis/geos/#creating-a-geometry), but since I'm not using Django in my application this feels a bit like an overshooting. Is there any simpler approach?

Upvotes: 5

Views: 5055

Answers (1)

albarji
albarji

Reputation: 890

After trying different libraries I found the most practical solution in a somewhat related question: Why can shapely/geos parse this 'invalid' Well Known Binary?. This involves using shapely (https://pypi.python.org/pypi/Shapely):

from shapely import wkb
hexlocation = "0101000020E6100000CB752BC86AC8ED3FF232E58BDA7E4440"
point = wkb.loads(hexlocation, hex=True)
longitude = point.x
latitude = point.y

That is, you just need to use wkb.loads to transform the HEXEWKB string to a shapely Point object, then extract the long/lat coordinates from that point.

Upvotes: 9

Related Questions