我有两个数据框,每个数据框都有一组坐标. Dataframe 1是生物量位置的列表,坐标在"lat"和"lng"列中. Dataframe 2是链接到销售价格的邮政编码坐标列表,坐标在"pc_lat"和"pc_lng"列中.
I've got two dataframes, each with a set of coordinates. Dataframe 1 is a list of biomass sites, with coordinates in columns 'lat' and 'lng'. Dataframe 2 is a list of postcode coordinates, linked to sale price, with coordinates in columns 'pc_lat' and 'pc_lng'.
我使用了这个stackoverflow问题,以计算出与每个属性最接近的生物量站点.这是我正在使用的代码:
I've used this stackoverflow question to work out the closest biomass site to each property. This is the code I am using:
def dist(lat1, long1, lat2, long2): return np.abs((lat1-lat2)+(long1-long2)) def find_site(lat, long): distances = biomass.apply( lambda row: dist(lat, long, row['lat'], row['lng']), axis=1) return biomass.loc[distances.idxmin(),'Site Name'] hp1995['BiomassSite'] = hp1995.apply( lambda row: find_site(row['pc_lat'], row['pc_long']), axis=1) print(hp1995.head())这很好用,因为我知道了最近的生物质发电站点的名称,但是我想知道这两个站点之间计算出的距离.
This has worked well, in that I've got the name of the closest Biomass generation site, however I want to know the distance calculated between these two sites.
我将如何计算距离?
How would I calculate the distance?
输出距离将是多少度量?我正尝试在距生物质基地2公里以内找到物业.
What metric would the output distance be in? I am trying to find properties within 2km from the biomass site.
推荐答案
要计算两个全局坐标之间的距离,您应该使用 Haversine公式,基于此页面,我已经实现了以下内容方法:
To calculate distance between two global coordinates you should use the Haversine Formula, based on this page I have implemented the following method:
import math def distanceBetweenCm(lat1, lon1, lat2, lon2): dLat = math.radians(lat2-lat1) dLon = math.radians(lon2-lon1) lat1 = math.radians(lat1) lat2 = math.radians(lat2) a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLon/2) * math.sin(dLon/2) * math.cos(lat1) * math.cos(lat2) c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) return c * 6371 * 100000 #multiply by 100k to get distance in cm您还可以通过乘以10的不同乘方来修改它以返回不同的单位.在本示例中,乘以100k得出的单位是厘米.如果不相乘,该方法将返回以公里为单位的距离.从那里可以根据需要执行更多单位转换.
You can also modify it to return different units, by multiplying by different powers of 10. In the example a multiplication by 100k results in units in centimeters. Without multiplying the method returns distance in km. From there you could perform more unit conversions if necessary .
如评论中所建议,对此的一种可能的优化方法是使用幂运算符而不是常规乘法,如下所示:
As suggested in the comments, one possible optimization for this would be using power operators instead of regular multiplication, like this:
a = math.sin(dLat/2)**2 + math.sin(dLon/2)**2 * math.cos(lat1) * math.cos(lat2)看看这个问题,以了解有关不同速度复杂性的更多信息python中计算能力的概念.
Take a look at this question to read more about different speed complexities of calculating powers in python.
更多推荐
如何在 pandas 的2个不同数据框中找到2个点之间的距离?
发布评论