99from rasterstats import zonal_stats , raster_stats
1010from rasterstats .utils import VALID_STATS
1111from rasterstats .io import read_featurecollection , read_features
12+ from shapely .geometry import Polygon
13+ from affine import Affine
1214
1315sys .path .append (os .path .dirname (os .path .abspath (__file__ )))
1416
@@ -27,6 +29,16 @@ def test_main():
2729 assert round (stats [0 ]['mean' ], 2 ) == 14.66
2830
2931
32+ # remove after band_num alias is removed
33+ def test_band_alias ():
34+ polygons = os .path .join (DATA , 'polygons.shp' )
35+ stats_a = zonal_stats (polygons , raster )
36+ stats_b = zonal_stats (polygons , raster , band = 1 )
37+ with pytest .deprecated_call ():
38+ stats_c = zonal_stats (polygons , raster , band_num = 1 )
39+ assert stats_a [0 ]['count' ] == stats_b [0 ]['count' ] == stats_c [0 ]['count' ]
40+
41+
3042def test_zonal_global_extent ():
3143 polygons = os .path .join (DATA , 'polygons.shp' )
3244 stats = zonal_stats (polygons , raster )
@@ -374,6 +386,27 @@ def test_some_nodata():
374386 assert stats [1 ]['nodata' ] == 19
375387 assert stats [1 ]['count' ] == 31
376388
389+
390+ # update this if nan end up being incorporated into nodata
391+ def test_nan_nodata ():
392+ polygon = Polygon ([[0 , 0 ], [2 , 0 ], [2 , 2 ], [0 , 2 ]])
393+ arr = np .array ([
394+ [np .nan , 12.25 ],
395+ [- 999 , 12.75 ]
396+ ])
397+ affine = Affine (1 , 0 , 0 ,
398+ 0 , - 1 , 2 )
399+
400+ stats = zonal_stats (polygon , arr , affine = affine , nodata = - 999 ,
401+ stats = 'nodata count sum mean min max' )
402+
403+ assert stats [0 ]['nodata' ] == 1
404+ assert stats [0 ]['count' ] == 2
405+ assert stats [0 ]['mean' ] == 12.5
406+ assert stats [0 ]['min' ] == 12.25
407+ assert stats [0 ]['max' ] == 12.75
408+
409+
377410def test_some_nodata_ndarray ():
378411 polygons = os .path .join (DATA , 'polygons.shp' )
379412 raster = os .path .join (DATA , 'slope_nodata.tif' )
@@ -425,6 +458,38 @@ def test_geojson_out():
425458 assert 'count' in feature ['properties' ] # from zonal stats
426459
427460
461+
462+ # do not think this is actually testing the line i wanted it to
463+ # since the read_features func for this data type is generating
464+ # the properties field
465+ def test_geojson_out_with_no_properties ():
466+ polygon = Polygon ([[0 , 0 ], [0 , 0 ,5 ], [1 , 1.5 ], [1.5 , 2 ], [2 , 2 ], [2 , 0 ]])
467+ arr = np .array ([
468+ [100 , 1 ],
469+ [100 , 1 ]
470+ ])
471+ affine = Affine (1 , 0 , 0 ,
472+ 0 , - 1 , 2 )
473+
474+ stats = zonal_stats (polygon , arr , affine = affine , geojson_out = True )
475+ assert 'properties' in stats [0 ]
476+ for key in ['count' , 'min' , 'max' , 'mean' ]:
477+ assert key in stats [0 ]['properties' ]
478+
479+ assert stats [0 ]['properties' ]['mean' ] == 34
480+
481+
482+ # remove when copy_properties alias is removed
483+ def test_copy_properties_warn ():
484+ polygons = os .path .join (DATA , 'polygons.shp' )
485+ # run once to trigger any other unrelated deprecation warnings
486+ # so the test does not catch them instead
487+ stats_a = zonal_stats (polygons , raster )
488+ with pytest .deprecated_call ():
489+ stats_b = zonal_stats (polygons , raster , copy_properties = True )
490+ assert stats_a == stats_b
491+
492+
428493def test_nan_counts ():
429494 from affine import Affine
430495 transform = Affine (1 , 0 , 1 , 0 , - 1 , 3 )
@@ -469,3 +534,4 @@ def test_geodataframe_zonal():
469534
470535 expected = zonal_stats (polygons , raster )
471536 assert zonal_stats (df , raster ) == expected
537+
0 commit comments