Skip to content

Commit

Permalink
Merge pull request #726 from girder/harden-file-not-found
Browse files Browse the repository at this point in the history
Harden detecting file-not-found.
  • Loading branch information
manthey authored Dec 13, 2021
2 parents 6a96d81 + 4ececc8 commit ffe4dbd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
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:
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

0 comments on commit ffe4dbd

Please sign in to comment.