Skip to content

Commit 7153fd1

Browse files
committed
add is_tif unit test and is_tif recognizes vrt as tif
1 parent 304002e commit 7153fd1

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

label_maker/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ def get_tile_wms(tile, imagery, folder, imagery_offset):
120120

121121
def is_tif(imagery):
122122
"""Determine if an imagery path leads to a valid tif"""
123+
valid_drivers = ['GTiff', 'VRT']
123124
try:
124125
with rasterio.open(imagery) as test_ds:
125-
if test_ds.meta['driver'] != 'GTiff':
126+
if test_ds.meta['driver'] not in valid_drivers:
126127
# rasterio can open path, but it is not a tif
128+
raise Exception(test_ds.meta['driver'])
127129
valid_tif = False
128130
else:
129131
valid_tif = True

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)