Convert complex types to float when it comes to nodata

Resolves #1747
This commit is contained in:
Sean Gillies 2019-08-22 12:46:03 -06:00
parent 7d316a5437
commit 6a1956e661
3 changed files with 30 additions and 0 deletions

View File

@ -4,6 +4,7 @@ Changes
1.0.26 (TBD)
------------
- Allow complex datasets to be created with a specified nodata value (#1747).
- When a MemoryFile is opened in write mode, a TypeError will be raised if
integer width and height are not given, fixing #1748.

View File

@ -420,6 +420,13 @@ cdef class DatasetBase(object):
for i in range(self._count):
band = self.band(i + 1)
dtype = _band_dtype(band)
# To address the issue in #1747.
if dtype == "complex128":
dtype = "float64"
elif dtype == "complex64":
dtype = "float32"
nodataval = GDALGetRasterNoDataValue(band, &success)
val = nodataval
# GDALGetRasterNoDataValue() has two ways of telling you that

View File

@ -36,3 +36,25 @@ def test_read_array(tempfile, dtype, height, width):
dataset.write(in_img, 1)
out_img = dataset.read(1)
assert (in_img == out_img).all()
def test_complex_nodata(tmpdir):
"""A complex dataset can be created with a real nodata value"""
import numpy as np
import rasterio
from rasterio.transform import Affine
x = np.linspace(-4.0, 4.0, 240)
y = np.linspace(-3.0, 3.0, 180)
X, Y = np.meshgrid(x, y)
Z1 = np.ones_like(X) + 1j
res = (x[-1] - x[0]) / 240.0
transform1 = Affine.translation(x[0] - res / 2, y[-1] - res / 2) * Affine.scale(res, -res)
tempfile = str(tmpdir.join("test.tif"))
with rasterio.open(tempfile, 'w', driver='GTiff', height=Z1.shape[0], width=Z1.shape[1], nodata=0, count=1, dtype=Z1.dtype, crs='+proj=latlong', transform=transform1) as dst:
dst.write(Z1, 1)
with rasterio.open(tempfile) as dst:
assert dst.nodata == 0