Skip to content

Commit

Permalink
Return self input in Vector class (#390)
Browse files Browse the repository at this point in the history
* Add support for vector input in vector class

* Add more comments to init

* Fix if in elif

* Vector instead of raster...
  • Loading branch information
rhugonnet authored Aug 15, 2023
1 parent 8bd76cd commit 6ab53ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions geoutils/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ 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)
warnings.filterwarnings("ignore", ".*attribute.*array_interface.*Polygon.*")
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):
Expand All @@ -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.")

Expand Down
8 changes: 6 additions & 2 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6ab53ad

Please sign in to comment.