Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear error handling for shp missing shx/dbf #288

Open
sgoodm opened this issue Apr 11, 2023 · 1 comment
Open

Unclear error handling for shp missing shx/dbf #288

sgoodm opened this issue Apr 11, 2023 · 1 comment

Comments

@sgoodm
Copy link
Contributor

sgoodm commented Apr 11, 2023

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.

@perrygeo
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants