微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

查找 latlong

如何解决查找 latlong

我有点卡住了。我有一个 CSV 文件,其中包括

网站名称 纬度 经度。

此 CSV 有 100,000 个位置。我需要为每个位置生成一个逗号分隔的列表,显示 5KM 内的其他位置

我已经尝试了附加的,它转置了表格并为我提供了 100,000 列和 100,000 行,并将距离填充为结果。但我不确定如何创建一个新的 Pandas 列,其中包含 5 公里内所有站点的列表。

你能帮忙吗?

from geopy.distance import geodesic
def distance(row,csr):
    lat = row['latitude']
    long = row['longitude']
    lat_long =  (lat,long)
    try:
        return round(geodesic(lat_long,lat_long_compare).kilometers,2)
    except:
        return 9999

for key,value in d.items():
    lat_compare = value['latitude']
    long_compare = value['longitude']
    lat_long_compare =  (lat_compare,long_compare)
    
    csr = key
    
    df[key] = df.apply([distance,csr],axis=1)
    

一些示例数据可以是:

destinations = { 'bigben' : {'latitude': 51.510357,'longitude': -0.116773},'heathrow' : {'latitude': 51.470020,'longitude': -0.454295},'alton_towers' : {'latitude': 52.987662716,'longitude': -1.888829778}
               }

bigben 距离伦敦眼 0.8 公里 希思罗机场距伦敦眼 23.55KM alton_towers 距离伦敦眼 204.63KM

因此,在这种情况下,该字段应仅显示大本钟。

所以我们得到:

网站 | 5KM以内的站点 28、大本

解决方法

这是 NearestNeighbors 的一种方式。

from sklearn.neighbors import NearestNeighbors

# data from your input
df = pd.DataFrame.from_dict(destinations,orient='index').rename_axis('Site Name').reset_index()

radius = 50 #change to whatever,in km

# crate the algo with the raidus and the metric for geospatial distance
neigh = NearestNeighbors(radius=radius/6371,metric='haversine')

# fit the data in radians
neigh.fit(df[['latitude','longitude']].to_numpy()*np.pi/180)

# extract result and transform to get the expected output
df[f'Site_within_{radius}km'] = (
    pd.Series(neigh.radius_neighbors()[1]) # get a list of index for each row
      .explode() 
      .map(df['Site Name']) # get the site name from row index
      .groupby(level=0) # transform back to row-row relation
      .agg(list) # can use ','.join instead of list 
)

print(df)
     Site Name   latitude  longitude Site_within_50km
0        bigben  51.510357  -0.116773       [heathrow]
1      heathrow  51.470020  -0.454295         [bigben]
2  alton_towers  52.987663  -1.888830            [nan]
,

另一种方式

from sklearn.neighbors import DistanceMetric
from math import radians
import pandas as pd
import numpy as np
#To Radians

df['latitude'] = np.radians(df['latitude'])
df['longitude'] = np.radians(df['longitude'])
#Pair the cities
df[['latitude','longitude']].to_numpy()
#Assume a sperical radius of 6373

dist = DistanceMetric.get_metric('haversine')#DistanceMetric class df=pd.DataFrame(dist.pairwise(df[['latitude','longitude']].to_numpy())*6373,columns=df.index.unique(),index=df.index.unique())

 s=df.gt(0)&df.le(50)

df['Site_within_50km']=s.agg(lambda x: x.index[x].values,axis=1)#Filter

     bigben    heathrow  alton_towers Site_within_50km
bigben          0.000000   23.802459    203.857533       [heathrow]
heathrow       23.802459    0.000000    195.048961         [bigben]
alton_towers  203.857533  195.048961      0.000000               []

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。