rasterio/tests/test_update.py
Robert Sare 4b0d3dd69e Mark tests that call GDAL binaries (#1555) (#1557)
* add webp to the available compression enum (#1515)

* Add webp to enums.Compression and increment version

* Add link to dev discussion group

* fix year specified in version 1.0.9 date (#1530)

Year specified in version 1.0.9 date is 2019, which is in the future at this time. Set to 2018, which is in the past.

* Update docstring for plotting_extent (#1516)

* added support for ESRI wkt strings - resolves issue #1537 (#1538)

* updated _handle_crswkt to use CRS.from_wkt; resolved issue #1540 (#1541)

* updated _handle_crswkt to use CRS.from_wkt; resolved issue #1540

* added test for datasetreader opening dataset with ESRI projection wkt string

* Fix a typo (probably) (#1526)

* Fix a typo (probably)

* Update url for manylinux

* Update README.rst

Co-Authored-By: ismailsunni <imajimatika@gmail.com>

* Update README.rst

Co-Authored-By: ismailsunni <imajimatika@gmail.com>

* Remove unused string

* modified _osr_from_crs to use CRS.from_user_input (#1546)

* modified _osr_from_crs to use CRS.from_user_input

* update to use tmpdir fixture for pytest

* update comparison for error output for rio warp

* updated to always attempt to MorphFromESRI to prevent exception from being raised

* added test to test warping with ESRI wkt

* updated data_dir to be used as fixture

* removed duplicate import of CRSError

* reordered initialization of spatial reference to prevent memory leaks

* updated to raise CRSError of auth != EPSG

* Revert "modified _osr_from_crs to use CRS.from_user_input (#1546)" (#1548)

This reverts commit b120a00a05e4f6174c02e855cc016fde9b54c4e0.

* Mark tests using gdal binaries

* Add pytest import
2018-11-21 09:58:28 -07:00

107 lines
3.5 KiB
Python

import subprocess
import re
import affine
import numpy as np
import pytest
import rasterio
from rasterio.env import GDALVersion
from .conftest import requires_gdal21
@pytest.mark.gdalbin
def test_update_tags(data):
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
f.update_tags(a='1', b='2')
f.update_tags(1, c=3)
with pytest.raises(IndexError):
f.update_tags(4, d=4)
assert f.tags() == {'AREA_OR_POINT': 'Area', 'a': '1', 'b': '2'}
assert ('c', '3') in f.tags(1).items()
info = subprocess.check_output(["gdalinfo", tiffname]).decode('utf-8')
assert re.search(r'Metadata:\W+a=1\W+AREA_OR_POINT=Area\W+b=2', info)
def test_update_band(data):
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
f.write(np.zeros(f.shape, dtype=f.dtypes[0]), indexes=1)
with rasterio.open(tiffname) as f:
assert not f.read(1).any()
def test_update_spatial(data):
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
f.transform = affine.Affine(1.0, 0.0, 1.0,
0.0, -1.0, 0.0)
f.crs = {'init': 'epsg:4326'}
with rasterio.open(tiffname) as f:
assert f.transform == affine.Affine(1.0, 0.0, 1.0,
0.0, -1.0, 0.0)
assert f.transform.to_gdal() == (1.0, 1.0, 0.0, 0.0, 0.0, -1.0)
assert f.crs == {'init': 'epsg:4326'}
def test_update_spatial_epsg(data):
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
f.transform = affine.Affine(1.0, 0.0, 1.0,
0.0, -1.0, 0.0)
f.crs = 'EPSG:4326'
with rasterio.open(tiffname) as f:
assert f.transform == affine.Affine(1.0, 0.0, 1.0,
0.0, -1.0, 0.0)
assert f.transform.to_gdal() == (1.0, 1.0, 0.0, 0.0, 0.0, -1.0)
assert f.crs == {'init': 'epsg:4326'}
def test_update_nodata(data):
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
f.nodata = 255
with rasterio.open(tiffname) as f:
assert f.nodatavals == (255, 255, 255)
@pytest.mark.skipif(
GDALVersion.runtime().at_least('2.1'),
reason='Tests behavior specific to GDAL versions < 2.1')
def test_update_nodatavals_none_fails(data):
"""GDAL 2.0 doesn't support un-setting nodata values."""
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
with pytest.raises(NotImplementedError):
f.nodata = None
@requires_gdal21
def test_update_nodatavals_none(data):
"""GDAL 2.1 does support un-setting nodata values."""
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
f.nodata = None
with rasterio.open(tiffname) as f:
assert f.nodatavals == (None, None, None)
def test_update_mask_true(data):
"""Provide an option to set a uniformly valid mask."""
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
f.write_mask(True)
with rasterio.open(tiffname) as f:
assert f.read_masks().all()
def test_update_mask_false(data):
"""Provide an option to set a uniformly invalid mask."""
tiffname = str(data.join('RGB.byte.tif'))
with rasterio.open(tiffname, 'r+') as f:
f.write_mask(False)
with rasterio.open(tiffname) as f:
assert not f.read_masks().any()