>我有一个显示时间和GPS坐标的数据框,看起来像这样:
2019-02-15 15:32:00 55.652480 12.510514
我需要将其提供给如下所示的函数:
import math
def haversine(coord1, coord2):
R = 6372800 # Earth radius in meters
lat1, lon1 = coord1
lat2, lon2 = coord2
phi1, phi2 = math.radians(lat1), math.radians(lat2)
dphi = math.radians(lat2 - lat1)
dlambda = math.radians(lon2 - lon1)
a = math.sin(dphi/2)**2 +
math.cos(phi1)*math.cos(phi2)*math.sin(dlambda/2)**2
return 2*R*math.atan2(math.sqrt(a), math.sqrt(1 - a))
for n in clean["gps"]:
print(n)
for city, coord in crimepoints.items():
distance = haversine(n, coord)
print(city, distance)
if distance <= 500:
print('alarm')
print(distance)
crimelist.append(city)
crimelist.append(distance)
此函数接收如下所示的数据: 坐标 = 51.5073219, -0.1276474 两个浮子 所以我用了这个:
clean["gps"] = clean["latitude"].map(str) + "," + " " + clean["longitude"].map(str)
2019-02-15 15:32:00 55.652480 12.510514 55.652480000000004, 12.510514
将纬度和经度放在一个变量中。 问题是我现在无法将字符串转换为一个变量中的 2 个浮点数。 我尝试了很多东西,如float((,ast.literal_eval((,map(float,test.split(','((,但没有结果。
我在收获函数中收到此错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-190-84732733cea4> in <module>
44
45 for city, coord in crimepoints.items():
---> 46 distance = haversine(n, tok)
47 print(city, distance)
48 if distance <= 500:
<ipython-input-190-84732733cea4> in haversine(coord1, coord2)
3 def haversine(coord1, coord2):
4 R = 6372800 # Earth radius in meters
----> 5 lat1, lon1 = coord1
6 lat2, lon2 = coord2
7
ValueError: too many values to unpack (expected 2)
在下面的代码中,我使用 float 辅助函数从包含在单个变量中的字符串转换 float:
sampleInp = "55.652480000000004, 12.510514"
def convert_float(inp):
splitted_data = inp.split(",")
return float(splitted_data[-2]), float(splitted_data[-1])
lat, long = convert_float(sampleInp)
print("Types : ",type(lat), type(long), "Values: ", lat, long)
输出:
Types : <class 'float'> <class 'float'> Values: 55.652480000000004 12.510514
希望这有帮助!!