Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support immutable coordinates #7

Open
mbway opened this issue Mar 12, 2021 · 2 comments
Open

Support immutable coordinates #7

mbway opened this issue Mar 12, 2021 · 2 comments

Comments

@mbway
Copy link

mbway commented Mar 12, 2021

When generating geojson geometry from shapely the coordinates are tuples and so geojson_rewind fails when it tries to modify the coordinates

from shapely.geometry import Polygon, mapping
import geojson_rewind

p = Polygon([(0, 0), (1, 1), (1, 0)])

data = mapping(p)
print(data)

data = geojson_rewind.rewind(data)
print(data)
{'type': 'Polygon', 'coordinates': (((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),)}
Traceback (most recent call last):
  File "rewind.py", line 9, in <module>
    data = geojson_rewind.rewind(data)
  File "/.../python3.7/site-packages/geojson_rewind/rewind.py", line 15, in rewind
    return _rewind(gj, rfc7946)
  File "/.../python3.7/site-packages/geojson_rewind/rewind.py", line 31, in _rewind
    return correct(gj, rfc7946)
  File "/.../python3.7/site-packages/geojson_rewind/rewind.py", line 36, in correct
    feature['coordinates'] = correctRings(feature['coordinates'], rfc7946)
  File "/.../python3.7/site-packages/geojson_rewind/rewind.py", line 49, in correctRings
    rings[0] = wind(rings[0], clockwise)
TypeError: 'tuple' object does not support item assignment
``
@chris48s
Copy link
Owner

Quick solution - dump it to JSON, which will convert the tuples to js arrays

import json
from shapely.geometry import Polygon, mapping
import geojson_rewind

p = Polygon([(0, 0), (1, 1), (1, 0)])

data = mapping(p)
print(data)

data = geojson_rewind.rewind(json.dumps(data))
print(data)

if you want a python dict as output rather than a JSON string, you can json.loads() it again.

data = geojson_rewind.rewind(json.loads(json.dumps(data)))
print(data)

or

data = geojson_rewind.rewind(json.dumps(data))
print(json.loads(data))

would both work.

@mbway
Copy link
Author

mbway commented Mar 12, 2021

thanks, I had the same idea as a workaround. Just letting you know about this situation in case you want to support it properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants