4

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?

Community
  • 1
  • 1
albarji
  • 850
  • 14
  • 20
  • A quick intro to the spec https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary includes a link to PyGeoIf https://pypi.python.org/pypi/pygeoif – Hugh Bothwell Dec 14 '15 at 15:48
  • Hugh, thanks for the suggestion. Unfortunately it seems that pygeoif can only process locations in Well-known Text format, while my data seems to be in HEXadecimal Extended Well-Known Binary (HEXEWKB). – albarji Dec 15 '15 at 09:00

1 Answers1

8

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.

Community
  • 1
  • 1
albarji
  • 850
  • 14
  • 20