nodes: 0, module; 1, function_definition; 2, function_name:_find_nearest_conn_objects; 3, parameters; 4, block; 5, identifier:network; 6, identifier:node; 7, identifier:branches; 8, expression_statement; 9, comment:# threshold which is used to determine if 2 objects are on the same position (see below for details on usage); 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, for_statement; 14, comment:# sort all objects by distance from node; 15, expression_statement; 16, return_statement; 17, comment:"""Searches all branches for the nearest possible connection object per branch
  
      It picks out 1 object out of 3 possible objects: 2 branch-adjacent stations
      and 1 potentially created branch tee on the line (using perpendicular projection).
      The resulting stack (list) is sorted ascending by distance from node.
  
      Parameters
      ----------
      network : :class:`~.grid.network.Network`
          The eDisGo container object
      node : :class:`~.grid.components.Component`
          Node to connect (e.g. :class:`~.grid.components.Generator`)
      branches :
          List of branches (NetworkX branch objects)
  
      Returns
      -------
      :obj:`list` of :obj:`dict`
          List of connection objects (each object is represented by dict with eDisGo object,
          shapely object and distance to node.
  
      Notes
      -----
      Adapted from `Ding0 <https://github.com/openego/ding0/blob/\
          21a52048f84ec341fe54e0204ac62228a9e8a32a/\
          ding0/grid/mv_grid/mv_connect.py#L38>`_.
      """; 18, assignment; 19, assignment; 20, assignment; 21, identifier:branch; 22, identifier:branches; 23, block; 24, assignment; 25, identifier:conn_objects_min_stack; 26, identifier:conn_diff_tolerance; 27, subscript; 28, identifier:conn_objects_min_stack; 29, list; 30, identifier:node_shp; 31, call; 32, expression_statement; 33, comment:# create shapely objects for 2 stations and line between them, transform to equidistant CRS; 34, expression_statement; 35, expression_statement; 36, expression_statement; 37, comment:# create dict with DING0 objects (line & 2 adjacent stations), shapely objects and distances; 38, expression_statement; 39, comment:# Remove branch from the dict of possible conn. objects if it is too close to a node.; 40, comment:# Without this solution, the target object is not unique for different runs (and so; 41, comment:# were the topology); 42, if_statement; 43, comment:# remove MV station as possible connection point; 44, if_statement; 45, comment:# find nearest connection point on given triple dict (2 branch-adjacent stations + cable dist. on line); 46, expression_statement; 47, expression_statement; 48, identifier:conn_objects_min_stack; 49, list_comprehension; 50, subscript; 51, string; 52, identifier:transform; 53, argument_list; 54, assignment; 55, assignment; 56, assignment; 57, assignment; 58, assignment; 59, parenthesized_expression; 60, block; 61, call; 62, block; 63, elif_clause; 64, assignment; 65, call; 66, identifier:_; 67, for_in_clause; 68, attribute; 69, string; 70, string_content:conn_diff_tolerance; 71, call; 72, attribute; 73, identifier:stations; 74, subscript; 75, identifier:station1_shp; 76, call; 77, identifier:station2_shp; 78, call; 79, identifier:line_shp; 80, call; 81, identifier:conn_objects; 82, dictionary; 83, boolean_operator; 84, delete_statement; 85, identifier:isinstance; 86, argument_list; 87, delete_statement; 88, call; 89, block; 90, identifier:conn_objects_min; 91, call; 92, attribute; 93, argument_list; 94, identifier:_; 95, call; 96, identifier:network; 97, identifier:config; 98, string_content:grid_connection; 99, identifier:proj2equidistant; 100, argument_list; 101, identifier:node; 102, identifier:geom; 103, identifier:branch; 104, string; 105, identifier:transform; 106, argument_list; 107, identifier:transform; 108, argument_list; 109, identifier:LineString; 110, argument_list; 111, pair; 112, pair; 113, pair; 114, comparison_operator:abs(conn_objects['s1']['dist'] - conn_objects['b']['dist']) < conn_diff_tolerance; 115, comparison_operator:abs(conn_objects['s2']['dist'] - conn_objects['b']['dist']) < conn_diff_tolerance; 116, subscript; 117, subscript; 118, identifier:MVStation; 119, subscript; 120, identifier:isinstance; 121, argument_list; 122, delete_statement; 123, identifier:min; 124, argument_list; 125, identifier:conn_objects_min_stack; 126, identifier:append; 127, identifier:conn_objects_min; 128, identifier:sorted; 129, argument_list; 130, identifier:network; 131, string_content:adj_nodes; 132, call; 133, attribute; 134, call; 135, attribute; 136, list; 137, string; 138, dictionary; 139, string; 140, dictionary; 141, string; 142, dictionary; 143, call; 144, identifier:conn_diff_tolerance; 145, call; 146, identifier:conn_diff_tolerance; 147, identifier:conn_objects; 148, string; 149, subscript; 150, string; 151, identifier:conn_objects; 152, string; 153, subscript; 154, identifier:MVStation; 155, subscript; 156, call; 157, keyword_argument; 158, identifier:conn_objects_min_stack; 159, keyword_argument; 160, identifier:proj2equidistant; 161, argument_list; 162, subscript; 163, identifier:geom; 164, identifier:proj2equidistant; 165, argument_list; 166, subscript; 167, identifier:geom; 168, identifier:station1_shp; 169, identifier:station2_shp; 170, string_content:s1; 171, pair; 172, pair; 173, pair; 174, string_content:s2; 175, pair; 176, pair; 177, pair; 178, string_content:b; 179, pair; 180, pair; 181, pair; 182, identifier:abs; 183, argument_list; 184, identifier:abs; 185, argument_list; 186, string_content:b; 187, identifier:conn_objects; 188, string; 189, string_content:obj; 190, string_content:s1; 191, subscript; 192, string; 193, identifier:conn_objects; 194, string; 195, attribute; 196, argument_list; 197, identifier:key; 198, lambda; 199, identifier:key; 200, lambda; 201, identifier:network; 202, identifier:stations; 203, integer:0; 204, identifier:network; 205, identifier:stations; 206, integer:1; 207, string; 208, subscript; 209, string; 210, identifier:station1_shp; 211, string; 212, binary_operator:node_shp.distance(station1_shp) * 0.999; 213, string; 214, subscript; 215, string; 216, identifier:station2_shp; 217, string; 218, binary_operator:node_shp.distance(station2_shp) * 0.999; 219, string; 220, identifier:branch; 221, string; 222, identifier:line_shp; 223, string; 224, call; 225, binary_operator:conn_objects['s1']['dist'] - conn_objects['b']['dist']; 226, binary_operator:conn_objects['s2']['dist'] - conn_objects['b']['dist']; 227, string_content:s1; 228, identifier:conn_objects; 229, string; 230, string_content:obj; 231, string_content:s2; 232, identifier:conn_objects; 233, identifier:values; 234, lambda_parameters; 235, subscript; 236, lambda_parameters; 237, subscript; 238, string_content:obj; 239, identifier:stations; 240, integer:0; 241, string_content:shp; 242, string_content:dist; 243, call; 244, float:0.999; 245, string_content:obj; 246, identifier:stations; 247, integer:1; 248, string_content:shp; 249, string_content:dist; 250, call; 251, float:0.999; 252, string_content:obj; 253, string_content:shp; 254, string_content:dist; 255, attribute; 256, argument_list; 257, subscript; 258, subscript; 259, subscript; 260, subscript; 261, string_content:s2; 262, identifier:v; 263, identifier:v; 264, string; 265, identifier:x; 266, identifier:x; 267, string; 268, attribute; 269, argument_list; 270, attribute; 271, argument_list; 272, identifier:node_shp; 273, identifier:distance; 274, identifier:line_shp; 275, subscript; 276, string; 277, subscript; 278, string; 279, subscript; 280, string; 281, subscript; 282, string; 283, string_content:dist; 284, string_content:dist; 285, identifier:node_shp; 286, identifier:distance; 287, identifier:station1_shp; 288, identifier:node_shp; 289, identifier:distance; 290, identifier:station2_shp; 291, identifier:conn_objects; 292, string; 293, string_content:dist; 294, identifier:conn_objects; 295, string; 296, string_content:dist; 297, identifier:conn_objects; 298, string; 299, string_content:dist; 300, identifier:conn_objects; 301, string; 302, string_content:dist; 303, string_content:s1; 304, string_content:b; 305, string_content:s2; 306, string_content:b
code: def _find_nearest_conn_objects(network, node, branches):
      """Searches all branches for the nearest possible connection object per branch
  
      It picks out 1 object out of 3 possible objects: 2 branch-adjacent stations
      and 1 potentially created branch tee on the line (using perpendicular projection).
      The resulting stack (list) is sorted ascending by distance from node.
  
      Parameters
      ----------
      network : :class:`~.grid.network.Network`
          The eDisGo container object
      node : :class:`~.grid.components.Component`
          Node to connect (e.g. :class:`~.grid.components.Generator`)
      branches :
          List of branches (NetworkX branch objects)
  
      Returns
      -------
      :obj:`list` of :obj:`dict`
          List of connection objects (each object is represented by dict with eDisGo object,
          shapely object and distance to node.
  
      Notes
      -----
      Adapted from `Ding0 <https://github.com/openego/ding0/blob/\
          21a52048f84ec341fe54e0204ac62228a9e8a32a/\
          ding0/grid/mv_grid/mv_connect.py#L38>`_.
      """
  
      # threshold which is used to determine if 2 objects are on the same position (see below for details on usage)
      conn_diff_tolerance = network.config['grid_connection'][
          'conn_diff_tolerance']
  
      conn_objects_min_stack = []
  
      node_shp = transform(proj2equidistant(network), node.geom)
  
      for branch in branches:
          stations = branch['adj_nodes']
  
          # create shapely objects for 2 stations and line between them, transform to equidistant CRS
          station1_shp = transform(proj2equidistant(network), stations[0].geom)
          station2_shp = transform(proj2equidistant(network), stations[1].geom)
          line_shp = LineString([station1_shp, station2_shp])
  
          # create dict with DING0 objects (line & 2 adjacent stations), shapely objects and distances
          conn_objects = {'s1': {'obj': stations[0],
                                 'shp': station1_shp,
                                 'dist': node_shp.distance(station1_shp) * 0.999},
                          's2': {'obj': stations[1],
                                 'shp': station2_shp,
                                 'dist': node_shp.distance(station2_shp) * 0.999},
                          'b': {'obj': branch,
                                'shp': line_shp,
                                'dist': node_shp.distance(line_shp)}}
  
          # Remove branch from the dict of possible conn. objects if it is too close to a node.
          # Without this solution, the target object is not unique for different runs (and so
          # were the topology)
          if (
                  abs(conn_objects['s1']['dist'] - conn_objects['b']['dist']) < conn_diff_tolerance
               or abs(conn_objects['s2']['dist'] - conn_objects['b']['dist']) < conn_diff_tolerance
             ):
              del conn_objects['b']
  
          # remove MV station as possible connection point
          if isinstance(conn_objects['s1']['obj'], MVStation):
              del conn_objects['s1']
          elif isinstance(conn_objects['s2']['obj'], MVStation):
              del conn_objects['s2']
  
          # find nearest connection point on given triple dict (2 branch-adjacent stations + cable dist. on line)
          conn_objects_min = min(conn_objects.values(), key=lambda v: v['dist'])
  
          conn_objects_min_stack.append(conn_objects_min)
  
      # sort all objects by distance from node
      conn_objects_min_stack = [_ for _ in sorted(conn_objects_min_stack, key=lambda x: x['dist'])]
  
      return conn_objects_min_stack