in libs/apls/apls.py [0:0]
def create_graph_midpoints(G_, linestring_delta=50, is_curved_eps=0.03,
n_id_add_val=1, allow_renaming=True,
verbose=False, super_verbose=False):
"""
Insert midpoint nodes into long edges on the graph.
Arguments
---------
G_ : networkx graph
Input networkx graph, with edges assumed to have a dictioary of
properties that includes the 'geometry' key.
linestring_delta : float
Distance in meters between linestring midpoints. Defaults to ``50``.
is_curved_eps : float
Minumum curvature for injecting nodes (if curvature is less than this
value, no midpoints will be injected). If < 0, always inject points
on line, regardless of curvature. Defaults to ``0.3``.
n_id_add_val : int
Sets min midpoint id above existing nodes
e.g.: G.nodes() = [1,2,4], if n_id_add_val = 5, midpoints will
be [9,10,11,...]
allow_renameing : boolean
Switch to allow renaming of an existing node with node_id if the
existing node is closest to the point. Defaults to ``False``.
verbose : boolean
Switch to print relevant values to screen. Defaults to ``False``.
super_verbose : boolean
Switch to print mucho values to screen. Defaults to ``False``.
Returns
-------
Gout, xms, yms : tuple
Gout is the updated graph
xms, yms are coordinates of the inserted points
"""
if len(G_.nodes()) == 0:
return G_, [], []
# midpoints
xms, yms = [], []
Gout = G_.copy()
midpoint_name_val, midpoint_name_inc = np.max(G_.nodes())+n_id_add_val, 1
for u, v, data in G_.edges(data=True):
# curved line
if 'geometry' in data:
# first edge props and get utm zone and letter
edge_props_init = G_.edges([u, v])
linelen = data['length']
line = data['geometry']
#################
# ignore empty line
if linelen == 0:
continue
# check if curved or not
minx, miny, maxx, maxy = line.bounds
# get euclidean distance
dst = scipy.spatial.distance.euclidean([minx, miny], [maxx, maxy])
# ignore if almost straight
if np.abs(dst - linelen) / linelen < is_curved_eps:
continue
#################
#################
# also ignore super short lines
if linelen < 0.75*linestring_delta:
continue
#################
if verbose:
print("create_graph_midpoints()...")
print(" u,v:", u, v)
print(" data:", data)
print(" edge_props_init:", edge_props_init)
# interpolate midpoints
# if edge is short, use midpoint, else get evenly spaced points
if linelen <= linestring_delta:
interp_dists = [0.5 * line.length]
else:
# get evenly spaced points
npoints = len(np.arange(0, linelen, linestring_delta)) + 1
interp_dists = np.linspace(0, linelen, npoints)[1:-1]
if verbose:
print(" interp_dists:", interp_dists)
# create nodes
node_id_new_list = []
xms_tmp, yms_tmp = [], []
for j, d in enumerate(interp_dists):
if verbose:
print(" ", j, "interp_dist:", d)
midPoint = line.interpolate(d)
xm0, ym0 = midPoint.xy
xm = xm0[-1]
ym = ym0[-1]
point = Point(xm, ym)
xms.append(xm)
yms.append(ym)
xms_tmp.append(xm)
yms_tmp.append(ym)
if verbose:
print(" midpoint:", xm, ym)
# add node to graph, with properties of u
node_id = midpoint_name_val
midpoint_name_val += midpoint_name_inc
node_id_new_list.append(node_id)
if verbose:
print(" node_id:", node_id)
# add to graph
Gout, node_props, _, _ = insert_point_into_G(
Gout, point, node_id=node_id,
allow_renaming=allow_renaming,
verbose=super_verbose)
return Gout, xms, yms