Skip to content

Commit

Permalink
Merge pull request #711 from nkorinek/crs-update
Browse files Browse the repository at this point in the history
Update crs_check with fixes
  • Loading branch information
nkorinek committed Jul 1, 2021
2 parents b578f8b + e48989b commit ad3de3f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
26 changes: 19 additions & 7 deletions earthpy/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def crs_check(path):
Parameters
----------
path : string
Path to the raster file.
Path to a raster file in a format that rasterio can read.
Returns
-------
Expand All @@ -643,16 +643,28 @@ def crs_check(path):

try:
with rio.open(path) as src:
crs = src.crs
# This section runs when the data is in a hierarchial format
if crs is None:
if len(src.subdatasets) > 0:
for data in src.subdatasets:
with rio.open(data) as data_src:
crs = data_src.crs
return crs
except rio.errors.RasterioIOError as e:
print("Please only input files that can be read as a raster.\n")
raise rio.errors.RasterioIOError(e.__str__())
else:
crs = src.crs
if crs is None:
raise ValueError(
"No CRS found in data. The raster may not have one."
)
else:
return crs
except rio.errors.RasterioIOError:
raise rio.errors.RasterioIOError(
"Oops, your data are not in a format "
"that rasterio can read. Please "
"check the rasterio documentation "
"for accepted file formats and make "
"sure that your data are in raster "
"format..\n"
)


# @deprecate
Expand Down
23 changes: 22 additions & 1 deletion earthpy/tests/test_epsg.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import pytest
import rasterio as rio
import os.path as op
import earthpy as et
import earthpy.spatial as es
from earthpy.io import path_to_example


@pytest.fixture
def output_dir(out_path):
return op.dirname(out_path)


def test_epsg():
"""Unit test for loading EPSG to Proj4 string dictionary."""

Expand All @@ -18,5 +24,20 @@ def test_crs_check_tif():


def test_crs_check_bad_file():
with pytest.raises(rio.errors.RasterioIOError):
with pytest.raises(rio.errors.RasterioIOError, match="Oops, your data ar"):
es.crs_check(path_to_example("rmnp.shp"))


def test_no_crs_in_file(output_dir):
output_path = op.join(output_dir, "no_crs.tif")

with rio.open(et.io.path_to_example("green.tif")) as src:
data = src.read(1)
profile = src.profile
profile.update(crs=None)

with rio.open(output_path, 'w', **profile) as dst:
dst.write(data, 1)

with pytest.raises(ValueError, match="No CRS found in data. The raster "):
es.crs_check(output_path)

0 comments on commit ad3de3f

Please sign in to comment.