History of grit_rivers
The changes to the source code of this entry in the repository, newest first, each with the differences it made to the entry's files. They are also listed on GitLab.
[entries] Lay out the spatial networks with hfd_layout() instead of cfd_layout() cc3bcb0
Changed: none
diff --git a/entries/grit_rivers/__init__.py b/entries/grit_rivers/__init__.py
index dff0984..f69f710 100644
--- a/entries/grit_rivers/__init__.py
+++ b/entries/grit_rivers/__init__.py
@@ -265,6 +265,6 @@ def parse(alts=None):
yield alt, g
def get_pos(g, sbm_fit):
- # Spatial network: laid out with the default parameters of cfd_layout(),
+ # Spatial network: laid out with the default parameters of hfd_layout(),
# without the partition of the SBM fit
- return cfd_layout(g)
+ return hfd_layout(g)
[entries] Add the date when each entry was added, and remove icon_hash beff7c9
The dates are those of the commits that first added the entries' files (2020-08-11 for the entries of the initial commit); renamed entries keep the date of their first name. Changed: none
diff --git a/entries/grit_rivers/__init__.py b/entries/grit_rivers/__init__.py
index 8f32556..dff0984 100644
--- a/entries/grit_rivers/__init__.py
+++ b/entries/grit_rivers/__init__.py
@@ -71,7 +71,7 @@ citation = [('M. Wortmann, L. Slater, L. Hawker, Y. Liu, J. Neal, B. Zhang, J. S
'https://doi.org/10.1029/2024WR038308'),
('M. Wortmann, L. Slater, L. Hawker, Y. Liu, J. Neal, "Global River Topology (GRIT) vector datasets." Zenodo, version 1.0 (2025)',
'https://doi.org/10.5281/zenodo.17435232')]
-icon_hash = None
+date_added = '2026-10-31'
upstream_license = ("CC BY-NC 4.0", "https://creativecommons.org/licenses/by-nc/4.0/")
# version 1.0 of the vector data (record, not concept, id), in the Equal
[entries] Prefix the properties in the descriptions with (Node), (Edge) or (Graph) bc3dbbd
The lists of properties at the end of the descriptions marked edge properties with "Edge" but left node properties unmarked, or grouped them in "Nodes: `a`, ...; `b`, ..." items, so the kind of a property was often unclear. Every item now starts with (Node), (Edge) or (Graph), followed by the property names, a colon and the explanation, one item per property or group of properties; qualifications (kinds of nodes, networks) follow the names in parentheses. Properties that were only named got a short explanation, and a few items that named the wrong kind of property were corrected.
diff --git a/entries/grit_rivers/__init__.py b/entries/grit_rivers/__init__.py
index 0a15718..8f32556 100644
--- a/entries/grit_rivers/__init__.py
+++ b/entries/grit_rivers/__init__.py
@@ -35,35 +35,35 @@ In all networks, a directed edge goes from the upstream node of a line (segment
The `segments_<region>` networks have the following properties:
-- `global_id`: the id of the node.
-- `catchment_id`: the id of the catchment (the connected river system) of the node.
-- `node_type`: the kind of node (`inlet`, `confluence`, `bifurcation`, `coastal_outlet`, `sink_outlet`, `outlet`, `grwl_change`).
-- `grwl_value`: the code of the GRWL river mask at the node; `width`: the width of the river at the node according to GRWL, in m (30 or NaN outside the river mask); `grwl_transition`: the codes of the change of the mask at `grwl_change` nodes.
-- `cycle`: positive if the node was part of an unresolved cycle, 0 otherwise; `continuity_violated`: whether the flow continuity is violated at the node.
-- `drainage_area`: the drainage area of the node, partitioned by width at bifurcations, in km$^2$; `drainage_area_mainstem`: the drainage area following the main stem, in km$^2$.
-- `downstream_width_share`: the shares of the width of the downstream segments (comma-separated).
-- `n_bifurcations_upstream`: the number of bifurcations upstream of the node.
-- `domain`: the id of the group of catchments of the node.
-- `x`, `y`: the position of the node in the Equal Earth projection (EPSG:8857), in m; `latitude`, `longitude`: its position in WGS84.
-- Edge `global_id`, `catchment_id`, `domain`: the id of the segment, of its catchment and of its group of catchments.
-- Edge `direction_algorithm`: the code of the RivGraph method used to set the direction of the segment.
-- Edge `width_adjusted`, `length_adjusted`: the median width of the river and the length of the segment, in m, not counting the width of the segments connected upstream and downstream; `length`: the length of the segment, in m.
-- Edge `is_mainstem`: whether the segment is the widest one of a bifurcated flow, or has no bifurcation upstream.
-- Edge `strahler_order`: the Strahler order of the segment.
-- Edge `cycle`: as for the nodes (0 for all segments).
-- Edge `azimuth`: the direction from the upstream to the downstream node, in degrees from north; `sinuosity`: the ratio of the length of the segment to the distance between its end nodes (named `sinuousity` in the data).
-- Edge `drainage_area_in`, `drainage_area_out`: the drainage area at the beginning and the end of the segment, partitioned by width at bifurcations, in km$^2$; `drainage_area_mainstem_in`, `drainage_area_mainstem_out`: the same, following the main stem; `bifurcation_balance_out`: the ratio $(a_\text{out} - a_\text{mainstem,out}) / \max(a_\text{out}, a_\text{mainstem,out})$ of these areas.
-- Edge `grwl_overlap`: the fraction of the segment overlapping with the GRWL river mask; `grwl_value`: the dominant GRWL code of the segment.
-- Edge `name`, `name_local`: the name of the river from OpenStreetMap, in English and in the local language, where available.
-- Edge `n_bifurcations_upstream`: the number of bifurcations upstream of the segment.
+- (Node) `global_id`: the id of the node.
+- (Node) `catchment_id`: the id of the catchment (the connected river system) of the node.
+- (Node) `node_type`: the kind of node (`inlet`, `confluence`, `bifurcation`, `coastal_outlet`, `sink_outlet`, `outlet`, `grwl_change`).
+- (Node) `grwl_value`: the code of the GRWL river mask at the node; `width`: the width of the river at the node according to GRWL, in m (30 or NaN outside the river mask); `grwl_transition`: the codes of the change of the mask at `grwl_change` nodes.
+- (Node) `cycle`: positive if the node was part of an unresolved cycle, 0 otherwise; `continuity_violated`: whether the flow continuity is violated at the node.
+- (Node) `drainage_area`: the drainage area of the node, partitioned by width at bifurcations, in km$^2$; `drainage_area_mainstem`: the drainage area following the main stem, in km$^2$.
+- (Node) `downstream_width_share`: the shares of the width of the downstream segments (comma-separated).
+- (Node) `n_bifurcations_upstream`: the number of bifurcations upstream of the node.
+- (Node) `domain`: the id of the group of catchments of the node.
+- (Node) `x`, `y`: the position of the node in the Equal Earth projection (EPSG:8857), in m; `latitude`, `longitude`: its position in WGS84.
+- (Edge) `global_id`, `catchment_id`, `domain`: the id of the segment, of its catchment and of its group of catchments.
+- (Edge) `direction_algorithm`: the code of the RivGraph method used to set the direction of the segment.
+- (Edge) `width_adjusted`, `length_adjusted`: the median width of the river and the length of the segment, in m, not counting the width of the segments connected upstream and downstream; `length`: the length of the segment, in m.
+- (Edge) `is_mainstem`: whether the segment is the widest one of a bifurcated flow, or has no bifurcation upstream.
+- (Edge) `strahler_order`: the Strahler order of the segment.
+- (Edge) `cycle`: as for the nodes (0 for all segments).
+- (Edge) `azimuth`: the direction from the upstream to the downstream node, in degrees from north; `sinuosity`: the ratio of the length of the segment to the distance between its end nodes (named `sinuousity` in the data).
+- (Edge) `drainage_area_in`, `drainage_area_out`: the drainage area at the beginning and the end of the segment, partitioned by width at bifurcations, in km$^2$; `drainage_area_mainstem_in`, `drainage_area_mainstem_out`: the same, following the main stem; `bifurcation_balance_out`: the ratio $(a_\text{out} - a_\text{mainstem,out}) / \max(a_\text{out}, a_\text{mainstem,out})$ of these areas.
+- (Edge) `grwl_overlap`: the fraction of the segment overlapping with the GRWL river mask; `grwl_value`: the dominant GRWL code of the segment.
+- (Edge) `name`, `name_local`: the name of the river from OpenStreetMap, in English and in the local language, where available.
+- (Edge) `n_bifurcations_upstream`: the number of bifurcations upstream of the segment.
The `reaches_<region>` networks have the following properties:
-- `global_id`: the id of the node; `segment_node_id`: the id of the corresponding node of the segments, for the nodes at the ends of segments (-1 otherwise); `n_segments`: the number of segments attached to the node (NaN for the nodes inside segments); `domain`; `x`, `y`, `latitude`, `longitude`: as above.
-- Edge `global_id`, `segment_id`, `catchment_id`, `domain`: the id of the reach, of its segment, of its catchment and of its group of catchments.
-- Edge `grwl_overlap`, `grwl_value`: as above; `grwl_width_median`, `grwl_width_std`: the median and standard deviation of the width of the GRWL river mask, in m.
-- Edge `drainage_area_in`, `drainage_area_out`, `drainage_area_mainstem_in`, `drainage_area_mainstem_out`: as above (NaN where not given; in version 1.0 they are given only for part of the reaches of South America, about 41% of them, and missing elsewhere).
-- Edge `length` (m), `sinuosity` and `azimuth`: as above."""
+- (Node) `global_id`: the id of the node; `segment_node_id`: the id of the corresponding node of the segments, for the nodes at the ends of segments (-1 otherwise); `n_segments`: the number of segments attached to the node (NaN for the nodes inside segments); `domain`; `x`, `y`, `latitude`, `longitude`: as above.
+- (Edge) `global_id`, `segment_id`, `catchment_id`, `domain`: the id of the reach, of its segment, of its catchment and of its group of catchments.
+- (Edge) `grwl_overlap`, `grwl_value`: as above; `grwl_width_median`, `grwl_width_std`: the median and standard deviation of the width of the GRWL river mask, in m.
+- (Edge) `drainage_area_in`, `drainage_area_out`, `drainage_area_mainstem_in`, `drainage_area_mainstem_out`: as above (NaN where not given; in version 1.0 they are given only for part of the reaches of South America, about 41% of them, and missing elsewhere).
+- (Edge) `length` (m), `sinuosity` and `azimuth`: as above."""
tags = ['Hydrology', 'Spatial', 'Multigraph', 'Metadata']
url = ['https://doi.org/10.5281/zenodo.17435232',
'https://michelwortmann.com/research/global-river-topology-grit/']
[entries] New entry: grit_rivers 657a592
diff --git a/entries/grit_rivers/__init__.py b/entries/grit_rivers/__init__.py
new file mode 100644
index 0000000..0a15718
--- /dev/null
+++ b/entries/grit_rivers/__init__.py
@@ -0,0 +1,270 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2026 Tiago de Paula Peixoto <tiago@skewed.de>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from .. import *
+import shutil
+import sqlite3
+import struct
+import sys
+from array import array
+
+title = "Global river networks with bifurcations (GRIT)"
+description = r"""The rivers of the world (except Antarctica and Greenland) as represented by the Global River Topology (GRIT) version 1.0 of Wortmann et al., a river network derived at 30 m resolution from the Landsat-based river mask of the Global River Widths from Landsat (GRWL) data, merged with streams derived from the FABDEM elevation model (for the rivers narrower than about 30 m), and in which the direction of flow of each line was then assigned by a combination of methods (elevation, flow angle, heuristics and continuity, based on RivGraph). Unlike most global river networks, which are trees, GRIT represents also the distributary parts of the river network, such as multi-thread (braided and anastomosing) rivers, canals and delta distributaries, where a river splits into several channels (bifurcations), so that the network has undirected cycles. It includes the rivers with a drainage area of at least 50 km$^2$.
+
+The data are given at two levels of detail, for seven regions (`af` Africa, `as` Asia without Siberia, `eu` Europe, `na` North America, `sa` South America, `si` Siberia, `sp` South Pacific and Australia), each a network:
+
+- `segments_<region>` (e.g. `segments_eu`): nodes are the points where rivers start (inlets), end (coastal outlets and inland sinks), join (confluences) or split (bifurcations), and points where the river mask of GRWL changes; edges are the river segments between these points.
+- `reaches_<region>` (e.g. `reaches_eu`): the same rivers, with the segments split into reaches of at most 1 km (and at least 500 m, unless the segment is shorter); nodes are the end points of the reaches.
+
+In all networks, a directed edge goes from the upstream node of a line (segment or reach) to its downstream node, following the direction of flow; the networks can have parallel edges (e.g. two channels between the same bifurcation and confluence). The geometries of the lines, the catchments and the simplified global segments (a subset of the segments) of the data are not included, and neither are the lists of the ids of the lines connected upstream and downstream of each line (which are given by the network). The data use the Equal Earth projection (EPSG:8857); the coordinates of the nodes are given also as latitude and longitude, converted with the inverse of the projection.
+
+The `segments_<region>` networks have the following properties:
+
+- `global_id`: the id of the node.
+- `catchment_id`: the id of the catchment (the connected river system) of the node.
+- `node_type`: the kind of node (`inlet`, `confluence`, `bifurcation`, `coastal_outlet`, `sink_outlet`, `outlet`, `grwl_change`).
+- `grwl_value`: the code of the GRWL river mask at the node; `width`: the width of the river at the node according to GRWL, in m (30 or NaN outside the river mask); `grwl_transition`: the codes of the change of the mask at `grwl_change` nodes.
+- `cycle`: positive if the node was part of an unresolved cycle, 0 otherwise; `continuity_violated`: whether the flow continuity is violated at the node.
+- `drainage_area`: the drainage area of the node, partitioned by width at bifurcations, in km$^2$; `drainage_area_mainstem`: the drainage area following the main stem, in km$^2$.
+- `downstream_width_share`: the shares of the width of the downstream segments (comma-separated).
+- `n_bifurcations_upstream`: the number of bifurcations upstream of the node.
+- `domain`: the id of the group of catchments of the node.
+- `x`, `y`: the position of the node in the Equal Earth projection (EPSG:8857), in m; `latitude`, `longitude`: its position in WGS84.
+- Edge `global_id`, `catchment_id`, `domain`: the id of the segment, of its catchment and of its group of catchments.
+- Edge `direction_algorithm`: the code of the RivGraph method used to set the direction of the segment.
+- Edge `width_adjusted`, `length_adjusted`: the median width of the river and the length of the segment, in m, not counting the width of the segments connected upstream and downstream; `length`: the length of the segment, in m.
+- Edge `is_mainstem`: whether the segment is the widest one of a bifurcated flow, or has no bifurcation upstream.
+- Edge `strahler_order`: the Strahler order of the segment.
+- Edge `cycle`: as for the nodes (0 for all segments).
+- Edge `azimuth`: the direction from the upstream to the downstream node, in degrees from north; `sinuosity`: the ratio of the length of the segment to the distance between its end nodes (named `sinuousity` in the data).
+- Edge `drainage_area_in`, `drainage_area_out`: the drainage area at the beginning and the end of the segment, partitioned by width at bifurcations, in km$^2$; `drainage_area_mainstem_in`, `drainage_area_mainstem_out`: the same, following the main stem; `bifurcation_balance_out`: the ratio $(a_\text{out} - a_\text{mainstem,out}) / \max(a_\text{out}, a_\text{mainstem,out})$ of these areas.
+- Edge `grwl_overlap`: the fraction of the segment overlapping with the GRWL river mask; `grwl_value`: the dominant GRWL code of the segment.
+- Edge `name`, `name_local`: the name of the river from OpenStreetMap, in English and in the local language, where available.
+- Edge `n_bifurcations_upstream`: the number of bifurcations upstream of the segment.
+
+The `reaches_<region>` networks have the following properties:
+
+- `global_id`: the id of the node; `segment_node_id`: the id of the corresponding node of the segments, for the nodes at the ends of segments (-1 otherwise); `n_segments`: the number of segments attached to the node (NaN for the nodes inside segments); `domain`; `x`, `y`, `latitude`, `longitude`: as above.
+- Edge `global_id`, `segment_id`, `catchment_id`, `domain`: the id of the reach, of its segment, of its catchment and of its group of catchments.
+- Edge `grwl_overlap`, `grwl_value`: as above; `grwl_width_median`, `grwl_width_std`: the median and standard deviation of the width of the GRWL river mask, in m.
+- Edge `drainage_area_in`, `drainage_area_out`, `drainage_area_mainstem_in`, `drainage_area_mainstem_out`: as above (NaN where not given; in version 1.0 they are given only for part of the reaches of South America, about 41% of them, and missing elsewhere).
+- Edge `length` (m), `sinuosity` and `azimuth`: as above."""
+tags = ['Hydrology', 'Spatial', 'Multigraph', 'Metadata']
+url = ['https://doi.org/10.5281/zenodo.17435232',
+ 'https://michelwortmann.com/research/global-river-topology-grit/']
+citation = [('M. Wortmann, L. Slater, L. Hawker, Y. Liu, J. Neal, B. Zhang, J. Schwenk, G. Allen, P. Ashworth, R. Boothroyd, H. Cloke, P. Delorme, S. H. Gebrechorkos, H. Griffith, J. Leyland, S. McLelland, A. P. Nicholas, G. Sambrook-Smith, E. Vahidi, D. Parsons, S. E. Darby, "Global River Topology (GRIT): A Bifurcating River Hydrography." Water Resources Research 61, e2024WR038308 (2025)',
+ 'https://doi.org/10.1029/2024WR038308'),
+ ('M. Wortmann, L. Slater, L. Hawker, Y. Liu, J. Neal, "Global River Topology (GRIT) vector datasets." Zenodo, version 1.0 (2025)',
+ 'https://doi.org/10.5281/zenodo.17435232')]
+icon_hash = None
+upstream_license = ("CC BY-NC 4.0", "https://creativecommons.org/licenses/by-nc/4.0/")
+
+# version 1.0 of the vector data (record, not concept, id), in the Equal
+# Earth projection (the files in WGS84 are larger)
+upstream_prefix = 'https://zenodo.org/records/17435232/files'
+regions = ["af", "as", "eu", "na", "sa", "si", "sp"]
+
+def _fname(level, region):
+ return f"GRITv1.0_{level}_{region.upper()}_EPSG8857.gpkg.zip"
+
+files = [((_fname(l, r),), f"{l}_{r}", None)
+ for l in ["segments", "reaches"] for r in regions]
+
+def fetch_upstream(force=False):
+ return fetch_upstream_files(__name__.split(".")[-1], upstream_prefix, files,
+ force)
+
+def gpkg_point(blob):
+ "The (x, y) coordinates of a GeoPackage point geometry."
+ flags = blob[3]
+ env = (0, 32, 48, 48, 64)[(flags >> 1) & 7]
+ wkb = blob[8 + env:]
+ order = "<" if wkb[0] == 1 else ">"
+ return struct.unpack(order + "dd", wkb[5:21])
+
+def equal_earth_to_wgs84(x, y):
+ """Latitude and longitude (WGS84) of coordinates in the Equal Earth
+ projection on the WGS84 ellipsoid (EPSG:8857): inverse of the projection
+ of the authalic latitude, followed by the conversion of the authalic to
+ the geodetic latitude (accurate to 1e-11 degrees)."""
+ x = numpy.asarray(x, dtype=float)
+ y = numpy.asarray(y, dtype=float)
+ a, f = 6378137.0, 1 / 298.257223563
+ e2 = f * (2 - f)
+ e = numpy.sqrt(e2)
+ qp = (1 - e2) * (1 / (1 - e2) - numpy.log((1 - e) / (1 + e)) / (2 * e))
+ R = a * numpy.sqrt(qp / 2) # authalic radius
+ A1, A2, A3, A4 = 1.340264, -0.081106, 0.000893, 0.003796
+ M = numpy.sqrt(3) / 2
+ yn = y / R
+ th = yn.copy()
+ for i in range(20):
+ th2 = th ** 2
+ th6 = th2 ** 3
+ fy = th * (A1 + A2 * th2 + th6 * (A3 + A4 * th2)) - yn
+ fpy = A1 + 3 * A2 * th2 + th6 * (7 * A3 + 9 * A4 * th2)
+ th = th - fy / fpy
+ th2 = th ** 2
+ th6 = th2 ** 3
+ lam = M * (x / R) * (A1 + 3 * A2 * th2 + th6 * (7 * A3 + 9 * A4 * th2)) / numpy.cos(th)
+ beta = numpy.arcsin(numpy.clip(numpy.sin(th) / M, -1, 1))
+ qb = qp * numpy.sin(beta)
+ phi = beta.copy()
+ for i in range(10):
+ s = numpy.sin(phi)
+ phi = phi + (1 - e2 * s ** 2) ** 2 / (2 * numpy.cos(phi)) * \
+ (qb / (1 - e2) - s / (1 - e2 * s ** 2) +
+ numpy.log((1 - e * s) / (1 + e * s)) / (2 * e))
+ return numpy.degrees(phi), numpy.degrees(lam)
+
+# The attributes kept, as (column, name, type); "float" columns may be given
+# as strings in the data
+NODE_COLS = {
+ "segments": [("global_id", "global_id", "int"),
+ ("catchment_id", "catchment_id", "int"),
+ ("node_type", "node_type", "string"),
+ ("grwl_value", "grwl_value", "int"),
+ ("width", "width", "float"),
+ ("grwl_transition", "grwl_transition", "string"),
+ ("cycle", "cycle", "int"),
+ ("continuity_violated", "continuity_violated", "bool"),
+ ("drainage_area", "drainage_area", "float"),
+ ("drainage_area_mainstem", "drainage_area_mainstem", "float"),
+ ("downstream_width_share", "downstream_width_share", "string"),
+ ("n_bifurcations_upstream", "n_bifurcations_upstream", "int"),
+ ("domain", "domain", "string")],
+ "reaches": [("global_id", "global_id", "int"),
+ ("segment_node_id", "segment_node_id", "int"),
+ ("n_segments", "n_segments", "float"),
+ ("domain", "domain", "string")]}
+EDGE_COLS = {
+ "segments": [("global_id", "global_id", "int"),
+ ("catchment_id", "catchment_id", "int"),
+ ("direction_algorithm", "direction_algorithm", "float"),
+ ("width_adjusted", "width_adjusted", "float"),
+ ("length_adjusted", "length_adjusted", "float"),
+ ("is_mainstem", "is_mainstem", "bool"),
+ ("strahler_order", "strahler_order", "int"),
+ ("cycle", "cycle", "int"),
+ ("length", "length", "float"),
+ ("azimuth", "azimuth", "float"),
+ ("sinuousity", "sinuosity", "float"),
+ ("drainage_area_in", "drainage_area_in", "float"),
+ ("drainage_area_out", "drainage_area_out", "float"),
+ ("drainage_area_mainstem_in", "drainage_area_mainstem_in", "float"),
+ ("drainage_area_mainstem_out", "drainage_area_mainstem_out", "float"),
+ ("bifurcation_balance_out", "bifurcation_balance_out", "float"),
+ ("grwl_overlap", "grwl_overlap", "float"),
+ ("grwl_value", "grwl_value", "int"),
+ ("name", "name", "string"),
+ ("name_local", "name_local", "string"),
+ ("n_bifurcations_upstream", "n_bifurcations_upstream", "int"),
+ ("domain", "domain", "string")],
+ "reaches": [("global_id", "global_id", "int"),
+ ("segment_id", "segment_id", "int"),
+ ("catchment_id", "catchment_id", "int"),
+ ("grwl_overlap", "grwl_overlap", "float"),
+ ("grwl_value", "grwl_value", "int"),
+ ("grwl_width_median", "grwl_width_median", "float"),
+ ("grwl_width_std", "grwl_width_std", "float"),
+ ("drainage_area_in", "drainage_area_in", "float"),
+ ("drainage_area_out", "drainage_area_out", "float"),
+ ("drainage_area_mainstem_in", "drainage_area_mainstem_in", "float"),
+ ("drainage_area_mainstem_out", "drainage_area_mainstem_out", "float"),
+ ("length", "length", "float"),
+ ("sinuosity", "sinuosity", "float"),
+ ("azimuth", "azimuth", "float"),
+ ("domain", "domain", "string")]}
+
+def _store():
+ "Compact column stores for the attributes of each type."
+ return {"int": lambda: array("q"), "float": lambda: array("d"),
+ "bool": lambda: array("b"), "string": list}
+
+def _convert(v, t):
+ if t == "string":
+ return sys.intern(v) if v is not None else ""
+ if v is None or v == "":
+ return -1 if t == "int" else numpy.nan if t == "float" else 0
+ if t == "int":
+ return int(v)
+ if t == "float":
+ return float(v)
+ return 1 if v else 0
+
+def _read_table(con, table, cols, extra):
+ """Read the columns `cols` of a table (ordered by fid), plus the columns
+ `extra`, returned as lists."""
+ st = _store()
+ data = {name: st[t]() for c, name, t in cols}
+ ext = [[] for c in extra]
+ q = ", ".join([f'"{c}"' for c, n, t in cols] + list(extra))
+ for r in con.execute(f"SELECT {q} FROM {table} ORDER BY fid"):
+ for (c, name, t), v in zip(cols, r):
+ data[name].append(_convert(v, t))
+ for l, v in zip(ext, r[len(cols):]):
+ l.append(v)
+ return data, ext
+
+TYPES = {"int": "int64_t", "float": "double", "bool": "bool", "string": "string"}
+
+@cache_network()
+@coerce_props()
+@annotate()
+def parse(alts=None):
+ name = __name__.split(".")[-1]
+ for fnames, alt, fmt in files:
+ if alts is not None and alt not in alts:
+ continue
+ level = alt.split("_")[0]
+ member = fnames[0][:-len(".zip")]
+ os.makedirs("cache/temp", exist_ok=True)
+ with tempfile.NamedTemporaryFile(dir="cache/temp", suffix=".gpkg") as tmp:
+ with open_upstream_file(name, f"{fnames[0]}:{member}", "rb") as f:
+ shutil.copyfileobj(f, tmp, 1 << 24)
+ tmp.flush()
+ con = sqlite3.connect(tmp.name)
+ vdata, (geoms,) = _read_table(con, "nodes", NODE_COLS[level], ["geom"])
+ xy = numpy.array([gpkg_point(b) for b in geoms])
+ del geoms
+ idx = {nid: i for i, nid in enumerate(vdata["global_id"])}
+ if len(idx) != len(vdata["global_id"]):
+ raise ValueError(f"{alt}: duplicate node ids")
+ edata, (up, down) = _read_table(con, "lines", EDGE_COLS[level],
+ ["upstream_node_id", "downstream_node_id"])
+ con.close()
+ g = Graph(directed=True)
+ g.add_vertex(len(idx))
+ g.add_edge_list(numpy.column_stack((numpy.fromiter((idx[u] for u in up), "int64", len(up)),
+ numpy.fromiter((idx[v] for v in down), "int64", len(down)))))
+ del up, down, idx
+ for (c, n, t) in NODE_COLS[level]:
+ g.vp[n] = g.new_vp(TYPES[t], vals=vdata.pop(n))
+ g.vp.x = g.new_vp("double", vals=xy[:, 0])
+ g.vp.y = g.new_vp("double", vals=xy[:, 1])
+ lat, lon = equal_earth_to_wgs84(xy[:, 0], xy[:, 1])
+ g.vp.latitude = g.new_vp("double", vals=lat)
+ g.vp.longitude = g.new_vp("double", vals=lon)
+ for (c, n, t) in EDGE_COLS[level]:
+ g.ep[n] = g.new_ep(TYPES[t], vals=edata.pop(n))
+ yield alt, g
+
+def get_pos(g, sbm_fit):
+ # Spatial network: laid out with the default parameters of cfd_layout(),
+ # without the partition of the SBM fit
+ return cfd_layout(g)