R/hls_sites.R

Defines functions hls_sites

Documented in hls_sites

#' Plot the available sites for HLS data
#'
#' @param static Boolean: TRUE
#'
#' @return Default a static map, if Static= FALSE interactive
#' @importFrom utils download.file
#' @importFrom utils unzip
#' @export
#'
#' @examples
#' # Without any argument will plot static map
#' # For the first time, the function will try to download
#' # shapefiles from https://hls.gsfc.nasa.gov/
#' # Next, it will plot static map using ggplot2 library
#' hls_sites()
#'
#' # For interactive map using leaflet package
#' hls_sites(static= FALSE)
#'
#' # For zooming to point of interest use
#' hls_site_interactive(long = 89.50,lat = 22) #interactive
#'
hls_sites <- function(static=TRUE){
  # url of the shpefile to be downloaded
  shapefile <- "https://hls.gsfc.nasa.gov/wp-content/uploads/2018/10/hls_s2_tiles.zip"

  if(file.exists(paste0("./data/",layer ="hls_s2_tiles.shp"))){
    test_sites <- rgdal::readOGR(paste0("./data/" ,layer ="hls_s2_tiles.shp" ),
                                 layer = "hls_s2_tiles", GDAL1_integer64_policy = TRUE)
  }  else {
    # download to the current directory
    dir.create("./data")

    download.file(url = shapefile,destfile = paste0("./data/","sites.zip"))
    # unzip the file
    unzip(zipfile = paste0("./data/","sites.zip"),exdir = paste0("./data"))
    # read the shpaefile using rgdal
    test_sites <- rgdal::readOGR(paste0("./data/" ,layer ="hls_s2_tiles.shp" ),
                                 layer = "hls_s2_tiles", GDAL1_integer64_policy = TRUE)
  }

  if(static==TRUE){
  #--------------------------------------------------------------------------
  # check if the shapefile already exits
  #--------------------------------------------------------------------------
  #--------------------------------------------------------------------------
  # download world map shapefile and data
  #--------------------------------------------------------------------------
  world <- rnaturalearth::ne_countries(scale = 110, type = "countries", returnclass = "sf")

  ggplot2::ggplot(data = world) +
    ggplot2::geom_sf() +
    ggplot2::geom_sf(data = sf::st_as_sf(test_sites), fill = NA, color = "red") +
    ggplot2::xlab("Longitude") + ggplot2::ylab("Latitude") +
    ggplot2::ggtitle("HLS test sites")

    } else {

    #--------------------------------------------------------------------------
    # using leaflet plot the shapefile
    #--------------------------------------------------------------------------

    m <- leaflet::leaflet(test_sites)
    m <- leaflet::addTiles(m)
    m <- leaflet::addPolylines(m, weight = 1,label = test_sites$Name,labelOptions =   leaflet::labelOptions(
        noHide = FALSE,textsize = "16px" ))
    m


  }

}
mshmandal/rhls documentation built on Dec. 21, 2021, 10:10 p.m.