License: All the original materials used in this workshop are available under CC-BY-SA.
There are several packages in R
dealing with satellite images (e.g., MODISTools
or satellite
). So, why developing a new package?
Centralize: Three major sources of freely accessible satellite images; Landsat, MODIS, and Sentinel. Their combination increases the spatio-temporal resolution of remotely sensed information (e.g., data fusion techniques as in Belgiu and Stein (2019)). A single access point can foster multi-program analyses.
Standardize: Satellite programs differ in their data standards and formats (compression formats, naming conventions, bands, tiling systems, etc.). Web services offer different sharing protocols (GUIs and APIs) and data products. Programming can deal with this complexity in the background and offer a consistent experience to the user.
Automate: The analysis of the earth’s surface patterns and dynamics frequently requires long series of images. Also, data-sets must undergo multiple transformations from acquisition to analysis. We found convenient to develop a comprehensive package that facilitates the use of computationally efficient routines.
There is a large number of functions in RGISTools
. Functions are named in a systematic way to easily find the function best suited for an specific satellite program and purpose:
ls
: Functions devoted to Landsat imagery.
ls7
: for Landsat-7.ls8
: for Landsat-8.mod
: Functions devoted to MODIS imagery.sen
: Functions devoted to Sentinel(-2) imagery.Search
: find imagery for a given ROI and time-span.Preview
: inspect the overlap with the ROI and cloud coverage.Download
: acquire images massively.Mosaic
: combine tiles/scenes and clip the extent of the ROI.FolderToVar
: calculate indexes, such as NDVI.CloudMask
: remove the values of cloudy pixels.Composition
: generate regular series and improve image quality (e.g., Wolfe, Roy, and Vermote (1998)).SmoothingIMA
: gap-filling and smoothing data to resolve missing values and outliers (Militino et al. 2019).For instance, the function that downloads Sentinel images is called senDownload()
. There are other functions in the package not mentioned in this list. Right now, the package is focused on multispectral images.
Today’s demo:
In this first session:
Install the latest version of the package from github, and load it:
library(devtools)
install_github("spatialstatisticsupna/RGISTools")
library(RGISTools)
The package interacts with the EarthData and SciHub web services. Web services provide access to archives of satellite images. They require a profile that you can create here and here. For this workshop, use the following credentials:
username <- "geomundus2019"
password <- "Geomundus2019"
(The profile will be deleted after the workshop. Make sure to register)
Since we are dealing with administrative divisions, we use the database of Global Administrative Division Maps provided by raster
(Hijmans 2019). The following code downloads the shape-file of Spain and its provinces:
spain <- getData('GADM', country = "Spain", level = 2)
From there, we select the region of Castellón:
castellon <- subset(spain, NAME_2 == "Castellón")
We can display the results using tmap
(Tennekes 2018). The package is loaded with RGISTools
and used for visualization. The tmap
package supports many spatial data formats, is flexible, and allows interactive and static maps with the same code.
We set the option to display interactive maps:
tmap_mode("view")
## tmap mode set to interactive viewing
We display our SpatialPolygonDataFrame
of Castellon on a map:
tmap_leaflet(tm_shape(castellon) +
tm_polygons(col = "red") +
tm_view(set.view = 4))
Germany
.Replace the <>
code conveniently:
germany <- getData('GADM', country = <COUNTRYNAME>, level = 2)
nordrhein <- subset(germany, NAME_1 == <REGIONAME>)
tmap_leaflet(tm_shape(nordrhein) +
tm_polygons(col = "red") +
tm_view(set.view = 4))
The first task is to search the imagery from a data product that intersects our ROI and covers the time period of analysis. This is the purpose of modSearch()
.
The function requires the definition of:
product
): The MODIS program provides NDVI and LST images. These are named MOD13A2 and MOD11A2 respectively. The full catalogue of products is available here.region
): An sf
, Spatial*
, or raster
class object.dates
): A (vector of) Date
class object(s).sres.ndvi <- modSearch(product = "MOD13A2",
region = castellon,
dates = seq(as.Date("2001-01-01"),
as.Date("2018-12-31"),1))
The function returns a vector of URLs with the series of MODIS images concerning our ROI and time-period. We can extract useful information from the results.
The total number of images that are found:
length(sres.ndvi)
## [1] 1656
The tiles intersecting our ROI with modGetPathRow()
:
sres.tileid <- unique(modGetPathRow(sres.ndvi))
sres.tileid
## [1] "h18v05" "h17v04" "h17v05" "h18v04"
The capturing dates of our images with modGetDate()
:
sres.dates <- unique(modGetDates(sres.ndvi))
sres.dates[1:4]
## [1] "2001-01-01" "2001-01-17" "2001-02-02" "2001-02-18"
length(sres.dates)
## [1] 414
Results show that there are 1656 images, as a combination of 4 tiles (h18v05, h17v04, h17v05, h18v04) and 414 dates (approximately, 2 n/month x 12months/year x 18 years). The dates confirm that temporal frequency of this product is 16 days.
nordrhein
variable) during the same time period as before.(a) How many images did you find?
(b) How many tiles intersect with Nordrhein-Westfalen?
(c) What is the temporal frequency?
Replace the <>
code conveniently:
sres.lst <- modSearch(product = <PRODUCT>,
region = <REGION>,
dates = <DATES>)
tileid.lst <- <CODE>
sres.dates <- <CODE>
RGISTools
works with satellite images locally on your computer (images stored on disk), which has two implications:
R
(GDAL via sf
, Pebesma (2018) whenever possible). The series of images are loaded at the end, when the data-set contains only useful information for the analysis.The function modDownload()
requires (at least):
searchres
argument) from Earth Data (sres.ndvi
).AppRoot
argument) where the images are placed.username
and password
arguments).extract.tif = TRUE
) the bands from the original files (in Hierarchical Data Format or HDF
) into GeoTiff
.As an example, we download the images for the first day (4 tiles) in a new directory ./exercises/ndvi
:
wdir.ndvi <- file.path("./", "exercises", "ndvi")
modDownload(searchres = sres.ndvi[1:4],
AppRoot = wdir.ndvi,
username = "geomundus2019",
password = "Geomundus2019",
extract.tif = TRUE)
The function creates two new folders:
./exercises/ndvi/hdf
: contains the HDF files../exercises/ndvi/tif
: contains the extracted bands, being each band a separate GeoTiff
.These 4 images take around 51 MB and 150 MB with the HDF
and GeoTiff
files! To be more efficient, we can specify:
bFilter
: converts into GeoTiffs
only the relevant bands.raw.rm
: removes the raw files once the GeoTiffs
are created.If we remove the tif
folder:
wdir.ndvi.tif <- file.path(wdir.ndvi, "tif")
unlink(wdir.ndvi.tif, recursive = TRUE)
And we run the function adding bFilter = "NDVI"
to select only the “NDVI” band and remove the original files by setting raw.rm = TRUE
:
modDownload(searchres = sres.ndvi[1:4],
AppRoot = wdir.ndvi,
username = "geomundus2019",
password = "Geomundus2019",
extract.tif = TRUE,
bFilter = "NDVI",
raw.rm = TRUE)
Then, the folders:
./exercises/ndvi/hdf
: has no image../exercises/ndvi/tif
: contains one band.We went from 201 MB to 11 MB. Note that the second time, the function runs faster. modDownload()
detects that the original files are there and therefore, only converts the bands to GeoTiffs
.
"LST_Day_1km"
) and indicate raw.rm = TRUE
.Replace the <>
code conveniently:
wdir.lst <- <LSTDIRECTORY>
modDownload(<ARGUMENTS>)
Mosaicking means joining images captured on the same date but belonging to different tiles. Cropping means removing the pixels outside the bounding box of the ROI. Both tasks are carried out by modMosaic()
, which requires:
src
argument) that stores the GeoTiff
s.region
argument) around which images are cropped.out.name
) where the results will be saved.AppRoot
argument) defined with the argument.modMosaic(src = wdir.ndvi.tif,
region = castellon,
out.name = "castellon",
AppRoot = wdir.ndvi)
You will notice the new folder that has been added to the current working directory. Now, the folders are:
./exercises/ndvi/hdf
: has no image../exercises/ndvi/tif
: contains one band../exercises/ndvi/castellon
: contains the mosaicked image.Thanks to cropping, now the size of memory storage consumed by our data-set is roughly 0.36 MB.
GeoTiff
image is saved.nordhrein
variable).Replace the <>
code conveniently:
wdir.lst.tif <- <LSTDIRECTORYTIF>
modMosaic(src = wdir.lst.tif,
<OTHERARGUMENTS>,
AppRoot = <LSTDIRECTORY>)
We can import the imagery using the stack()
function from the raster
package (Hijmans 2019) as follows:
wdir.ndvi.mos <- file.path(wdir.ndvi, "castellon")
files.ndvi <- list.files(wdir.ndvi.mos, full.names = TRUE, recursive = TRUE)
imgs.ndvi <- stack(files.ndvi)
It is necessary to re-scale the image to the [-1,1] NDVI range. Information about the scaling parameter can be found in the product catalogue. There, we find that the re-scaling factor is 0.0001:
imgs.ndvi <- imgs.ndvi * 0.0001
RGISTools
has the genPlotGIS()
function to visualize your data. It is a wrapper function of tmap
(Tennekes 2018), configured to rapidly display the spatial data usually handled within the package. The basic inputs are:
RasterStack
of satellite images (r
argument).region
argument).tmap_mode("view")
tmap_leaflet(
genPlotGIS(r = imgs.ndvi,
region = castellon,
tm.raster.r.title = "NDVI",
tm.raster.r.alpha = 0.35), add.titles = FALSE)
Replace the <>
code conveniently:
wdir.lst <- <LSTDIRECTORYMOSAIC>
files.lst <- <FILES>
imgs.lst <- stack(files.lst)
scale.factor <- <SCALEFACTOR> # no re-scaling, set to 1
imgs.lst <- imgs.lst * scale.factor
genPlotGIS(<CODE>)
Belgiu, Mariana, and Alfred Stein. 2019. “Spatiotemporal image fusion in remote sensing.” Remote sensing 11 (7): 818. https://doi.org/10.3390/rs11070818.
Hijmans, Robert J. 2019. Raster: Geographic Data Analysis and Modeling. https://CRAN.R-project.org/package=raster.
Militino, Ana F, M Dolores Ugarte, Unai Pérez-Goya, and Marc G Genton. 2019. “Interpolation of the Mean Anomalies for Cloud Filling in Land Surface Temperature and Normalized Difference Vegetation Index.” IEEE Transactions on Geoscience and Remote Sensing 57 (8): 6068–78. https://doi.org/10.1109/TGRS.2019.2904193.
Pebesma, Edzer. 2018. “Simple Features for R: Standardized Support for Spatial Vector Data.” The R Journal 10 (1): 439–46. https://doi.org/10.32614/RJ-2018-009.
Tennekes, Martijn. 2018. “tmap: Thematic Maps in R.” Journal of Statistical Software 84 (6): 1–39. https://doi.org/10.18637/jss.v084.i06.
Wolfe, Robert E, David P Roy, and Eric Vermote. 1998. “MODIS Land Data Storage, Gridding, and Compositing Methodology: Level 2 Grid.” IEEE Transactions on Geoscience and Remote Sensing 36 (4): 1324–38. https://doi.org/10.1109/36.701082.