如何使用python在二维网格中查找最近的nighboring点坐标

如何使用python在二维网格中查找最近的nighboring点坐标,python,sqlite,coordinates,geospatial,latitude-longitude,Python,Sqlite,Coordinates,Geospatial,Latitude Longitude,我尝试在geoida数据库中为存储在第二个数据库中的每个点查找最近的点。 这是梅的方法,非常缓慢。geoida.db存储+55000个坐标 import sqlite3 from kdtree import KDTree database = sqlite3.connect('geoida.db') cursor = database.cursor() cursor.execute("select lat, lon from coords") geoid = cursor.fetchall(

我尝试在geoida数据库中为存储在第二个数据库中的每个点查找最近的点。 这是梅的方法,非常缓慢。geoida.db存储+55000个坐标

import sqlite3
from kdtree import KDTree

database = sqlite3.connect('geoida.db')
cursor = database.cursor()
cursor.execute("select lat, lon  from coords")
geoid = cursor.fetchall()

database = sqlite3.connect('F.tsj')
cursor = database.cursor()
cursor.execute("select C1, C2 from tblSoPoints") 
results = cursor.fetchall()
for line in results:
    tree = KDTree.construct_from_data(geoid)
    nearest = tree.query(query_point=line, t=2)
    print nearest[0]

两个数据库都包含纬度和经度

为什么要反复构建KDTree?在我看来,您应该构造它一次,并为每个点查询它。根据算法,在日志N或日志N^2上构造树,这样做N次会使算法在^2日志N上。构造一次树并查询它将使复杂性保持在日志N上。

只需在循环外创建树:

tree = KDTree.construct_from_data(geoid)
for line in results:
  nearest = tree.query(query_point=line, t=2)

Thaks,现在脚本运行得非常快。