示例#1
0
def get_nearest_node(graph, xy, edge_gdf, node_gdf, nts=[], db_costs={}, orig_node=None, logging=False):
    coords = geom_utils.get_coords_from_xy(xy)
    point = Point(coords)
    nearest_edge = find_nearest_edge(xy, edge_gdf)
    if (nearest_edge is None):
        return None
    nearest_node = find_nearest_node(xy, node_gdf)
    # parse node geom from node attributes
    nearest_node_geom = geom_utils.get_point_from_xy(graph.nodes[nearest_node])
    # get the nearest point on the nearest edge
    nearest_edge_point = geom_utils.get_closest_point_on_line(nearest_edge['geometry'], point)
    # return the nearest node if it is as near (or nearer) as the nearest edge
    if (nearest_edge_point.distance(nearest_node_geom) < 1 or nearest_node_geom.distance(point) < nearest_edge['geometry'].distance(point)):
        return { 'node': nearest_node, 'offset': round(nearest_node_geom.distance(point), 1) }
    # check if the nearest edge of the destination is one of the linking edges created for origin 
    if (orig_node is not None and 'link_edges' in orig_node):
        if (nearest_edge_point.distance(orig_node['link_edges']['link1']['geometry']) < 0.2):
            nearest_edge = orig_node['link_edges']['link1']
        if (nearest_edge_point.distance(orig_node['link_edges']['link2']['geometry']) < 0.2):
            nearest_edge = orig_node['link_edges']['link2']
    # create a new node on the nearest edge
    new_node = nw.add_new_node_to_graph(graph, nearest_edge_point, logging=logging)
    # link added node to the origin and destination nodes of the nearest edge (by adding two linking edges)
    link_edges = nw.add_linking_edges_for_new_node(graph, new_node, nearest_edge_point, nearest_edge, nts, db_costs, logging=logging)
    return { 'node': new_node, 'link_edges': link_edges, 'offset': round(nearest_edge_point.distance(point), 1) }
示例#2
0
def get_edge_line_coords(graph, node_from, edge_d):
    from_point = geom_utils.get_point_from_xy(graph.nodes[node_from])
    edge_line = edge_d['geometry']
    edge_coords = edge_line.coords
    first_point = Point(edge_coords[0])
    last_point = Point(edge_coords[len(edge_coords) - 1])
    if (from_point.distance(first_point) > from_point.distance(last_point)):
        return edge_coords[::-1]
    return edge_coords
示例#3
0
def find_nearest_node(xy, node_gdf):
    # start_time = time.time()
    nodes_sind = node_gdf.sindex
    point_geom = geom_utils.get_point_from_xy(xy)
    possible_matches_index = list(nodes_sind.intersection(point_geom.buffer(700).bounds))
    possible_matches = node_gdf.iloc[possible_matches_index]
    points_union = possible_matches.geometry.unary_union
    nearest_geom = nearest_points(point_geom, points_union)[1]
    nearest = possible_matches.geometry.geom_equals(nearest_geom)
    nearest_point =  possible_matches.loc[nearest]
    nearest_node = nearest_point.index.tolist()[0]
    # utils.print_duration(start_time, 'found nearest node')
    return nearest_node
示例#4
0
def find_nearest_edge(xy, edge_gdf):
    # start_time = time.time()
    edges_sind = edge_gdf.sindex
    point_geom = geom_utils.get_point_from_xy(xy)
    for radius in [80, 150, 250, 350, 650]:
        possible_matches_index = list(edges_sind.intersection(point_geom.buffer(radius).bounds))
        if (len(possible_matches_index) > 0):
            possible_matches = edge_gdf.iloc[possible_matches_index].copy()
            possible_matches['distance'] = [geom.distance(point_geom) for geom in possible_matches['geometry']]
            shortest_dist = possible_matches['distance'].min()
            if (shortest_dist < (radius - 50) or len(possible_matches_index) > 20):
                break
    if (len(possible_matches_index) == 0):
        print('no near edges found')
        return None
    nearest = possible_matches['distance'] == shortest_dist
    nearest_edges =  possible_matches.loc[nearest]
    nearest_edge = nearest_edges.iloc[0]
    nearest_edge_dict = nearest_edge.to_dict()
    # utils.print_duration(start_time, 'found nearest edge')
    return nearest_edge_dict
示例#5
0
    for geom in commutes['geom_work']
]
commutes = commutes[[
    'txyind', 'axyind', 'geom_home', 'geom_work', 'home_latLon', 'work_latLon',
    'yht'
]]
commutes.head()

#%% read grid
grid = files.get_statfi_grid()
# add centroid point geometry to grid
grid['grid_geom'] = [geometry.geoms[0] for geometry in grid['geometry']]
grid = grid.set_geometry('grid_geom')
grid['grid_centr'] = grid.apply(
    lambda row: geom_utils.get_point_from_xy({
        'x': row['x'],
        'y': row['y']
    }),
    axis=1)
grid['xyind'] = [int(xyind) for xyind in grid['xyind']]
grid = grid[['xyind', 'grid_geom', 'grid_centr']]
grid.head()

#%% read city extent (polygon of Helsinki)
hel_poly = files.get_hel_poly()
hel_poly = geom_utils.project_to_wgs(hel_poly)


def outside_hel_extent(geometry):
    return 'no' if geometry.within(hel_poly) else 'yes'

示例#6
0
def get_edge_geom_from_node_pair(graph, node_1, node_2):
    node_1_geom = geom_utils.get_point_from_xy(graph.nodes[node_1])
    node_2_geom = geom_utils.get_point_from_xy(graph.nodes[node_2])
    edge_line = LineString([node_1_geom, node_2_geom])
    return edge_line