From 76d707190b55f4d15cfa5b62ace55b3c5022b8ff Mon Sep 17 00:00:00 2001 From: sgoodm Date: Tue, 21 Mar 2017 15:25:33 -0400 Subject: [PATCH 1/7] Add additional test coverage (arg alias, nans, missing feature properties) --- .gitignore | 3 ++- tests/test_zonal.py | 60 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) 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/tests/test_zonal.py b/tests/test_zonal.py index c91b207..802ac20 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -27,6 +27,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 +384,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') @@ -424,6 +455,34 @@ def test_geojson_out(): assert 'id' in feature['properties'] # from orig assert 'count' in feature['properties'] # from zonal stats + +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) + for key in ['count', 'min', 'max', 'mean']: + assert key in stats[0] + + assert stats[0]['mean'] == 34 + + +# remove when copy_properties alias is removed +def test_geojson_out_alias(): + 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 + + # Optional tests def test_geodataframe_zonal(): polygons = os.path.join(DATA, 'polygons.shp') @@ -438,3 +497,4 @@ def test_geodataframe_zonal(): expected = zonal_stats(polygons, raster) assert zonal_stats(df, raster) == expected + From 9896562ff7c6135db38e633585199ba9ea8da152 Mon Sep 17 00:00:00 2001 From: sgoodm Date: Tue, 21 Mar 2017 15:33:03 -0400 Subject: [PATCH 2/7] Fix --- tests/test_zonal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_zonal.py b/tests/test_zonal.py index 802ac20..9d536b0 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -480,7 +480,7 @@ def test_geojson_out_alias(): stats_a = zonal_stats(polygons, raster) with pytest.deprecated_call(): stats_b = zonal_stats(polygons, raster, copy_properties=True) - assert stats_a = stats_b + assert stats_a == stats_b # Optional tests From 5ffce1a511dae7b5889b06f5a22ea6bf9cad18bf Mon Sep 17 00:00:00 2001 From: sgoodm Date: Tue, 21 Mar 2017 15:39:10 -0400 Subject: [PATCH 3/7] Fix band_num var and add import for tests --- src/rasterstats/main.py | 4 ++-- tests/test_zonal.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rasterstats/main.py b/src/rasterstats/main.py index 8b2b450..747cd53 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_zonal.py b/tests/test_zonal.py index 9d536b0..042f409 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -9,6 +9,7 @@ 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 sys.path.append(os.path.dirname(os.path.abspath(__file__))) From b9020cfaf6ab2a7754b8bf1964ef897b86c49757 Mon Sep 17 00:00:00 2001 From: sgoodm Date: Tue, 21 Mar 2017 15:53:14 -0400 Subject: [PATCH 4/7] Add import for tests and expand cli test --- tests/test_cli.py | 3 ++- tests/test_zonal.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) 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_zonal.py b/tests/test_zonal.py index 042f409..11976cd 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -10,6 +10,7 @@ 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__))) From 0fab4d6e71c1c575f3e493adf817d40ecb8ccc7f Mon Sep 17 00:00:00 2001 From: sgoodm Date: Tue, 21 Mar 2017 16:00:40 -0400 Subject: [PATCH 5/7] Fix test case --- tests/test_zonal.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_zonal.py b/tests/test_zonal.py index 11976cd..0be6eea 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -468,10 +468,11 @@ def test_geojson_out_with_no_properties(): 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] + assert key in stats[0]['properties'] - assert stats[0]['mean'] == 34 + assert stats[0]['properties']['mean'] == 34 # remove when copy_properties alias is removed From 4eeffdbb1d513595eae1f6ab2166617510b2e2e9 Mon Sep 17 00:00:00 2001 From: sgoodm Date: Tue, 21 Mar 2017 16:09:36 -0400 Subject: [PATCH 6/7] Add raster read test cases --- tests/test_io.py | 5 +++++ 1 file changed, 5 insertions(+) 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) From d32dcf012a0c1efc7efc668c443e98dbb183bf10 Mon Sep 17 00:00:00 2001 From: sgoodm Date: Tue, 21 Mar 2017 16:21:43 -0400 Subject: [PATCH 7/7] Add test note and rename test case --- tests/test_zonal.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_zonal.py b/tests/test_zonal.py index 0be6eea..63783d3 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -458,6 +458,9 @@ 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([ @@ -476,7 +479,7 @@ def test_geojson_out_with_no_properties(): # remove when copy_properties alias is removed -def test_geojson_out_alias(): +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