← Back to Projects
How Green Is Your Town? — Urban Canopy & Change Viewer
Tech: Python, FastAPI, Rasterio, OpenCV, PostgreSQL/PostGIS, React, TypeScript
Overview
A full-stack geospatial web app that lets you type in any town, city, or coordinate worldwide and compare its summer tree and foliage coverage between 2017 and 2026. Instead of downloading multi-gigabyte Sentinel-2 scenes, the backend fetches only the exact bounding-box tiles needed over HTTP/2 range requests, masks out clouds, water, and unincorporated land outside city limits, and serves a split-screen slider in React.
How It Works
- Geocoding & Administrative Boundary Extraction: Resolves user queries via OpenStreetMap Nominatim (with Photon fallback) to pull the municipal boundary polygon, framing a 2:1 aspect-ratio viewport and guarding against country-scale queries.
- Windowed Sentinel-2 COG Streaming: Queries Microsoft Planetary Computer STAC APIs for low-cloud summer scenes across the two years, then uses Rasterio across thread pools to read only the required Red (
B04), NIR (B08), and Scene Classification (SCL) windows.
- Radiometric Harmonization & Paired Pixel Comparison: Accounts for ESA's Processing Baseline 04.00 digital-number offset so each years reflectance values are directly comparable, then intersects cloud-free land pixels across both years before computing foliage percentage.
- Polygon Rasterization & OpenCV Contouring: Rasterizes the city's GeoJSON perimeter onto the target grid so surrounding farmland or ocean doesn't skew municipal canopy stats, and draws clean boundary contours onto the rendered PNG layers.
- Two-Tier PostgreSQL/PostGIS Cache: Checks a composite unique B-tree index on
(query_key, baseline_year, target_year) before hitting external geocoders, and falls back to a GiST-indexed ST_Contains spatial lookup so alias searches (like "Durham" vs. "Durham, NC") return in milliseconds.
NDVI Classification Thresholds
| Surface Class |
NDVI Range |
Interpretation |
| Water / Cloud / Masked |
<= 0.08 or SCL masked |
Excluded from denominator so seasonal water levels or clouds don't distort stats |
| Impervious / Urban / Bare Ground |
0.08 – 0.36 |
Roads, rooftops, gravel, and dry non-foliage surfaces |
| Active Foliage & Tree Canopy |
>= 0.36 |
Urban tree canopy, parks, and dense summer vegetation counted toward coverage |