Skip to content

Commit

Permalink
Remove raster if exists before creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainv committed Jun 14, 2024
1 parent 34d15ff commit 44ee781
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions forestatrisk/data/compute/compute_osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def compute_osm(proj, extent, verbose=False):
# Convert osm.pbf to o5m
cmd = "osmconvert country.osm.pbf -o=country.o5m"
rtn = subprocess.run(cmd, shell=True,
check=True,
capture_output=True,
text=True)
if verbose:
Expand Down
10 changes: 9 additions & 1 deletion forestatrisk/predict/interpolate_rho.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ def interpolate_rho(
dirname = os.path.dirname(output_file)
rho_orig_filename = os.path.join(dirname, "rho_orig.tif")
driver = gdal.GetDriverByName("GTiff")
rho_R = driver.Create(rho_orig_filename, ncell_X, ncell_Y, 1, gdal.GDT_Float64)
if os.path.isfile(rho_orig_filename):
os.remove(rho_orig_filename)
rho_R = driver.Create(
rho_orig_filename,
ncell_X,
ncell_Y,
1,
gdal.GDT_Float64
)
rho_R.SetProjection(r.GetProjection())
gt = list(gt)
gt[1] = csize_orig
Expand Down
2 changes: 2 additions & 0 deletions forestatrisk/predict/predict_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def predict_raster(
if verbose:
print("Create a raster file on disk for projections")
driver = gdal.GetDriverByName("GTiff")
if os.path.isfile(output_file):
os.remove(output_file)
Pdrv = driver.Create(
output_file,
ncol,
Expand Down
2 changes: 2 additions & 0 deletions forestatrisk/predict/predict_raster_binomial_iCAR.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def predict_raster_binomial_iCAR(
if verbose:
print("Create a raster file on disk for projections")
driver = gdal.GetDriverByName("GTiff")
if os.path.isfile(output_file):
os.remove(output_file)
Pdrv = driver.Create(
output_file,
ncol,
Expand Down
9 changes: 8 additions & 1 deletion forestatrisk/predict/wrast_rho.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ def wrast_rho(rho, input_raster, csize=10, output_file="output/rho_orig.tif"):
dirname = os.path.dirname(output_file)
rho_orig_filename = os.path.join(dirname, "rho_orig.tif")
driver = gdal.GetDriverByName("GTiff")
rho_R = driver.Create(rho_orig_filename, ncell_X, ncell_Y, 1, gdal.GDT_Float64)
if os.path.isfile(rho_orig_filename):
os.remove(rho_orig_filename)
rho_R = driver.Create(
rho_orig_filename,
ncell_X,
ncell_Y,
1,
gdal.GDT_Float64)
rho_R.SetProjection(r.GetProjection())
gt = list(gt)
gt[1] = csize_m
Expand Down
3 changes: 3 additions & 0 deletions forestatrisk/project/deforest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Standard library imports
from __future__ import division, print_function # Python 3 compatibility
import os
import warnings

# Third party imports
Expand Down Expand Up @@ -128,6 +129,8 @@ def deforest(input_raster, hectares, output_file="output/fcc.tif", blk_rows=128)
# Raster of predictions
print("Create a raster file on disk for forest-cover change")
driver = gdal.GetDriverByName("GTiff")
if os.path.isfile(output_file):
os.remove(output_file)
fccR = driver.Create(
output_file,
ncol,
Expand Down
3 changes: 3 additions & 0 deletions forestatrisk/validate/diffproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Standard library imports
from __future__ import division, print_function # Python 3 compatibility
import os

# Third party imports
import numpy as np
Expand Down Expand Up @@ -59,6 +60,8 @@ def r_diffproj(inputA, inputB, output_file="diffproj.tif", blk_rows=128):
# Raster of predictions
print("Create a raster file on disk for projections")
driver = gdal.GetDriverByName("GTiff")
if os.path.isfile(output_file):
os.remove(output_file)
ds_out = driver.Create(
output_file,
ncol,
Expand Down
3 changes: 3 additions & 0 deletions forestatrisk/validate/resample_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Standard library imports
from __future__ import division, print_function # Python 3 compatibility
import os

# Third party imports
import numpy as np
Expand Down Expand Up @@ -85,6 +86,8 @@ def resample_sum(input_raster, output_raster, val=0, window_size=2):
# Raster of result
print("Create output raster file on disk")
driver = gdal.GetDriverByName("GTiff")
if os.path.isfile(output_raster):
os.remove(output_raster)
ds_out = driver.Create(
output_raster,
ncol_out,
Expand Down
9 changes: 6 additions & 3 deletions forestatrisk/validate/validation_udef_arp.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def validation_udef_arp(
deforestation, 3: remaining forest at the end of the second
period. No data value must be 0 (zero).
:param period: Either "calibration" (from t1 to t2), or
"validation" (or "confirmation" from t2 to t3).
:param period: Either "calibration" (from t1 to t2), "validation"
(from t2 to t3), or "historical" (from t1 to t3).
:param time_interval: Duration (in years) of the period.
Expand Down Expand Up @@ -148,9 +148,12 @@ def validation_udef_arp(
if period == "calibration":
df.loc[s, "nfor_obs"] = np.sum(fcc_data > 0)
df.loc[s, "ndefor_obs"] = np.sum(fcc_data == 1)
elif period in ["validation", "confirmation"]:
elif period == "validation":
df.loc[s, "nfor_obs"] = np.sum(fcc_data > 1)
df.loc[s, "ndefor_obs"] = np.sum(fcc_data == 2)
else: # historical
df.loc[s, "nfor_obs"] = np.sum(fcc_data > 0)
df.loc[s, "ndefor_obs"] = np.sum(np.isin(fcc_data, [1, 2]))

# Predicted deforestation
defor_cat_data = defor_cat_band.ReadAsArray(
Expand Down

0 comments on commit 44ee781

Please sign in to comment.