-2

For my dataset im trying to get all the Latitude and longitude Coordinates so I am able to plot these in a map(folium map). Although the latitude column and longitude column in my dataset exists out of DD's and DMS's mixed up together.

My dataframe looks like this:

Latitude Longitude
0 36.922223 -81.878056
1 30.757778 -88.355555
2 047257N 0109280W
3 182724N 0066554W

I need DD (decimal degrees) instead of DMS (degrees minutes seconds). The first two lines are correct in DD, only the last two are not.

Anyone who knows how to convert these and apply it to the whole column?

The dataset im using can be found on kaggle: https://www.kaggle.com/datasets/khsamaha/aviation-accident-database-synopses

There are some formula's that are able to convert the decimal degrees to degrees minutes seconds but i cant apply them in to the columns.

Found formula link: How to convert degree minute second to degree decimal

enter image description here

Jordy
  • 1,802
  • 2
  • 6
  • 25
Maxim
  • 13
  • 3
  • Are you asking someone to write the code for you? That is definitely not what this site is about. – David Hoelzer Apr 06 '23 at 10:55
  • You say 'I need DD [...] instead of DMS' but the title is 'Convert DD [...] to DMS', which way round do you want to go? – Pete Kirkham Apr 06 '23 at 10:59
  • @PeteKirkham Excuse, you are right. I mean convert DMS (degrees minutes seconds) to DD (decimal degrees). Changed the title too. Thanks – Maxim Apr 06 '23 at 11:46
  • @David Hoelzer Unfortunately, I am not an expert like you. Just started writing codes for over two months now, never had experience. So yes, I ask someone to help me out find a code to solve my problem as this is meant for study and educational purposes. If you dont want to help me then leave me and my problem be. Thank you – Maxim Apr 06 '23 at 11:50
  • My point to you is that this is not a site where people write code for you. This is a site where, after trying to write code and having trouble, you come and ask people to help you diagnose what is wrong. – David Hoelzer Apr 06 '23 at 12:03

1 Answers1

0

I assume your valid DMS is ddmmss.sN or ddmmss.sW. So I think it can be changed like this to make it more sense for dms2dd function like this:

047257N  => 04°72'57"N   => ['04','72','57.','N']
0109280W => 01°09'28.0"W => ['01','09','28.0','W']
182724N  => 18°27'24"N   => ['18','27','24.','N']
0066554W => 00°66'55.4"W => ['00','66','55.4','W']

so you should convert that into that pattern with customSplit function as stated below.

import pandas as pd

def dms2dd(degrees, minutes, seconds, direction):
    dd = float(degrees) + float(minutes)/60 + float(seconds)/(60*60)
    if direction == 'E' or direction == 'S':
        dd *= -1
    return dd

def customSplit(s, l):
    sec = s[4:].split(l)[0]
    return [s[:2], s[2:4], sec[:2] + '.' + sec[2:], s[-1]]

df = pd.DataFrame({
    "Latitude": ['047257N', '182724N'],
    "Longitude": ['0109280W', '0066554W']
})

for i in range(len(df)):
    for j in df.columns:
        data = df.at[i, j]
        if 'N' in data or 'W' in data:
            dms = customSplit(data, data[-1])
            df.at[i, j] = dms2dd(dms[0], dms[1], dms[2], dms[3])
print(df)

input

  Latitude Longitude
0  047257N  0109280W
1  182724N  0066554W

output

    Latitude Longitude
0   5.215833  1.157778
1  18.456667  1.115389
Jordy
  • 1,802
  • 2
  • 6
  • 25