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 precision for GeometryCollection geometries in fiona.transform #972

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions fiona/_transform.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ def _transform_geom(
g = geom

if precision >= 0:

if g['type'] == 'Point':
def round_point(g):
coords = list(g['coordinates'])
x, y = coords[:2]
x = round(x, precision)
Expand All @@ -172,8 +172,10 @@ def _transform_geom(
if len(coords) == 3:
z = coords[2]
new_coords.append(round(z, precision))

elif g['type'] in ['LineString', 'MultiPoint']:
return new_coords


def round_linestring(g):
coords = list(zip(*g['coordinates']))
xp, yp = coords[:2]
xp = [round(v, precision) for v in xp]
Expand All @@ -184,8 +186,10 @@ def _transform_geom(
new_coords = list(zip(xp, yp, zp))
else:
new_coords = list(zip(xp, yp))
return new_coords


elif g['type'] in ['Polygon', 'MultiLineString']:
def round_polygon(g):
new_coords = []
for piece in g['coordinates']:
coords = list(zip(*piece))
Expand All @@ -198,8 +202,9 @@ def _transform_geom(
new_coords.append(list(zip(xp, yp, zp)))
else:
new_coords.append(list(zip(xp, yp)))
return new_coords

elif g['type'] == 'MultiPolygon':
def round_multipolygon(g):
parts = g['coordinates']
new_coords = []
for part in parts:
Expand All @@ -216,7 +221,24 @@ def _transform_geom(
else:
inner_coords.append(list(zip(xp, yp)))
new_coords.append(inner_coords)

g['coordinates'] = new_coords
return new_coords

def round_geometry(g):
if g['type'] == 'Point':
g['coordinates'] = round_point(g)
elif g['type'] in ['LineString', 'MultiPoint']:
g['coordinates'] = round_linestring(g)
elif g['type'] in ['Polygon', 'MultiLineString']:
g['coordinates'] = round_polygon(g)
elif g['type'] == 'MultiPolygon':
g['coordinates'] = round_multipolygon(g)
else:
raise RuntimeError("Unsupported geometry type: {}".format(g['type']))

if g['type'] == 'GeometryCollection':
for _g in g['geometries']:
round_geometry(_g)
else:
round_geometry(g)

return g
12 changes: 12 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ def test_axis_ordering(crs):
geom = {"type": "Point", "coordinates": [-8427998.647958742, 4587905.27136252]}
g2 = transform.transform_geom("epsg:3857", crs, geom, precision=3)
assert g2["coordinates"] == pytest.approx(rev_expected)


def test_transform_issue971():
""" See https://github.com/Toblerity/Fiona/issues/971 """
source_crs = "epsg:25832"
dest_src = "epsg:4326"
geom = {'type': 'GeometryCollection', 'geometries': [{'type': 'LineString',
'coordinates': [(512381.8870945257, 5866313.311218272),
(512371.23869999964, 5866322.282500001),
(512364.6014999999, 5866328.260199999)]}]}
geom_transformed = transform.transform_geom(source_crs, dest_src, geom, precision=3)
assert geom_transformed['geometries'][0]['coordinates'][0] == pytest.approx((9.184, 52.946))