python-rasterstats icon indicating copy to clipboard operation
python-rasterstats copied to clipboard

Unclear error handling for shp missing shx/dbf

Open sgoodm opened this issue 2 years ago • 1 comments

When attempting to use a shapefile and you only have the .shp (not .shx / .dbf) the error you get is ultimately tied to trying to read a GeoJSON.

Traceback (most recent call last):
  File “/opt/anaconda3/envs/geodata38/lib/python3.8/site-packages/rasterstats/io.py”, line 109, in read_features
    mapping = json.loads(obj)
  File “/opt/anaconda3/envs/geodata38/lib/python3.8/json/__init__.py”, line 357, in loads
    return _default_decoder.decode(s)
  File “/opt/anaconda3/envs/geodata38/lib/python3.8/json/decoder.py”, line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File “/opt/anaconda3/envs/geodata38/lib/python3.8/json/decoder.py”, line 355, in raw_decode
    raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  ...
  File “/opt/anaconda3/envs/geodata38/lib/python3.8/site-packages/rasterstats/io.py”, line 116, in read_features
    features_iter = [parse_feature(obj)]
  File “/opt/anaconda3/envs/geodata38/lib/python3.8/site-packages/rasterstats/io.py”, line 83, in parse_feature
    raise ValueError(“Can’t parse %s as a geojson Feature object” % obj)
ValueError: Can’t parse /path/to/my.shp as a geojson Feature object

This might fall into the realm of what Fiona should handle, but a relatively simple check when using a shapefile to make sure the .shp, .shx, and .dbf all exist would solve the issue.

sgoodm avatar Apr 11 '23 16:04 sgoodm

@sgoodm that's a great point, the current io.py is generally pretty terrible at handling edge cases. Since it uses exceptions as control flow, uncaught exceptions slip through and present themselves as WTF errors.

My suggestion is: don't rely on a zonal stats library to do generic GIS format validation. There are more appropriate libraries for determining if e.g. a shapefile is valid.

If anything, I'm leaning towards removing io.py, certainly not extending it.

perrygeo avatar Apr 11 '23 22:04 perrygeo