Search code examples
pythoncoordinates

Converting UN locations `coordinates` to lat and lng


I have coordinates from the UNLOCATION dataset, that look like this: 4230N 00131E.

According to their site, this is how it should be processed: https://service.unece.org/trade/locode/Service/LocodeColumn.htm#Coordinates

This column contains the geographical coordinates (latitude/longitude) of the location, if there is any.

In order to avoid unnecessary use of non-standard characters and space, the following standard presentation is used:

0000lat 00000long

(lat - Latitude: N or S ; long – Longitude: W or E, only one digit, capital letter)

Where the last two rightmost digits refer to minutes and the first two or three digits refer to the degrees for latitude and longitude respectively. In addition, you must specify N or S for latitude and W or E for longitude, as appropriate.

Is there a formula for converting this structure into a valid lat & long or a package that does that in Python? As it seems that converting 4230N 00131E to 4230,00131 is not the correct solution.


Solution

  • Eventually I wrote this code:

    def convert_unlocation_to_lat_lng(unlocation_coordinates: str) -> Tuple[Optional[float], Optional[float]]:
        # Split the input string into latitude and longitude parts
        coordinates = unlocation_coordinates.split(" ")
        if not coordinates[0]:
            return None, None
        lat_str = coordinates[0]
        lng_str = coordinates[1]
    
        # Extract latitude and longitude values
        lat_numbers = lat_str[:-1]
        lat_deg = int(lat_numbers[:-2])
        lat_min = int(lat_numbers[-2:])
        lng_numbers = lng_str[:-1]
        lng_deg = int(lng_numbers[:3])
        lng_min = int(lng_numbers[3:])
    
        # Determine direction of latitude and longitude
        lat_dir = 'N' if lat_str.endswith('N') else 'S'
        lng_dir = 'E' if lng_str.endswith('E') else 'W'
    
        # Calculate latitude and longitude in decimal format
        latitude = lat_deg + lat_min / 60
        longitude = lng_deg + lng_min / 60
    
        # Adjust latitude and longitude based on direction
        if lat_dir == 'S':
            latitude *= -1
        if lng_dir == 'W':
            longitude *= -1
    
        return latitude, longitude