如何使用python确定网格数据库中给定纬度和经度周围的4个最近相邻点
我有一个名为 coords 的表,其中包含以下列 |name|lat|lon|und|
import sqlite3
database = sqlite3.connect('geoida.db')
cursor = database.cursor()
cursor.execute("select lat, lon, und from coords")
results = cursor.fetchall()
表中的每一行都存储 网格的一个点的坐标 点到点的距离始终为小数 0.041667, 2.5 英寸等于多少。
我想要实现的是在给定的纬度和经度周围找到 4 个最近的相邻点(以十进制表示)。 我们必须记住,这四个点的纬度和经度必须满足非常简单的条件:
相邻点的纬度、经度与给定点的纬度、经度之间的差值必须小于/等于 + 或 -
上的 0.041667 或者我们可以将此值视为从给定点除法寻找的相邻点的最大半径。
例如:
对于给定点 56.02050000 13.02040000
从我的坐标表中获取的 4 个最近的相邻点是:
56.000000 13.000000
56.000000 13.041667
56.041667 13.000000
56.041667 13.041667
给定点存储在另一个数据库中,其中 C1 是纬度,C2 是经度
database = sqlite3.connect('F.tsj')
cursor = database.cursor()
cursor.execute("select C1, C2 from tblSoPoints")
results = cursor.fetchall()
如何使用 python 进行这样的查询?
抱歉,代码有问题,但格式有问题。
I have a table named coords with the following columns |name|lat|lon|und|
import sqlite3
database = sqlite3.connect('geoida.db')
cursor = database.cursor()
cursor.execute("select lat, lon, und from coords")
results = cursor.fetchall()
every line in table stores coordinates of one point of g r i d and distance from point to point is always in decimal 0.041667,
what is equal to 2.5''.
What I would like to achieve is to find 4 nearest adjacent points around given latitude and longitude in decimal.
We have to keep in mind that latitude and longitude of these four points have to fill quite simple condition:
excess between lat, lon of adjacent point and lat, lon
of given point must be less/equal 0.041667 on + or -
or we can treat this value as max radius divisive sought neighboring points from the given one.
for example:
for given point 56.02050000 13.02040000
4 nearest adjacent points taken from my coords table are:
56.000000 13.000000
56.000000 13.041667
56.041667 13.000000
56.041667 13.041667
Given points are stored in another database, where C1 is latitude and C2 is longitude
database = sqlite3.connect('F.tsj')
cursor = database.cursor()
cursor.execute("select C1, C2 from tblSoPoints")
results = cursor.fetchall()
How can I put such query using python?
Sorry for code but there's something wrong with formating.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
带有 rtree 索引的完整示例
注意:此代码不包含边界。
对于非常有效的范围查询,如果有数百万个坐标,您可能需要 SQLite R-Tree 索引。
对于 1000000 个条目,上述方法大约需要 0.16 秒,但使用 rtree 的函数需要不到 1 毫秒。对于 10000 个条目,测试数据的基于 rtree 的解决方案为 800 µs,而基于 rtree 的解决方案为 20 µs。免责声明:这些数字是我发布的在我的机器上运行的代码的数字。
full example with rtree index
Note: this code doesn't include boundaries.
For very efficient range queries if there are millions of coordinates you might need SQLite R-Tree index.
For 1000000 entries the above approach takes ~0.16 seconds, but the function that uses rtree requires less than 1ms. For 10000 entries it is 800 µs vs. 20 µs for rtree-based solution for the data from the test. DISCLAIMER: The numbers are for the code I've posted that I run on my machine.