开发者

How to find the nearest nighboring point coordinates in 2d grid using python

I try to find the nearest point in geoida database for every point stored in second database. Here's may approach, which is extremely slow. geoida.db stores +55000 coordinates

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]

both databases contain latitudes and longitudes


Why are you constructing the KDTree over and over again? It seems to me that you should construct it once and query it for every point. Constructing the tree is O(N log N) (or O(N (log N)^2) depending on the algorithm) so doing it N times makes your algorithm O(N^2 log N). Constructing the tree once and querying it will keep the complexity at O(N log N).


Simply create the tree outside the loop:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜