Updated Polygon-to-polygon weight calculation.#
This notebook demonstrates generating weights, or creating a cross-walk between a source GeoDataFrame and a target GeoDataFrame. The example here demonstrates calculating the weights from HUC-12 to HUC-8 polygons encompassing the Delaware River Basin.
Open Source Python Tools#
Data#
pynhd pynhd provides access to several hydro-linked datasets including those used here: WaterData and NLDI. We use the NLDI to get the basin geometry upstream of USGS gage Delaware River ad Del Mem Bridge at Wilmington De (01482100). Using the geometry of that basin we can extract a set up HUC12 and HUC08 basins for demonstration purposes.
Maintenance Schedule#
This notebook is part of the gdptools demo’s and will be updated with new versions of gdptools.
Description#
A common workflow in geospatial hydrology is to interpolate geospatial values from one set of polygons to another. For example, watershed characteristics associated with HUC12’s could be interpolated to HUC08’s by using an area-weighted statistic. This notebook demonstrates a function of gdptools for calculating the weights or cross-walk for calculating that interpolation.
Getting Started#
Directions for creating an environment to run this notebook.
Requirements:#
Conda-based package manager such as miniconda or mambaforge.
Create conda/mamba environment.#
Download environment-examples.yml
To create the conda environment follow the steps below.
conda env create -f environment-examples.yml
conda activate gdptools-examples
jupyter lab
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import numpy as np
import xarray as xr
import pyproj
from pynhd import NLDI
from pynhd import WaterData
import datashader as ds
import holoviews as hv
from bokeh.io import export_svgs
import hvplot.xarray
import hvplot.pandas
import hvplot.dask
from gdptools import WeightGenP2P
Generate example data using pynhd functions#
# USGS gage 01482100 Delaware River at Del Mem Bridge at Wilmington De
gage_id = "01482100"
nldi = NLDI()
del_basins = nldi.get_basins(gage_id)
huc08_basins = WaterData("huc08").bygeom(del_basins.geometry[0])
# huc12_basins = WaterData("huc12").bybox(tuple(huc08_basins.geometry.total_bounds))
huc12_basins = WaterData("huc12").bybox(tuple(huc08_basins.geometry.total_bounds), sort_attr="huc12")
huc08_basins.head()
geometry | id | ogc_fid | tnmid | metasource | sourcedata | sourceorig | sourcefeat | loaddate | gnis_id | areaacres | areasqkm | states | huc8 | name | shape_leng | shape_area | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | MULTIPOLYGON (((-75.49355 39.71630, -75.49391 ... | huc08.1010 | 1010 | {C1CD6D79-BA33-487E-B58F-12DC868EB079} | None | None | None | None | 2012-06-11 | None | 265273 | 1074.35565 | DE,NJ | 02040204 | Delaware Bay | 3.317813 | 0.111800 |
1 | MULTIPOLYGON (((-75.79380 40.14290, -75.79306 ... | huc08.1011 | 1011 | {D93EE135-0A9B-424D-A41D-C5F4B2C90E92} | None | None | None | None | 2012-06-11 | None | 483688 | 1958.93640 | DE,PA | 02040205 | Brandywine-Christina | 3.253687 | 0.205834 |
2 | MULTIPOLYGON (((-75.91020 39.87222, -75.90936 ... | huc08.1849 | 1849 | {7C5D25BB-7EAA-4338-9822-67D67CD0C6A0} | None | None | None | None | 2012-06-11 | None | 833419 | 3375.34695 | DE,MD | 02060002 | Chester-Sassafras | 4.338178 | 0.351979 |
3 | MULTIPOLYGON (((-74.16751 41.36033, -74.16725 ... | huc08.220 | 220 | {7A003D74-F050-4EEC-8802-8EECFBCC10ED} | None | None | None | None | 2012-06-11 | None | 725668 | 2938.95540 | NJ | 02030103 | Hackensack-Passaic | 3.758885 | 0.314142 |
4 | MULTIPOLYGON (((-74.62983 40.91488, -74.63058 ... | huc08.222 | 222 | {F8C2024F-B0F1-4536-97D4-63A669FA7780} | None | None | None | None | 2012-06-11 | None | 707448 | 2865.16440 | NJ | 02030105 | Raritan | 3.877156 | 0.304252 |
huc12_basins.head()
geometry | huc12 | tohuc | areaacres | areasqkm | name | hutype | humod | states | noncontrib | |
---|---|---|---|---|---|---|---|---|---|---|
0 | MULTIPOLYGON (((-73.20155 42.58472, -73.20482 ... | 011000050101 | 011000050104 | 38661.113837 | 156.455977 | West Branch Housatonic River | S | NM | MA | 0 |
1 | MULTIPOLYGON (((-73.15862 42.38487, -73.15382 ... | 011000050104 | 011000050105 | 26200.586480 | 106.030012 | Sackett Brook-Housatonic River | S | DM | MA | 0 |
2 | MULTIPOLYGON (((-73.19828 42.37134, -73.19586 ... | 011000050105 | 011000050107 | 23209.280937 | 93.924628 | Goose Pond Brook-Housatonic River | S | DM | MA | 0 |
3 | MULTIPOLYGON (((-73.21295 42.27217, -73.21216 ... | 011000050106 | 011000050107 | 14218.847720 | 57.541635 | Hop Brook | S | NM | MA | 0 |
4 | MULTIPOLYGON (((-73.27977 42.35277, -73.28102 ... | 011000050107 | 011000050204 | 33910.433430 | 137.230655 | Konkapot Brook-Housatonic River | S | DM | MA | 0 |
Plot basins#
from holoviews.element.tiles import EsriTerrain
drb_12 = huc12_basins.hvplot(
geo=True, coastline='50m', alpha=0.2, c='r', frame_width=300,
xlabel="longitude", ylabel="latitude",
title="Delaware River HUC12 basins", xlim=(-78.0, -73.0), aspect='equal'
)
drb_08 = huc08_basins.hvplot(
geo=True, coastline='50m', alpha=0.2, c='r', frame_width=300,
xlabel="longitude", ylabel="latitude",
title="Delaware River HUC08 basins", xlim=(-78.0, -73.0), aspect='equal'
)
EsriTerrain() * drb_12 + EsriTerrain() * drb_08