如何将HEXEWKB转换为Latitude,Longitude(在python中)?

时间:2015-12-14 15:31:30

标签: python gis openstreetmap latitude-longitude

我从OpenStreetMap下载了一些Point of Interest数据,当它出现时,这些位置以HEXEWKB格式编码:

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 '___' 

我需要将它们转换为经度/纬度值。之前曾问过Java语言(How to convert HEXEWKB to Latitude, Longitude (in java)?)这个问题,但我需要一个Python解决方案。

到目前为止,我的尝试主要集中在尝试使用GeoDjango的GEOS模块(https://docs.djangoproject.com/en/1.8/ref/contrib/gis/geos/#creating-a-geometry),但由于我在我的应用程序中没有使用Django,这感觉有点像过度。有没有更简单的方法?

1 个答案:

答案 0 :(得分:3)

在尝试不同的库之后,我在一个相关的问题中找到了最实用的解决方案:Why can shapely/geos parse this 'invalid' Well Known Binary?。这涉及使用匀称https://pypi.python.org/pypi/Shapely):

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

也就是说,您只需要使用 wkb.loads 将HEXEWKB字符串转换为匀称的Point对象,然后从该点提取长/纬度坐标。