R/hls_site_interactive.R

Defines functions hls_site_interactive

Documented in hls_site_interactive

#' Plots interactive map for available sites for HLS data
#'
#' @param long Provide longitude in numbers
#' @param lat Provide latitude in numbers
#'
#' @return Interactive map using leaflet
#' @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_site_interactive <- function(long= 89.50, lat=22){
  #--------------------------------------------------------------------------
  # Initial parameters
  #--------------------------------------------------------------------------
  # the current directory

  # url of the shpefile to be downloaded
  shapefile <- "https://hls.gsfc.nasa.gov/wp-content/uploads/2018/10/hls_s2_tiles.zip"
  #--------------------------------------------------------------------------
  # check if the shapefile already exits
  #--------------------------------------------------------------------------

  if(file.exists(paste0("./data/",layer ="hls_s2_tiles.shp"))){
    # read the shapefile using rgdal
    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
    download.file(url = shapefile,destfile = paste0("./data/","sites.zip"))
    # unzip the file
    unzip(zipfile = paste0("./data/","sites.zip"),exdir = paste0("./data/"))

    # read the shapefile using rgdal
    test_sites <- rgdal::readOGR(paste0("./data/" ,layer ="hls_s2_tiles.shp" ),
                          layer = "hls_s2_tiles", GDAL1_integer64_policy = TRUE)

  }
  #--------------------------------------------------------------------------
  # 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 <- leaflet::addMarkers(m,lng = long,lat =lat, popup = "Point of interest" )
  m <- leaflet::setView(m, lng = long, lat = lat, zoom = 6)
  m
}
mshmandal/rhls documentation built on Dec. 21, 2021, 10:10 p.m.