How to convert latitude longitude to decimal in python?

2024/10/3 2:16:09

Assuming I have the following:

latitude = "20-55-70.010N"
longitude = "32-11-50.000W"

What is the easiest way to convert to decimal form? Is there some library?

Would converting from this seconds form be simpler?

 "149520.220N""431182.897W"
Answer

To handle "N", "S", "W" and "E", one would tweak @wwii's solution:

def convert(tude):multiplier = 1 if tude[-1] in ['N', 'E'] else -1return multiplier * sum(float(x) / 60 ** n for n, x in enumerate(tude[:-1].split('-')))

then:

print('20-55-70.010N: ' + convert('20-55-70.010N'))
print('32-11-50.000W: ' + convert('32-11-50.000W'))

results in:

20-55-70.010N: 20.9361138889
32-11-50.000W: -32.1972222222
https://en.xdnf.cn/q/70777.html

Related Q&A

No module named main, wkhtmltopdf issue

Im new in python, but all search results i found was useless for me.C:\Users\Aero>pip install wkhtmltopdf Collecting wkhtmltopdfUsing cached wkhtmltopdf-0.2.tar.gz Installing collected packages: wkh…

Is there a Python shortcut for variable checking and assignment?

Im finding myself typing the following a lot (developing for Django, if thats relevant):if testVariable then:myVariable = testVariable else:# something elseAlternatively, and more commonly (i.e. buildi…

python scipy Delaunay plotting point cloud

I have a pointlist=[p1,p2,p3...] where p1 = [x1,y1],p2=[x2,y2] ...I want to use scipy.spatial.Delaunay to do trianglation on these point clouds and then plot itHow can i do this ?The documentation for…

Pythonic way to verify parameter is a sequence but not string

I have a function that gets a list of DB tables as parameter, and returns a command string to be executed on these tables, e.g.:pg_dump( file=/tmp/dump.sql,tables=(stack, overflow),port=5434name=europe…

How to get a random (bootstrap) sample from pandas multiindex

Im trying to create a bootstrapped sample from a multiindex dataframe in Pandas. Below is some code to generate the kind of data I need.from itertools import product import pandas as pd import numpy a…

Python Regex - replace a string not located between two specific words

Given a string, I need to replace a substring with another in an area not located between two given words.For example:substring: "ate" replace to "drank", 1st word - "wolf"…

Vectorized Lookups of Pandas Series to a Dictionary

Problem Statement:A pandas dataframe column series, same_group needs to be created from booleans according to the values of two existing columns, row and col. The row needs to show True if both cells …

Why cant I get my static dir to work with django 1.3?

This problem is very simple, but I just cant figure it outadded to my urlpatternsurl(r^static/(?P<path>.*)$, django.views.static.serve, {document_root: /home/user/www/site/static})where my main.…

Desktop Launcher for Python Script Starts Program in Wrong Path

I can not launch a python script from a .desktop launcher created on Linux Mint 17.1 Cinnamon.The problem is that the script will be launched in the wrong path - namely the home folder instead of the d…

How does numpy broadcasting perform faster?

In the following question, https://stackoverflow.com/a/40056135/5714445Numpys broadcasting provides a solution thats almost 6x faster than using np.setdiff1d() paired with np.view(). How does it manage…