Skip to content

Commit d15a6b6

Browse files
committed
Use a stream instead of a list for validation in estimate-area
1 parent 042e0fb commit d15a6b6

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

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,21 +751,18 @@ 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:
750-
features = builtins.list(features)
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
760+
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
)
755765

756-
# expect users to bypass source validation when users rerun command and their features passed validation previously
757-
if not no_validation:
758-
for feature in features:
759-
utils.validate_geojson(feature)
760-
761-
features = builtins.list(filter_features(features))
762-
763766
area = utils.calculate_tiles_area(features, precision)
764767
area = str(int(round(area)))
765768

tests/test_cli_estimate_area.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ def test_cli_estimate_area_large_valid_features_files_and_valid_feature_input():
113113
assert validated_result.output == output
114114

115115

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+
116127
def test_cli_estimate_area_valid_features_files_and_1cm_precision():
117128
output = '{"km2": "0", "precision": "1cm", "pricing_docs": "For more information, visit https://www.mapbox.com/pricing/#tilesets"}\n'
118129
runner = CliRunner()

0 commit comments

Comments
 (0)