当我上传压缩的shapefile时,我想更改地图的视图。为此,我认为函数“ flyToBounds”可能很有用,但我不知道如何使它适应我的代码。我有一个添加传单地图的功能,然后有一个读取zipfile的功能,另一个在此地图上添加shapefile的功能。
# server.R
#--------- ADD PACKAGES --------#
library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(rgeos)
library(sp)
library(leaflet.extras)
library(raster)
#--------- APPLICATION INSTRUCTIONS --------#
shinyServer(function(input, output, session){
#--------- ADD LEAFLET MAP --------#
output$map <- renderLeaflet({
leaflet() %>%
enableTileCaching() %>%
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)) %>%
setView(lng = 97.963,lat = 20.380, zoom = 6 )
})
#-------- READ ZIP --------#
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 = ""))
shpName
}
})
#-------- ADD SHAPEFILE --------#
observeEvent(input$zip, {
data = uploadShpfile()
map = leafletProxy("map")
if (!is.null(uploadShpfile())){
if(inherits(data, "SpatialPolygons")){
shinyalert("Successful upload !", type = "info", timer = 2000)
cent <- gCentroid(spgeom = uploadShpfile(), byid = FALSE)
leafletProxy("map")%>%
addPolygons(data = uploadShpfile(),
stroke = TRUE,
# color = "#00FFEC",
# fillColor = "white",
fillOpacity = 0.5)
}
if(inherits(data, "SpatialPoints")){
shinyalert("Successful upload !", type = "info")
cent <- gCentroid(spgeom = uploadShpfile(), byid = FALSE)
leafletProxy("map") %>%
addCircleMarkers(data = uploadShpfile(),
stroke = TRUE,
# color = "white",
# fillColor = "#00FFEC",
radius = 6,
fillOpacity = 0.9)
}
}
})
})
# ui.R
#--------- ADD PACKAGES --------#
library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(rgeos)
library(sp)
library(leaflet.extras)
library(raster)
#--------- USER INTERFACE --------#
shinyUI(
dashboardPage(
dashboardHeader(title =""),
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Map", tabName= "carte", icon = icon("globe"))
)
),
dashboardBody(
tabItems(
#--------- ELEMENTS TAB "carte" --------#
tabItem(tabName ="carte",
fluidRow(
box(
width = 3,
title = "Settings",
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
fileInput(inputId = "zip", label = "Upload your zipped shapefile :", multiple = FALSE, accept = c('.zip'))
),
box(
width = 9,
title = "Map",
status = "primary",
solidHeader = TRUE,
collapsible = FALSE,
height = "100%",
leafletOutput(outputId = "map", width="100%", height = 940)
)
)
)
)
)
)
)
这是我的代码。