Search code examples
pythonnetworkxopenstreetmapadjacency-matrix

Create adjacency matrix for large id numbers


I extracted some road data from Openstreetmap the relation between the crossroad are represented in a node and each node has an id, the extracted data has been converted to a data frame of shape (50,2) since I'm trying to implement on small size land

The data frame looks like this

    id_1        id_2
0     42807002  8219742091
1   8219742091  8219737904
2   8219737904    42807004
3     42839143  8219742075
4   8219742075    42807002
5     42807002    42839146
6     42839146    42839155
7     42839155    42839161
8     42839161    42839168
9     42839168  2875111804
10  2875111804    42839172
11    42839146  8219742083
12  8219742083  8219742024
13  8219742024    42845825
14    42839161  8219742048
15  8219742048  8219742063
16  8219742063    42862789
17    42839155  8219742066
18  8219742066  5427796641
19  5427796641  5427796639
20  5427796639  5427796630 

and for further illustration, this is the graph relation between my nodes.

enter image description here

Each record shows that there is a link between id_1 and id_2.

I would like to create an adjacency matrix for this dataframe but I have a huge problem that the id names are so big (the largest value is 8219742092). I tried to use this code using networkx

G = nx.from_pandas_adjacency(df)

but as I expected it doesn't work since the networkx adjacency require nodes from 0 to 8219742092 and the number 8219742092 is so huge to create a dataset from it.

How can I make an adjacency matrix based on this data?

Small note: I can't rename the id.


Solution

  • try use this code using crosstab

    cross = pd.crosstab(df['id_1'],df['id_2'])
    adjacency_matrix=cross.values