Overview

Objectives

Learn to search and query data, establishing a connection from the Images Archives (for example, ESA), and your computer through an automated script. This step it is quite useful when working with big datasets.

Procedure

1. Install and/or load necessary packages

As for every new session of R, you should set your working directory and load the necessary libraries. The package for downloading the satellite images is called getSpatialData. Use ipak to install required packages.

# Install the current beta version of the package directly from source
devtools::install_github("16EAGLE/getSpatialData")
# Load libraries
ipak <- function(pkg){
    new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
    if (length(new.pkg)) 
        install.packages(new.pkg, dependencies = TRUE)
    sapply(pkg, require, character.only = TRUE)
}


packages <- c("rgdal", "raster", "dplyr","sf", "magrittr", "sp", "getSpatialData")
ipak(packages)

2. Set the working environment

# Set the working directory ("wd") and assign it to an object (here, "w")
w <- setwd("your_path\\here\\")

# Check where your wd is
getwd()

3. Make a query for Landsat data

For this task, you have first to decide from which area you need data, from which Satellite, from which provider you want to download the data from, and characteristics of the images like date and cloud cover.

In this example, we will be downloading data from Beirut that covers an specific area. Therefore, we define this area with the use of a shapefile that covers our area of interest (AOI).

## Define an AOI 
aoi <- readOGR(w, "aoi_beirut")

Afterwards, login in into your USGS account. In case you have not access data, you can do that directly at the USGS Archive website

## set login credentials and archive directory
login_USGS(username = "your_username") #asks you for password
set_archive("/path/to/archive/") # where to save your images that will be downloaded

We will download images from “2018-09-04” and “2018-09-12”, with less than 1% cloud cover.

## get available products and select one
product_names <- getLandsat_products()
product_names
##  [1] "SP_TILE_DSWE"     "LANDSAT_8_C1"     "SP_TILE_FSCA"     "LIMA"            
##  [5] "LANDSAT_ETM_C1"   "LANDSAT_TM_C1"    "LIMA_MOSAIC"      "SP_TILE_BA"      
##  [9] "ORTHO_MOSAIC_ETM" "LANDSAT_MSS_C1"   "ORTHO_MOSAIC"     "ESAT_ETM_PAN"    
## [13] "ESAT_ETM_NOPAN"   "ESAT_TM"          "ORTHO_MSS_SCENE"  "SYS_ETM"         
## [17] "MSS_FILM"         "RBV_FILM"         "TM_FILM"          "ARD_TILE"
## set aoi for the query
set_aoi(aoi)

Explore in a separate windows, all the possible scenes available given the input parameters. Check name, date and the other variables provided.

## Use getLandsat_query to search for data (using the session AOI)
records <- getLandsat_records(time_range = c("2018-09-04", "2018-09-12"), 
                             name = product_names[2]) #or LANDSAT_8_C1
## Searching records for product name 'LANDSAT_8_C1'...
View(records)
## Filter the records
colnames(records) #see all available filter attributes
##  [1] "product_group"          "product"                "record_id"             
##  [4] "entity_id"              "summary"                "date_acquisition"      
##  [7] "start_time"             "stop_time"              "date_modified"         
## [10] "footprint"              "tile_id"                "tile_number_horizontal"
## [13] "tile_number_vertical"   "preview_url"            "meta_url"              
## [16] "meta_url_fgdc"          "sensor_id"              "cloudcov_land"         
## [19] "cloudcov"               "level"                  "cloudcov.1"            
## [22] "ImageQuality"
# Explore the column of the Land cloud cover and Scene cloud cover. What is the difference?
View(records$cloudcov_land)
View(records$cloudcov)
# Filter images that have a land cloud cover < OR = 1%
cloud5_filtered <- records[as.numeric(records$cloudcov_land) <= 1,]

# View records table
View(cloud5_filtered)
# Preview a single record on a mapview with session AOI
x11()
getLandsat_preview(record = cloud5_filtered[1,])

# For the previous function, you can define whether to show the aoi or not. Try adding: 
###### 'show_aoi = FALSE' 

Figure 1. Preview of Landsat 8 scene, ready to download. Provided by USGS

Now, try yourself to make a second query for Sentinel image. Use the following commands. The pattern is similar as for the Landsat search.

login_CopHub() - define your Copernicus Open Access login credentials once for the present R session to be able to call each getSentinel* function without defining login arguments each time you use them.

getSentinel_query() - querys the Copernicus Open Access Hubs for Sentinel-1, Sentinel-2, Sentinel-3, Sentinel-5 Precursor and Sentinel GNSS data and returns a data frame containing the found records (rows) and their attributes (columns).

getSentinel_restore() requests to restore Setninel datasets that have been archived by ESA to the Copernicus Long-Term Archive (LTA) (see argument check_avail of getSentinel_query).

getSentinel_preview() - uses the output of getSentinel_query() to preview (quick-look) a user-selected record even before downloading it. By default, the preview is displayed corner-georeferenced in a map viewer in relation to the session AOI.

getSentinel_data() - uses the output of getSentinel_query() to download Sentinel data.