使用Google App Engine上的Python,根据GeoPT计算城市之间的距离并查找周边城市

使用Google App Engine上的Python,根据GeoPT计算城市之间的距离并查找周边城市,第1张

概述我定义了一个城市模型,用于保存城市的geoname_id和位置(如GeoPt).我想要实现两件事.>我希望距离某个城市500公里半径范围内的所有城市.>我想计算两个城市之间以km为单位的距离.实现这一目标的最佳方式是什么,请记住,我有一个非常庞大的城市数据库,我不想在性能因素上牺牲很多.任何帮助或建议表示赞赏.最佳答案这很完美,但速度很慢:计算距

我定义了一个城市模型,用于保存城市的geoname_ID和位置(如GeoPt).我想要实现两件事.

>我希望距离某个城市500公里半径范围内的所有城市.
>我想计算两个城市之间以km为单位的距离.

实现这一目标的最佳方式是什么,请记住,我有一个非常庞大的城市数据库,我不想在性能因素上牺牲很多.任何帮助或建议表示赞赏.最佳答案@H_419_10@这很完美,但速度很慢:

计算距离的功能.传递给此函数的参数是位置的纬度和经度元组或Geopt():

def haversinedistance(location1,location2):  """Method to calculate distance between two sets of Lat/Lon."""  lat1,lon1 = location1  lat2,lon2 = location2  earth = 6371 #Earth's Radius in Kms. #Calculate distance based in haversine Formula dlat = math.radians(lat2-lat1) dlon = math.radians(lon2-lon1) a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2) c = 2 * math.atan2(math.sqrt(a),math.sqrt(1-a)) d = earth * c return d

用于计算半径内的周边城市的函数.这是City模型下存储所有城市的方法:

def get_closest_citIEs(self,kms):  citIEs = []  #Find surrounding CitIEs of a given city within a given radius  allcitIEs = self.country.city_set  for city in allcitIEs:    distance = haversinedistance((self.location.lat,self.location.lon),(city.location.lat,city.location.lon))    if not distance >= kms:      citIEs.append((city.name,int(distance)))  citIEs.remove(citIEs[0])  return citIEs
总结

以上是内存溢出为你收集整理的使用Google App Engine上的Python,根据GeoPT计算城市之间的距离并查找周边城市全部内容,希望文章能够帮你解决使用Google App Engine上的Python,根据GeoPT计算城市之间的距离并查找周边城市所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1205436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存