簡體   English   中英

Python - 將坐標列表轉換為緯度和經度

[英]Python - Converting list of coordinates to latitude and longitude

我最近開始學習編程和 Python。

現在我一直在嘗試將坐標列表 x,y 轉換為緯度和經度。 我在這篇文章中搜索並找到了 python 中的一種方法: How to convert from UTM to LatLng in python or Javascript

但是,當我嘗試將 function 應用於 dataframe 中的浮點數列表時,出現錯誤:“無法將系列轉換為 <class 'float'>”。 我做錯了什么?

我的代碼:


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

錯誤:


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'>

當您讀取輸入文件時,您將第一列的所有內容分配給一個變量,將第二列的所有內容分配給一個變量:

>>> 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

當您調用 function 時,您會將整個列傳遞給它以獲取 x 和 y,而不僅僅是一行。

試試這個:

for i in range(len(df)):
    lati,longi = utmToLatLng(23, x[i], y[i], False)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM