[英]Python - Converting list of coordinates to latitude and longitude
I recently started learning programing and Python.我最近开始学习编程和 Python。
Now I've been trying to convert a list of coordinates x,y to latitude and longitude.现在我一直在尝试将坐标列表 x,y 转换为纬度和经度。 I searched and found a method in python in this post: How to convert from UTM to LatLng in python or Javascript我在这篇文章中搜索并找到了 python 中的一种方法: How to convert from UTM to LatLng in python or Javascript
But when I try to apply the function to a list of floats from a dataframe I get an error:" cannot convert the series to <class 'float'> ".但是,当我尝试将 function 应用于 dataframe 中的浮点数列表时,出现错误:“无法将系列转换为 <class 'float'>”。 What could I be doing wrong?我做错了什么?
import math
def utmToLatLng(zone, easting, northing, northernHemisphere=True):
if not northernHemisphere:
northing = 10000000 - northing
a = 6378137
e = 0.081819191
e1sq = 0.006739497
k0 = 0.9996
arc = northing / k0
mu = arc / (a * (1 - math.pow(e, 2) / 4.0 - 3 * math.pow(e, 4) / 64.0 - 5 * math.pow(e, 6) / 256.0))
ei = (1 - math.pow((1 - e * e), (1 / 2.0))) / (1 + math.pow((1 - e * e), (1 / 2.0)))
ca = 3 * ei / 2 - 27 * math.pow(ei, 3) / 32.0
cb = 21 * math.pow(ei, 2) / 16 - 55 * math.pow(ei, 4) / 32
cc = 151 * math.pow(ei, 3) / 96
cd = 1097 * math.pow(ei, 4) / 512
phi1 = mu + ca * math.sin(2 * mu) + cb * math.sin(4 * mu) + cc * math.sin(6 * mu) + cd * math.sin(8 * mu)
n0 = a / math.pow((1 - math.pow((e * math.sin(phi1)), 2)), (1 / 2.0))
r0 = a * (1 - e * e) / math.pow((1 - math.pow((e * math.sin(phi1)), 2)), (3 / 2.0))
fact1 = n0 * math.tan(phi1) / r0
_a1 = 500000 - easting
dd0 = _a1 / (n0 * k0)
fact2 = dd0 * dd0 / 2
t0 = math.pow(math.tan(phi1), 2)
Q0 = e1sq * math.pow(math.cos(phi1), 2)
fact3 = (5 + 3 * t0 + 10 * Q0 - 4 * Q0 * Q0 - 9 * e1sq) * math.pow(dd0, 4) / 24
fact4 = (61 + 90 * t0 + 298 * Q0 + 45 * t0 * t0 - 252 * e1sq - 3 * Q0 * Q0) * math.pow(dd0, 6) / 720
lof1 = _a1 / (n0 * k0)
lof2 = (1 + 2 * t0 + Q0) * math.pow(dd0, 3) / 6.0
lof3 = (5 - 2 * Q0 + 28 * t0 - 3 * math.pow(Q0, 2) + 8 * e1sq + 24 * math.pow(t0, 2)) * math.pow(dd0, 5) / 120
_a2 = (lof1 - lof2 + lof3) / math.cos(phi1)
_a3 = _a2 * 180 / math.pi
latitude = 180 * (phi1 - fact1 * (fact2 + fact3 + fact4)) / math.pi
if not northernHemisphere:
latitude = -latitude
longitude = ((zone > 0) and (6 * zone - 183.0) or 3.0) - _a3
return (latitude, longitude)
import pandas as pd
df = pd.read_csv('Coord_rj.csv')
x = df['x']
y = df['y']
for i in range(len(df)):
lati,longi = utmToLatLng(23,x,y, False)
x y
529025.0 7422210.0
529114.0 7422343.0
545227.0 7435702.0
545582.0 7435741.0
The error:错误:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_7368/1776418913.py in <module>
1 for i in range(len(df)):
----> 2 lati,longi = utmToLatLng(23,x,y, False)
3
~\AppData\Local\Temp/ipykernel_7368/3107957551.py in utmToLatLng(zone, easting, northing, northernHemisphere)
20 cc = 151 * math.pow(ei, 3) / 96
21 cd = 1097 * math.pow(ei, 4) / 512
---> 22 phi1 = mu + ca * math.sin(2 * mu) + cb * math.sin(4 * mu) + cc * math.sin(6 * mu) + cd * math.sin(8 * mu)
23
24 n0 = a / math.pow((1 - math.pow((e * math.sin(phi1)), 2)), (1 / 2.0))
~\anaconda3\lib\site-packages\pandas\core\series.py in wrapper(self)
183 if len(self) == 1:
184 return converter(self.iloc[0])
--> 185 raise TypeError(f"cannot convert the series to {converter}")
186
187 wrapper.__name__ = f"__{converter.__name__}__"
TypeError: cannot convert the series to <class 'float'>
When you're reading in your input file you're assigning all of the first column to a variable, and all of the second column to a variable:当您读取输入文件时,您将第一列的所有内容分配给一个变量,将第二列的所有内容分配给一个变量:
>>> import pandas as pd
>>> df = pd.read_csv('Coord_rj.csv')
>>> df
x y
0 529025.0 7422210.0
1 529114.0 7422343.0
2 545227.0 7435702.0
3 545582.0 7435741.0
>>> df['x']
0 529025.0
1 529114.0
2 545227.0
3 545582.0
Name: x, dtype: float64
>>> df['y']
0 7422210.0
1 7422343.0
2 7435702.0
3 7435741.0
Name: y, dtype: float64
When you call your function you're passing the entire column to it for x and y, rather than just a row.当您调用 function 时,您会将整个列传递给它以获取 x 和 y,而不仅仅是一行。
Try this instead:试试这个:
for i in range(len(df)):
lati,longi = utmToLatLng(23, x[i], y[i], False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.