问题描述
是否有一个 Python 模块,我可以在其中创建具有地理位置坐标(纬度和经度)的对象,并在给定坐标的 5 公里距离(即半径)内查询所有对象?
Is there a Python module where I can create objects with a geographical location coordinate (latitude and longitude), and query all the objects for ones which are within a 5km distance (i.e. radius) of a given coordinate?
我一直在尝试将纬度和经度作为键存储在字典中(因为它们是通过键索引的)并使用一些测距算法来查询它们.但这感觉就像一个可怕的黑客攻击.
I've been trying to store the latitude and longitude as keys in dictionaries (as they're indexed by key) and use some distance finding algorithms to query them. But this feels like a horrible hack.
基本上类似于 PostGIS for PostgreSQL,但都在我的 Python 应用程序的内存中.
Essentially something like PostGIS for PostgreSQL, but all within my Python app's memory.
推荐答案
是的,试试 geopy.
import geopy
import geopy.distance
pt1 = geopy.Point(48.853, 2.349)
pt2 = geopy.Point(52.516, 13.378)
dist = geopy.distance.distance(pt1, pt2).km
# 878.25
之后您可以查询您的积分列表:
afterwards you can query your lists of points:
[pt for pt in points if geopy.distance.distance(orig, pt).km < 5.]
这篇关于用于存储和查询地理坐标的 Python 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!