Skip to content

Commit 23b74ac

Browse files
authored
Merge pull request #128 from mapbox/fix-estimate-area-stream
Validate before filtering so that estimate-area works with multipolygons
2 parents 87500ca + c961478 commit 23b74ac

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
# 1.6.0 (2021-02-16)
4+
- Fix problem that prevented estimate-area from working for MultiPolygons
5+
- Improve documentation for delete-source and upload-source
6+
37
# 1.5.1 (2020-10-19)
48
- Update README for estimate-area
59

mapbox_tilesets/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""mapbox_tilesets package"""
22

3-
__version__ = "1.5.1"
3+
__version__ = "1.6.0"

mapbox_tilesets/scripts/cli.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,12 @@ def list_sources(username, token=None):
707707
raise errors.TilesetsError(r.text)
708708

709709

710+
def validate_stream(features):
711+
for feature in features:
712+
utils.validate_geojson(feature)
713+
yield feature
714+
715+
710716
@cli.command("estimate-area")
711717
@cligj.features_in_arg
712718
@click.option(
@@ -745,23 +751,20 @@ def estimate_area(features, precision, no_validation=False, force_1cm=False):
745751
"The --force-1cm flag is enabled but the precision is not 1cm."
746752
)
747753

748-
# builtins.list because there is a list command in the cli & will thrown an error
749754
try:
755+
# expect users to bypass source validation when users rerun command and their features passed validation previously
756+
if not no_validation:
757+
features = validate_stream(features)
758+
# builtins.list because there is a list command in the cli & will thrown an error
759+
# It is a list at all because calculate_tiles_area does not work with a stream
750760
features = builtins.list(filter_features(features))
751761
except (ValueError, json.decoder.JSONDecodeError):
752762
raise errors.TilesetsError(
753763
"Error with feature parsing. Ensure that feature inputs are valid and formatted correctly. Try 'tilesets estimate-area --help' for help."
754764
)
755-
except Exception:
756-
raise errors.TilesetsError("Error with feature filtering.")
757-
758-
# expect users to bypass source validation when users rerun command and their features passed validation previously
759-
if not no_validation:
760-
for feature in features:
761-
utils.validate_geojson(feature)
762765

763766
area = utils.calculate_tiles_area(features, precision)
764-
area = str(round(area))
767+
area = str(int(round(area)))
765768

766769
click.echo(
767770
json.dumps(

tests/fixtures/twostates.ldgeojson

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

tests/test_cli_estimate_area.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ def test_cli_estimate_area_valid_features_files_and_valid_feature_input():
102102
assert validated_result.output == output
103103

104104

105+
def test_cli_estimate_area_large_valid_features_files_and_valid_feature_input():
106+
output = '{"km2": "1390828", "precision": "10m", "pricing_docs": "For more information, visit https://www.mapbox.com/pricing/#tilesets"}\n'
107+
runner = CliRunner()
108+
validated_result = runner.invoke(
109+
estimate_area,
110+
["tests/fixtures/twostates.ldgeojson", "-p", "10m"],
111+
)
112+
assert validated_result.exit_code == 0
113+
assert validated_result.output == output
114+
115+
116+
def test_cli_estimate_area_large_valid_features_files_and_valid_feature_input_no_validation():
117+
output = '{"km2": "1390828", "precision": "10m", "pricing_docs": "For more information, visit https://www.mapbox.com/pricing/#tilesets"}\n'
118+
runner = CliRunner()
119+
validated_result = runner.invoke(
120+
estimate_area,
121+
["tests/fixtures/twostates.ldgeojson", "-p", "10m", "--no-validation"],
122+
)
123+
assert validated_result.exit_code == 0
124+
assert validated_result.output == output
125+
126+
105127
def test_cli_estimate_area_valid_features_files_and_1cm_precision():
106128
output = '{"km2": "0", "precision": "1cm", "pricing_docs": "For more information, visit https://www.mapbox.com/pricing/#tilesets"}\n'
107129
runner = CliRunner()

0 commit comments

Comments
 (0)