Skip to content

Commit

Permalink
fix: catch fiona DriverError for zones when fiona is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
martibosch committed May 14, 2024
1 parent bc65a4c commit 6fa816c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions pylandstats/zonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@
from shapely import geometry
from shapely.geometry import base as geometry_base

try:
from fiona import errors as fiona_errors
except ImportError:
fiona_errors = None

from . import multilandscape
from .landscape import Landscape

__all__ = ["ZonalAnalysis", "BufferAnalysis", "ZonalGridAnalysis"]

ZONES_READ_ERRORS = [AttributeError, TypeError]
if fiona_errors is not None:
ZONES_READ_ERRORS.append(fiona_errors.DriverError)
ZONES_READ_ERRORS = tuple(ZONES_READ_ERRORS)

_compute_zonal_statistics_gdf_doc = """
Compute the zonal statistics geo-data frame over the landscape raster.
Expand Down Expand Up @@ -98,12 +108,10 @@ def __init__(
# first, try to read the `zones` argument as a geo-data frame-like file
try:
zones = gpd.read_file(zones)
except (AttributeError, TypeError):
# for GeoDataFrame, GeoSeries, list-like or ndarrays, we will get
# "AttributeError: object has no attribute 'startswith'" in windows, fiona
# will try to read `masks` as a regular expression and raise a TypeError
# (see https://github.com/Toblerity/Fiona/blob/master/fiona/path.py). we let
# this continue and try to read the `zones` argument differently below
except ZONES_READ_ERRORS:
# Depending on the system and installed libraries, geopandas may raise an
# `AttributeError`, `TypeError` or `fiona.errors.DriverError`. we let this
# continue and try to read the `zones` argument differently below
pass

with rio.open(landscape_filepath) as src:
Expand Down

0 comments on commit 6fa816c

Please sign in to comment.