Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. server.r:
  2.  
  3. library(shiny)
  4. library(shinydashboard)
  5. library(leaflet)
  6. library(rgdal)
  7. library(utf8)
  8. library(sf)
  9. library(rgeos)
  10.  
  11. shinyServer(function(input, output, session){
  12.  
  13.  
  14. # create reactive upload file function to store data
  15. uploadShpfile <- reactive({
  16. if (!is.null(input$shp)) {
  17. shpDF <- input$shp
  18. pwd <- getwd()
  19. updir <- dirname(shpDF$datapath[1])
  20. setwd(updir)
  21. for (i in 1:nrow(shpDF)) {
  22. file.rename(shpDF$datapath[i], shpDF$name[i])
  23. }
  24. shpName <- shpDF$name[grep(x = shpDF$name, pattern = "*.shp")]
  25. shpPath <- paste(updir, shpName, sep = "/")
  26. setwd(pwd)
  27. shpFile <- readOGR(shpPath)
  28. shpFile <- spTransform(shpFile,CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0"))
  29. }
  30. })
  31.  
  32. output$map <- renderLeaflet({
  33.  
  34. leaflet() %>%
  35. addProviderTiles("Esri.WorldImagery", group = "Esri World Imagery", options = providerTileOptions(minZoom = 2, maxZoom = 17)) %>%
  36. addTiles(group = "OSM", options = providerTileOptions(minZoom = 2, maxZoom = 17)) %>%
  37. addMiniMap(toggleDisplay = T) %>%
  38. setView(lng = 97.963,lat = 20.380, zoom = 6 ) %>%
  39. addLayersControl(baseGroups = c("Esri World Imagery", "OSM"))
  40. })
  41.  
  42. observeEvent(input$shp, {
  43. if (!is.null(uploadShpfile())) {
  44. cent <- gCentroid(spgeom = uploadShpfile(), byid = FALSE)
  45. leafletProxy("map") %>%
  46. addPolygons(data = uploadShpfile()) %>%
  47. setView(lat = slot(cent, "coords")[[2]], lng = slot(cent, "coords")[[1]], zoom = 7)
  48. }
  49. })
  50. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement