Exemple #1
0
def estimate_area(features, precision, no_validation=False, force_1cm=False):
    """Estimate area of features with a precision level.

    tilesets estimate-area <features> <precision>

    features must be a list of paths to local files containing GeoJSON feature collections or feature sequences from argument or stdin, or a list of string-encoded coordinate pairs of the form "[lng, lat]", or "lng, lat", or "lng lat".
    """
    area = 0
    if precision == "1cm" and not force_1cm:
        raise errors.TilesetsError(
            "The --force-1cm flag must be present to enable 1cm precision area calculation and may take longer for large feature inputs or data with global extents. 1cm precision for tileset processing is only available upon request after contacting Mapbox support."
        )
    if precision != "1cm" and force_1cm:
        raise errors.TilesetsError(
            "The --force-1cm flag is enabled but the precision is not 1cm."
        )

    # builtins.list because there is a list command in the cli & will thrown an error
    try:
        features = builtins.list(filter_features(features))
    except (ValueError, json.decoder.JSONDecodeError):
        raise errors.TilesetsError(
            "Error with feature parsing. Ensure that feature inputs are valid and formatted correctly. Try 'tilesets estimate-area --help' for help."
        )
    except Exception:
        raise errors.TilesetsError("Error with feature filtering.")

    # expect users to bypass source validation when users rerun command and their features passed validation previously
    if not no_validation:
        for feature in features:
            utils.validate_geojson(feature)

    area = utils.calculate_tiles_area(features, precision)
    area = str(round(area))

    click.echo(
        json.dumps(
            {
                "km2": area,
                "precision": precision,
                "pricing_docs": "For more information, visit https://www.mapbox.com/pricing/#tilesets",
            }
        )
    )
Exemple #2
0
def test_calculate_tiles_area_with_10m_precision():
    filename = "tests/fixtures/precision-testing.ldgeojson"
    f = open(filename)
    features = json.load(f)
    area = round(calculate_tiles_area(features, "10m"))
    assert area == 1485128