-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_process_world_kopen.py
More file actions
253 lines (197 loc) · 7.69 KB
/
Copy path11_process_world_kopen.py
File metadata and controls
253 lines (197 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# -*- coding: utf-8 -*-
# =============================================================================
# Copyright (C) Les solutions géostack, Inc
#
# This file was produced as part of a research project conducted for
# The World Bank Group and is licensed under the terms of the MIT license.
#
# For inquiries, contact: info@geostack.ca
# Repository: https://github.com/geo-stack/sahel
# =============================================================================
"""
Extract and process Köppen-Geiger climate zones for the African continent.
This script performs the following tasks:
1. Extracts the Köppen-Geiger climate zone raster from
the "hess-11-1633-2007-supplement" archive.
2. Converts the raster to GeoTIFF format and assigns the WGS84 CRS.
3. Projects the raster to Africa Albers Equal Area Conic (ESRI:102022) and
clips to the African bounding box.
4. Fills all NoData cells in the climate raster with the nearest valid class
using distance-based nearest neighbor.
5. Clips the filled raster to the precise African continent shape, including
all pixels that touch the landmass polygon.
6. Outputs a clean, analysis-ready raster of Köppen climate zones spatially
aligned with other project datasets.
The output raster is specifically prepared to ensure that every land pixel in
the 30 m NASADEM of Africa has a corresponding valid Köppen climate zone,
enabling straightforward pixel-to-pixel association.
Note: This script is OPTIONAL.
The complete source "hess-11-1633-2007-supplement.zip" and preprocessed
output "koppen_climate_zones.tif" are included in the repository. Only
rerun this script if you wish to regenerate the climate raster, use a
different mask geometry, or test new reprojection/fill settings.
Requirements
------------
- Manual download of 'hess-11-1633-2007-supplement.zip' (see Data Source)
- The downloaded ZIP archive must be placed in 'hdml/data/climate_zones/'
- Simplified Africa landmass geometry (see 'process_usgs_coastal.py')
Storage Requirements
--------------------
- 'hess-11-1633-2007-supplement.zip': ~6.8 MB
- Intermediate rasters: minimal, deleted after processing
- Output GeoTIFF: 'climate_zones/koppen_climate_zones.tif' ~21 KB
Data Source
-----------
World Map of the Köppen-Geiger Climate Classification (Beck et al. 2007)
Download: https://hess.copernicus.org/articles/11/1633/2007/
Required file: 'hess-11-1633-2007-supplement.zip'
The Köppen-Geiger classification provides a globally standardized climate
zone raster widely used in geoscientific research, hydrology, and modeling.
Outputs
-------
- 'climate_zones/koppen_climate_zones.tif':
Raster of Köppen-Geiger climate classes clipped and filled for the
entire African continent (projected to ESRI:102022, with all pixels
covering Africa assigned a valid climate class)
Note: All paths are relative to the repository's 'data/' directory
(e.g., if cloned to 'C:/Users/User/Documents/hydrodepthml/', outputs are in
'C:/Users/User/Documents/hydrodepthml/data/').
"""
# ---- Standard imports
import shutil
import zipfile
# ---- Third party imports
import geopandas as gpd
import numpy as np
from osgeo import gdal
import rasterio
from rasterio.mask import mask
from scipy.ndimage import distance_transform_edt
# ---- Local imports
from hdml import __datadir__ as datadir
from hdml.gishelpers import clip_and_project_raster
gdal.UseExceptions()
CLIMZ_DIR = datadir / 'climate_zones'
CLIMZ_DIR.mkdir(parents=True, exist_ok=True)
# %% Extract 'hess-11-1633-2007-supplement' content
print("Extract USGS global islands database...")
# Extract with 7zip (because zipfile does not support the 'mpk' format)
exepath = datadir / '7za.exe'
# Extract the .zip archive.
zip_fname = 'hess-11-1633-2007-supplement.zip'
zip_path = CLIMZ_DIR / zip_fname
zip_url = 'https://hess.copernicus.org/articles/11/1633/2007/'
if not zip_path.exists():
raise FileNotFoundError(
f"\n[Updated world map of the Köppen-Geiger climate"
f" classification Missing]\n"
f"\nCould not locate required ZIP archive:\n"
f" {zip_path}\n"
f"\nTo resolve:\n"
f" 1. Download the file '{zip_fname}' from:\n"
f" {zip_url}\n"
f" 2. Move it to the folder:\n"
f" {CLIMZ_DIR}\n"
)
extract_dir = CLIMZ_DIR / 'hess-11-1633-2007-supplement'
if not extract_dir.exists():
print("Extrating zip archive...", flush=True)
extract_dir.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_dir)
# %%
# Convert to GeoTiff and assign CRS to the Köppen raster (we assume that
# it is WGS84)
print('Creating a GeoTiff file and set CRS to WGS84...')
adf_folder_path = extract_dir / 'Raster files' / 'world_koppen'
with_crs_path = CLIMZ_DIR / 'koppen_with_crs.tif'
translate_options = gdal.TranslateOptions(
format='GTiff',
outputSRS='EPSG:4326',
creationOptions=['COMPRESS=LZW', 'TILED=YES']
)
ds = gdal.Translate(
str(with_crs_path),
str(adf_folder_path),
options=translate_options
)
ds.FlushCache()
del ds
# Delete extract folder since we don't need it anymore.
shutil.rmtree(extract_dir)
# %%
print("Clipping to the African continent bounding box...")
# Read Africa landmass and get bounding box.
africa_gdf = gpd.read_file(
datadir / 'coastline' / 'africa_landmass_simple.gpkg')
africa_shape = africa_gdf.union_all()
africa_bbox = africa_shape.bounds # (minx, miny, maxx, maxy)
africa_102022_path = CLIMZ_DIR / 'koppen_africa_albers.tif'
clip_and_project_raster(
input_raster=with_crs_path,
output_raster=africa_102022_path,
output_crs='ESRI:102022',
clipping_bbox=africa_bbox,
resample_alg='nearest'
)
# Remove temp files.
with_crs_path.unlink(missing_ok=True)
with_crs_path.with_suffix('.tif.aux.xml').unlink(missing_ok=True)
# %%
print("Filling all nodata cells...")
with rasterio.open(africa_102022_path) as src:
arr = src.read(1)
nodata = src.nodata
# Create a mask of nodata pixels.
nodata_mask = arr == nodata
if np.any(nodata_mask):
# Find indices of nearest non-nodata for each pixel
distance, (inds_y, inds_x) = distance_transform_edt(
nodata_mask, return_indices=True)
arr_filled = arr[inds_y, inds_x]
else:
arr_filled = arr.copy()
# Prepare metadata for output.
out_meta = src.meta.copy()
out_meta.update({'compress': 'lzw', 'tiled': True})
# Save the filled raster
filled_path = CLIMZ_DIR / 'koppen_africa_albers_filled.tif'
with rasterio.open(filled_path, 'w', **out_meta) as dest:
dest.write(arr_filled, 1)
# Remove temp files.
africa_102022_path.unlink(missing_ok=True)
africa_102022_path.with_suffix('.tif.aux.xml').unlink(missing_ok=True)
# %%
print('Clipping to the African continent shape...')
final_path = CLIMZ_DIR / 'koppen_climate_zones.tif'
with rasterio.open(filled_path) as src:
# Ensure matching CRS
africa_gdf = africa_gdf.to_crs(src.crs)
# Get geometry as GeoJSON-like dicts
africa_geom = [
feature["geometry"] for feature in
africa_gdf.__geo_interface__["features"]
]
# Mask with all_touched=True
out_image, out_transform = mask(
src,
africa_geom,
crop=True,
nodata=src.nodata,
all_touched=True,
)
out_meta = src.meta.copy()
out_meta.update({
"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform,
"compress": "lzw",
"tiled": True,
})
# Write clipped raster
with rasterio.open(final_path, "w", **out_meta) as dest:
dest.write(out_image)
# Remove temp files.
filled_path.unlink(missing_ok=True)
filled_path.with_suffix('.tif.aux.xml').unlink(missing_ok=True)