Skip to content

Commit

Permalink
Merge pull request #128 from bryik/master
Browse files Browse the repository at this point in the history
Fix map_geometries() loss of feature IDs
  • Loading branch information
rayrrr authored Jul 13, 2019
2 parents e96c0e9 + 20880c0 commit 813117e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion geojson/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ def map_geometries(func, obj):
return {'type': obj['type'], 'geometries': geoms}
elif obj['type'] == 'Feature':
geom = func(obj['geometry']) if obj['geometry'] else None
return {
result = {
'type': obj['type'],
'geometry': geom,
'properties': obj['properties'],
}
if 'id' in obj:
result['id'] = obj['id']
return result
elif obj['type'] == 'FeatureCollection':
feats = [map_geometries(func, feat) for feat in obj['features']]
return {'type': obj['type'], 'features': feats}
Expand Down
21 changes: 20 additions & 1 deletion tests/test_coords.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

import geojson
from geojson.utils import coords, map_coords
from geojson.utils import coords, map_coords, map_tuples


class CoordsTestCase(unittest.TestCase):
Expand Down Expand Up @@ -66,6 +66,25 @@ def test_map_multipolygon(self):
self.assertEqual(result['coordinates'][0][0][0], (3.78, 9.28))
self.assertEqual(result['coordinates'][-1][-1][-1], (23.18, -34.29))

def test_map_feature(self):
f = geojson.Feature(
id='0',
geometry=geojson.Point([-77.1291115237051, 38.7993076720178]),
properties={
'name': 'Van Dorn Street',
'marker-col': '#0000ff',
'marker-sym': 'rail-metro',
'line': 'blue',
},
)
result = map_tuples(lambda t: t, f)
self.assertEqual(result['type'], 'Feature')
self.assertEqual(result['id'], '0')
self.assertEqual(
result['geometry']['coordinates'],
(-77.1291115237051, 38.7993076720178)
)

def test_map_invalid(self):
with self.assertRaises(ValueError):
map_coords(lambda x: x, {"type": ""})

0 comments on commit 813117e

Please sign in to comment.