我有以下情况:
(1)我的网格很大。在某些情况下,我想进一步观察此网格中的特定点/像元。每个单元格都有一个ID
并分别坐标X
,Y
。因此,在这种情况下,让我们仅观察一个位于网格边缘的单元格-在图像上标记为C。通过某些公式,我可以获得所有第一阶(在图像上标记为1)和第二阶(在图像上标记为2)的所有相邻像元。
(2)在另一个条件下,我在相邻单元格中标识了一些单元格,并在第二张图像上用橙色标记。我想做的是通过优化距离来相互连接所有橙色单元,并且仅考虑min()
的距离。我的第一个尝试是仅通过计算到较低阶像元的距离来观察像元。因此,当查看相邻单元2中的单元时,我仅查看1中的单元。连接的解决方案显示在图像2上,但这并不是最佳选择,因为理想的解决方案将比较所有像元的距离,而不仅是比较低邻阶的像元的距离。通过这样做,我得到的情况如图3所示。问题是单元格当然没有连接到中心。该怎么办?
当前代码为:
CO
-中心点列表。
data
-d具有X,Y值的所有ID
CO_list = CO['ID'].tolist()
neighbor100 = []
for p in IskanjeCO_list:
d = get_neighbors100k2(p, len(data)) #function that finds the ID's of neighbours of the first order
neighbor100.append(d)
neighbor200 = []
for p in IskanjeCO_list:
d = get_neighbors200k2(p, len(data)) #function that finds the ID's of neighbours of the second order
neighbor200.append(d)
flat100 = []
for i in neighbor100:
for j in i:
flat100.append(j)
flat200 = []
for i in neighbor200:
for j in i:
flat200.append(j)
neighbors100 = flat100
neighbors200 = flat200
data_sosedi100 = data.iloc[flat100,].reset_index(drop=True)
data_sosedi200 = data.iloc[flat200,].reset_index(drop=True)
dist200 = []
for b in flat200:
d = ((pd.DataFrame((data_sosedi100['X']* - data.iloc[b,]['X'])**2
+ (data_sosedi100['Y'] - data.iloc[b,]['Y'])**2 )**0.5)).sum(1)
dist200.append(d.min())
data_sosedi200['dist'] = dist200
data_sosedi200['id'] = None
for e in CO_list:
data_sosedi200.loc[data_sosedi200['FID_2'].isin((get_neighbors200k2(e, len(data)))),'id'] = e
您对如何进一步优化它有任何建议吗?我希望我展示了整个形象。如果需要,我将进一步澄清。如果您看到了代码的一部分,在这里我可以进一步优化此循环,我将不胜感激!
答案 0 :(得分:1)
我手动定义了要使用的要点: 将numpy导入为np 从操作员导入itemgetter,attrgetter
nodes = [[-2,1], [-2,0], [-1,0], [0,0], [1,1], [2,1], [2,0], [1,2], [2,2]]
center = [0,0]
def find_neighbor(node):
n=[]
for i in range(-1,2):
for j in range(-1,2):
if not (i ==0 and j ==0):
n.append([node[0]+i,node[1]+j])
return [N for N in n if N in nodes]
def distance_to_center(node):
return np.sqrt(node[0]**2+node[1]**2)
def distance_between_two_nodes(node1, node2):
return np.sqrt((node1[0]-node2[0])**2+(node1[1]-node2[1])**2)
def next_node_closest_to_center(node):
min = distance_to_center(node)
next_node = node
for n in find_neighbor(node):
if distance_to_center(n) < min:
min = distance_to_center(n)
next_node = n
return next_node, min
def get_path_to_center(node):
node_path = [node]
distance = 0.
while node!= center:
new_node = next_node_closest_to_center(node)[0]
distance += distance_between_two_nodes(node, new_node)
node_path.append(new_node)
node=new_node
return node_path,distance
def furthest_nodes_from_center(nodes):
max = 0.
for n in nodes:
if get_path_to_center(n)[1] > max:
furthest_nodes_pathwise = []
max = get_path_to_center(n)[1]
furthest_nodes_pathwise.append(n)
elif get_path_to_center(n)[1] == max:
furthest_nodes_pathwise.append(n)
return furthest_nodes_pathwise
def farthest_node_from_center(nodes):
max = 0.
farthest_node = center
for n in nodes:
if distance_to_center(n) > max:
max = distance_to_center(n)
farthest_node = n
return farthest_node
def closest_node_to_center(nodes):
min = distance_to_center(farthest_node_from_center(nodes))
for n in nodes:
if distance_to_center(n) < min:
min = distance_to_center(n)
closest_node = n
return closest_node
def closest_node_center_with_furthest_distance(node_selection):
if len(node_selection) == 1:
return node_selection[0]
else:
return closest_node_to_center(node_selection)
print(closest_node_center_with_furthest_distance(furthest_nodes_from_center(nodes)))
输出:
[2, 0]
[Finished in 0.266s]
通过在所有节点上运行,我现在可以确定距离路径最远但仍最靠近中心距离的最远节点是[2,0]
而不是[2,2]
。所以我们从那里开始。要找到另一侧,只需像我说的那样将数据拆分为负x值和正数即可。如果在仅包含负x值单元格的列表上运行它,则会得到[-2,1]
现在您有2个起始单元格[2,0]
和[-2,1]
,我将根据您的注释中的步骤,让您找出算法导航到所有单元格经过的中心(您现在可以跳过步骤1,因为这是发布的答案)