跳转到主要内容

地表水网络

项目描述

地表水网络

DOI Codacy Codcov CI

一个Python包,用于创建和分析地表水网络。

Python包

需要Python 3.8+。

必需

  • geopandas >=0.9 - 类似于pandas处理空间数据
  • packaging - 用于检查包版本
  • pandas >=1.2 - 表格数据分析
  • pyproj >=2.2 - 空间投影支持
  • rtree - 空间索引支持

可选

  • flopy >=3.3.6 - 读取/写入MODFLOW模型
  • netCDF4 - 用于读取TopNet文件

测试

运行 pytest -vpython3 -m pytest -v

对于更快的多核 pytest -v -n 2(使用 pytest-xdist

运行doctests pytest -v swn --doctest-modules

示例

import geopandas
import pandas as pd
import swn

从Shapefile读取

shp_srs = 'tests/data/DN2_Coastal_strahler1z_stream_vf.shp'
lines = geopandas.read_file(shp_srs)
lines.set_index('nzsegment', inplace=True, verify_integrity=True)  # optional

或者,从PostGIS读取

from sqlalchemy import create_engine, engine

con_url = engine.url.URL(drivername='postgresql', database='scigen')
con = create_engine(con_url)
sql = 'SELECT * FROM wrc.rec2_riverlines_coastal'
lines = geopandas.read_postgis(sql, con)
lines.set_index('nzsegment', inplace=True, verify_integrity=True)  # optional

初始化并创建网络

n = swn.SurfaceWaterNetwork.from_lines(lines.geometry)
print(n)
# <SurfaceWaterNetwork: with Z coordinates
#   304 segments: [3046409, 3046455, ..., 3050338, 3050418]
#   154 headwater: [3046409, 3046542, ..., 3050338, 3050418]
#   3 outlets: [3046700, 3046737, 3046736]
#   no diversions />

绘制网络,写入Shapefile,写入和读取SurfaceWaterNetwork文件

n.plot()

swn.file.gdf_to_shapefile(n.segments, 'segments.shp')

n.to_pickle('network.pkl')
n = swn.SurfaceWaterNetwork.from_pickle('network.pkl')

删除满足条件(河流顺序)的段,或者从特定位置上游/下游的段

n.remove(
    n.segments.stream_order == 1,
    segnums=n.gather_segnums(upstream=3047927))

从TopNet netCDF文件读取流量数据,从m3/s转换为m3/day

nc_path = 'tests/data/streamq_20170115_20170128_topnet_03046727_strahler1.nc'
flow = swn.file.topnet2ts(nc_path, 'mod_flow', 86400)
# remove time and truncate to closest day
flow.index = flow.index.floor('d')

# 7-day mean
flow7d = flow.resample('7D').mean()

# full mean
flow_m = pd.DataFrame(flow.mean(0)).T

处理MODFLOW/flopy模型

import flopy

m = flopy.modflow.Modflow.load('h.nam', model_ws='tests/data', check=False)
nm = swn.SwnModflow.from_swn_flopy(n, m)
nm.default_segment_data()
nm.set_segment_data_inflow(flow_m)
nm.plot()
nm.to_pickle('sfr_network.pkl')
nm = swn.SwnModflow.from_pickle('sfr_network.pkl', n, m)
nm.set_sfr_obj()
m.sfr.write_file('file.sfr')
nm.grid_cells.to_file('grid_cells.shp')
nm.reaches.to_file('reaches.shp')

引用

托伊,M. W.;赫明斯,B. 2019. 一种通用水系网络方法:河流泛化与快速地下水模型开发。载于:新西兰水文学会会议,罗托鲁阿,2019年12月3-6日。第166-169页。