Skip to content

Commit

Permalink
Merge branch 'clone_space'
Browse files Browse the repository at this point in the history
  • Loading branch information
omanges committed Oct 15, 2020
2 parents 515723a + 1ba7f98 commit 88f6beb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/space/test_space_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,18 @@ def test_add_features_shapefile_diff_projection(empty_space):
38.79930767,
0,
]


@pytest.mark.skipif(not XYZ_TOKEN, reason="No token found.")
def test_space_clone(space_object, space_id, empty_space):
"""Test space cloning functionality."""
space = space_object.read(id=space_id)
cloned_space = space.clone()
cloned_specific_space = space.clone(space_id=empty_space.info["id"])
assert cloned_space.get_statistics()["count"]["value"] == 180
assert cloned_specific_space.get_statistics()["count"]["value"] == 180
assert cloned_space.get_feature("IND")["properties"]["name"] == "India"
assert (
cloned_specific_space.get_feature("IND")["properties"]["name"]
== "India"
)
36 changes: 36 additions & 0 deletions xyzspaces/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,3 +1326,39 @@ def add_features_geopandas(
features_size=features_size,
chunk_size=chunk_size,
)

def clone(self, space_id: str = None, chunks: int = 1000):
"""
Copy current space data into a newly created or into an existing space.
:param space_id: space id into which to copy data,
if not provided will create a new space and copy the data.
:param chunks: A max. number of features to read in a single iteration
while iterating over the source space.
:return: The cloned Space Object
"""
if space_id:
cloned_space = Space.from_id(space_id=space_id)
else:
title = self.info["title"]
desc = self.info["description"]
cloned_space = Space.new(title=title, description=desc)
features = []
feature_collection: Dict[Any, Any] = {}
for f in self.iter_feature(limit=chunks):
features.append(f)
if len(features) == chunks:
feature_collection = dict(
type="FeatureCollection", features=features
)
cloned_space.add_features(features=feature_collection)
features = []
feature_collection = {}

if len(features) >= 0:
feature_collection = dict(
type="FeatureCollection", features=features
)
cloned_space.add_features(features=feature_collection)

return cloned_space

0 comments on commit 88f6beb

Please sign in to comment.