【发布时间】:2026-01-13 19:10:02
【问题描述】:
帮帮我。我被困住了。我有一个class 或point。该类的属性是lat、Long、point_id、name。有些点具有相同的Lat 和Long。所以,我需要point_id 作为积分标识。实际上,我想使用kdTree 的树来获得我点的最近邻居。我在构造kdTree 时没有问题,但是当我想知道我的点最近邻居时,结果是最近邻居的列表(Lat,Long) 而我想要point_id 作为我的结果,因为有些点具有相同的@987654334 @ 和 Long。
这是我的代码:
import numpy as np
from scipy import spatial
import psycopg2
import math
import psycopg2.extensions
class Point:
def __init__(self, id_point, name, Lat, Long):
self.id_point=id_point
self.name=name
self.Lat=Lat
self.Long=Long
def Struktur_KdTree(points):
tree= spatial.KDTree(points)
return tree
def getNearestPoint(tree,point,radius):
return tree.query_ball_point(point,radius)
try:
conn = psycopg2.connect("dbname='Chicago_crime' user='postgres' host='localhost' password='1234'")
except :
print "I'm unable to connect to the database"
cur = conn.cursor()
cur.execute("""SELECT id_objek, primary_descript, lat, long from data_crime""")
data = []
Lat = []
Long = []
for id_jenis, jenis, lat, long in cur.fetchall():
data.append(Point(id_point,name,Lat,Long))
Lat.append(lat)
Long.append(long)
dataPoints = zip (Lat,Long)
tree = Struktur_KdTree(dataPoints)
result=[]
radius=2
for point in dataPoint:
result.append(getNearestPoint(tree,point,radius))
请给我一些建议?
【问题讨论】:
标签: python python-2.7 kdtree spatial-index