Download

forestatrisk.data.country_download(get_fcc_args, iso3, output_dir='.', gadm=True, srtm=True, wdpa=True, osm=True, forest=True)[source]

Download country geospatial data.

Function to download all the data for a specific country. It includes GEE forest data, GADM, OSM, SRTM, and WDPA data.

Added in version 1.2: The get_fcc_args parameter.

Removed in version 1.2: The gdrive_remote_rclone and gdrive_folder parameters.

Parameters:
  • get_fcc_args – Dictionary of arguments for function get_fcc() from Python package geefcc. For example: {"aoi": "MTQ", "buff": 0.08983152841195216 , "years": [2000, 2010, 2020], "source": "tmf", "perc": 75, "tile_size": 1, "parallel": True, "ncpu": None, "output_file": "forest_MTQ.tiff"}.

  • iso3 – Country iso code used to download GADM, OSM, SRTM, and WDPA data. The iso code should correspond to the country to which the aoi provided in get_fcc_args belongs.

  • output_dir – Directory where data is downloaded. Default to current working directory.

  • gadm – Toggle GADM data download.

  • srtm – Toggle SRTM data download.

  • wdpa – Toggle WDPA data download.

  • osm – Toggle OSM data download.

  • forest – Toggle forest data download.

forestatrisk.data.download.download_gadm(iso3, output_file)[source]

Download GADM data for a country.

Download GADM (Global Administrative Areas) for a specific country. See https://gadm.org.

Parameters:
  • iso3 – Country ISO 3166-1 alpha-3 code.

  • output_file – Path to output GPKG file.

forestatrisk.data.download.download_osm(iso3, output_dir='.')[source]

Download OSM data for a country.

Download OpenStreetMap data from Geofabrik.de or OpenStreetMap.fr for aspecific country.

Parameters:
  • iso3 – Country ISO 3166-1 alpha-3 code.

  • output_dir – Directory where data is downloaded. Default to current working directory.

forestatrisk.data.download.download_srtm(iso3, output_dir='.')[source]

Download SRTM data for a country.

Download SRTM data (Shuttle Radar Topographic Mission v4.1) from CSI-CGIAR for a specific country. This function uses the iso3 code to download the country borders (as a shapefile) from GADM. Download is skipped if the shapefile is already present. Country borders are used to identify the SRTM tiles to be downloaded.

Parameters:
  • iso3 – Country ISO 3166-1 alpha-3 code.

  • output_dir – Directory where data is downloaded. Default to current working directory.

forestatrisk.data.download.download_wdpa(iso3, output_dir='.')[source]

Download protected areas.

Protected areas come from the World Database on Protected Areas (https://www.protectedplanet.net/). This function uses the pywdpa python package.

Parameters:
  • iso3 – Country ISO 3166-1 alpha-3 code.

  • output_dir – Directory where shapefiles for protected areas are downloaded. Default to current working directory.

Compute

forestatrisk.data.country_compute(iso3, temp_dir='data_raw', output_dir='data', proj='EPSG:3395', data_country=True, data_forest=True, keep_temp_dir=False)[source]

Process country geospatial data.

This function computes and formats the country data. Computations are done in a temporary directory where data have been downloaded (default to “data_raw”). Then data are copied to an output directory (default to “data”).

Parameters:
  • iso3 – Country ISO 3166-1 alpha-3 code.

  • temp_dir – Temporary directory for computation. Relative path to the current working directory.

  • output_dir – Output directory. Relative path to the current working directory.

  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Default to “EPSG:3395” (World Mercator).

  • data_country – Boolean to compute environmental variables for the country. Default to “True”.

  • data_forest – Boolean to compute forest variables. Default to “True”.

  • keep_temp_dir – Boolean to keep the temporary directory. Default to “False”.

forestatrisk.data.compute.compute_biomass_avitabile(proj, extent, verbose=False)[source]

Compute aboveground biomass.

Using Avitabile et al. 2016 data.

Parameters:
  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Used for reprojecting data.

  • extent – Extent (xmin, ymin, xmax, ymax) of output rasters.

  • verbose – Logical. Whether to print messages or not. Default to False.

forestatrisk.data.compute.compute_biomass_whrc(iso3, input_dir='data_raw', output_dir='data', proj='EPSG:3395')[source]

Function to mosaic and resample biomass data from WHRC.

This function mosaics and resamples the biomass data obtained from GEE. A reprojection can be performed.

Parameters:
  • iso3 – Country ISO 3166-1 alpha-3 code.

  • input_dir – Directory with input files for biomass.

  • output_dir – Output directory.

  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Default to “EPSG:3395” (World Mercator).

forestatrisk.data.compute.compute_distance(input_file, dist_file, values=0, nodata=4294967295, input_nodata=False, verbose=True)[source]

Computing the shortest distance to pixels with given values in a raster file.

This function computes the shortest distance to pixels with given values in a raster file. Distances generated are in georeferenced coordinates.

Parameters:
  • input_file – Input raster file.

  • dist_file – Path to the distance raster file that is created.

  • values – Values of the raster to compute the distance to. If several values, they must be separated with a comma in a string (eg. ‘0,1’). Default is 0.

  • nodata – NoData value for the output raster. Default is 4294967295 for UInt32.

  • input_nodata – Whether nodata pixels in the input raster should be nodata in the output raster (default to False).

  • verbose – Logical. Whether to print messages or not. Default to True.

Returns:

None. A distance raster file is created (see dist_file). Raster data type is UInt32 ([0, 4294967295]).

forestatrisk.data.compute.compute_gadm(ifile, ofile, proj, verbose=False)[source]

Processing GADM data.

Extract layers to a new GPKG file called “aoi_latlong.gpkg” (if it doesn’t exist yet) and reproject this file to output file. Layer names for country border and subjurisdictions are “aoi” (for “area of interest”) and “subj” respectively. The extent of the area of interest is returned with a buffer of 5 km.

Parameters:
  • input_file – Path to input GADM GPKG file.

  • output_file – Path to aoi GPKG output file.

  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Used for reprojecting data.

  • verbose – Logical. Whether to print messages or not. Default to False.

forestatrisk.data.compute.compute_osm(proj, extent, verbose=False)[source]

Compute distance to road, town, and river.

Parameters:
  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Used for reprojecting data.

  • extent – Extent (xmin, ymin, xmax, ymax) of output rasters.

  • verbose – Logical. Whether to print messages or not. Default to False.

forestatrisk.data.compute.compute_srtm(proj, extent, verbose=False)[source]

Compute elevation and slope.

Parameters:
  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Used for reprojecting data.

  • extent – Extent (xmin, ymin, xmax, ymax) of output rasters.

  • verbose – Logical. Whether to print messages or not. Default to False.

forestatrisk.data.compute.compute_wdpa(iso, proj, extent, where=None, verbose=False)[source]

Process geospatial data on protected areas.

Parameters:
  • iso – Country iso code.

  • where – SQL statement to filter features. Default to "status="Designated" OR status="Inscribed" OR status="Established" OR (status="Proposed" AND CAST(SUBSTR(date,7,4) AS INTEGER) < 2010)"

  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Used for reprojecting data.

  • extent – Extent (xmin, ymin, xmax, ymax) of output rasters.

  • verbose – Logical. Whether to print messages or not. Default to False.

forestatrisk.data.compute.compute_forest(proj, extent, verbose=False)[source]

Processing forest data.

Parameters:
  • iso – Country iso code.

  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Used for reprojecting data.

  • extent – Extent (xmin, ymin, xmax, ymax) of output rasters.

  • verbose – Logical. Whether to print messages or not. Default to False.

forestatrisk.data.compute.mosaic_biomass_whrc(iso3, input_dir='data_raw', output_dir='data', proj='EPSG:3395')[source]

Function to mosaic biomass images from WHRC.

This function mosaics the biomass data obtained from GEE. No reprojection is performed.

Parameters:
  • iso3 – Country ISO 3166-1 alpha-3 code.

  • input_dir – Directory with input files for biomass.

  • output_dir – Output directory.

  • proj – Projection definition (EPSG, PROJ.4, WKT) as in GDAL/OGR. Default to “EPSG:3395” (World Mercator).

Sample

forestatrisk.sample(nsamp=10000, adapt=True, seed=1234, csize=10, var_dir='data', input_forest_raster='forest.tif', output_file='sample.txt', blk_rows=0, verbose=True)[source]

Sample points and extract raster values.

This function (i) randomly draws spatial points in deforested and forested areas and (ii) extract environmental variable values for each spatial point.

Parameters:
  • nsamp – Number of random spatial points.

  • adapt – Boolean. Adapt nsamp to forest area: 1000 for 1 Mha of forest, with min=10000 and max=50000. Default to True.

  • seed – Seed for random number generator.

  • csize – Spatial cell size in km.

  • var_dir – Directory with raster data.

  • input_forest_raster – Name of the forest raster file (1=forest, 0=deforested) in the var_dir directory.

  • output_file – Path to file to save sample points.

  • blk_rows – If > 0, number of lines per block.

  • verbose – Toogle progress bar.

Returns:

A Pandas DataFrame, each row being one observation.

Various

forestatrisk.get_vector_extent(input_file)[source]

Compute the extent of a vector file.

This function computes the extent (xmin, ymin, xmax, ymax) of a shapefile.

Parameters:

input_file – Path to the input vector file.

Returns:

The extent as a tuple (xmin, ymin, xmax, ymax).

Deprecated

forestatrisk.extent_shp(input_file)[source]

Compute the extent of a vector file.

Deprecated since version 1.2: Use forestatrisk.get_vector_extent().

Warning

Will be removed in future versions.

This function computes the extent (xmin, ymin, xmax, ymax) of a shapefile.

Parameters:

input_file – Path to the input vector file.

Returns:

The extent as a tuple (xmin, ymin, xmax, ymax).