@@ -2170,6 +2170,68 @@ def test_serialization(self):
2170
2170
with xr .open_dataarray (tmp_nc_file ) as ncds :
2171
2171
assert_identical (rioda , ncds )
2172
2172
2173
+ @requires_scipy_or_netCDF4
2174
+ def test_nodata (self ):
2175
+ import rasterio
2176
+ from rasterio .transform import from_origin
2177
+
2178
+ # Create a geotiff file in utm proj
2179
+ with create_tmp_file (suffix = '.tif' ) as tmp_file :
2180
+ # data
2181
+ nx , ny , nz = 4 , 3 , 3
2182
+ data = np .arange (nx * ny * nz ,
2183
+ dtype = rasterio .float32 ).reshape (nz , ny , nx )
2184
+ transform = from_origin (5000 , 80000 , 1000 , 2000. )
2185
+ with rasterio .open (
2186
+ tmp_file , 'w' ,
2187
+ driver = 'GTiff' , height = ny , width = nx , count = nz ,
2188
+ crs = {'units' : 'm' , 'no_defs' : True , 'ellps' : 'WGS84' ,
2189
+ 'proj' : 'utm' , 'zone' : 18 },
2190
+ transform = transform ,
2191
+ nodata = - 9765 ,
2192
+ dtype = rasterio .float32 ) as s :
2193
+ s .write (data )
2194
+ expected_nodatavals = [- 9765 , - 9765 , - 9765 ]
2195
+ with xr .open_rasterio (tmp_file ) as rioda :
2196
+ np .testing .assert_array_equal (rioda .attrs ['nodatavals' ],
2197
+ expected_nodatavals )
2198
+ with create_tmp_file (suffix = '.nc' ) as tmp_nc_file :
2199
+ rioda .to_netcdf (tmp_nc_file )
2200
+ with xr .open_dataarray (tmp_nc_file ) as ncds :
2201
+ np .testing .assert_array_equal (ncds .attrs ['nodatavals' ],
2202
+ expected_nodatavals )
2203
+
2204
+ @requires_scipy_or_netCDF4
2205
+ def test_nodata_missing (self ):
2206
+ import rasterio
2207
+ from rasterio .transform import from_origin
2208
+
2209
+ # Create a geotiff file in utm proj
2210
+ with create_tmp_file (suffix = '.tif' ) as tmp_file :
2211
+ # data
2212
+ nx , ny , nz = 4 , 3 , 3
2213
+ data = np .arange (nx * ny * nz ,
2214
+ dtype = rasterio .float32 ).reshape (nz , ny , nx )
2215
+ transform = from_origin (5000 , 80000 , 1000 , 2000. )
2216
+ with rasterio .open (
2217
+ tmp_file , 'w' ,
2218
+ driver = 'GTiff' , height = ny , width = nx , count = nz ,
2219
+ crs = {'units' : 'm' , 'no_defs' : True , 'ellps' : 'WGS84' ,
2220
+ 'proj' : 'utm' , 'zone' : 18 },
2221
+ transform = transform ,
2222
+ dtype = rasterio .float32 ) as s :
2223
+ s .write (data )
2224
+
2225
+ expected_nodatavals = [np .nan , np .nan , np .nan ]
2226
+ with xr .open_rasterio (tmp_file ) as rioda :
2227
+ np .testing .assert_array_equal (rioda .attrs ['nodatavals' ],
2228
+ expected_nodatavals )
2229
+ with create_tmp_file (suffix = '.nc' ) as tmp_nc_file :
2230
+ rioda .to_netcdf (tmp_nc_file )
2231
+ with xr .open_dataarray (tmp_nc_file ) as ncds :
2232
+ np .testing .assert_array_equal (ncds .attrs ['nodatavals' ],
2233
+ expected_nodatavals )
2234
+
2173
2235
def test_utm (self ):
2174
2236
import rasterio
2175
2237
from rasterio .transform import from_origin
0 commit comments