Skip to content

Commit c2a85c8

Browse files
committed
Address review findings: fix _composite alpha, tighten comment (#3640)
- animated_hillshade.py: _composite had a dead alpha branch (line 120 reassigned base to RGB, so the base.shape[-1]==4 guard on line 121 was always false and output alpha was hard-coded to 255). Rewrite to keep rgb separate and rebuild base with np.concatenate, matching the _stack helper used in the notebooks. Also add xrspatial to the requires line in the docstring. - pharmacy-deserts.ipynb: sharpen the merge='last' comment to state the non-overlap assumption (each pixel covered by exactly one polygon) that makes it equivalent to ds.mean.
1 parent 36c26a1 commit c2a85c8

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

examples/animated_hillshade.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Generates a synthetic terrain, places trees and water on it, and renders a
44
rotating-hillshade GIF plus a composited build-up GIF.
55
6-
Requires: numpy, xarray, matplotlib, pillow.
6+
Requires: numpy, xarray, matplotlib, pillow, xrspatial.
77
"""
88
from functools import partial
99

@@ -117,10 +117,9 @@ def _composite(layers):
117117
base = layers[0].astype(np.float64)
118118
for top in layers[1:]:
119119
a = (top[..., 3:4] / 255.0)
120-
base = top[..., :3] * a + base[..., :3] * (1 - a)
121-
base_a = np.maximum(top[..., 3:4], base[..., 3:4] if base.shape[-1] == 4
122-
else np.full_like(a, 255))
123-
base = np.concatenate([base, base_a * 255], axis=-1)
120+
rgb = top[..., :3] * a + base[..., :3] * (1 - a)
121+
out_a = np.maximum(top[..., 3:4], base[..., 3:4])
122+
base = np.concatenate([rgb, out_a], axis=-1)
124123
return np.clip(base, 0, 255).astype(np.uint8)
125124

126125

examples/pharmacy-deserts.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320
"outputs": [],
321321
"source": [
322322
"# Rasterize the block-group polygons with their PCT_ABOVE_65 attribute.\n",
323-
"# Block groups tile the plane (non-overlapping), so merge='last' is\n",
323+
"# Block groups tile the plane (non-overlapping: each pixel is\n # covered by exactly one polygon), so merge='last' is\n",
324324
"# equivalent to datashader's ds.mean('PCT_ABOVE_65') here.\n",
325325
"age_raster = template.xrs.rasterize(\n",
326326
" blockgroup_df, column='PCT_ABOVE_65', merge='last',\n",

0 commit comments

Comments
 (0)