Skip to content

Commit

Permalink
Correcting benchmark to account for forest mask in vulnerability map
Browse files Browse the repository at this point in the history
forest_file parameter added to vulnerability_map.py
  • Loading branch information
ghislainv committed Jun 7, 2024
1 parent 0c436fd commit f12cafb
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions riskmapjnr/benchmark/vulnerability_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


def vulnerability_map(
forest_file,
dist_file,
dist_bins,
subj_file,
Expand All @@ -20,6 +21,10 @@ def vulnerability_map(
values indicate higher vulnerability. Raster type is UInt16 ([0,
65535]). NoData value is set to 0.
:param forest_file: Input forest cover file. Necessary to have
forest extent within country borders (info not provided by
dist_file).
:param dist_file: Input file of distance to forest edge.
:param dist_bins: The distance bins used to convert distance to
Expand All @@ -44,19 +49,23 @@ def vulnerability_map(
# Input rasters
# ==============================================================

# Forest cover
forest_ds = gdal.Open(forest_file)
forest_band = forest_ds.GetRasterBand(1)
# Raster size
xsize = forest_band.XSize
ysize = forest_band.YSize

# Distance to forest edge raster file
dist_ds = gdal.Open(dist_file)
dist_band = dist_ds.GetRasterBand(1)
# Raster size
xsize = dist_band.XSize
ysize = dist_band.YSize

# Subjurisdictions raster file
subj_ds = gdal.Open(subj_file)
subj_band = subj_ds.GetRasterBand(1)

# Make blocks
blockinfo = makeblock(dist_file, blk_rows=blk_rows)
blockinfo = makeblock(forest_file, blk_rows=blk_rows)
nblock = blockinfo[0]
nblock_x = blockinfo[1]
x = blockinfo[3]
Expand Down Expand Up @@ -96,6 +105,7 @@ def vulnerability_map(
px = b % nblock_x
py = b // nblock_x
# Data
forest_data = forest_band.ReadAsArray(x[px], y[py], nx[px], ny[py])
dist_data = dist_band.ReadAsArray(x[px], y[py], nx[px], ny[py])
subj_data = subj_band.ReadAsArray(x[px], y[py], nx[px], ny[py])
# Categorize
Expand All @@ -104,9 +114,9 @@ def vulnerability_map(
right=True)
cat_data = cat_data.reshape(dist_data.shape)
cat_data = n_classes + 1 - cat_data
# Set classes 0 and 1
cat_data[dist_data == 0] = 0
# Set classes 1 beyond distance threshold
cat_data[dist_data > dist_thresh] = 1
cat_data[forest_data == 0] = 0 # Mask with forest
# Adding subjurisdiction info
cat_data = cat_data * 1000 + subj_data
cat_data[cat_data <= 1000] = 0
Expand Down

0 comments on commit f12cafb

Please sign in to comment.