From 1fdc9b45fdcb1794ac5ed8fbd06fbd396857d4d3 Mon Sep 17 00:00:00 2001 From: RSG admin Date: Wed, 10 May 2017 11:04:34 +0100 Subject: [PATCH 1/8] added properties and id to output array, included test --- src/rasterstats/main.py | 6 ++++++ tests/test_zonal.py | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/rasterstats/main.py b/src/rasterstats/main.py index 540ed20..a04a025 100644 --- a/src/rasterstats/main.py +++ b/src/rasterstats/main.py @@ -195,6 +195,12 @@ def gen_zonal_stats( feature_stats = remap_categories(category_map, feature_stats) else: feature_stats = {} + + + if 'properties' in feat: + feature_stats['properties'] = feat['properties'] + if 'id' in feat['properties']: + feature_stats['id'] = feat['properties']['id'] if 'min' in stats: feature_stats['min'] = float(masked.min()) diff --git a/tests/test_zonal.py b/tests/test_zonal.py index 48babc9..fe5ec03 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -29,6 +29,14 @@ def test_main(): assert round(stats[0]['mean'], 2) == 14.66 +def test_polygon_id(): + polygons = os.path.join(DATA, 'polygons.shp') + features = read_features(polygons) + + stats = zonal_stats(polygons, raster) + for x, feature in enumerate(features): + assert stats[x]['id'] == feature['properties']['id'] + # remove after band_num alias is removed def test_band_alias(): polygons = os.path.join(DATA, 'polygons.shp') From 45aa361985d42c78c88fbb8c8a6fe442b8c45e37 Mon Sep 17 00:00:00 2001 From: Oliver Clements Date: Wed, 10 May 2017 11:06:30 +0100 Subject: [PATCH 2/8] added properties and id to output array, included test --- src/rasterstats/main.py | 6 ++++++ tests/test_zonal.py | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/rasterstats/main.py b/src/rasterstats/main.py index 540ed20..a04a025 100644 --- a/src/rasterstats/main.py +++ b/src/rasterstats/main.py @@ -195,6 +195,12 @@ def gen_zonal_stats( feature_stats = remap_categories(category_map, feature_stats) else: feature_stats = {} + + + if 'properties' in feat: + feature_stats['properties'] = feat['properties'] + if 'id' in feat['properties']: + feature_stats['id'] = feat['properties']['id'] if 'min' in stats: feature_stats['min'] = float(masked.min()) diff --git a/tests/test_zonal.py b/tests/test_zonal.py index 48babc9..fe5ec03 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -29,6 +29,14 @@ def test_main(): assert round(stats[0]['mean'], 2) == 14.66 +def test_polygon_id(): + polygons = os.path.join(DATA, 'polygons.shp') + features = read_features(polygons) + + stats = zonal_stats(polygons, raster) + for x, feature in enumerate(features): + assert stats[x]['id'] == feature['properties']['id'] + # remove after band_num alias is removed def test_band_alias(): polygons = os.path.join(DATA, 'polygons.shp') From 80999a4efdbaf7878065a5a513dbc0622d613d7d Mon Sep 17 00:00:00 2001 From: Oliver Clements Date: Wed, 10 May 2017 11:35:15 +0100 Subject: [PATCH 3/8] made the properties a default off and ids default on and added properties test --- src/rasterstats/main.py | 12 ++++++++---- tests/test_zonal.py | 10 +++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/rasterstats/main.py b/src/rasterstats/main.py index a04a025..0c15f30 100644 --- a/src/rasterstats/main.py +++ b/src/rasterstats/main.py @@ -42,7 +42,9 @@ def gen_zonal_stats( zone_func=None, raster_out=False, prefix=None, - geojson_out=False, **kwargs): + geojson_out=False, + preserve_properties=False, + preserve_ids=True, **kwargs): """Zonal statistics of raster values aggregated to vector geometries. Parameters @@ -198,9 +200,11 @@ def gen_zonal_stats( if 'properties' in feat: - feature_stats['properties'] = feat['properties'] - if 'id' in feat['properties']: - feature_stats['id'] = feat['properties']['id'] + if preserve_properties: + feature_stats['properties'] = feat['properties'] + if preserve_ids: + if 'id' in feat['properties']: + feature_stats['id'] = feat['properties']['id'] if 'min' in stats: feature_stats['min'] = float(masked.min()) diff --git a/tests/test_zonal.py b/tests/test_zonal.py index fe5ec03..6cd5f64 100644 --- a/tests/test_zonal.py +++ b/tests/test_zonal.py @@ -33,10 +33,18 @@ def test_polygon_id(): polygons = os.path.join(DATA, 'polygons.shp') features = read_features(polygons) - stats = zonal_stats(polygons, raster) + stats = zonal_stats(polygons, raster, preserve_ids=True) for x, feature in enumerate(features): assert stats[x]['id'] == feature['properties']['id'] +def test_polygon_properties(): + polygons = os.path.join(DATA, 'polygons.shp') + features = read_features(polygons) + stats = zonal_stats(polygons, raster, preserve_properties=True) + for x, feature in enumerate(features): + assert stats[x]['properties'] == feature['properties'] + + # remove after band_num alias is removed def test_band_alias(): polygons = os.path.join(DATA, 'polygons.shp') From f77bb0e096b1ca632c4592ca987d623c38c7a0b5 Mon Sep 17 00:00:00 2001 From: Oliver Clements Date: Wed, 10 May 2017 11:39:02 +0100 Subject: [PATCH 4/8] started adding cli options --- src/rasterstats/cli.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/rasterstats/cli.py b/src/rasterstats/cli.py index d365439..81cb9b1 100644 --- a/src/rasterstats/cli.py +++ b/src/rasterstats/cli.py @@ -24,10 +24,12 @@ @click.option('--nodata', type=int, default=None) @click.option('--prefix', type=str, default='_') @click.option('--stats', type=str, default=None) +@click.option('--keep_ids', default=True) +@click.option('--keep_properties', default=False) @cligj.sequence_opt @cligj.use_rs_opt def zonalstats(features, raster, all_touched, band, categorical, - indent, info, nodata, prefix, stats, sequence, use_rs): + indent, info, nodata, prefix, stats,keep_ids, keep_properties, sequence, use_rs): '''zonalstats generates summary statistics of geospatial raster datasets based on vector features. @@ -61,6 +63,8 @@ def zonalstats(features, raster, all_touched, band, categorical, nodata=nodata, stats=stats, prefix=prefix, + preserve_properties=keep_properties, + preserve_ids=keep_ids, geojson_out=True) if sequence: From 4afb8dda7ac7d0579b2376b3ac967a7b90388534 Mon Sep 17 00:00:00 2001 From: Olly Clements Date: Fri, 12 May 2017 10:05:59 +0100 Subject: [PATCH 5/8] changed id and prop to be false by default, fixed tests --- src/rasterstats/main.py | 2 +- tests/test_cli.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rasterstats/main.py b/src/rasterstats/main.py index 0c15f30..248221a 100644 --- a/src/rasterstats/main.py +++ b/src/rasterstats/main.py @@ -44,7 +44,7 @@ def gen_zonal_stats( prefix=None, geojson_out=False, preserve_properties=False, - preserve_ids=True, **kwargs): + preserve_ids=False, **kwargs): """Zonal statistics of raster values aggregated to vector geometries. Parameters diff --git a/tests/test_cli.py b/tests/test_cli.py index b6834aa..7d4c59c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -18,6 +18,7 @@ def test_cli_feature(): '--stats', 'mean', '--prefix', 'test_']) assert result.exit_code == 0 + print result.output outdata = json.loads(result.output) assert len(outdata['features']) == 1 feature = outdata['features'][0] From 3c74428bac8a32ffb970ed318b3c23595deca0c4 Mon Sep 17 00:00:00 2001 From: Olly Clements Date: Fri, 12 May 2017 10:09:08 +0100 Subject: [PATCH 6/8] forcing build test --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 6260062..93543b4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,3 +28,4 @@ install: script: py.test --cov rasterstats --cov-report term-missing after_success: - coveralls + From 6ce560df6911ea70e108735ececfecbe5243c27d Mon Sep 17 00:00:00 2001 From: Olly Clements Date: Fri, 12 May 2017 10:15:35 +0100 Subject: [PATCH 7/8] forcing build test --- tests/test_cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 7d4c59c..b6834aa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -18,7 +18,6 @@ def test_cli_feature(): '--stats', 'mean', '--prefix', 'test_']) assert result.exit_code == 0 - print result.output outdata = json.loads(result.output) assert len(outdata['features']) == 1 feature = outdata['features'][0] From 6c29d5f9d8be097de6ba14bcba650325dd19e975 Mon Sep 17 00:00:00 2001 From: Olly Clements Date: Thu, 12 Oct 2017 13:42:35 +0100 Subject: [PATCH 8/8] added entries in docstring and cleaned up branching logic --- src/rasterstats/main.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/rasterstats/main.py b/src/rasterstats/main.py index 248221a..e656fb8 100644 --- a/src/rasterstats/main.py +++ b/src/rasterstats/main.py @@ -110,6 +110,12 @@ def gen_zonal_stats( Original feature geometry and properties will be retained with zonal stats appended as additional properties. Use with `prefix` to ensure unique and meaningful property names. + + preserve_properties: boolean (default: False) + preserve the properties of each feature in the returned stats data + + preserve_ids: boolean (default: False) + Preserve the IDs of each feature in the returned stats data Returns ------- @@ -199,9 +205,8 @@ def gen_zonal_stats( feature_stats = {} - if 'properties' in feat: - if preserve_properties: - feature_stats['properties'] = feat['properties'] + if preserve_properties and 'properties' in feat: + feature_stats['properties'] = feat['properties'] if preserve_ids: if 'id' in feat['properties']: feature_stats['id'] = feat['properties']['id']