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
This commit is contained in:
Sean Gillies 2019-12-07 14:02:28 -07:00 committed by GitHub
parent 9ec7187c5e
commit 36eaebb4bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 13 deletions

View File

@ -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).

View File

@ -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,

View File

@ -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,

38
tests/test_rio_bounds.py Normal file
View File

@ -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

View File

@ -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():