nodes: 0, module; 1, function_definition; 2, function_name:get_nearest_edge; 3, parameters; 4, block; 5, identifier:G; 6, identifier:point; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, return_statement; 17, comment:"""
      Return the nearest edge to a pair of coordinates. Pass in a graph and a tuple
      with the coordinates. We first get all the edges in the graph. Secondly we compute
      the euclidean distance from the coordinates to the segments determined by each edge.
      The last step is to sort the edge segments in ascending order based on the distance
      from the coordinates to the edge. In the end, the first element in the list of edges
      will be the closest edge that we will return as a tuple containing the shapely
      geometry and the u, v nodes.
  
      Parameters
      ----------
      G : networkx multidigraph
      point : tuple
          The (lat, lng) or (y, x) point for which we will find the nearest edge
          in the graph
  
      Returns
      -------
      closest_edge_to_point : tuple (shapely.geometry, u, v)
          A geometry object representing the segment and the coordinates of the two
          nodes that determine the edge section, u and v, the OSM ids of the nodes.
      """; 18, assignment; 19, assignment; 20, assignment; 21, assignment; 22, assignment; 23, assignment; 24, assignment; 25, call; 26, expression_list; 27, identifier:start_time; 28, call; 29, identifier:gdf; 30, call; 31, identifier:graph_edges; 32, call; 33, identifier:edges_with_distances; 34, list_comprehension; 35, identifier:edges_with_distances; 36, call; 37, identifier:closest_edge_to_point; 38, subscript; 39, pattern_list; 40, identifier:closest_edge_to_point; 41, identifier:log; 42, argument_list; 43, identifier:geometry; 44, identifier:u; 45, identifier:v; 46, attribute; 47, argument_list; 48, identifier:graph_to_gdfs; 49, argument_list; 50, attribute; 51, argument_list; 52, tuple; 53, for_in_clause; 54, identifier:sorted; 55, argument_list; 56, subscript; 57, integer:0; 58, identifier:geometry; 59, identifier:u; 60, identifier:v; 61, call; 62, identifier:time; 63, identifier:time; 64, identifier:G; 65, keyword_argument; 66, keyword_argument; 67, attribute; 68, identifier:tolist; 69, identifier:graph_edge; 70, call; 71, identifier:graph_edge; 72, identifier:graph_edges; 73, identifier:edges_with_distances; 74, keyword_argument; 75, identifier:edges_with_distances; 76, integer:0; 77, attribute; 78, argument_list; 79, identifier:nodes; 80, False; 81, identifier:fill_edge_geometry; 82, True; 83, subscript; 84, identifier:values; 85, attribute; 86, argument_list; 87, identifier:key; 88, lambda; 89, string; 90, identifier:format; 91, tuple; 92, identifier:point; 93, binary_operator:time.time() - start_time; 94, identifier:gdf; 95, list; 96, call; 97, identifier:distance; 98, subscript; 99, lambda_parameters; 100, subscript; 101, string_content:Found nearest edge ({}) to point {} in {:,.2f} seconds; 102, identifier:u; 103, identifier:v; 104, call; 105, identifier:start_time; 106, string:"geometry"; 107, string:"u"; 108, string:"v"; 109, identifier:Point; 110, argument_list; 111, identifier:graph_edge; 112, integer:0; 113, identifier:x; 114, identifier:x; 115, integer:1; 116, attribute; 117, argument_list; 118, call; 119, identifier:time; 120, identifier:time; 121, identifier:tuple; 122, argument_list; 123, call; 124, identifier:reversed; 125, argument_list; 126, identifier:point
code: def get_nearest_edge(G, point):
      """
      Return the nearest edge to a pair of coordinates. Pass in a graph and a tuple
      with the coordinates. We first get all the edges in the graph. Secondly we compute
      the euclidean distance from the coordinates to the segments determined by each edge.
      The last step is to sort the edge segments in ascending order based on the distance
      from the coordinates to the edge. In the end, the first element in the list of edges
      will be the closest edge that we will return as a tuple containing the shapely
      geometry and the u, v nodes.
  
      Parameters
      ----------
      G : networkx multidigraph
      point : tuple
          The (lat, lng) or (y, x) point for which we will find the nearest edge
          in the graph
  
      Returns
      -------
      closest_edge_to_point : tuple (shapely.geometry, u, v)
          A geometry object representing the segment and the coordinates of the two
          nodes that determine the edge section, u and v, the OSM ids of the nodes.
      """
      start_time = time.time()
  
      gdf = graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
      graph_edges = gdf[["geometry", "u", "v"]].values.tolist()
  
      edges_with_distances = [
          (
              graph_edge,
              Point(tuple(reversed(point))).distance(graph_edge[0])
          )
          for graph_edge in graph_edges
      ]
  
      edges_with_distances = sorted(edges_with_distances, key=lambda x: x[1])
      closest_edge_to_point = edges_with_distances[0][0]
  
      geometry, u, v = closest_edge_to_point
  
      log('Found nearest edge ({}) to point {} in {:,.2f} seconds'.format((u, v), point, time.time() - start_time))
  
      return geometry, u, v