Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Vagrantfile
*.ipynb_checkpoints*
.idea
venv
.eggs
.eggs
.cache
4 changes: 2 additions & 2 deletions src/rasterstats/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ 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
outdata = json.loads(result.output)
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():
Expand Down
5 changes: 5 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
66 changes: 66 additions & 0 deletions tests/test_zonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)))

Expand All @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -469,3 +534,4 @@ def test_geodataframe_zonal():

expected = zonal_stats(polygons, raster)
assert zonal_stats(df, raster) == expected