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

Move deep-copy from top-level to dict-level #127

Merged
merged 1 commit into from
Sep 5, 2021
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
54 changes: 54 additions & 0 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,57 @@ def test_extract_fiona_file_gpkg():
topo = Extract(feats).to_dict()

assert len(topo["bookkeeping_geoms"]) == 4

def test_extract_dict_org_data_untouched():
data = {
"foo": {"type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]]},
"bar": {"type": "LineString", "coordinates": [[0, 0], [1, 0], [2, 0]]},
}
topo = Extract(data).to_dict()
topo_foo = topo['objects']['foo']
data_foo = data["foo"]

assert 'arcs' in topo_foo.keys()
assert 'arcs' not in data_foo.keys()

def test_extract_list_org_data_untouched():
data = [
geometry.Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]),
geometry.Polygon([[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]),
]
topo = Extract(data).to_dict()
topo_0 = topo['objects'][0]
data_0 = data[0]

assert 'arcs' in topo_0.keys()
assert data_0.type == 'Polygon'

def test_extract_gdf_org_data_untouched():
data = geopandas.read_file(
"tests/files_geojson/naturalearth_alb_grc.geojson", driver="GeoJSON"
)
topo = Extract(data).to_dict()
topo_0 = topo['objects'][0]
data_0 = data.iloc[0]

assert 'arcs' in topo_0.keys()
assert data_0.geometry.type == 'Polygon'

def test_extract_shapely_org_data_untouched():
data = geometry.LineString([[0, 0], [1, 0], [1, 1], [0, 1]])
topo = Extract(data).to_dict()
topo_0 = topo['objects'][0]

assert 'arcs' in topo_0.keys()
assert data.type == 'LineString'

def test_extract_shapefile_org_data_untouched():
import shapefile

data = shapefile.Reader("tests/files_shapefile/southamerica.shp")
topo = Extract(data).to_dict()
topo_0 = topo['objects']['feature_00']['geometries'][0]
data_0 = data.__geo_interface__['features'][0]['geometry']

assert 'arcs' in topo_0.keys()
assert 'arcs' not in data_0.keys()
27 changes: 14 additions & 13 deletions topojson/core/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ def __init__(self, data, options={}):
self._invalid_geoms = 0
self._tried_geojson = False

if instance(data) == "Collection": # fiona.Collection
copydata = data
else:
# FIXME: try except is not necessary once the following issue is fixed:
# https://github.com/geopandas/geopandas/issues/1070
try:
copydata = copy.deepcopy(data)
except TypeError:
if hasattr(data, "copy"):
copydata = data.copy()
else:
copydata = data
self.output = self._extractor(copydata)
# if instance(data) == "Collection": # fiona.Collection
# copydata = data
# else:
# # FIXME: try except is not necessary once the following issue is fixed:
# # https://github.com/geopandas/geopandas/issues/1070
# try:
# copydata = copy.deepcopy(data)
# except TypeError:
# if hasattr(data, "copy"):
# copydata = data.copy()
# else:
# copydata = data
self.output = self._extractor(data)

def __repr__(self):
return "Extract(\n{}\n)".format(pprint.pformat(self.output))
Expand Down Expand Up @@ -610,6 +610,7 @@ def _extract_dictionary(self, geom):
"""

self._is_single = False
self._data = copy.deepcopy(self._data)
# iterate over the input dictionary or geographical object
for key in list(self._data):
# based on the geom type the right function is serialized
Expand Down