Python 如何确定点坐标是否存在于其他两个点坐标之间的路径上?
我创建了一个应用程序,它可以从房屋列表中提取数据,并计算到最近的商店和火车站的距离。我已经得到了所有商店、列表和火车站的纬度、经度坐标,目前我使用geopy.distancePython 如何确定点坐标是否存在于其他两个点坐标之间的路径上?,python,vector,coordinates,line,geopy,Python,Vector,Coordinates,Line,Geopy,我创建了一个应用程序,它可以从房屋列表中提取数据,并计算到最近的商店和火车站的距离。我已经得到了所有商店、列表和火车站的纬度、经度坐标,目前我使用geopy.distance import geopy.distance distanceToShop = geopy.distance.distance(shopCoordinate, listingCoordinate).km distanceToStation = geopy.distance.distance(stationCoordinate
import geopy.distance
distanceToShop = geopy.distance.distance(shopCoordinate, listingCoordinate).km
distanceToStation = geopy.distance.distance(stationCoordinate, listingCoordinate).km
这会在两点之间产生一个以公里为单位的值。然后,我筛选出离商店和火车站不够近的位置。对于火车站,我容忍的距离比商店要长。有时商店太远,无法过滤,但在去车站的路上。在这种情况下,我会容忍到商店的距离更长,因为它在去车站的路上
在这种情况下,商店位于房子和车站之间。车站位于坐标(40.651100,-73.949626)处。房子在坐标(40.651401,-73.937987)处。房屋和车站之间的距离为0.98公里
在下面的场景中,车间位于坐标(40.650913,-73.948301)处,从房屋到车间的距离为870米
从房子到商店的可接受距离为650米。但是,如果商店正在从车站出发的路上,则可以接受更长的距离。如果因为最近的商店太远(870m>650m),我天真地放弃了房子,那么这所房子就不合格,但在这种情况下,商店在从车站(回家的路上)出发的路上,所以它应该合格
更稳健的解决方案是计算两点之间的面积(例如正方形),并确定商店是否位于正方形内。这将产生更现实的结果
我如何通过编程来确定情况是否如此?有我可以学习的图书馆吗
编辑:已解决-找到一种计算方向角(两个坐标之间的角度)的方法,该方法可以操作并用于计算其他坐标。通过计算方位角,我可以将其偏移+/-90度,为目标和目标坐标创建角点。然后使用另一个导入(shapely),我从坐标中绘制了一个多边形,并检查该矩阵中是否存在商店
编辑2:使用GeographicalIB.geodesic的更整洁的解决方案,导入更少:
import geopy.distance
from geographiclib.geodesic import Geodesic
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
def getAreaCorners(origin, target, distanceInMeters, angle):
geod = Geodesic.WGS84
bearing = geod.Inverse(origin[0], origin[1], target[0], target[1])['azi1']
rightBearing = bearing + angle
if rightBearing < 0: rightBearing += 360
leftBearing = bearing - angle
if leftBearing < 0: leftBearing += 360
og1 = geod.Direct(origin[0], origin[1], rightBearing, distanceInMeters)
og2 = geod.Direct(origin[0], origin[1], leftBearing, distanceInMeters)
og1 = (og1['lat2'], og1['lon2'])
og2 = (og2['lat2'], og2['lon2'])
tg1 = geod.Direct(target[0], target[1], rightBearing, distanceInMeters)
tg2 = geod.Direct(target[0], target[1], leftBearing, distanceInMeters)
tg1 = (tg1['lat2'], tg1['lon2'])
tg2 = (tg2['lat2'], tg2['lon2'])
return [og1, og2, tg2, tg1]
def isShopOnPath(areaCoordinateList, shopCoordinate):
polygon = Polygon(areaCoordinateList)
shopCoordinatePoint = Point(shopCoordinate)
return polygon.contains(shopCoordinatePoint)
有什么问题?测试一个点是否在一个矩形内(甚至旋转)似乎不是很困难..我用一个更清晰的例子更新了原始问题。我需要关于解决问题的工具的帮助/指导-我不知道从哪里开始。我心里有一个概念,但不知道如何在代码中应用它。为什么不对总站商店回家的路径长度设置一个限制?有时房子位于车站和商店之间。在这种情况下,到商店的距离可能为400米,到车站的距离为900米,但到商店的距离为1.3公里。我主要想避免去商店的长途旅行(超过6500米),因为提沉重的袋子并不好玩,但如果商店在下班(车站)的路上,我认为这是可以管理的。在过滤过程中确定一个可接受的坐标区域可能会在我尚未考虑的其他方面证明是有用的。问题是什么?测试一个点是否在一个矩形内(甚至旋转)似乎不是很困难..我用一个更清晰的例子更新了原始问题。我需要关于解决问题的工具的帮助/指导-我不知道从哪里开始。我心里有一个概念,但不知道如何在代码中应用它。为什么不对总站商店回家的路径长度设置一个限制?有时房子位于车站和商店之间。在这种情况下,到商店的距离可能为400米,到车站的距离为900米,但到商店的距离为1.3公里。我主要想避免去商店的长途旅行(超过6500米),因为提沉重的袋子并不好玩,但如果商店在下班(车站)的路上,我认为这是可以管理的。在过滤过程中确定一个可接受的坐标区域可能在我尚未考虑的其他方面证明是有用的。
origin = (40.650809, -73.949583)
target = (40.644661, -73.943108)
shopCoordinate = (40.650467, -73.948553)
distanceInMeters = 100
angle = 90
distanceToShop = round(geopy.distance.distance(shopCoordinate, target).km,2)
distanceToStation = round(geopy.distance.distance(origin, target).km,2)
print('House is {} km from station. Shop is {} km from house.'.format(distanceToStation, distanceToShop))
if distanceToShop >= 0.65:
if isShopOnPath(getAreaCorners(origin, target, distanceInMeters, angle), shopCoordinate):
print('Shop is too far, but on the way from the station.')
else:
print('Shop is too far and not on the way from the station')
else:
print('Shop is less than 650m from the house.')