试图找到一定范围内的所有坐标点



我想在这里实现的是我有一个源csv文件,包含坐标和附加的目标csv文件有更多的坐标,我想从中找到所有的坐标在目标csv文件源csv文件中的每个坐标都在一定范围内.

坐标格式为xx.xxxxxxyy.yyyyyy.

"lat1"one_answers";long1"是源CSV中坐标列的名称和"lat2"one_answers";long2"是目标csv中的坐标列。

import pandas as pd
import numpy as np
import time 
from playsound import playsound
fast_df = pd.read_csv('target.csv') # 2
el_df = pd.read_csv('source.csv') # 1
"""
Commandos:

coords_file.columns - get columns
coords_file.drop_duplicates() - removes identical rows
coords_flie.iloc[] - fetch row with index
coords_file[['OBJEKT_ID', 'EXTERNID', 'DETALJTYP']]

"""

def findDistance(row, source_lat, source_long):
# print(row, source_lat, source_long)
row_lat = row['lat2']
row_long = row['long2']
lat_diff = np.abs(source_lat - row_lat)/0.00001 # divide by 0.00001 to convert to meter
long_diff = np.abs(source_long - row_long)/0.00001
row['Distance'] = np.sqrt(lat_diff**2+long_diff**2)
return row
def findDistance_(source_coordinates, target_coordinates):
lat_diff = np.abs(source_coordinates[0] - target_coordinates[0])/0.00001 # divide by 0.00001 to convert to meter
long_diff = np.abs(source_coordinates[1] - target_coordinates[1])/0.00001
Distance = np.sqrt(lat_diff**2+long_diff**2)
easyDistanceReader(Distance)
return Distance
def easyDistanceReader(Distance):
if Distance > 1000:
Distance = Distance/1000
print("Distance:", Distance, "km")
else:
print("Distance:", Distance, "m")

def runProgram(target_df, source_df, distans_threshold):

"""
Loop over coord in source.csv 
--> Find all the coordinates within the interval in target.csv
"""

"Using this in order to skip coordinates in source.csv which are outside the target.csv     area"
latInterval = min(target_df['lat2']), max(target_df['lat2'])
longInterval = min(target_df['long2']), max(target_df['long2'])

"Find all relevant coordinates based on the source coordinates"
source_df = source_df.loc[(source_df['lat1'].between(min(latInterval), max(latInterval))) &     (source_df['long1'].between(min(longInterval), max(longInterval)))]
dataframes = []
start = time.time()
for index in range(len(source_df)):
row = source_df.iloc[index]
source_coordinates = row[['lat1','long1']]

indices = []
target_df = target_df.apply(findDistance, args=(row['lat1'],row['long1']), axis=1)

relevantTargets = target_df.loc[target_df['Distance'] < distans_threshold]
if len(relevantTargets) > 0:
indices.append(relevantTargets.index[0])
if len(indices) > 0:
new_df = target_df.loc[indices]
dataframes.append(new_df)

final_df = pd.concat(dataframes)

final_df = final_df.loc[:, final_df.columns != 'Distance'].drop_duplicates()
print(final_df)

end = time.time()
print("Elapsed time per iteration:", end-start)

final_df.to_csv('final.csv')
playsound('audio.mp3')
runProgram(fast_df,el_df, 300) # This number indicates the distance in meters from source coordinates I want to find target coordinates.

我目前得到的结果是这样的。这是我在5000米处运行代码时的结果。你可以清楚地看到,很多坐标点都被遗漏了,我不知道为什么。黑点为source点,棕色目标点和粉色是结果点。

任何想法都将非常感激!

indices.append(relevantTargets.index[0])行似乎只附加了relevantTarget的第一个索引,当您想要所有relevantTarget的索引时。试着用indices += [*relevantTargets.index]代替它。然而,我不知道为什么你不能直接在这里做dataframes.append(relevantTargets)

如果我对您的问题的理解正确的话,可以通过使用GeoPandas来简化这种方法。Sjoin_nearest[链接这里]。我已经用过地质仪器了。Sjoin模块在过去连接点到相交的多边形。我相信sjoin_nearest是在最近的版本中添加的。

相关内容

  • 没有找到相关文章

最新更新