diff --git a/rasterio/crs.py b/rasterio/crs.py index 7b36f2ce..20e331f9 100644 --- a/rasterio/crs.py +++ b/rasterio/crs.py @@ -10,8 +10,11 @@ # {'proj': 'longlat', 'ellps': 'WGS84', 'datum': 'WGS84', 'no_defs': True} # + import json + from rasterio._base import is_geographic_crs, is_projected_crs, is_same_crs +from rasterio.errors import CRSError from rasterio.five import string_types @@ -57,8 +60,9 @@ def from_string(prjs): val = json.loads(prjs, strict=False) except ValueError: raise ValueError('crs appears to be JSON but is not valid') + if not val: - raise ValueError("crs is empty JSON") + raise CRSError("crs is empty JSON") else: return val @@ -86,7 +90,12 @@ def from_string(prjs): lambda kv: len(kv) == 2 and (kv[0], parse(kv[1])) or (kv[0], True), (p.split('=') for p in parts)) - return dict((k, v) for k, v in items if k in all_proj_keys) + out = dict((k, v) for k, v in items if k in all_proj_keys) + + if not out: + raise CRSError("crs is empty or invalid: {}".format(prjs)) + + return out def from_epsg(code): diff --git a/tests/test_crs.py b/tests/test_crs.py index b1ea1406..8ae1a974 100644 --- a/tests/test_crs.py +++ b/tests/test_crs.py @@ -8,6 +8,7 @@ import rasterio from rasterio import crs from rasterio.crs import ( is_geographic_crs, is_projected_crs, is_same_crs, is_valid_crs) +from rasterio.errors import CRSError logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) @@ -149,5 +150,9 @@ def test_is_valid(): def test_empty_json(): - with pytest.raises(ValueError): + with pytest.raises(CRSError): crs.from_string('{}') + with pytest.raises(CRSError): + crs.from_string('[]') + with pytest.raises(CRSError): + crs.from_string('')