Skip to content

Instantly share code, notes, and snippets.

@kgjenkins
Last active September 12, 2023 20:45
Show Gist options
  • Save kgjenkins/c7d1d0558798cbe970c90bc42933b29d to your computer and use it in GitHub Desktop.
Save kgjenkins/c7d1d0558798cbe970c90bc42933b29d to your computer and use it in GitHub Desktop.
Reclassifying UrbanWatch rasters using QGIS and Rasterio

Reclassifying UrbanWatch rasters using QGIS and Rasterio

See the video demo at https://youtu.be/t-pb5-1Q_Qg

The files here were used to convert UrbanWatch from RGB images to numeric rasters with simple landcover values 0-9, as follows:

Value Category Color
0 No Data Black
1 Building Red
2 Road Gray
3 Parking Lot Pink
4 Tree Canopy Dark Green
5 Grass/Shrub Light Green
6 Agriculture Yellow
7 Water Blue
8 Barren Brown
9 Other White

To save the files below, right-click the corresponding "Raw" button then "Save link as..."

BONUS: Each file can also be converted to a COG (Cloud-Optimized GeoTIFF) using rio cogeo using a command like:

rio cogeo create in.tif out.tif
import rasterio
files = [
"urbanwatch-atlanta-2017.tif",
"urbanwatch-boston-2014.tif",
"urbanwatch-charlotte-2016.tif",
"urbanwatch-chicago-2017.tif",
"urbanwatch-dallas-2016.tif",
"urbanwatch-denton-county-2020.tif",
"urbanwatch-denver-2017.tif",
"urbanwatch-des-moines-2017.tif",
"urbanwatch-detroit-2014.tif",
"urbanwatch-houston-2016.tif",
"urbanwatch-los-angeles-2014.tif",
"urbanwatch-miami-2017.tif",
"urbanwatch-minneapolis-2017.tif",
"urbanwatch-new-york-city-2017.tif",
"urbanwatch-philadelphia-2017.tif",
"urbanwatch-phoenix-2015.tif",
"urbanwatch-raleigh-2016.tif",
"urbanwatch-riverside-2014.tif",
"urbanwatch-san-diego-2014.tif",
"urbanwatch-san-francisco-2014.tif",
"urbanwatch-seattle-2017.tif",
"urbanwatch-st-louis-2014.tif",
"urbanwatch-washington-dc-2017.tif"
]
with rasterio.Env():
for f in files:
with rasterio.open('input/{}'.format(f)) as src:
orig = src.read(1)
meta = src.meta
meta.update(compress='deflate', nodata=0)
# deflate is better than the lzw used in the video
with rasterio.open('output/{}'.format(f), 'w', **meta) as dst:
dst.write(orig, indexes=1)
dst.write_colormap(
1, {
0: (0, 0, 0, 255),
1: (255, 0, 0, 255),
2: (133, 133, 133, 255),
3: (255, 0, 192, 255),
4: (34, 139, 34, 255),
5: (128, 236, 104, 255),
6: (255, 193, 37, 255),
7: (0, 0, 255, 255),
8: (128, 0, 0, 255),
9: (255, 255, 255, 255) })
RRB LC
0 0
102 4
255 7
256 8
360 5
399 2
510 1
547 6
702 3
765 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment