Network generation¶
The reservoir network represents the connectivity of the reservoirs in a basin. It is represented by a directed graph, where each node represents a reservoir and the edges represent the direction of flow of water between them.
Let us first visualize the flow direction file and the reservoir locations.
import rioxarray as rxr
import xarray as xr
import numpy as np
import geopandas as gpd
import cartopy.crs as ccrs
import cartopy
import matplotlib.pyplot as plt
from pathlib import Path
import holoviews as hv
import geoviews as gv
import hvplot.xarray
import hvplot.pandas
import networkx as nx
import hvplot.networkx as hvnx
gv.extension('bokeh')
Let's visualize the reservoirs and the flow direction file.
# read in the flow direction file
flow_dir_fn = "../../data/cumberland/flow-directions.tif"
res_location_fn = "../../data/cumberland/cumberland-stations.csv"
save_dir = "../../data/cumberland/regulation_data"
fdr = rxr.open_rasterio(flow_dir_fn, band_as_variable=True, masked=True)
reservoirs = gpd.read_file(res_location_fn)
reservoirs['geometry'] = gpd.points_from_xy(reservoirs['lon'], reservoirs['lat'])
reservoirs.set_crs('epsg:4326', inplace=True)
gv.tile_sources.OSM() \
* fdr.hvplot(x='x', y='y', geo=True, cmap='colorwheel') \
* reservoirs.hvplot(geo=True, hover_cols=['lat', 'lon', 'name']).opts(marker='triangle', fill_color='white', line_color='black', size=15, line_width=3)
Here, the triangles denote the location of the reservoirs and the colored raster denotes the flow direction of each cell in the flow direction file. The values correspond to the direction of flow of water as follows - 1: N, 2: NE, 3: E, 4: SE, 5: S, 6: SW, 7: W and 8: NW.
We will be using the generate_network function in the resorr.data_prep module to generate the reservoir network. The function returns the reservoir network as a geonetworkx.GeoDiGraph object, but can also save the network as shp files if the save_dir arguemnt is passed.
from resorr.data_prep import generate_network
G = generate_network(
flow_dir_fn, res_location_fn, save_dir
)
G
<geonetworkx.geodigraph.GeoDiGraph at 0x15e4cefd0>
nx.draw(G)