Data set details

Data set description: Land cover classification in Zimbabwe
Source: Moderate Resolution Imaging Spectroradiometer (MODIS)
Details on the retrieved data: Land cover classification (Land Cover Products) in Zimbabwe in 2019.
Spatial and temporal resolution: Annually averaged data at a 500 meter spatial resolution.

MODIS (Moderate Resolution Imaging Spectroradiometer)

MODIS is an instrument aboard the Terra and Aqua satellites, which orbits the entire Earth every 1-2 days, acquiring data at different spatial resolutions. The data acquired by MODIS describes features of the land, oceans and the atmosphere. A complete list of MODIS data products can be found on the MODIS website.

Downloading Land Cover data using MODIStsp

MODIStsp is an R package for downloading and preprocessing time series of raster data from MODIS data products. The package’s name is an acronym for ‘MODIS Time Series Processing’. This tutorial will focus on downloading and visualising land cover data, but the same process can be followed with other MODIS data products as well.

Installing MODIStsp

The MODIStsp package can be downloaded from CRAN as follows.

install.packages("MODIStsp")

Identifying the MODIS data product

The first step of downloading data is to identify which MODIS data product to use.

This tutorial will use the Land Cover Products, which uses different classification schemes to identify types of land cover. The primary land cover scheme identifies 17 classes defined by the International Geosphere-Biosphere Programme (IGBP) as detailed in their user guide.This product contains annually averaged data, collected bi-annually allowing for hemispheric differences in the growing seasons, and enabling the product to capture two growth cycles if necessary.

The product IDs for each of these products can also be found on the data product page.

This tutorial will use the ‘Land Cover Type Yearly L3 Global 500m’ product with the product IDs MCD12Q1 (Combined Aqua and Terra Product ID)

Retrieving MODIS layers for a product

The product layers (original MODIS layers, quality layers and spectral indexes) available for a given product can be retrieved using the following function.

library(MODIStsp)
MODIStsp_get_prodlayers("MCD12Q1")
## $prodname
## [1] "LandCover_Type_Yearly_500m (MCD12Q1)"
## 
## $bandnames
##  [1] "LC1"                 "LC2"                 "LC3"                
##  [4] "LC4"                 "LC5"                 "LC_Prop1"           
##  [7] "LC_Prop2"            "LC_Prop3"            "LC_Prop1_Assessment"
## [10] "LC_Prop2_Assessment" "LC_Prop3_Assessment" "LC_QC"              
## [13] "LC_LW"              
## 
## $bandfullnames
##  [1] "Land Cover Type 1 (IGBP)*"                                      
##  [2] "Land Cover Type 2 (UMD)*"                                       
##  [3] "Land Cover Type 3 (LAI/fPAR)*"                                  
##  [4] "Land Cover Type 4 (NPP/BGC)*"                                   
##  [5] "Land Cover Type 5: Annual Plant Functional Types classification"
##  [6] "FAO-Land Cover Classification System 1 (LCCS1) land cover layer"
##  [7] "FAO-LCCS2 land use layer"                                       
##  [8] "FAO-LCCS3 surface hydrology layer"                              
##  [9] "LCCS1 land cover layer confidence"                              
## [10] "LCCS2 land use layer confidence"                                
## [11] "LCCS3 surface hydrology layer confidence"                       
## [12] "Land Cover QC"                                                  
## [13] "Binary land/water mask derived from MOD44W"                     
## 
## $quality_bandnames
## NULL
## 
## $quality_fullnames
## NULL
## 
## $indexes_bandnames
## NULL
## 
## $indexes_fullnames
## NULL

Note how the \$bandfullnames define each of the \$bandnames.

The MODIStsp() function

MODIStsp() is the main function of the MODIStsp package, and allows us to download MODIS data products. While this is a very comprehensive function and we only a very few of its arguments in this tutorial, the entire list of arguments can be found in the MODIStsp documentation.

The MODIStsp() function provides two ways of downloading data, namely, through a GUI (interactive) or through an R script (non-interactive). This tutorial will focus on the non-interactive execution.

Downloading IGBP land cover classification data

To download the IGBP land cover classification data in Zimbabwe, first we download the boundary of Zimbabwe with the geoboundaries() function from the rgeoboundaries package and save it on our computer.

# remotes::install_github("wmgeolab/rgeoboundaries")
# install.packages("sf")
library(rgeoboundaries)
library(sf)
# Downloading the country boundary of Zimbabwe
map_boundary <- geoboundaries("Zimbabwe")
# Defining filepath to save downloaded spatial file
spatial_filepath <- "LandCoverData/zimbabwe.shp"
# Saving downloaded spatial file on to our computer
st_write(map_boundary, paste0(spatial_filepath))

Then we use the MODIStsp() function to download the IGBP land cover classification data.

To download data in Zimbabwe, we use the boundary of Zimbabwe we downloaded. So in the MODIStsp() function we set the spatmeth argument as “file” and set the spafile argument as the path of the map we saved.

Since IGBP land cover classification data is provided annually and dated on the 1st of January each year, the start-date and end_date arguments should contain the 1st of January of each year for which we want the data to be downloaded.

The start-date and end_date arguments define the period for which we want the data to be downloaded.

Note that this tutorial uses a test username and password. The user and password arguments should be the username and password corresponding to your earthdata credentials.

library(MODIStsp)
MODIStsp(gui             = FALSE,
         out_folder      = "LandCoverData",
         out_folder_mod  = "LandCoverData",
         selprod         = "LandCover_Type_Yearly_500m (MCD12Q1)",
         bandsel         = "LC1", 
         user            = "mstp_test" ,
         password        = "MSTP_test_01",
         start_date      = "2019.01.01", 
         end_date        = "2019.12.31", 
         verbose         = FALSE,
         spatmeth        = "file",
         spafile         = spatial_filepath,
         out_format      = "GTiff")

Understanding the downloaded data

The downloaded files are saved in subfolders within the defined output folder.

A separate subfolder is created for each processed original MODIS layer, Quality Indicator or Spectral Index with an image for each processed date. The images will be placed in the following folder structure and named using the following naming convention.

<defined_out_folder>/<shape_file_name>/<product_name>/<layer_name>/<prodcode>_<layername>_<YYYY>_<day_of_year>.<extension>

Visualising land cover in Zimbabwe

The following example uses the geom_raster() function from the ggplot2 package to visualise the downloaded IGBP land cover classification in Zimbabwe

# install.packages(c("sf", "raster", "here", "ggplot2", "viridis"))
# remotes::install_github("wmgeolab/rgeoboundaries")
library(rgeoboundaries)
library(sf)
library(raster)
library(here)
library(ggplot2)
library(viridis)
library(dplyr)

# Downloading the boundary of Zimbabwe
map_boundary <- geoboundaries("Zimbabwe")

# Reading in the downloaded landcover raster data
IGBP_raster <- raster(here::here("LandCoverData/zimbabwe/LandCover_Type_Yearly_500m_v6/LC1/MCD12Q1_LC1_2019_001.tif"))

# Transforming data
IGBP_raster <- projectRaster(IGBP_raster, crs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

# Cropping data
IGBP_raster <- raster::mask(IGBP_raster, as_Spatial(map_boundary))

# Converting the raster object into a dataframe and converting the IGBP classification into a factor
IGBP_df <- as.data.frame(IGBP_raster, xy = TRUE, na.rm = TRUE) %>%
  mutate(MCD12Q1_LC1_2019_001 = as.factor(round(MCD12Q1_LC1_2019_001)))
rownames(IGBP_df) <- c()
# Renaming IGBP classification levels
levels(IGBP_df$MCD12Q1_LC1_2019_001) <- c( "Evergreen needleleaf forests",
                                           "Evergreen broadleaf forests",
                                           "Deciduous needleleaf forests",
                                           "Deciduous broadleaf forests",
                                           "Mixed forests",
                                           "Closed shrublands",
                                           "Open shrublands",
                                           "Woody savannas",
                                           "Savannas",
                                           "Grasslands",
                                           "Permanent wetlands",
                                           "Croplands",
                                           "Urban and built-up lands",
                                           "Cropland/natural vegetation mosaics",
                                           "Snow and ice",
                                           "Barren",
                                           "Water bodies")
# Visualising using ggplot2
ggplot() + 
  geom_raster(data = IGBP_df,
              aes(x = x, y = y, fill = MCD12Q1_LC1_2019_001)) +
  geom_sf(data = map_boundary, inherit.aes = FALSE, fill = NA) +
  scale_fill_viridis(name = "Land Cover Type", discrete = TRUE) +
  labs(title = "Land Cover classification in Zimbabwe",
       subtitle = "01-01-2019 - 31-12-2019",
       x = "Longitude",
       y = "Latitude") +
  theme_minimal()

References


Last updated: 2023-01-07
Source code: https://github.com/rspatialdata/rspatialdata.github.io/blob/main/land_cover.Rmd

Tutorial was complied using: (click to expand)
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] here_1.0.1                MODIStsp_2.0.9           
##  [3] nasapower_4.0.7           terra_1.5-17             
##  [5] rnaturalearthhires_0.2.0  rnaturalearth_0.1.0      
##  [7] viridis_0.5.1             viridisLite_0.3.0        
##  [9] raster_3.5-15             sp_1.4-5                 
## [11] sf_1.0-7                  elevatr_0.4.2            
## [13] kableExtra_1.3.4          rdhs_0.7.2               
## [15] DT_0.17                   forcats_0.5.1            
## [17] stringr_1.4.0             dplyr_1.0.4              
## [19] purrr_0.3.4               readr_2.1.2              
## [21] tidyr_1.1.4               tibble_3.1.6             
## [23] tidyverse_1.3.1           openair_2.9-1            
## [25] leaflet_2.1.1             ggplot2_3.3.5            
## [27] rgeoboundaries_0.0.0.9000
## 
## loaded via a namespace (and not attached):
##   [1] readxl_1.3.1        backports_1.4.1     systemfonts_1.0.4  
##   [4] splines_4.0.3       storr_1.2.5         crosstalk_1.2.0    
##   [7] urltools_1.7.3      digest_0.6.27       htmltools_0.5.2    
##  [10] fansi_0.4.2         magrittr_2.0.1      memoise_2.0.1      
##  [13] cluster_2.1.0       gdalUtilities_1.2.1 tzdb_0.3.0         
##  [16] modelr_0.1.8        vroom_1.5.7         xts_0.12.1         
##  [19] svglite_1.2.3.2     prettyunits_1.1.1   jpeg_0.1-9         
##  [22] colorspace_2.0-3    rvest_1.0.2         rappdirs_0.3.3     
##  [25] hoardr_0.5.2        haven_2.5.0         xfun_0.30          
##  [28] rgdal_1.5-23        crayon_1.5.1        jsonlite_1.8.0     
##  [31] hexbin_1.28.2       progressr_0.10.1    zoo_1.8-8          
##  [34] countrycode_1.2.0   glue_1.6.2          gtable_0.3.0       
##  [37] webshot_0.5.2       maps_3.4.0          scales_1.1.1       
##  [40] DBI_1.1.2           Rcpp_1.0.7          progress_1.2.2     
##  [43] units_0.8-0         bit_4.0.4           mapproj_1.2.8      
##  [46] htmlwidgets_1.5.4   httr_1.4.2          RColorBrewer_1.1-2 
##  [49] wk_0.5.0            ellipsis_0.3.2      pkgconfig_2.0.3    
##  [52] farver_2.1.0        sass_0.4.0          dbplyr_2.1.1       
##  [55] utf8_1.1.4          crul_1.2.0          tidyselect_1.1.0   
##  [58] labeling_0.4.2      rlang_1.0.2         munsell_0.5.0      
##  [61] cellranger_1.1.0    tools_4.0.3         cachem_1.0.6       
##  [64] cli_3.2.0           generics_0.1.2      broom_0.8.0        
##  [67] evaluate_0.15       fastmap_1.1.0       yaml_2.2.1         
##  [70] knitr_1.33          bit64_4.0.5         fs_1.5.2           
##  [73] s2_1.0.7            nlme_3.1-149        xml2_1.3.2         
##  [76] compiler_4.0.3      rstudioapi_0.13     curl_4.3.2         
##  [79] png_0.1-7           e1071_1.7-4         reprex_2.0.1       
##  [82] bslib_0.3.1         stringi_1.5.3       highr_0.9          
##  [85] gdtools_0.2.4       lattice_0.20-41     Matrix_1.2-18      
##  [88] classInt_0.4-3      vctrs_0.3.8         slippymath_0.3.1   
##  [91] pillar_1.7.0        lifecycle_1.0.1     triebeard_0.3.0    
##  [94] jquerylib_0.1.4     data.table_1.14.2   bitops_1.0-7       
##  [97] R6_2.5.0            latticeExtra_0.6-29 KernSmooth_2.23-17 
## [100] gridExtra_2.3       codetools_0.2-16    MASS_7.3-53        
## [103] assertthat_0.2.1    rprojroot_2.0.2     withr_2.5.0        
## [106] httpcode_0.3.0      mgcv_1.8-33         parallel_4.0.3     
## [109] hms_1.1.1           grid_4.0.3          class_7.3-17       
## [112] rmarkdown_2.11      lubridate_1.8.0

Corrections: If you see mistakes or want to suggest additions or modifications, please create an issue on the source repository or submit a pull request
Reuse: Text and figures are licensed under Creative Commons Attribution CC BY 4.0.