Let's assume we have the following Shapely object:
from shapely import geometry
# (lat, long) coordinates
p1 = geometry.Point(-96.87,32.88)
p2 = geometry.Point(-96.77,33.00)
p3 = geometry.Point(-96.67,32.95)
p4 = geometry.Point(-96.65,32.84)
pointList = [p1, p2, p3, p4, p1]
# creat geometric object
p = geometry.Polygon([[p.x, p.y] for p in pointList])
Next we want to create OSMnx compatible NetworkX graph from this polygon. The standard way would be:
import osmnx as ox
G = ox.graph.graph_from_polygon(p, network_type='drive', simplify=False)
But instead of underlining the 'real' road network, I want to generate a 'fake' road network with some density of nodes. Each node should be connected with some number of nearest neighbors. I am not interested in buildings, lakes, etc. I just want a plain NetworkX graph inside of my polygon with some number of randomly distributed nodes and some number of edges that connect those nodes.
def f_generate_plain_polygon(p, number_of_nodes, min_number_of_connection_for_each_node):
...
The generated graph should have some functionality from OSMnx. For example, we could visualize such a graph:
ox.plot_graph(G, show=False, close=False, edge_color='black', bgcolor='w', edge_alpha=0.5, node_color='black')
ox.utils_graph.remove_isolated_nodes(G)
Here is an example of what function f_generate_plain_polygon should generate. It is how polygon looks like:
And we are adding graph with 60 nodes and at least 3 connections with nearest neighbors for each node:
If for some reason it is more simple to generate a graph without explicitly specifying a number of nodes but just saying that we want to generate nodes with distance e.g. 0.001 between them it is also fine.
source https://stackoverflow.com/questions/74008354/how-to-create-networkx-graph-with-n-nodes-osmnx-compatible-from-shapely-object


Comments
Post a Comment