I would like to configure an automatic zoom at the canvas of the shapefile layer that I upload. For that it would be necessary that a function automatically detects the latitude and the longitude of this layer but my researches were not conclusive. Do you know any function that could be useful to me?
Server.UI
library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(utf8)
library(rgeos)
library(shinyjs)
library(sp)
library(V8)
library(shinyalert)
library(leaflet.extras)
library(raster)
shinyServer(function(input, output, session){
#--------- ADD LEAFLET MAP --------#
output$map <- renderLeaflet({
leaflet() %>%
enableTileCaching() %>%
addProviderTiles("Esri.WorldImagery", group = "Esri World Imagery",
options = providerTileOptions(minZoom = 2, maxZoom = 17),
tileOptions(useCache = TRUE, crossOrigin = TRUE)) %>%
addTiles(group = "OSM", urlTemplate = "https://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png",
attribution = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
options = providerTileOptions(minZoom = 2, maxZoom = 17),
tileOptions(useCache = TRUE, crossOrigin = TRUE)) %>%
addMiniMap(toggleDisplay = T) %>%
addScaleBar(position = 'bottomleft') %>%
setView(lng = 97.963,lat = 20.380, zoom = 6 ) %>%
addLayersControl(baseGroups = c("Esri World Imagery", "OSM"))
})
#-------- READ SHAPEFILE --------#
#-------- SAVE IN TEMP FOLDER --------#
#-------- CREATE A CSV --------#
uploadShpfile <- reactive({
if (!is.null(input$zip)) {
zipFile <- input$zip
zipPath <- substr(zipFile$datapath, 1, nchar(zipFile$datapath) - 5)
unzip(zipFile$datapath, exdir = zipPath)
pwd <- getwd()
updir <- dirname(zipFile$datapath[1])
setwd(updir)
for (i in 1:nrow(zipFile)) {
file.rename(zipFile$datapath[i], zipFile$name[i])
}
shpName <- zipFile$name[grep(zipFile$name, pattern = "*.shp")]
shpPath <- paste(updir, shpName, sep = "/")
setwd(updir)
Layers <- ogrListLayers(shpPath)
shpName <- readOGR(shpPath)
shpName <- spTransform(shpName,CRS("+proj=longlat +datum=WGS84"))
shapefile(shpName, paste(shpPath, Layers, "_WGS84.shp", sep = ""))
write.table(paste(shpPath, Layers, "_WGS84.shp", sep = ""),
file = "info_shp.csv",
row.names = FALSE,
col.names = FALSE,
quote = FALSE)
shpName
}
})
#-------- ADD SHAPEFILE --------#
observeEvent(input$zip, {
data = uploadShpfile()
map = leafletProxy("map")
if (!is.null(uploadShpfile())){
if(inherits(data, "SpatialPolygons")){
shinyalert("Successful upload !", type = "success")
cent <- gCentroid(spgeom = uploadShpfile(), byid = FALSE)
leafletProxy("map")%>%
addPolygons(data = uploadShpfile(),
stroke = TRUE,
fillOpacity = 0.5)
}
if(inherits(data, "SpatialPoints")){
shinyalert("Successful upload !", type = "success")
cent <- gCentroid(spgeom = uploadShpfile(), byid = FALSE)
leafletProxy("map") %>%
addCircleMarkers(data = uploadShpfile(),
stroke = TRUE,
radius = 6,
fillOpacity = 0.9)
}
}
})
ui.R :
library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(utf8)
library(rgeos)
library(shinyjs)
library(sp)
library(V8)
library(shinyalert)
library(leaflet.extras)
library(raster)
shinyUI(
dashboardPage(
dashboardHeader(title ="Sen2extract"),
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Map", tabName= "carte", icon = icon("globe"))
)
),
dashboardBody(
tabItems(
tabItem(tabName ="carte",
fluidRow(
fileInput(inputId = "zip", label = "Upload your file (.zip) :", multiple = FALSE, accept = c('.zip')),
leafletOutput(outputId = "map", width="100%", height = 940)
)
)
)
)
)
)