Skip to content

Instantly share code, notes, and snippets.

@evandiewald
Created August 29, 2022 01:52
Show Gist options
  • Save evandiewald/6e53e522bb3513520cf2a242cbf754f5 to your computer and use it in GitHub Desktop.
Save evandiewald/6e53e522bb3513520cf2a242cbf754f5 to your computer and use it in GitHub Desktop.
@callback(
Output("route-path", "data"),
Input("map", "click_lat_lng"),
Input("distance-target", "value"),
Input("elevation-min", "value"),
Input("mode-select", "value"),
State("route-path", "data")
)
def cycles(click_lat_lng, distance_target: Optional[Union[int, float]], elevation_min: Optional[Union[int, float]], mode, path):
if click_lat_lng is None:
# prevent the None callbacks is important with the store component.
# you don't want to update the store for nothing.
raise PreventUpdate
nearest_node_id = ox.nearest_nodes(G, click_lat_lng[1], click_lat_lng[0])
if mode == 1:
path = {"points": []}
with rasterio.open("static/gis-data/SRTM_GL3/SRTM_GL3_srtm.vrt") as dataset:
pt = find_best_loop(G2, nearest_node_id, distance_target, tol=1.0, min_elev=elevation_min, dataset=dataset)
# path["points"] = path_to_coords_list(G, pt)
for u, v in zip(pt[:-1], pt[1:]):
# if there are parallel edges, select the shortest
data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])
if "geometry" in data:
# if geometry attribute exists, add all its coords to list
xs, ys = data["geometry"].xy
pts = [[ys[i], xs[i]] for i in range(len(xs))]
path["points"] += pts
else:
# otherwise, the edge is a straight line from node to node
path["points"].append([G.nodes[u]["y"], G.nodes[u]["x"]])
elif mode == 2:
if path:
if "nodes" not in path:
path = {"points": [], "nodes": [], "last_click_idx": 0}
else:
path = {"points": [], "nodes": [], "last_click_idx": 0}
nearest_node = G.nodes[nearest_node_id]
if len(path["points"]) == 0:
path["points"].append([nearest_node["y"], nearest_node["x"]])
else:
pt = ox.shortest_path(G, path["nodes"][-1], nearest_node_id)
# path["points"] = path_to_coords_list(G, pt)
for u, v in zip(pt[:-1], pt[1:]):
# if there are parallel edges, select the shortest
data = min(G.get_edge_data(u, v).values(), key=lambda d: d["length"])
if "geometry" in data:
# if geometry attribute exists, add all its coords to list
xs, ys = data["geometry"].xy
pts = [[ys[i], xs[i]] for i in range(len(xs))]
path["points"] += pts
else:
# otherwise, the edge is a straight line from node to node
path["points"].append([G.nodes[u]["y"], G.nodes[u]["x"]])
path["nodes"].append(nearest_node_id)
path["last_click_idx"] = len(path["points"])
return path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment