louis-e / arnis

Arnis - Generate cities from real life in Minecraft
GNU General Public License v3.0
431 stars 23 forks source link

[FEATURE] check if bounding box is valid before continuing #44

Closed amir16yp closed 3 months ago

amir16yp commented 3 months ago

As of now, the bounding box isnt being "validated" before requesting the data from the api. we should make sure the bounding box makes sense before proceeding, and error out if it isnt. this could be a rather simple check. here's an example i wrote

def validate_bounding_box(bbox):
    """
    Validates a bounding box represented as a string in the format "min_lng,min_lat,max_lng,max_lat".

    Parameters:
        bbox (str): The bounding box string.

    Returns:
        bool: True if the bounding box is valid, False otherwise.
    """
    try:
        # Split the input string into components
        parts = bbox.split(',')
        if len(parts) != 4:
            return False

        # Convert the components to float
        min_lng, min_lat, max_lng, max_lat = map(float, parts)

        # Validate the ranges
        if not (-180 <= min_lng <= 180 and -180 <= max_lng <= 180):
            return False
        if not (-90 <= min_lat <= 90 and -90 <= max_lat <= 90):
            return False
        if min_lng >= max_lng or min_lat >= max_lat:
            return False

        return True

    except ValueError:
        # In case of conversion error, input was not a valid float
        return False

# Example usage:
bbox = "12.34,-56.78,23.45,-45.67"
print(validate_bounding_box(bbox))  # Should print True or False based on validity
louis-e commented 3 months ago

Thanks, I created a PR for your implementation and merged it!