diff --git a/geoutils/vector.py b/geoutils/vector.py index d5a7c35b..010923ef 100644 --- a/geoutils/vector.py +++ b/geoutils/vector.py @@ -74,6 +74,7 @@ def __init__(self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | :param filename_or_dataset: Path to file, or GeoPandas dataframe or series, or Shapely geometry. """ + # If filename is passed if isinstance(filename_or_dataset, (str, pathlib.Path)): with warnings.catch_warnings(): # This warning shows up in numpy 1.21 (2021-07-09) @@ -81,6 +82,7 @@ def __init__(self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | ds = gpd.read_file(filename_or_dataset) self._ds = ds self._name: str | gpd.GeoDataFrame | None = filename_or_dataset + # If GeoPandas or Shapely object is passed elif isinstance(filename_or_dataset, (gpd.GeoDataFrame, gpd.GeoSeries, shapely.Geometry)): self._name = None if isinstance(filename_or_dataset, gpd.GeoDataFrame): @@ -89,6 +91,11 @@ def __init__(self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | self._ds = gpd.GeoDataFrame(geometry=filename_or_dataset) else: self._ds = gpd.GeoDataFrame({"geometry": [filename_or_dataset]}, crs=None) + # If Vector is passed, simply point back to Vector + elif isinstance(filename_or_dataset, Vector): + for key in filename_or_dataset.__dict__: + setattr(self, key, filename_or_dataset.__dict__[key]) + return else: raise TypeError("Filename argument should be a string, Path or geopandas.GeoDataFrame.") diff --git a/tests/test_vector.py b/tests/test_vector.py index e1c5d323..4ca06489 100644 --- a/tests/test_vector.py +++ b/tests/test_vector.py @@ -45,15 +45,19 @@ def test_init(self) -> None: v0 = gu.Vector(self.aster_outlines_path) assert isinstance(v0, gu.Vector) - # Second, with a pathlib path + # Third, with a pathlib path path = pathlib.Path(self.aster_outlines_path) v1 = gu.Vector(path) assert isinstance(v1, gu.Vector) - # Third, with a geopandas dataframe + # Fourth, with a geopandas dataframe v2 = gu.Vector(gpd.read_file(self.aster_outlines_path)) assert isinstance(v2, gu.Vector) + # Fifth, passing a Vector itself (points back to Vector passed) + v3 = gu.Vector(v2) + assert isinstance(v3, gu.Vector) + # Check errors are raised when filename has wrong type with pytest.raises(TypeError, match="Filename argument should be a string, Path or geopandas.GeoDataFrame."): gu.Vector(1) # type: ignore