0% found this document useful (0 votes)
4 views

Chapter 9

The document contain geography related pages about spatial mapping

Uploaded by

maham22farooq44
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 9

The document contain geography related pages about spatial mapping

Uploaded by

maham22farooq44
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

osmnx

graph_from_polygon
graph_to_gdfs

In [1]: # Import osmnx for geospatial data from OpenStreetMap


import osmnx as ox

# Define the polygon forthe 1st district of Budapest


admin_district = ox.geocode_to_gdf('1st district, Budapest')
admin_poly = admin_district.geometry.values[0]

# Download the road network for driving within the admin boundary
G = ox.graph_from_polygon(admin_poly, network_type='drive')

# Print the number of nodes and edges in the road network


print("Number of nodes in the original graph:", G.number_of_nodes())
print("Number of edges in the original graph:", G.number_of_edges())
print()

# Convert the network graph to GeoDataFrames for nodes and edges


nodes, edges = ox.graph_to_gdfs(G)

# Display the first few rows of the nodes GeoDataFrame


display(nodes.head(3))

# Display the first few rows of the edges GeoDataFrame


display(edges.head(3))

# Print the features stored in each GeoDataFrame


print("Keys in the nodes table:", list(nodes.keys()))
print("Keys in the edges table:", list(edges.keys()))
print()

# Print the total number of nodes and edges


print("Number of nodes in the node table:", len(nodes))
print("Number of edges in the node table:", len(edges))
Number of nodes in the original graph: 359
Number of edges in the original graph: 706

Keys in the nodes table: ['y', 'x', 'street_count', 'highway', 'geometry']


Keys in the edges table: ['osmid', 'oneway', 'lanes', 'ref', 'highway', 'ma
xspeed', 'reversed', 'length', 'bridge', 'geometry', 'name', 'junction', 't
unnel', 'width', 'access']

Number of nodes in the node table: 359


Number of edges in the node table: 706
networkx
nx.MultiGraph
edges

nodes

In [2]: # Import the libraries we useNetworkX for network analysis


import networkx as nx
import osmnx as ox

# Define the polygon for the admin boundary the 1st district of Budapest
admin_district = ox.geocode_to_gdf('1st district, Budapest')
admin_poly = admin_district.geometry.values[0]

# Download the road network for driving within the administrative boundary
G = ox.graph_from_polygon(admin_poly, network_type='drive')

# Print the number of nodes and edges in the road network


print("Number of nodes in the original graph:", G.number_of_nodes())
print("Number of edges in the original graph:", G.number_of_edges())

Number of nodes in the original graph: 359


Number of edges in the original graph: 706

In [3]: # Convert the network graph to GeoDataFrames for nodes and edges
nodes, edges = ox.graph_to_gdfs(G)

# Print the number of nodes and edges in the road network


print("Number of nodes in the node table:", len(nodes))
print("Number of edges in the edge table:", len(edges))

Number of nodes in the node table: 359


Number of edges in the edge table: 706

In [4]: # Create an empty graph


G = nx.MultiGraph()

# Add edges to the graph


for idx, row in edges.iterrows():
G.add_edge(idx[0], idx[1], weight=row['length'])
# Add nodes with location data to the graph
for idx, row in nodes.iterrows():
G.add_node(idx, y=row['y'], x=row['x'])

# Print the number of nodes and edges in the graph


print("Number of nodes in the reconstructed graph:", G.number_of_nodes())
print("Number of edges in the reconstructed graph:", G.number_of_edges())

Number of nodes in the reconstructed graph: 359


Number of edges in the reconstructed graph: 706

In [5]: # Convert node attributes to a list


nodes_dict = list(G.nodes(data=True))
print(nodes_dict[0:5])
print()

# Convert edge attributes to a list


edges_dict = list(G.edges(data=True))
print(edges_dict[0:5])

[(35500835, {'y': 47.4901141, 'x': 19.0456276}), (1676318037, {'y': 47.4899


404, 'x': 19.046544}), (1676318015, {'y': 47.4892281, 'x': 19.0467569}), (3
5523878, {'y': 47.4913026, 'x': 19.0456141}), (10921844764, {'y': 47.491220
6, 'x': 19.0452095})]

[(35500835, 1676318037, {'weight': 74.333}), (35500835, 1676318015, {'weigh


t': 132.01100000000002}), (35500835, 1676318029, {'weight': 723.61999999999
99}), (35500835, 1676318118, {'weight': 71.951}), (1676318037, 1676318015,
{'weight': 100.84100000000002})]

osmnx
matplotlib
length

You might also like