Skip to content

Commit a0eef4e

Browse files
authored
Merge pull request #144 from sgoodm/test_coverage
Test coverage
2 parents 2a4e4f3 + 25825a1 commit a0eef4e

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ Vagrantfile
1515
*.ipynb_checkpoints*
1616
.idea
1717
venv
18-
.eggs
18+
.eggs
19+
.cache

src/rasterstats/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ def gen_zonal_stats(
134134
warnings.warn("Use `geojson_out` to preserve feature properties",
135135
DeprecationWarning)
136136

137-
bn = kwargs.get('band_num')
138-
if bn:
137+
band_num = kwargs.get('band_num')
138+
if band_num:
139139
warnings.warn("Use `band` to specify band number", DeprecationWarning)
140140
band = band_num
141141

tests/test_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ def test_cli_feature_stdin():
3434
warnings.simplefilter('ignore')
3535
result = runner.invoke(zonalstats,
3636
['--raster', raster,
37-
'--stats', 'mean',
37+
'--stats', 'all',
3838
'--prefix', 'test_'],
3939
input=open(vector, 'r').read())
4040
assert result.exit_code == 0
4141
outdata = json.loads(result.output)
4242
assert len(outdata['features']) == 1
4343
feature = outdata['features'][0]
4444
assert 'test_mean' in feature['properties']
45+
assert 'test_std' in feature['properties']
4546

4647

4748
def test_cli_features_sequence():

tests/test_io.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ def test_Raster():
268268

269269
r2 = Raster(arr, affine, nodata, band=1).read(bounds)
270270

271+
with pytest.raises(ValueError):
272+
r3 = Raster(arr, affine, nodata, band=1).read()
273+
with pytest.raises(ValueError):
274+
r4 = Raster(arr, affine, nodata, band=1).read(bounds=1, window=1)
275+
271276
# If the abstraction is correct, the arrays are equal
272277
assert np.array_equal(r1.array, r2.array)
273278

tests/test_zonal.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from rasterstats import zonal_stats, raster_stats
1010
from rasterstats.utils import VALID_STATS
1111
from rasterstats.io import read_featurecollection, read_features
12+
from shapely.geometry import Polygon
13+
from affine import Affine
1214

1315
sys.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+
3042
def 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+
377410
def 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+
428493
def 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

Comments
 (0)