Search code examples
graphnetworkxosmnx

Generate random points on osmnx graph


I have created a road network graph using osmnx library. now I want to generate some random points on the network but I don't have any idea how to do it. need some help :( here is my code:

import geopandas as gpd
import osmnx  as ox
top=  gpd.GeoDataFrame(columns = ['name', 'geometry'], crs = 4326, geometry = 'geometry')
top.at[0, 'geometry'] = Point(100.40823730180041,14.207021554191956)
top.at[0, 'name'] = 'tl'
top.at[1, 'geometry'] = Point(100.74774714891429, 14.196946042603166)
top.at[1, 'name'] = 'tr'
bottom=  gpd.GeoDataFrame(columns = ['name', 'geometry'], crs = 4326, geometry = 'geometry')
bottom.at[0, 'geometry'] = Point(100.38860578002853,13.612931284522707)
bottom.at[0, 'name'] = 'bl'
bottom.at[1, 'geometry'] = Point(100.7131032869639, 13.581503263247015)
bottom.at[1, 'name'] = 'br'
combined = top.append(bottom)
convex = combined.unary_union.convex_hull
graph_extent = convex.buffer(0.02)
graph = ox.graph_from_polygon(graph_extent, network_type = "drive")

Following are the steps of what I did:

  1. I created two geodataframes top and bottom top define the extent of my road network
  2. Then I combined them and used ox.graph_from_polygon to create a road network.
  3. My road network looks something like this roadNetwork
  4. Now I want to generate some random points that should be on the links/edges of the network created.

Solution

  • The sample_points function does exactly that. See the OSMnx usage examples and documentation for usage: https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.utils_geo.sample_points