I wanto to build a function (let's call it
clip) that accepts a vector-file (can be a shapefile, a geopackage....) and a raster. How can I make sure the argument that the users gives to the function is a type that for example
gepandas can read? I though about something like (completey pseudocode):
def clip(shape, raster): if driver(shape) not in geopandas.drivers: raise TypeError else: clip(raster, shape)
I know this is kind of random pseudocode, but maybe some understand what I mean and has an idea.
When I try:
import geopandas shape = "no_shape.sph" try: shape = geopandas.read_file(shape) except CPLE_OpenFailedError: print("that's not a the path to a vector geometry") except DriverError: print("that's not a valid vector geometry")
CPLE_OpenFailedError Traceback (most recent call last) fiona/_shim.pyx in fiona._shim.gdal_open_vector() fiona/_err.pyx in fiona._err.exc_wrap_pointer() CPLE_OpenFailedError: no_shape.sph: No such file or directory During handling of the above exception, another exception occurred: DriverError Traceback (most recent call last) <ipython-input-17-7ed5ed6090fb> in <module> 5 try: ----> 6 shape = geopandas.read_file(shape) 7 except CPLE_OpenFailedError:
And I have no idea how to catch that exception and print a message that this is no valid shape (or any other vector supported by geopandas).