From 36eaebb4bca3fe24b32a61a01692d036b91eaa4e Mon Sep 17 00:00:00 2001 From: Sean Gillies Date: Sat, 7 Dec 2019 14:02:28 -0700 Subject: [PATCH] Remove FeatureCollection from output GeoJSON types (#1841) * Remove FeatureCollection from output GeoJSON types This case is covered by the --sequence/--collection switch Resolves #1807 * Update change log * add tests * Add tests from #1812 --- CHANGES.txt | 3 +++ rasterio/rio/bounds.py | 3 +-- rasterio/rio/helpers.py | 13 +++---------- tests/test_rio_bounds.py | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_rio_info.py | 2 +- 5 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 tests/test_rio_bounds.py diff --git a/CHANGES.txt b/CHANGES.txt index aaa90982..e186637a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,9 @@ Changes Next ---- +- The conflict between the --bbox/--feature/--collection and + --sequence/--collection option of rio-bounds (#1807) has been fixed by + removing "collection" from the possible JSON type options. - Increase default precision for the merge tool from 7 to 10 (#1837). - Allow rio-clip and rio-convert to overwrite output files (#1836). - Allow src_crs parameter to fully override the source dataset's CRS (#1808). diff --git a/rasterio/rio/bounds.py b/rasterio/rio/bounds.py index c00b5ef7..dd8f4aaf 100644 --- a/rasterio/rio/bounds.py +++ b/rasterio/rio/bounds.py @@ -33,8 +33,7 @@ logger = logging.getLogger(__name__) help="Output in specified coordinates.") @options.sequence_opt @use_rs_opt -@geojson_type_collection_opt(True) -@geojson_type_feature_opt(False) +@geojson_type_feature_opt(True) @geojson_type_bbox_opt(False) @click.pass_context def bounds(ctx, input, precision, indent, compact, projection, dst_crs, diff --git a/rasterio/rio/helpers.py b/rasterio/rio/helpers.py index 51e541e9..b2a97800 100644 --- a/rasterio/rio/helpers.py +++ b/rasterio/rio/helpers.py @@ -38,25 +38,18 @@ def write_features( bbox = (min(xs), min(ys), max(xs), max(ys)) if use_rs: fobj.write(u'\u001e') - if geojson_type == 'feature': - fobj.write(json.dumps(feat, **dump_kwds)) - elif geojson_type == 'bbox': + if geojson_type == 'bbox': fobj.write(json.dumps(bbox, **dump_kwds)) else: - fobj.write( - json.dumps({ - 'type': 'FeatureCollection', - 'bbox': bbox, - 'features': [feat]}, **dump_kwds)) + fobj.write(json.dumps(feat, **dump_kwds)) fobj.write('\n') + # Aggregate all features into a single object expressed as # bbox or collection. else: features = list(collection()) if geojson_type == 'bbox': fobj.write(json.dumps(collection.bbox, **dump_kwds)) - elif geojson_type == 'feature': - fobj.write(json.dumps(features[0], **dump_kwds)) else: fobj.write(json.dumps({ 'bbox': collection.bbox, diff --git a/tests/test_rio_bounds.py b/tests/test_rio_bounds.py new file mode 100644 index 00000000..8c85a8c9 --- /dev/null +++ b/tests/test_rio_bounds.py @@ -0,0 +1,38 @@ +import pytest + +import rasterio +from rasterio.rio.main import main_group + + +def test_bounds_sequence_single(runner, basic_image_file): + """ + --sequence option should produce a feature collection for a single image. + """ + result = runner.invoke(main_group, ["bounds", "--sequence", basic_image_file]) + + assert result.output.count('"FeatureCollection"') == 0 + assert result.output.count('"Feature"') == 1 + + +def tests_bounds_sequence_multiple(runner, basic_image_file): + """ + --sequence option should produce a feature collection for each image passed as argument. + """ + result = runner.invoke( + main_group, ["bounds", "--sequence", basic_image_file, basic_image_file] + ) + + assert result.output.count('"FeatureCollection"') == 0 + assert result.output.count('"Feature"') == 2 + + +def test_bounds_no_sequence_multiple(runner, basic_image_file): + """ + --no-sequence option should produce a single feature collection + """ + result = runner.invoke( + main_group, ["bounds", "--collection", basic_image_file, basic_image_file] + ) + + assert result.output.count('"FeatureCollection"') == 1 + assert result.output.count('"Feature"') == 2 diff --git a/tests/test_rio_info.py b/tests/test_rio_info.py index e4c3f7b8..45797b68 100644 --- a/tests/test_rio_info.py +++ b/tests/test_rio_info.py @@ -266,7 +266,7 @@ def test_bounds_defaults(): 'tests/data/RGB.byte.tif' ]) assert result.exit_code == 0 - assert 'FeatureCollection' in result.output + assert 'Feature' in result.output def test_bounds_err():