Validate Latitude and Longitude

Description: Validates if latitude and longitude are within normal values.
Tested Platform: Python 3.11
Language: Python
import re

def is_valid_latitude(lat):
    """Validates a latitude."""
    return re.match(r'^[-]?((([0-8]?[0-9])\.(\d+))|(90(\.0+)?))$', str(lat)) is not None

def is_valid_longitude(long):
    """Validates a longitude."""
    return re.match(r'^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$', str(long)) is not None

Posted: March 21, 2023

Return to the snippets listing