Skip to content

Commit 0b76ebb

Browse files
authored
Merge pull request #82 from jreiberkyle/geotiff-download-80
use rasterio to determine if path is a valid tif
2 parents a694ecb + c4efb28 commit 0b76ebb

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

label_maker/utils.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,21 @@ def get_tile_wms(tile, imagery, folder, imagery_offset):
119119

120120

121121
def is_tif(imagery):
122-
"""Determine if an imagery path has a valid tif extension"""
123-
return op.splitext(imagery)[1].lower() in ['.tif', '.tiff', '.vrt']
122+
"""Determine if an imagery path leads to a valid tif"""
123+
valid_drivers = ['GTiff', 'VRT']
124+
try:
125+
with rasterio.open(imagery) as test_ds:
126+
if test_ds.meta['driver'] not in valid_drivers:
127+
# rasterio can open path, but it is not a tif
128+
valid_tif = False
129+
else:
130+
valid_tif = True
131+
except rasterio._err.CPLE_HttpResponseError: #pylint: disable=protected-access
132+
# rasterio cannot open the path. this is the case for a
133+
# tile service
134+
valid_tif = False
135+
136+
return valid_tif
124137

125138
def is_wms(imagery):
126139
"""Determine if an imagery path is a WMS endpoint"""

test/unit/test_utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Tests for utils.py"""
22
import os
33
from os import path as op, makedirs
4+
import shutil
5+
import tempfile
46
import unittest
57
import numpy as np
68
from PIL import Image
79

8-
from label_maker.utils import url, class_match, get_tile_tif, get_tile_wms
10+
from label_maker.utils import url, class_match, get_tile_tif, get_tile_wms, is_tif
911

1012
class TestUtils(unittest.TestCase):
1113
"""Tests for utility functions"""
@@ -43,6 +45,33 @@ def test_class_match_segmentation(self):
4345
self.assertTrue(class_match(ml_type, passing, class_index))
4446
self.assertFalse(class_match(ml_type, failing, class_index))
4547

48+
def test_is_tif(self):
49+
"""Test identifying tif or vrt files as tif"""
50+
img_dir = op.join('test', 'fixtures')
51+
52+
# tif with .tif extension identified as tif
53+
test_tif = op.join(img_dir, 'drone.tif')
54+
self.assertTrue(is_tif(test_tif))
55+
56+
# vrt with .vrt extension identified as tif
57+
test_vrt = op.join(img_dir, 'drone.vrt')
58+
self.assertTrue(is_tif(test_vrt))
59+
60+
61+
# tif with no extension identified as tif
62+
with tempfile.TemporaryDirectory() as tmpdirname:
63+
test_tif_no_ext = op.join(tmpdirname, 'drone')
64+
shutil.copy(test_tif, test_tif_no_ext)
65+
self.assertTrue(is_tif(test_tif_no_ext))
66+
67+
# vrt with no extension identified as tif
68+
with tempfile.TemporaryDirectory() as tmpdirname:
69+
test_vrt_no_ext = op.join(tmpdirname, 'drone')
70+
shutil.copy(test_vrt, test_vrt_no_ext)
71+
self.assertTrue(is_tif(test_vrt_no_ext))
72+
73+
74+
4675
def test_get_tile_tif(self):
4776
"""Test reading of tile from geotiff"""
4877
tile = '1087767-1046604-21'

0 commit comments

Comments
 (0)