Implementing Binary Dasymetric Interpolation

Following an NHGIS-like Methodology

import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np

from tobler.area_weighted import area_interpolate
from tobler.dasymetric import extract_raster_features
from tobler.util import h3fy

%load_ext watermark
%watermark -a 'eli knaap' -v -d -u -p tobler,geopandas,rasterio
Author: eli knaap

Last updated: 2026-11-18

Python implementation: CPython
Python version       : 3.14.10
IPython version      : 9.18.1

tobler   : 0.14.1
geopandas: 1.2.0
rasterio : 1.5.2

Note: This notebook relies on functionality from the contextily package that provides convenient basemaps for geospatial plots, the osmnx package that provides access to OpenStreetMap data, and the pytidycensus package that provides an interface to the U.S. Census API. Set the CENSUS_API_KEY environment variable to your Census API key before running the data-fetching cells. Install the packages with

pip install contextily osmnx pytidycensus
or
conda install contextily osmnx -c conda-forge followed by pip install pytidycensus

import osmnx as ox

ox.settings.use_cache = True
ox.settings.cache_folder = "./cache"

NHGIS uses a two step process, in which they first apply a binary dasymetric filter, then apply target density weighting. Here we will follow a similar strategy, relplicating their BD filtering (albeit with slightly different open data and replicable, open source tooling) before carrying out a simpler areal interpolation.

In NHGIS’s BD model for 2000 block data, the inhabited zone consists of all areas that are at least 5% developed impervious surface (within each 30-meter square cell of NLCD 2001 data) and lie within 300 feet of a residential road center line2 but not in a water body, using road definitions and water polygons from the 2010 TIGER/Line Shapefiles.

so we’ll start by grabbing residential streets from OSM, buffering them out 300 feet, then using that geometry to select the impervious surfaces within

rside_streets = ox.features_from_place(
    "Riverside County, California", tags={"highway": "residential"}
)
rside_streets = rside_streets.to_crs(2230)
rside_streets = rside_streets.set_geometry(rside_streets.buffer(300))

Now we use the street buffers to extract any cells between 5 and 100% impervious surface

First, download NLCD data from here: https://s3-us-west-2.amazonaws.com/mrlc/NLCD_2016_Impervious_L48_20190405.zip, then unzip the archive and pass the path to the img file to tobler’s extract_raster_features function

nlcd_path = "s3://spatial-ucr/nlcd/impervious/nlcd_impervious_2016.tif"
import rasterio

with rasterio.Env(AWS_NO_SIGN_REQUEST="YES"):
    urban_rside = extract_raster_features(
        rside_streets, nlcd_path, pixel_values=list(range(5, 101)), collapse_values=True
    )
urban_rside = urban_rside.to_crs(2230)
fig, ax = plt.subplots(figsize=(20, 14))
urban_rside.plot(ax=ax);
../_images/7f1b5ea2f0c0e44c282ac65f2313e62a63f3ad7d9dff583675e1fe10a9840afd.png

This geodataframe represents the areas that meet both criteria NHGIS suggests

Now we need some census data to transfer geometries. Let’s use tract-level median home values. The cell below requires an API key, so we read the result from the cache. If you’d like to replicate this analysis for another area, use pytidycensus as indicated below.

rside_census = gpd.read_file("cache/rside_census.gpkg")

And lets generate some synthetic zones to transfer this population data into using h3 hexagons

rside_hex = h3fy(rside_census, resolution=8)
rside_hex.plot();
../_images/df8983e33f76f5d4f628d5270df8835f11e158669082054c94d582c90a982250.png

Ok, now all that’s left is to clip our input census data, “restricting” it to the locations NLCD defines as 5-100% impervious. To do so, we just need to clip the census data using our extracted urban features as a mask

dasy = rside_census.clip(urban_rside)

Clipping census geometries by the urban features leaves the census attributes intact but reshapes them. If a census tract had 2000 people in it and covered 1 sqmi, but only a quarter mile of the tract was urbanized, the new geodataframe effectively shows these 2000 people occupying the quarter mile. By passing these new data to the areal_interpolate function, we’re still assuming that population density is constant across our new geometries (a condition that may not be true in reality) but that assumption is much more plausible than when using original census geometries.

dasy.plot();
../_images/73e44799ce3f8d94ce9ba3380865e0221c55b847c201bd7b9b26f87b55a13aa7.png
dasy.B25077_001E = dasy.B25077_001E.replace("<NA>", None).astype(float)
dasy.B25077_001E
350    528600.0
161    443700.0
327    368600.0
384    454700.0
54     351500.0
         ...   
33     336200.0
34     251200.0
117    251200.0
231    263800.0
226    305600.0
Name: B25077_001E, Length: 453, dtype: float64
interp = area_interpolate(dasy, rside_hex, extensive_variables=["B25077_001E"])
/home/runner/micromamba/envs/test/lib/python3.14/site-packages/tobler/area_weighted/area_interpolate.py:331: UserWarning: nan values in variable: B25077_001E, replacing with 0.0
  vals = _nan_check(source_df, variable, fill_value=fill_nan)
interp["plot"] = np.log1p(interp.B25077_001E)
fig, ax = plt.subplots(figsize=(20, 20))
interp.to_crs(3857).plot("plot", ax=ax, alpha=0.4, tiles=True)
ax.axis("off");
../_images/1844484e7f9466ef052551c0e15b97cdbb7d83bb25b6b1b198369f31c99a8e81.png

And there we have it. Riverside’s 2017 population estimated at a regular hexgrid