我想从给定半径内的表格中提取所有坐标。 我需要如何设置for循环
我使用哈弗斯公式,输入中心点的lat和lon值,如果在给定的半径内,输入要测试点的lat和lon值
所以我想我需要一个for循环,在这里我为lat和lon列的每一行运行haversine公式,如果cooridnate在半径范围内,我将它们保存在一个列表中
#Get coordinates
#Center coordinates = nearest road location
lat1 = float(lowParkingUtilization.iloc[roadIndex].toLat)
lon1 = float(lowParkingUtilization.iloc[roadIndex].toLon)
#Test coordinates = scooter coordinates
insideRadius = []
radius = 2.50 # in kilometer
for i in eScooterVOI['lat']:
lat2 = float(eScooterVOI['lat'][i])
lon2 = float(eScooterVOI['lon'][i])
a = haversine(lon1, lat1, lon2, lat2)
if a <= radius:
insideRadius += str(lon2)+","+str(lat2)
else:
使用给定的代码,我得到以下错误消息:
File "<ipython-input-574-02dadebee55c>", line 18
^
SyntaxError: unexpected EOF while parsing
出现此错误是因为
else
块。 当Python读取它时,它希望编写一些代码。Python没有发现任何错误,因此发生了错误您的代码可能已经运行得很好了,只需删除
else
块,就可以使用if
块,而不必绑定使用else
块无论如何,如果您确实想使用
else
块,请尝试以下操作:但我不认为这是推荐的
SyntaxError: unexpected EOF while parsing
错误消息意味着一些代码块没有完成,代码已经结束else
块至少需要一行应该在其中的代码例如:
“我需要如何设置for循环?”这个问题的正确答案是:你不需要
pandas
数据帧不用于在其行上循环。您需要做的是在dataframe中创建两个新列,一个用于计算距离,另一个用于以所需格式存储名称:然后,要获得仅包含距离小于半径的坐标名称的列表,请使用:
顺便说一句:
haversine
函数的构建方式是它接收一个pandas
序列而不是一个值,通过这种方式,它的实现速度可能比使用df.apply
要快得多,但这需要更改一些不在这里讨论的代码相关问题 更多 >
编程相关推荐