diff --git a/.gitignore b/.gitignore index 432f35f..b2e04b2 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ Vagrantfile *.ipynb_checkpoints* .idea venv -.eggs \ No newline at end of file +.eggs +.cache diff --git a/src/rasterstats/main.py b/src/rasterstats/main.py index 408e6d2..540ed20 100644 --- a/src/rasterstats/main.py +++ b/src/rasterstats/main.py @@ -134,8 +134,8 @@ def gen_zonal_stats( warnings.warn("Use `geojson_out` to preserve feature properties", DeprecationWarning) - bn = kwargs.get('band_num') - if bn: + band_num = kwargs.get('band_num') + if band_num: warnings.warn("Use `band` to specify band number", DeprecationWarning) band = band_num diff --git a/tests/test_cli.py b/tests/test_cli.py index d323eba..b6834aa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -34,7 +34,7 @@ def test_cli_feature_stdin(): warnings.simplefilter('ignore') result = runner.invoke(zonalstats, ['--raster', raster, - '--stats', 'mean', + '--stats', 'all', '--prefix', 'test_'], input=open(vector, 'r').read()) assert result.exit_code == 0 @@ -42,6 +42,7 @@ def test_cli_feature_stdin(): assert len(outdata['features']) == 1 feature = outdata['features'][0] assert 'test_mean' in feature['properties'] + assert 'test_std' in feature['properties'] def test_cli_features_sequence(): diff --git a/tests/test_io.py b/tests/test_io.py index 72583fc..b624133 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -268,6 +268,11 @@ def test_Raster(): r2 = Raster(arr, affine, nodata, band=1).read(bounds) + with pytest.raises(ValueError): + r3 = Raster(arr, affine, nodata, band=1).read() + with pytest.raises(ValueError): + r4 = Raster(arr, affine, nodata, band=1).read(bounds=1, window=1) + # If the abstraction is correct, the arrays are equal assert np.array_equal(r1.array, r2.array) diff --git a/tests/test_zonal.py b/tests/test_zonal.py index 1abd979..48babc9 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -9,6 +9,8 @@ from rasterstats import zonal_stats, raster_stats from rasterstats.utils import VALID_STATS from rasterstats.io import read_featurecollection, read_features +from shapely.geometry import Polygon +from affine import Affine sys.path.append(os.path.dirname(os.path.abspath(__file__))) @@ -27,6 +29,16 @@ def test_main(): assert round(stats[0]['mean'], 2) == 14.66 +# remove after band_num alias is removed +def test_band_alias(): + polygons = os.path.join(DATA, 'polygons.shp') + stats_a = zonal_stats(polygons, raster) + stats_b = zonal_stats(polygons, raster, band=1) + with pytest.deprecated_call(): + stats_c = zonal_stats(polygons, raster, band_num=1) + assert stats_a[0]['count'] == stats_b[0]['count'] == stats_c[0]['count'] + + def test_zonal_global_extent(): polygons = os.path.join(DATA, 'polygons.shp') stats = zonal_stats(polygons, raster) @@ -374,6 +386,27 @@ def test_some_nodata(): assert stats[1]['nodata'] == 19 assert stats[1]['count'] == 31 + +# update this if nan end up being incorporated into nodata +def test_nan_nodata(): + polygon = Polygon([[0, 0], [2, 0], [2, 2], [0, 2]]) + arr = np.array([ + [np.nan, 12.25], + [-999, 12.75] + ]) + affine = Affine(1, 0, 0, + 0, -1, 2) + + stats = zonal_stats(polygon, arr, affine=affine, nodata=-999, + stats='nodata count sum mean min max') + + assert stats[0]['nodata'] == 1 + assert stats[0]['count'] == 2 + assert stats[0]['mean'] == 12.5 + assert stats[0]['min'] == 12.25 + assert stats[0]['max'] == 12.75 + + def test_some_nodata_ndarray(): polygons = os.path.join(DATA, 'polygons.shp') raster = os.path.join(DATA, 'slope_nodata.tif') @@ -425,6 +458,38 @@ def test_geojson_out(): assert 'count' in feature['properties'] # from zonal stats + +# do not think this is actually testing the line i wanted it to +# since the read_features func for this data type is generating +# the properties field +def test_geojson_out_with_no_properties(): + polygon = Polygon([[0, 0], [0, 0,5], [1, 1.5], [1.5, 2], [2, 2], [2, 0]]) + arr = np.array([ + [100, 1], + [100, 1] + ]) + affine = Affine(1, 0, 0, + 0, -1, 2) + + stats = zonal_stats(polygon, arr, affine=affine, geojson_out=True) + assert 'properties' in stats[0] + for key in ['count', 'min', 'max', 'mean']: + assert key in stats[0]['properties'] + + assert stats[0]['properties']['mean'] == 34 + + +# remove when copy_properties alias is removed +def test_copy_properties_warn(): + polygons = os.path.join(DATA, 'polygons.shp') + # run once to trigger any other unrelated deprecation warnings + # so the test does not catch them instead + stats_a = zonal_stats(polygons, raster) + with pytest.deprecated_call(): + stats_b = zonal_stats(polygons, raster, copy_properties=True) + assert stats_a == stats_b + + def test_nan_counts(): from affine import Affine transform = Affine(1, 0, 1, 0, -1, 3) @@ -469,3 +534,4 @@ def test_geodataframe_zonal(): expected = zonal_stats(polygons, raster) assert zonal_stats(df, raster) == expected +