Skip to content

Commit

Permalink
Harden detecting file-not-found.
Browse files Browse the repository at this point in the history
This handles some difference in the conda and pip installations of gdal.
  • Loading branch information
manthey committed Dec 13, 2021
1 parent 8e8deaa commit e9e42c9
Show file tree
Hide file tree
Showing 2 changed files with 18 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
7 changes: 7 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

0 comments on commit e9e42c9

Please sign in to comment.