@@ -1216,21 +1216,6 @@ def test_weakrefs(self):
1216
1216
@requires_rasterio
1217
1217
class TestRasterIO (CFEncodedDataTest , Only32BitTypes , TestCase ):
1218
1218
1219
- # def setUp(self):
1220
- #
1221
- #
1222
- # name = 'test_latlong.tif'
1223
- #
1224
- #
1225
- # name = 'test_utm.tif'
1226
- # transform = from_origin(-300, 200, 1000, 1000)
1227
- # with rasterio.open(
1228
- # name, 'w',
1229
- # driver='GTiff', height=3, width=4, count=1,
1230
- # transform=transform,
1231
- # dtype=rasterio.float32) as s:
1232
- # s.write(a, indexes=1)
1233
-
1234
1219
def test_write_store (self ):
1235
1220
# RasterIO is read-only for now
1236
1221
pass
@@ -1239,71 +1224,166 @@ def test_orthogonal_indexing(self):
1239
1224
# RasterIO also does not support list-like indexing
1240
1225
pass
1241
1226
1242
- def test_latlong_coords (self ):
1227
+ def test_latlong_basics (self ):
1243
1228
1244
1229
import rasterio
1245
1230
from rasterio .transform import from_origin
1231
+ from ..core .utils import get_latlon_coords_from_crs
1246
1232
1247
1233
# Create a geotiff file in latlong proj
1248
- data = np .arange (12 , dtype = rasterio .float32 ).reshape (3 , 4 )
1249
1234
with create_tmp_file (suffix = '.tif' ) as tmp_file :
1250
- transform = from_origin (1 , 2 , 0.5 , 1. )
1235
+ # data
1236
+ nx , ny = 8 , 10
1237
+ data = np .arange (80 , dtype = rasterio .float32 ).reshape (ny , nx )
1238
+ transform = from_origin (1 , 2 , 0.5 , 2. )
1251
1239
with rasterio .open (
1252
1240
tmp_file , 'w' ,
1253
- driver = 'GTiff' , height = 3 , width = 4 , count = 1 ,
1241
+ driver = 'GTiff' , height = ny , width = nx , count = 1 ,
1254
1242
crs = '+proj=latlong' ,
1255
1243
transform = transform ,
1256
1244
dtype = rasterio .float32 ) as s :
1257
1245
s .write (data , indexes = 1 )
1258
1246
actual = xr .open_dataset (tmp_file , engine = 'rasterio' )
1259
1247
1248
+ # ref
1260
1249
expected = Dataset ()
1261
- expected ['x' ] = ('x' , [ 1 , 1.5 , 2 , 2.5 ] )
1262
- expected ['y' ] = ('y' , [ 2. , 1 , 0 ] )
1250
+ expected ['x' ] = ('x' , np . arange ( nx ) * 0.5 + 1 )
1251
+ expected ['y' ] = ('y' , - np . arange ( ny ) * 2 + 2 )
1263
1252
expected ['band' ] = ('band' , [1 ])
1264
1253
expected ['raster' ] = (('band' , 'y' , 'x' ), data [np .newaxis , ...])
1265
1254
lon , lat = np .meshgrid (expected ['x' ], expected ['y' ])
1266
1255
expected ['lon' ] = (('y' , 'x' ), lon )
1267
1256
expected ['lat' ] = (('y' , 'x' ), lat )
1268
1257
1258
+ # tests
1269
1259
assert_allclose (actual .y , expected .y )
1270
1260
assert_allclose (actual .x , expected .x )
1271
1261
assert_allclose (actual .raster , expected .raster )
1262
+
1263
+ actual = get_latlon_coords_from_crs (actual )
1272
1264
assert_allclose (actual .lon , expected .lon )
1273
1265
assert_allclose (actual .lat , expected .lat )
1274
1266
1275
- print ( actual )
1267
+ assert 'crs' in actual . attrs
1276
1268
1277
- def test_utm_coords (self ):
1269
+ def test_utm_basics (self ):
1278
1270
1279
1271
import rasterio
1280
1272
from rasterio .transform import from_origin
1273
+ from ..core .utils import get_latlon_coords_from_crs
1281
1274
1282
1275
# Create a geotiff file in utm proj
1283
- data = np .arange (24 , dtype = rasterio .float32 ).reshape (2 , 3 , 4 )
1284
1276
with create_tmp_file (suffix = '.tif' ) as tmp_file :
1285
- transform = from_origin (- 3000 , 1000 , 1000 , 1000 )
1277
+ # data
1278
+ nx , ny , nz = 4 , 3 , 3
1279
+ data = np .arange (nx * ny * nz ,
1280
+ dtype = rasterio .float32 ).reshape (nz , ny , nx )
1281
+ transform = from_origin (5000 , 80000 , 1000 , 2000. )
1286
1282
with rasterio .open (
1287
1283
tmp_file , 'w' ,
1288
- driver = 'GTiff' , height = 3 , width = 4 , count = 2 ,
1284
+ driver = 'GTiff' , height = ny , width = nx , count = nz ,
1289
1285
crs = {'units' : 'm' , 'no_defs' : True , 'ellps' : 'WGS84' ,
1290
1286
'proj' : 'utm' , 'zone' : 18 },
1291
1287
transform = transform ,
1292
1288
dtype = rasterio .float32 ) as s :
1293
1289
s .write (data )
1294
1290
actual = xr .open_dataset (tmp_file , engine = 'rasterio' )
1295
1291
1292
+ # ref
1296
1293
expected = Dataset ()
1297
- expected ['x' ] = ('x' , [ - 3000. , - 2000 , - 1000 , 0 ] )
1298
- expected ['y' ] = ('y' , [ 1000. , 0 , - 1000 ] )
1299
- expected ['band' ] = ('band' , [1 , 2 ])
1294
+ expected ['x' ] = ('x' , np . arange ( nx ) * 1000 + 5000 )
1295
+ expected ['y' ] = ('y' , - np . arange ( ny ) * 2000 + 80000 )
1296
+ expected ['band' ] = ('band' , [1 , 2 , 3 ])
1300
1297
expected ['raster' ] = (('band' , 'y' , 'x' ), data )
1301
1298
1299
+ # data obtained independently with pyproj
1300
+ lon = np .array (
1301
+ [[- 79.44429834 , - 79.43533803 , - 79.42637762 , - 79.4174171 ],
1302
+ [- 79.44428102 , - 79.43532075 , - 79.42636037 , - 79.41739988 ],
1303
+ [- 79.44426413 , - 79.4353039 , - 79.42634355 , - 79.4173831 ]])
1304
+ lat = np .array (
1305
+ [[0.72159393 , 0.72160275 , 0.72161156 , 0.72162034 ],
1306
+ [0.70355411 , 0.70356271 , 0.70357129 , 0.70357986 ],
1307
+ [0.68551428 , 0.68552266 , 0.68553103 , 0.68553937 ]])
1308
+ expected ['lon' ] = (('y' , 'x' ), lon )
1309
+ expected ['lat' ] = (('y' , 'x' ), lat )
1310
+
1311
+ # tests
1302
1312
assert_allclose (actual .y , expected .y )
1303
1313
assert_allclose (actual .x , expected .x )
1304
1314
assert_allclose (actual .raster , expected .raster )
1305
1315
1306
- print (actual )
1316
+ actual = get_latlon_coords_from_crs (actual )
1317
+ assert_allclose (actual .lon , expected .lon )
1318
+ assert_allclose (actual .lat , expected .lat )
1319
+
1320
+ assert 'crs' in actual .attrs
1321
+
1322
+ def test_indexing (self ):
1323
+
1324
+ import rasterio
1325
+ from rasterio .transform import from_origin
1326
+
1327
+ # Create a geotiff file in latlong proj
1328
+ with create_tmp_file (suffix = '.tif' ) as tmp_file :
1329
+ # data
1330
+ nx , ny , nz = 8 , 10 , 3
1331
+ data = np .arange (nx * ny * nz ,
1332
+ dtype = rasterio .float32 ).reshape (nz , ny , nx )
1333
+ transform = from_origin (1 , 2 , 0.5 , 2. )
1334
+ with rasterio .open (
1335
+ tmp_file , 'w' ,
1336
+ driver = 'GTiff' , height = ny , width = nx , count = nz ,
1337
+ crs = '+proj=latlong' ,
1338
+ transform = transform ,
1339
+ dtype = rasterio .float32 ) as s :
1340
+ s .write (data )
1341
+ actual = xr .open_dataset (tmp_file , engine = 'rasterio' )
1342
+
1343
+ # ref
1344
+ expected = Dataset ()
1345
+ expected ['x' ] = ('x' , np .arange (nx )* 0.5 + 1 )
1346
+ expected ['y' ] = ('y' , - np .arange (ny )* 2 + 2 )
1347
+ expected ['band' ] = ('band' , [1 , 2 , 3 ])
1348
+ expected ['raster' ] = (('band' , 'y' , 'x' ), data )
1349
+
1350
+ # tests
1351
+ _ex = expected .isel (band = 1 )
1352
+ _ac = actual .isel (band = 1 )
1353
+ assert_allclose (_ac .y , _ex .y )
1354
+ assert_allclose (_ac .x , _ex .x )
1355
+ assert_allclose (_ac .band , _ex .band )
1356
+ assert_allclose (_ac .raster , _ex .raster )
1357
+
1358
+ _ex = expected .isel (x = slice (2 , 5 ), y = slice (5 , 7 ))
1359
+ _ac = actual .isel (x = slice (2 , 5 ), y = slice (5 , 7 ))
1360
+ assert_allclose (_ac .y , _ex .y )
1361
+ assert_allclose (_ac .x , _ex .x )
1362
+ assert_allclose (_ac .raster , _ex .raster )
1363
+
1364
+ _ex = expected .isel (band = slice (1 , 2 ), x = slice (2 , 5 ), y = slice (5 , 7 ))
1365
+ _ac = actual .isel (band = slice (1 , 2 ), x = slice (2 , 5 ), y = slice (5 , 7 ))
1366
+ assert_allclose (_ac .y , _ex .y )
1367
+ assert_allclose (_ac .x , _ex .x )
1368
+ assert_allclose (_ac .raster , _ex .raster )
1369
+
1370
+ _ex = expected .isel (x = 1 , y = 2 )
1371
+ _ac = actual .isel (x = 1 , y = 2 )
1372
+ assert_allclose (_ac .y , _ex .y )
1373
+ assert_allclose (_ac .x , _ex .x )
1374
+ # TODO: this doesnt work properly because of the shape
1375
+ # assert_allclose(_ac.raster, _ex.raster)
1376
+ np .testing .assert_allclose (_ac .raster .values .flatten (),
1377
+ _ex .raster .values )
1378
+
1379
+ _ex = expected .isel (band = 0 , x = 1 , y = 2 )
1380
+ _ac = actual .isel (band = 0 , x = 1 , y = 2 )
1381
+ assert_allclose (_ac .y , _ex .y )
1382
+ assert_allclose (_ac .x , _ex .x )
1383
+ # TODO: this doesnt work properly because of the shape
1384
+ # assert_allclose(_ac.raster, _ex.raster)
1385
+ np .testing .assert_allclose (_ac .raster .values .flatten (),
1386
+ _ex .raster .values )
1307
1387
1308
1388
1309
1389
class TestEncodingInvalid (TestCase ):
0 commit comments