in libs/apls/apls.py [0:0]
def _refine_gt_graph(G0gt_init, im_test_file,
subgraph_filter_weight='length',
min_subgraph_length=5,
travel_time_key='travel_time_s',
speed_key='inferred_speed_mps',
verbose=False,
super_verbose=False):
"""refine ground truth graph"""
t1 = time.time()
# save latlon geometry (osmnx overwrites the 'geometry' tag)
# also compute pixel geom
for i, (u, v, key, data) in enumerate(G0gt_init.edges(keys=True, data=True)):
if 'geometry' not in data:
sourcex, sourcey = G0gt_init.nodes[u]['x'], G0gt_init.nodes[u]['y']
targetx, targety = G0gt_init.nodes[v]['x'], G0gt_init.nodes[v]['y']
line_geom = LineString([Point(sourcex, sourcey),
Point(targetx, targety)])
else:
line_geom = data['geometry']
data['geometry_latlon'] = line_geom.wkt
if os.path.exists(im_test_file):
# get pixel geom (do this after simplify so that we don't have to
# collapse the lines (see apls_wkt_to_G.wkt_to_G)
geom_pix = apls_utils.geomGeo2geomPixel(line_geom,
input_raster=im_test_file)
data['geometry_pix'] = geom_pix.wkt
data['length_pix'] = geom_pix.length
if len(G0gt_init.nodes()) == 0:
return G0gt_init
G0gt = osmnx_funcs.project_graph(G0gt_init)
if verbose:
print("len G0gt.nodes():", len(G0gt.nodes()))
print("len G0gt.edges:", len(G0gt.edges()))
if verbose:
print("Simplifying graph...")
try:
G2gt_init0 = osmnx_funcs.simplify_graph(G0gt).to_undirected()
except:
print("Cannot simplify graph, using original")
G2gt_init0 = G0gt
# make sure all edges have a geometry assigned to them
G2gt_init1 = create_edge_linestrings(
G2gt_init0.copy(), remove_redundant=True)
t2 = time.time()
if verbose:
print("Time to project, simplify, and create linestrings:",
t2 - t1, "seconds")
# clean up connected components
G2gt_init2 = _clean_sub_graphs(
G2gt_init1.copy(), min_length=min_subgraph_length,
weight=subgraph_filter_weight,
verbose=verbose, super_verbose=super_verbose)
# add pixel coords
try:
if os.path.exists(im_test_file):
G_gt_almost, _, gt_graph_coords = apls_utils._set_pix_coords(
G2gt_init2.copy(), im_test_file)
else:
G_gt_almost = G2gt_init2
except:
pass
# !!!!!!!!!!!!!!!
# ensure nodes have coorect xpix and ypix since _set_pix_coords is faulty!
for j, n in enumerate(G_gt_almost.nodes()):
x, y = G_gt_almost.nodes[n]['x'], G_gt_almost.nodes[n]['y']
geom_pix = apls_utils.geomGeo2geomPixel(Point(x, y),
input_raster=im_test_file)
[(xp, yp)] = list(geom_pix.coords)
G_gt_almost.nodes[n]['x_pix'] = xp
G_gt_almost.nodes[n]['y_pix'] = yp
# update pixel and lat lon geometries that get turned into lists upon
# simplify() that produces a 'geometry' tag in wmp
if verbose:
print("Merge 'geometry' linestrings...")
keys_tmp = ['geometry_pix', 'geometry_latlon']
for i, (u, v, attr_dict) in enumerate(G_gt_almost.edges(data=True)):
for key_tmp in keys_tmp:
if key_tmp not in attr_dict.keys():
continue
if super_verbose:
print("Merge", key_tmp, "...")
geom = attr_dict[key_tmp]
if type(geom) == list:
# check if the list items are wkt strings, if so, create
# linestrigs
# or (type(geom_pix[0]) == unicode):
if (type(geom[0]) == str):
geom = [shapely.wkt.loads(ztmp) for ztmp in geom]
# merge geoms
attr_dict[key_tmp] = shapely.ops.linemerge(geom)
elif type(geom) == str:
attr_dict[key_tmp] = shapely.wkt.loads(geom)
else:
pass
# update wkt_pix?
if 'wkt_pix' in attr_dict.keys():
attr_dict['wkt_pix'] = attr_dict['geometry_pix'].wkt
# update 'length_pix'
if 'length_pix' in attr_dict.keys():
attr_dict['length_pix'] = np.sum([attr_dict['length_pix']])
# check if simplify created various speeds on an edge
speed_keys = [speed_key, 'inferred_speed_mph', 'inferred_speed_mps']
for sk in speed_keys:
if sk not in attr_dict.keys():
continue
if type(attr_dict[sk]) == list:
if verbose:
print(" Taking mean of multiple speeds on edge:", u, v)
attr_dict[sk] = np.mean(attr_dict[sk])
if verbose:
print("u, v, speed_key, attr_dict)[speed_key]:",
u, v, sk, attr_dict[sk])
# add travel time
G_gt = add_travel_time(G_gt_almost.copy(),
speed_key=speed_key,
travel_time_key=travel_time_key)
return G_gt