如何从python中的坐标数据框创建图形网络?

问题描述 投票:-1回答:1

我有一个DataFrame,其中包含两列,一列是纬度,另一列是地图上位置的经度。另外,我有起点和终点的坐标。 (它们不在DataFrame中)

我想创建一个在起点和终点之间定向的图形网络(矩阵),如下图所示。

Graph Network Schematic

#Desired output
np.array([[0,1,1,0,0,0],
          [0,0,1,1,1,0],
          [0,0,0,1,1,0],
          [0,0,0,0,1,1],
          [0,0,0,0,0,1],
          [0,0,0,0,0,0]])

到目前为止,我已经尝试过此代码:


#start_df and end_df are DataFrames containing lat and lon of start and end points 

#location_inbetween containts lat and lon of points between start and end

graph_df = pd.concat([start_df,location_inbetween,end_df],axis=0)
graph_df.reset_index(inplace = True,drop = True)


a = np.zeros(shape=(graph_df.shape[0],graph_df.shape[0]))
graph_mat = pd.DataFrame(a)

for i in range(graph_df.shape[0]): 
    for j in range(graph_df.shape[0]): 
        test = ( abs(graph_df.iloc[i,1]) < abs(graph_df.iloc[j,1]) ) | ( abs(graph_df.iloc[i,0]) < abs(graph_df.iloc[j,0]) )  

        if test == True: 
            graph_mat.iloc[i,j] =  1

但这不是一个好答案。

python gis
1个回答
0
投票
def First(graph_df):

graph_mat = pd.DataFrame(np.zeros(shape=(graph_df.shape[0],graph_df.shape[0])))

for i in range(graph_df.shape[0]): 
    for j in range(graph_df.shape[0]): 
        test = ( abs(graph_df.iloc[i,1]) < abs(graph_df.iloc[j,1]) ) | ( abs(graph_df.iloc[i,0]) < abs(graph_df.iloc[j,0]) )  
        if test == True: 
            graph_mat.iloc[i,j] =  1

for i in range(graph_mat.shape[0]): 
    for j in range(graph_mat.shape[0]): 
        for k in range(graph_mat.shape[0]): 
            test1 = ( abs(graph_df.iloc[i,1]) < abs(graph_df.iloc[j,1]) ) &  ( abs(graph_df.iloc[j,1]) < abs(graph_df.iloc[k,1]) ) & ( abs(graph_df.iloc[i,1]) < abs(graph_df.iloc[k,1]) )
            test2 = ( abs(graph_df.iloc[i,0]) < abs(graph_df.iloc[j,0]) ) &  ( abs(graph_df.iloc[j,0]) < abs(graph_df.iloc[k,0]) ) & ( abs(graph_df.iloc[i,0]) < abs(graph_df.iloc[k,0]) )
            test3 = (graph_mat.iloc[i,j] == 1) & (graph_mat.iloc[j,k] ==1)
            test4 = test1 & test2 & test3 
            if test4 == True:
                graph_mat.iloc[i,k] = 0
return graph_mat
graph_mat = First(graph_df)
© www.soinside.com 2019 - 2024. All rights reserved.