From e9e42c9128936e06fa9f0c3e2e6378c42885072d Mon Sep 17 00:00:00 2001 From: David Manthey Date: Mon, 13 Dec 2021 09:08:43 -0500 Subject: [PATCH] Harden detecting file-not-found. This handles some difference in the conda and pip installations of gdal. --- large_image/tilesource/__init__.py | 19 +++++++++++-------- test/test_source_base.py | 7 +++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/large_image/tilesource/__init__.py b/large_image/tilesource/__init__.py index 6a3a43549..1f29a636d 100644 --- a/large_image/tilesource/__init__.py +++ b/large_image/tilesource/__init__.py @@ -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 @@ -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) diff --git a/test/test_source_base.py b/test/test_source_base.py index d1693ccc0..81a0e88be 100644 --- a/test/test_source_base.py +++ b/test/test_source_base.py @@ -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):