Search code examples
pythoncoordinatestransformopenstreetmapgeopandas

How do I convert these coordinates to real numbers?


I have something like this in two columns in one dataframe data= pd.read_csv('xxxxxxxxxxxx.dat',delimiter=';',encoding='latin_1') in python Latitude=60°11.7765 Longitude=024°57.3073

And I would like to get this:

Latitude=60.1177.... Longitude=024.573....


Solution

  • The question you asked seems to want a simple substitution of some char, not a real conversion of coordinates. Thanks to the comments I understood that this ambiguity was due to the fact that you yourself did not have clear how to get the conversion. Among the different type of coordinates we find DDM (degrees decimal minutes) format. When you ask for "a real number" you are actually referring to the DD format (decimal degrees format, ex 13.41° N). So in DDM the coordinate Latitude: 15°41.13 N consists of 3 values and a reference:

    • D: degrees, here 15
    • M: minutes, here 41
    • S: seconds, here 13
    • N or S: North or South (In longitude we have East or West)

    As wikipedia says, the formula for conversion from DDM to DD is

    decimal degrees = degrees + (minutes/60 + seconds/3600)
    

    I tried two online converters (one and two) and it seems that the coordinates you wrote are in the DMM format. The only difference with the DDM format is that here the seconds are already divided by 60, so we can list:

    • D: degrees, here 15
    • M: minutes, here 41
    • S: seconds/60, here 13
    • N or S: North or South (In longitude we have East or West)

    That means that the formula from DMM to DD is

    decimal degrees = degrees + (minutes/60 + seconds/60)
    

    Code

    import pandas as pd
    
    df_csv = pd.read_csv('file.dat',delimiter=';',encoding='UTF_8')
    latitude = df_csv['Latitude'].astype('str')
    for element in latitude:
        degree= float(element[:element.find('°')])
        minutes = float(element[(element.find('°')+1):(element.find('.'))])
        seconds= float(element[element.find('.'):])
        decimalDegrees=degree + minutes/60 + seconds/60
    

    Input

    Lat: 31°11.7765
    

    Output

    31.196275
    

    In your coordinates you don't talk about any cardinal point. The rule is that if it is N or E the result is positive, if it's S or W the result is negative (-1 * output)