1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
"""Unittests for rasterio.plot"""
import pytest
plt = pytest.importorskip("matplotlib.pyplot")
import matplotlib
import numpy as np
import rasterio
from rasterio.plot import show, show_hist, get_plt, plotting_extent, adjust_band
from rasterio.enums import ColorInterp
matplotlib.use("agg")
plt.show = lambda: None
def test_show_raster_band():
"""Test plotting a single raster band."""
with rasterio.open('tests/data/RGB.byte.tif') as src:
show((src, 1))
fig = plt.gcf()
plt.close(fig)
def test_show_raster_mult_bands():
"""Test multiple bands plotting."""
with rasterio.open('tests/data/RGB.byte.tif') as src:
show((src, (1, 2, 3)))
fig = plt.gcf()
plt.close(fig)
def test_show_raster_object():
"""Test plotting a raster object."""
with rasterio.open('tests/data/RGB.byte.tif') as src:
show(src)
fig = plt.gcf()
plt.close(fig)
def test_show_raster_float():
"""Test plotting a raster object with float data."""
with rasterio.open('tests/data/float.tif') as src:
show(src)
fig = plt.gcf()
plt.close(fig)
def test_show_cmyk_interp(tmpdir):
"""A CMYK TIFF has cyan, magenta, yellow, black bands."""
with rasterio.open('tests/data/RGB.byte.tif') as src:
profile = src.profile
profile['photometric'] = "CMYK"
profile['count'] = 4
del profile["nodata"]
tiffname = str(tmpdir.join('foo.tif'))
with rasterio.open(tiffname, 'w', **profile) as dst:
assert dst.colorinterp == (
ColorInterp.cyan,
ColorInterp.magenta,
ColorInterp.yellow,
ColorInterp.black)
with rasterio.open(tiffname) as src:
try:
show(src)
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_raster_no_bounds():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show((src, 1), with_bounds=False)
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_raster_title():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show((src, 1), title="insert title here")
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_hist_large():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
try:
rand_arr = np.random.randn(10, 718, 791)
show_hist(rand_arr)
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_raster_cmap():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show((src, 1), cmap='jet')
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_raster_ax():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
fig, ax = plt.subplots(1)
show((src, 1), ax=ax)
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_array():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show(src.read(1))
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_array3D():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show(src.read((1, 2, 3)))
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_hist():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show_hist((src, 1), bins=256)
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
try:
show_hist(src.read(), bins=256)
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
try:
fig, ax = plt.subplots(1)
show_hist(src.read(), bins=256, ax=ax)
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
try:
show_hist(src.read(), range=[0, 255])
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
try:
show_hist(src)
fig = plt.gcf()
ax = plt.gca()
assert ax.get_legend_handles_labels()[1] == ["1", "2", "3"]
plt.close(fig)
except ImportError:
pass
def test_show_hist_mplargs():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show_hist(src, bins=50, lw=0.0, stacked=False, alpha=0.3,
histtype='stepfilled', title="World Histogram overlaid",
range=[0, 255])
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_contour():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show((src, 1), contour=True)
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_show_contour_mplargs():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif') as src:
try:
show((src, 1), contour=True,
levels=[25, 125], colors=['white', 'red'], linewidths=4,
contour_label_kws=dict(fontsize=18, fmt="%1.0f", inline_spacing=15, use_clabeltext=True))
fig = plt.gcf()
plt.close(fig)
except ImportError:
pass
def test_get_plt():
"""
This test only verifies that code up to the point of plotting with
matplotlib works correctly. Tests do not exercise matplotlib.
"""
with rasterio.open('tests/data/RGB.byte.tif'):
try:
assert plt == get_plt()
except ImportError:
pass
def test_plt_transform():
with rasterio.open('tests/data/RGB.byte.tif') as src:
show(src.read(), transform=src.transform)
show(src.read(1), transform=src.transform)
def test_plotting_extent():
from rasterio.plot import reshape_as_image
expected = (101985.0, 339315.0, 2611485.0, 2826915.0)
with rasterio.open('tests/data/RGB.byte.tif') as src:
assert plotting_extent(src) == expected
assert plotting_extent(
reshape_as_image(src.read()), transform=src.transform) == expected
assert plotting_extent(
src.read(1), transform=src.transform) == expected
# array requires a transform
with pytest.raises(ValueError):
plotting_extent(src.read(1))
def test_plot_normalize():
a = np.linspace(1, 6, 10)
b = adjust_band(a, 'linear')
np.testing.assert_array_almost_equal(np.linspace(0, 1, 10), b)
def test_issue3007():
"""Don't squeeze further than 2D."""
with rasterio.open('tests/data/RGB.byte.tif') as src:
arr = src.read(1)[0:1, :]
assert arr.shape == (1, 791)
show(arr)
fig = plt.gcf()
plt.close(fig)
|