Skip to content

Commit

Permalink
Do not compute forest_src.tif if present
Browse files Browse the repository at this point in the history
Allow using user's fcc data.
  • Loading branch information
ghislainv committed Jul 17, 2024
1 parent f40fd50 commit 44ba5bb
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions forestatrisk/data/compute/compute_forest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Processing forest data."""

import os
import subprocess
from glob import glob

Expand Down Expand Up @@ -28,30 +29,33 @@ def compute_forest(proj, extent, verbose=False):
# Creation options
copts = ["COMPRESS=DEFLATE", "PREDICTOR=2", "BIGTIFF=YES"]

# Make vrt
tif_forest_files = glob("forest_tiles/forest_*.tif")
forest_vrt = gdal.BuildVRT(
"forest.vrt",
tif_forest_files,
callback=cback)
# Flush cache
forest_vrt.FlushCache()
forest_vrt = None

# Reproject
param = gdal.WarpOptions(
warpOptions=["overwrite"],
srcSRS="EPSG:4326",
dstSRS=proj,
outputBounds=extent,
targetAlignedPixels=True,
resampleAlg=gdal.GRA_NearestNeighbour,
xRes=30,
yRes=30,
creationOptions=copts,
callback=cback,
)
gdal.Warp("forest_src.tif", "forest.vrt", options=param)
# Only execute the following if forest_src.tif is absent
# Useful to use user's fcc data
if not os.path.isfile("forest_src.tif"):
# Make vrt
tif_forest_files = glob("forest_tiles/forest_*.tif")
forest_vrt = gdal.BuildVRT(
"forest.vrt",
tif_forest_files,
callback=cback)
# Flush cache
forest_vrt.FlushCache()
forest_vrt = None

# Reproject
param = gdal.WarpOptions(
warpOptions=["overwrite"],
srcSRS="EPSG:4326",
dstSRS=proj,
outputBounds=extent,
targetAlignedPixels=True,
resampleAlg=gdal.GRA_NearestNeighbour,
xRes=30,
yRes=30,
creationOptions=copts,
callback=cback,
)
gdal.Warp("forest_src.tif", "forest.vrt", options=param)

# Number of bands (or years)
with gdal.Open("forest_src.tif") as ds:
Expand Down

0 comments on commit 44ba5bb

Please sign in to comment.