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

Harden detecting file-not-found. #726

Merged
merged 2 commits into from
Dec 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions large_image/tilesource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ def isGeospatial(path):
ds = gdal.Open(path, gdalconst.GA_ReadOnly)
except Exception:
return False
if ds.GetGCPs() and ds.GetGCPProjection():
return True
if ds.GetProjection():
return True
if ds.GetGeoTransform(can_return_null=True):
return True
if ds.GetDriver().ShortName in {'NITF', 'netCDF'}:
return True
if ds:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the conda installation, GDAL returns a dataset handle even when the file isn't found. In the pip installation, it raises an exception. Both have gdal 3.4.0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is very odd... I wonder what exactly causes this difference? Perhaps a build flag? This would be good to raise in the GDAL conda-forge feedstock

if ds.GetGCPs() and ds.GetGCPProjection():
return True
if ds.GetProjection():
return True
if ds.GetGeoTransform(can_return_null=True):
return True
if ds.GetDriver().ShortName in {'NITF', 'netCDF'}:
return True
return False


Expand Down Expand Up @@ -113,6 +114,8 @@ def getTileSourceFromDict(availableSources, pathOrUri, *args, **kwargs):
sourceName = getSourceNameFromDict(availableSources, pathOrUri, *args, **kwargs)
if sourceName:
return availableSources[sourceName](pathOrUri, *args, **kwargs)
if not os.path.exists(pathOrUri) and '://' not in pathOrUri:
raise TileSourceFileNotFoundError(pathOrUri)
raise TileSourceError('No available tilesource for %s' % pathOrUri)


Expand Down
19 changes: 19 additions & 0 deletions test/test_source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def testSourcesFileNotFound(source):
large_image.tilesource.AvailableTileSources[source]('nosuchfile.ext')


def testBaseFileNotFound():
with pytest.raises(large_image.exceptions.TileSourceFileNotFoundError):
large_image.open('nosuchfile')
with pytest.raises(large_image.exceptions.TileSourceFileNotFoundError):
large_image.open('nosuchfile.ext')


@pytest.mark.parametrize('filename', registry)
@pytest.mark.parametrize('source', SourceAndFiles)
def testSourcesCanRead(source, filename):
Expand Down Expand Up @@ -132,3 +139,15 @@ def testSourcesTilesAndMethods(source, filename):
tsf = sourceClass(imagePath, frame=len(tileMetadata['frames']) - 1)
tileMetadata = tsf.getMetadata()
utilities.checkTilesZXY(tsf, tileMetadata)


@pytest.mark.parametrize('filename,isgeo', [
('04091217_ruc.nc', True),
('HENormalN801.czi', False),
('landcover_sample_1000.tif', True),
('oahu-dense.tiff', True),
('region_gcp.tiff', True),
])
def testIsGeospatial(filename, isgeo):
imagePath = datastore.fetch(filename)
assert large_image.tilesource.isGeospatial(imagePath) == isgeo