Using regex to convert latitude and longitude coordinates of degrees/minutes/direction to decimal in python

Asked

Viewed 248 times

1

I am trying to convert latitude and longitude data into python which are in the following format:

Latitude    Longitude   
27º59' N    86º55'E 

This data was extracted from the following web page and converted into a csv file: http://www.peaklist.org/WWlists/WorldTop50.html

I made the code to convert but got the result as Nan. I tried several different ways and the last code I tried was the following:

data['Longitude'] = data['Longitude'].str.extract('°\'([N|S|E|W])', expand=True)
(parts[0].astype(int) + parts[1].astype(float) / 60) * parts[2].map({'N':1, 'S':-1, 'E': 1, 'W':-1})

The data in the latitude and longitude columns are in string format. Thank you

  • Try this regex r"(\d{1,2})[º](\d{1,2}\.\d{1,2})*[']\s(\w)" works for "37º45.3' E" for example. But if you don’t have the .3 (in the example), you will have to modify the regex a little.

1 answer

0


Using this as a basis: https://www.pgc.umn.edu/apps/convert

And having:

   Latitude Longitude
0  27º59' N   86º55'E

I think you can start by creating a function for DDM to DD conversion, note that since you don’t have seconds then 0 is assumed in that place:

import re

def convert(s):
    degrees, minutes, direction = re.split('[º\' "]+', s)
    dd = float(degrees) + float(minutes)/60
    return dd*-1 if direction in 'SW' else dd
... 
data['Latitude_dd'] = data['Latitude'].apply(convert)
data['Longitude_dd'] = data['Longitude'].apply(convert)

We get:

   Latitude Longitude  Latitude_dd  Longitude_dd
0  27º59' N   86º55'E    27.983333     86.916667

I think it fits because your example being Mount Everest (http://www.peaklist.org/WWlists/WorldTop50.html) the result gave precisely there:

  • thank you for the reply. Unfortunately it didn’t work in my code with the full dataframe, I had the answer: Valueerror: Too Many values to unpack (expected 3). Strange that making only the lines 1 to 10 of the dataset worked... The data in the columns are in the same format, from row 1 to 50 so I did not understand why it only worked in the first 10 rows

  • Will I see Mariana, which line did the error occur? Use this http://www.peaklist.org/WWlists/WorldTop50.html

  • I now turned the conversation to latitude and longitude on separate lines and Longitude converted the 50 lines correctly, but gave error in latitude. The original data seems to have some space in the latitudes so it can be this, I see here (I am beginner in this but the code that Voce sent already helped a lot!)

  • But in my answer I charge this scenario Mariana, copies exactly the function I put pf

  • @Mariana, copy and execute this in its entirety: https://pastebin.com/raw/3BeYwU8u

Browser other questions tagged

You are not signed in. Login or sign up in order to post.