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

Update crs_check with fixes #711

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)