Network1d#
UGRID topology_dimension == 1 data: river networks and 1-D hydraulic models.
Nodes plus an edge-node connectivity table, with no faces — the core element is the edge, so
there are no centroids or areas. Network1d is a standalone sibling of Mesh2d, holding plain
numpy arrays with no GDAL objects and no dependency on Mesh2d.
It offers edge lengths and midpoints, bounds, nearest-neighbour KD-trees on both nodes and edge midpoints, and a topological sort for upstream/downstream ordering of a directed network.
In-memory only. There is no 1-D reader or writer yet: the UGRID topology parser recognises
topology_dimension == 2 meshes and UgridDataset carries a Mesh2d, so a network is built from
arrays rather than read from a file, and does not travel through read_file / to_file.
pyramids.netcdf.ugrid.network.Network1d
#
1D network mesh topology (nodes + edges, no faces).
Holds node coordinates and an edge-node connectivity table describing a 1D network such as a river tree or a 1D hydraulic model. Provides derived geometric properties (edge lengths, edge midpoints, bounds), nearest-neighbour KD-trees on nodes and edge midpoints, and a topological network sort for upstream/downstream ordering of a directed network.
This class does NOT inherit from Dataset or RasterBase and is independent
of :class:Mesh2d. The core element is the edge; there are no faces,
centroids, or areas.
Built from arrays, not read from a file: there is no 1-D reader or writer yet, and
:class:UgridDataset holds a :class:Mesh2d, so a network does not travel through
read_file / to_file.
Attributes:
| Name | Type | Description |
|---|---|---|
_node_x |
Node x-coordinates (n_node,), float64. |
|
_node_y |
Node y-coordinates (n_node,), float64. |
|
_edge_node_connectivity |
Edge-to-node connectivity (n_edge, 2). |
|
_directed |
Whether the edge orientation (from-node -> to-node) is meaningful. |
|
_crs |
Optional CRS object (pyproj.CRS) for the node coordinates. |
Source code in src/pyramids/netcdf/ugrid/network.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
node_x
property
#
Node x-coordinates array (n_node,).
node_y
property
#
Node y-coordinates array (n_node,).
edge_node_connectivity
property
#
Edge-to-node connectivity array (n_edge, 2).
directed
property
#
Whether edge orientation (from-node -> to-node) is meaningful.
crs
property
#
The coordinate reference system object, or None.
n_node
property
#
Number of nodes in the network.
n_edge
property
#
Number of edges in the network.
n_face
property
#
A 1D network has no faces; accessing this raises.
Raises:
| Type | Description |
|---|---|
AttributeError
|
Always — a 1D network has only nodes and edges. |
bounds
property
#
Network bounding box as (xmin, ymin, xmax, ymax).
Raises:
| Type | Description |
|---|---|
ValueError
|
The network has no nodes, so it has no extent. |
real_edge_indices
property
#
The positions of the rows that describe an actual edge.
This is the index map for :attr:edge_tree, which holds only the real edges: a tree
position p is edge real_edge_indices[p]. The two differ as soon as a padded row
is not last, so reading a tree index as an edge index would name the wrong edge.
Returns:
| Type | Description |
|---|---|
NDArray
|
np.ndarray: Edge positions, ascending. |
edge_lengths
property
#
Euclidean length of each edge, shape (n_edge,).
Lengths are computed from the node coordinates in the network's own coordinate units (no geodesic correction).
A padded row describes no edge, so its length is nan rather than 0.0 -- zero is
a plausible length for a real degenerate edge, and nothing downstream could tell the two
apart.
Returns:
| Type | Description |
|---|---|
NDArray
|
np.ndarray: One length per declared row, |
edge_midpoints
property
#
Midpoint coordinates of every edge as (mx, my), each shape (n_edge,).
A padded row has no midpoint, so both coordinates are nan -- it previously reported
the last node's position, a point the network really contains, which made the fabricated
value indistinguishable from a real one.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[NDArray, NDArray]
|
|
node_tree
property
#
Lazily built KD-tree on the node coordinates.
edge_tree
property
#
Lazily built KD-tree on the edge midpoint coordinates.
Built over the real edges only. A padded row has a nan midpoint and cKDTree
refuses non-finite data, so it cannot simply be included; nor may it be faked into a
finite point, which is the fabrication this guards against -- placed on the last node it
won every query near that node at distance 0.0.
A query therefore answers a position into this tree, which is not an edge index when
anything is padded. Map it through :attr:real_edge_indices.
Returns:
| Type | Description |
|---|---|
cKDTree
|
scipy.spatial.cKDTree: A tree over the real edges' midpoints. |
__init__(node_x, node_y, edge_node_connectivity, directed=False, crs=None)
#
Initialise a 1D network from node coordinates and edge connectivity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_x
|
ndarray
|
Node x-coordinates, shape (n_node,). |
required |
node_y
|
ndarray
|
Node y-coordinates, shape (n_node,). |
required |
edge_node_connectivity
|
Connectivity
|
Edge-to-node connectivity wrapper; its data is an (n_edge, 2) integer array of node indices. |
required |
directed
|
bool
|
When True, each edge's node order (from-node then to-node) encodes flow direction and the network sort uses it. Defaults to False. |
False
|
crs
|
Any
|
Optional coordinate reference system (a |
None
|
Source code in src/pyramids/netcdf/ugrid/network.py
read_file(path, mesh_name=None, driver=None, lazy=True)
classmethod
#
Read a 1D network from a file, through a registered mesh driver.
The 1D counterpart of :meth:UgridDataset.read_file. Several mesh formats carry
channel networks rather than a surface — 2DM's E2L line elements, FLO-2D's and
3Di's 1D domains — and a driver reading one returns a :class:Network1d, which
:meth:UgridDataset.read_file refuses by design rather than hand back as the wrong
type. This is where it is read instead.
There is no built-in route: pyramids' own UGRID reader builds a :class:Mesh2d
whatever topology dimension a file declares, so a network always comes from a driver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the file. |
required |
mesh_name
|
str | None
|
Which topology to read, for a file declaring more than one. |
None
|
driver
|
str | None
|
Name of a registered mesh driver to use, bypassing detection — the
name a plugin declares, not a GDAL driver short name. |
None
|
lazy
|
bool
|
Forwarded to the driver verbatim. :class: |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Network1d |
Network1d
|
The network the driver read. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
The file does not exist. |
DriverNotExistError
|
|
UnsupportedMeshFormatError
|
No installed driver reads the file, or the driver returned a surface mesh rather than a network. |
Source code in src/pyramids/netcdf/ugrid/network.py
get_edge_nodes(edge_idx)
#
Return the node indices of a single edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_idx
|
int
|
Edge index. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
1D array of the (two) node indices defining the edge. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Source code in src/pyramids/netcdf/ugrid/network.py
get_edge_coords(edge_idx)
#
Return start and end coordinates for an edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_idx
|
int
|
Edge index. |
required |
Returns:
| Type | Description |
|---|---|
tuple[NDArray, NDArray]
|
Tuple of (start_xy, end_xy) where each is a (2,) array. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|
Source code in src/pyramids/netcdf/ugrid/network.py
sort(direction='downstream')
#
Topologically order the nodes of a directed network.
Uses Kahn's algorithm on the directed edges (each edge runs from its
first node to its second). For "downstream" the result starts at the
source nodes (headwaters, in-degree 0) and ends at the sink nodes
(outlets); "upstream" reverses every edge first, so the result starts
at the outlets. Either way the result is a valid topological sort of the
edges as traversed: "downstream" puts an upstream node before the
downstream node it drains into, and "upstream" -- walking the reversed
edges -- puts the downstream node first. On a chain 0->1->2->3 that is
[0, 1, 2, 3] and [3, 2, 1, 0] respectively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
direction
|
str
|
|
'downstream'
|
Returns:
| Type | Description |
|---|---|
NDArray
|
1D integer array of node indices in traversal order (length n_node). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/pyramids/netcdf/ugrid/network.py
topology_subset(edge_indices)
#
Return a sub-network holding only the selected edges.
The surviving edges' nodes are deduplicated and renumbered to a compact
0..k-1 range, mirroring the 2D topology_subset contract. Isolated
nodes (not referenced by any surviving edge) are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_indices
|
ndarray
|
Indices of the edges to keep. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Network1d |
Network1d
|
A new network with renumbered nodes and the kept edges. |
Source code in src/pyramids/netcdf/ugrid/network.py
to_geodataframe(location='edge')
#
Convert the network to a GeoDataFrame.
For edges each row is a LineString; for nodes each row is a Point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
|
'edge'
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
geopandas.GeoDataFrame with a |
GeoDataFrame
|
the network has a CRS. For |
GeoDataFrame
|
edge-located variable stays aligned with the frame -- which means a padded row |
GeoDataFrame
|
(one the connectivity declares but leaves as fill) contributes an empty |
GeoDataFrame
|
LineString rather than being dropped or drawn as a degenerate line. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/pyramids/netcdf/ugrid/network.py
from_arrays(node_x, node_y, edge_node_connectivity, directed=False, epsg=None, crs=None)
classmethod
#
Create a Network1d programmatically from numpy arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_x
|
ndarray
|
Node x-coordinates, shape (n_node,). |
required |
node_y
|
ndarray
|
Node y-coordinates, shape (n_node,). |
required |
edge_node_connectivity
|
ndarray
|
(n_edge, 2) array of node indices per edge. |
required |
directed
|
bool
|
Whether edge orientation encodes flow direction. Defaults to False. |
False
|
epsg
|
int | None
|
Optional EPSG code to build the CRS from. Ignored when |
None
|
crs
|
Any
|
Optional CRS object ( |
None
|
Returns:
| Type | Description |
|---|---|
Network1d
|
Network1d instance. |