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.

I'm trying to convert it easily to lat,lng format, preferably through python.

I found the latlon3 package and it requires a string format for this one, So once I'm splitting 4230N 00131E to 4230N and 00131E, I tried to present it as d%%m%%H but it doesn't work.

What am I missing, or how can I turn this string into lat and long easily?


1 Answer 1


Eventually I wrote this function:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.