|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import h3 |
| 4 | +import networkx as nx |
| 5 | +import numpy as np |
| 6 | +from sklearn.metrics.pairwise import haversine_distances |
| 7 | + |
| 8 | + |
| 9 | +def create_hexagonal_nodes( |
| 10 | + resolutions: list[int], |
| 11 | + area: Optional[dict] = None, |
| 12 | +) -> tuple[nx.Graph, np.ndarray, list[int]]: |
| 13 | + """Creates a global mesh from a refined icosahedro. |
| 14 | +
|
| 15 | + This method relies on the H3 python library, which covers the earth with hexagons (and 5 pentagons). At each |
| 16 | + refinement level, a hexagon cell (nodes) has 7 child cells (aperture 7). |
| 17 | +
|
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + resolutions : list[int] |
| 21 | + Levels of mesh resolution to consider. |
| 22 | + area : dict |
| 23 | + A region, in GeoJSON data format, to be contained by all cells. Defaults to None, which computes the global |
| 24 | + mesh. |
| 25 | +
|
| 26 | + Returns |
| 27 | + ------- |
| 28 | + graph : networkx.Graph |
| 29 | + The specified graph (nodes & edges). |
| 30 | + coords_rad : np.ndarray |
| 31 | + The node coordinates (not ordered) in radians. |
| 32 | + node_ordering : list[int] |
| 33 | + Order of the nodes in the graph to be sorted by latitude and longitude. |
| 34 | + """ |
| 35 | + graph = nx.Graph() |
| 36 | + |
| 37 | + area_kwargs = {"area": area} |
| 38 | + |
| 39 | + for resolution in resolutions: |
| 40 | + graph = add_nodes_for_resolution(graph, resolution, **area_kwargs) |
| 41 | + |
| 42 | + coords = np.deg2rad(np.array([h3.h3_to_geo(node) for node in graph.nodes])) |
| 43 | + |
| 44 | + # Sort nodes by latitude and longitude |
| 45 | + node_ordering = np.lexsort(coords.T[::-1], axis=0) |
| 46 | + |
| 47 | + return graph, coords, list(node_ordering) |
| 48 | + |
| 49 | + |
| 50 | +def add_nodes_for_resolution( |
| 51 | + graph: nx.Graph, |
| 52 | + resolution: int, |
| 53 | + **area_kwargs: Optional[dict], |
| 54 | +) -> nx.Graph: |
| 55 | + """Add all nodes at a specified refinement level to a graph. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + graph : networkx.Graph |
| 60 | + The graph to add the nodes. |
| 61 | + resolution : int |
| 62 | + The H3 refinement level. It can be an integer from 0 to 15. |
| 63 | + area_kwargs: dict |
| 64 | + Additional arguments to pass to the get_nodes_at_resolution function. |
| 65 | + """ |
| 66 | + |
| 67 | + nodes = get_nodes_at_resolution(resolution, **area_kwargs) |
| 68 | + |
| 69 | + for idx in nodes: |
| 70 | + graph.add_node(idx, hcoords_rad=np.deg2rad(h3.h3_to_geo(idx))) |
| 71 | + |
| 72 | + return graph |
| 73 | + |
| 74 | + |
| 75 | +def get_nodes_at_resolution( |
| 76 | + resolution: int, |
| 77 | + area: Optional[dict] = None, |
| 78 | +) -> set[str]: |
| 79 | + """Get nodes at a specified refinement level over the entire globe. |
| 80 | +
|
| 81 | + If area is not None, it will return the nodes within the specified area |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + resolution : int |
| 86 | + The H3 refinement level. It can be an integer from 0 to 15. |
| 87 | + area : dict |
| 88 | + An area as GeoJSON dictionary specifying a polygon. Defaults to None. |
| 89 | +
|
| 90 | + Returns |
| 91 | + ------- |
| 92 | + nodes : set[str] |
| 93 | + The set of H3 indexes at the specified resolution level. |
| 94 | + """ |
| 95 | + nodes = h3.uncompact(h3.get_res0_indexes(), resolution) if area is None else h3.polyfill(area, resolution) |
| 96 | + |
| 97 | + # TODO: AOI not used in the current implementation. |
| 98 | + |
| 99 | + return nodes |
| 100 | + |
| 101 | + |
| 102 | +def add_edges_to_nx_graph( |
| 103 | + graph: nx.Graph, |
| 104 | + resolutions: list[int], |
| 105 | + x_hops: int = 1, |
| 106 | + depth_children: int = 1, |
| 107 | +) -> nx.Graph: |
| 108 | + """Adds the edges to the graph. |
| 109 | +
|
| 110 | + This method includes multi-scale connections to the existing graph. The different scales |
| 111 | + are defined by the resolutions (or refinement levels) specified. |
| 112 | +
|
| 113 | + Parameters |
| 114 | + ---------- |
| 115 | + graph : networkx.Graph |
| 116 | + The graph to add the edges. |
| 117 | + resolutions : list[int] |
| 118 | + Levels of mesh resolution to consider. |
| 119 | + x_hops: int |
| 120 | + The number of hops to consider for the neighbours. |
| 121 | + depth_children : int |
| 122 | + The number of resolution levels to consider for the connections of children. Defaults to 1, which includes |
| 123 | + connections up to the next resolution level. |
| 124 | +
|
| 125 | + Returns |
| 126 | + ------- |
| 127 | + graph : networkx.Graph |
| 128 | + The graph with the added edges. |
| 129 | + """ |
| 130 | + |
| 131 | + graph = add_neighbour_edges(graph, resolutions, x_hops) |
| 132 | + graph = add_edges_to_children( |
| 133 | + graph, |
| 134 | + resolutions, |
| 135 | + depth_children, |
| 136 | + ) |
| 137 | + return graph |
| 138 | + |
| 139 | + |
| 140 | +def add_neighbour_edges( |
| 141 | + graph: nx.Graph, |
| 142 | + refinement_levels: tuple[int], |
| 143 | + x_hops: int = 1, |
| 144 | +) -> nx.Graph: |
| 145 | + for resolution in refinement_levels: |
| 146 | + nodes = select_nodes_from_graph_at_resolution(graph, resolution) |
| 147 | + |
| 148 | + for idx in nodes: |
| 149 | + # neighbours |
| 150 | + for idx_neighbour in h3.k_ring(idx, k=x_hops) & set(nodes): |
| 151 | + graph = add_edge( |
| 152 | + graph, |
| 153 | + h3.h3_to_center_child(idx, refinement_levels[-1]), |
| 154 | + h3.h3_to_center_child(idx_neighbour, refinement_levels[-1]), |
| 155 | + ) |
| 156 | + |
| 157 | + return graph |
| 158 | + |
| 159 | + |
| 160 | +def add_edges_to_children( |
| 161 | + graph: nx.Graph, |
| 162 | + refinement_levels: tuple[int], |
| 163 | + depth_children: Optional[int] = None, |
| 164 | +) -> nx.Graph: |
| 165 | + """Adds edges to the children of the nodes at the specified resolution levels. |
| 166 | +
|
| 167 | + Parameters |
| 168 | + ---------- |
| 169 | + graph : nx.Graph |
| 170 | + graph to which the edges will be added |
| 171 | + refinement_levels : tuple[int] |
| 172 | + set of refinement levels |
| 173 | + depth_children : Optional[int], optional |
| 174 | + The number of resolution levels to consider for the connections of children. Defaults to 1, which includes |
| 175 | + connections up to the next resolution level, by default None |
| 176 | + """ |
| 177 | + if depth_children is None: |
| 178 | + depth_children = len(refinement_levels) |
| 179 | + |
| 180 | + for i_level, resolution_parent in enumerate(refinement_levels[0:-1]): |
| 181 | + parent_nodes = select_nodes_from_graph_at_resolution(graph, resolution_parent) |
| 182 | + |
| 183 | + for parent_idx in parent_nodes: |
| 184 | + # add own children |
| 185 | + for resolution_child in refinement_levels[i_level + 1 : i_level + depth_children + 1]: |
| 186 | + for child_idx in h3.h3_to_children(parent_idx, res=resolution_child): |
| 187 | + graph = add_edge( |
| 188 | + graph, |
| 189 | + h3.h3_to_center_child(parent_idx, refinement_levels[-1]), |
| 190 | + h3.h3_to_center_child(child_idx, refinement_levels[-1]), |
| 191 | + ) |
| 192 | + |
| 193 | + return graph |
| 194 | + |
| 195 | + |
| 196 | +def select_nodes_from_graph_at_resolution(graph: nx.Graph, resolution: int): |
| 197 | + parent_nodes = [node for node in graph.nodes if h3.h3_get_resolution(node) == resolution] |
| 198 | + return parent_nodes |
| 199 | + |
| 200 | + |
| 201 | +def add_edge( |
| 202 | + graph: nx.Graph, |
| 203 | + source_node_h3_idx: str, |
| 204 | + target_node_h3_idx: str, |
| 205 | +) -> nx.Graph: |
| 206 | + """Add edge between two nodes to a graph. |
| 207 | +
|
| 208 | + The edge will only be added in case both target and source nodes are included in the graph. |
| 209 | +
|
| 210 | + Parameters |
| 211 | + ---------- |
| 212 | + graph : networkx.Graph |
| 213 | + The graph to add the nodes. |
| 214 | + source_node_h3_idx : str |
| 215 | + The H3 index of the tail of the edge. |
| 216 | + target_node_h3_idx : str |
| 217 | + The H3 index of the head of the edge. |
| 218 | + """ |
| 219 | + if not graph.has_node(source_node_h3_idx) or not graph.has_node(target_node_h3_idx): |
| 220 | + return graph |
| 221 | + |
| 222 | + if source_node_h3_idx != target_node_h3_idx: |
| 223 | + source_location = np.deg2rad(h3.h3_to_geo(source_node_h3_idx)) |
| 224 | + target_location = np.deg2rad(h3.h3_to_geo(target_node_h3_idx)) |
| 225 | + graph.add_edge( |
| 226 | + source_node_h3_idx, target_node_h3_idx, weight=haversine_distances([source_location, target_location])[0][1] |
| 227 | + ) |
| 228 | + |
| 229 | + return graph |
0 commit comments