Skip to content

Commit

Permalink
Adding allocate deforestation
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainv committed Jul 11, 2024
1 parent 124fa45 commit 78aabac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions forestatrisk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# Project
from .project import deforest_diffusion, deforest_diffusion_t_nofor
from .project import deforest, emissions
from .project import allocate_deforestation

# Validate
from .validate import computeAUC, accuracy_indices, cross_validation
Expand Down
1 change: 1 addition & 0 deletions forestatrisk/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
from .deforest_diffusion import deforest_diffusion, deforest_diffusion_t_nofor
from .deforest import deforest
from .emissions import emissions
from .allocate_deforestation import allocate_deforestation

# EOF
44 changes: 27 additions & 17 deletions forestatrisk/project/allocate_deforestation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

def allocate_deforestation(riskmap_juris_file, defor_rate_tab,
defor_juris_ha, years_forecast,
project_boundaries,
project_borders,
output_file="defor_project.csv",
verbose=False):
"""Allocating deforestation.
:param riskmap_juris: Raster file with classes of deforestation
:param riskmap_juris_file: Raster file with classes of deforestation
risk at the jurisdictional level.
:param defor_rate_tab: CSV file including the table with
Expand All @@ -27,7 +27,7 @@ def allocate_deforestation(riskmap_juris_file, defor_rate_tab,
:param years_forecast: Length of the forecasting period (in years).
:param project_boundaries: Vector file for project boundaries.
:param project_borders: Vector file for project borders.
:param output_file: Output file with deforestation
allocated to the project.
Expand All @@ -51,7 +51,7 @@ def allocate_deforestation(riskmap_juris_file, defor_rate_tab,
gdal.Warp(ofile, riskmap_juris_file,
cropToCutline=True,
warpOptions=["CUTLINE_ALL_TOUCHED=TRUE"],
cutlineDSName=project_boundaries,
cutlineDSName=project_borders,
creationOptions=copts,
callback=cback)

Expand Down Expand Up @@ -89,6 +89,10 @@ def allocate_deforestation(riskmap_juris_file, defor_rate_tab,
# Deforestation density (ha/pixel/yr)
df_rate["defor_dens"] = df_rate["rate_abs"] * pixel_area / years_forecast

# Save the df_rate table
ofile = opj(out_dir, "defrate_cat_forecast.csv")
df_rate.to_csv()

# -----------------------------
# Join tables
# -----------------------------
Expand All @@ -101,21 +105,27 @@ def allocate_deforestation(riskmap_juris_file, defor_rate_tab,
# Save results
data = {
"period": ["annual", "entire"],
"deforestation (ha)": [round(defor_project, 1), round(defor_project * years_forecast, 1)]
"length (yr)": [1, years_forecast],
"deforestation (ha)": [
round(defor_project, 1),
round(defor_project * years_forecast, 1)
]
}
res = pd.DataFrame(data)
res.to_csv(output_file, header=True, index=False)


# Test
import os
os.chdir(os.path.expanduser("~/deforisk/MTQ-tuto-bak/outputs/far_models/forecast/"))
allocate_deforestation(
riskmap_juris_file="prob_icar_t3.tif",
defor_rate_tab="defrate_cat_icar_forecast.csv",
defor_juris_ha=4000, # About 400 ha/yr in MTQ on 2010-2020.
years_forecast=10,
project_boundaries="project_boundaries.gpkg",
output_file="defor_project.csv",
verbose=False,
)
# # Test
# import os
# os.chdir(os.path.expanduser("~/deforisk/MTQ-tuto-bak/outputs/far_models/forecast/"))
# allocate_deforestation(
# riskmap_juris_file="prob_icar_t3.tif",
# defor_rate_tab="defrate_cat_icar_forecast.csv",
# defor_juris_ha=4000, # About 400 ha/yr in MTQ on 2010-2020.
# years_forecast=10,
# project_boundaries="project_boundaries.gpkg",
# output_file="defor_project.csv",
# verbose=False,
# )

# End Of File

0 comments on commit 78aabac

Please sign in to comment.