jazzband / geojson

Python bindings and utilities for GeoJSON
https://pypi.python.org/pypi/geojson/
BSD 3-Clause "New" or "Revised" License
908 stars 120 forks source link

geojson.load() chokes on non-ascii property keys #38

Closed mharnold closed 9 years ago

mharnold commented 9 years ago

The following input data causes geojson.load() to fail with:

  File "/usr/local/lib/python2.7/site-packages/geojson/base.py", line 47, in <genexpr>
    d = dict((str(k), mapping[k]) for k in mapping)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 9: ordinal not in range(128)

data.geojson:

{
  "features": [
    {
      "geometry": {
        "coordinates": [
          [
            700251.2679938598, 
            6154023.085332587
          ], 
      [
            700516.3475396622, 
            6155237.880750462
          ]
        ], 
        "type": "LineString"
      }, 
      "id": 3498, 
      "properties": {
        "osm$highway": "motorway", 
        "osm$stamv\u00e4g": "yes", 
        "osm$way_id": 2036271 
      }, 
      "type": "Feature"
    }
  ],
"type": "FeatureCollection"
}

bug.py:

import geojson
fname = 'data.geojson'
f = open(fname,'r')
geo = geojson.load(f)

The following fix works for me: In site-packages/geojson/base.py, in the to_instance method of GeoJson replace:

       d = dict((str(k), mapping[k]) for k in mapping)

with:

       #d = dict((str(k), mapping[k]) for k in mapping)
        d = {}
        for k in mapping:
            try:
                str_key = str(k)
            except (UnicodeEncodeError):
                str_key = unicode(k)
            d[str_key] = mapping[k]
frewsxcv commented 9 years ago

Thanks for the bug report. I'll deploy a new version in the next couple days with the fix.

frewsxcv commented 9 years ago

Just pushed a new version (1.0.8) with this fix.

mharnold commented 9 years ago

Thanks!

Cheers, Michael spiderOSM.org

On Mon, Sep 29, 2014 at 9:47 PM, Corey Farwell notifications@github.com wrote:

Just pushed a new version (1.0.8) with this fix.

— Reply to this email directly or view it on GitHub https://github.com/frewsxcv/python-geojson/issues/38#issuecomment-57266692 .