Download shapefile (writeOGR)

438 views
Skip to first unread message

ForestCAS

unread,
May 19, 2014, 4:18:41 PM5/19/14
to shiny-...@googlegroups.com
Hi Everyone, 

I am trying to write a code to convert x,y points from .csv to shapefile using writeOGR.
When I click in the download button, the a new page opens, but it doesn't download the shapefile.

Do you have a suggestion?

Thanks a lot!
Best Regards
ForestCAS


runApp(list(
  ui = bootstrapPage(
    fileInput('inputdata', 'Input file',accept=c('.csv')),
    plotOutput("xyPlot"),
    downloadButton('downloadData', 'DownloadSHP')
  ),
  server = function(input, output) {
    
    output$xyPlot <-  renderPlot({
      
      myXY<- input$inputdata
      if (is.null(myXY)) 
        return(NULL)       
      
      xyPoints<-read.table(myXY$datapath, sep=",", header=T)
      
      SHP <- SpatialPointsDataFrame(coords= cbind(xyPoints[,1:2]), data =  xyPoints)
      proj4string = CRS('+proj=longlat +datum=NAD83')
      
      plot(SHP)
      
      output$downloadData <- downloadHandler(
        
        filename = function(file) { paste('ShapeFile','.shp', sep='') },
        content = function(file) {
          
          writeOGR(SHP, dsn="DownloadShapes", driver="ESRI Shapefile", layer="XY_Shapefile")})   
      
    }) 
  }
))
XY_points.csv

Robin Cura

unread,
May 19, 2014, 4:50:31 PM5/19/14
to ForestCAS, shiny-...@googlegroups.com
Hi,

downloadHandler can't handle multiple files, as in a shape.

So, you first have to zip those files, and then send the zip back to the user.
Check this gist, I have this working in one of my apps : https://gist.github.com/RCura/b6b1759ddb8ab4035f30

HTH,

Robin

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Abhijeet Patil

unread,
May 17, 2017, 2:10:40 PM5/17/17
to Shiny - Web Framework for R, carlos_en...@outlook.com, robin...@parisgeo.cnrs.fr
Hi Guys,

I am trying to create small converter to convert csv to shapefile. I am using code suggested by Robin but could not download the shapefile. I have attached my error.

I am using this code

runApp(list(
  ui = bootstrapPage(
    fileInput('inputdata', 'Input file',accept=c('.csv')),
    plotOutput("xyPlot"),
    downloadButton('downloadShp', 'DownloadSHP')
  ),
  server = function(input, output) {
    
    output$xyPlot <-  renderPlot({
      
      myXY<- input$inputdata
      if (is.null(myXY)) 
        return(NULL)       
      
      xyPoints<-read.table(myXY$datapath, sep=",", header=T)
      
      SHP <- SpatialPointsDataFrame(coords= cbind(xyPoints[,1:2]), data =  xyPoints)
      proj4string = CRS('+proj=longlat +datum=NAD83')
      
      plot(SHP)
      
      # output$downloadData <- downloadHandler(
      #   
      #   filename = function(file) { paste('ShapeFile','.shp', sep='') },
      #   content = function(file) {
      #     
      #     writeOGR(SHP, dsn="DownloadShapes", driver="ESRI Shapefile", layer="XY_Shapefile")})
      
      
      output$downloadShp <- downloadHandler(
        filename = 'fbCrawlExport.zip',
        content = function(file) {
          if (length(Sys.glob("fbCrawl.*"))>0){
            file.remove(Sys.glob("fbCrawl.*"))
          }
          writeOGR(SHP, dsn="fbCrawl.shp", layer="fbCrawl", driver="ESRI Shapefile")
          write.csv(as.data.frame(cbind(SHP@data, as.data.frame(SHP@coords))), "fbCrawl.csv")
          zip(zipfile='fbCrawlExport.zip', files=Sys.glob("fbCrawl.*"))
          file.copy("fbCrawlExport.zip", file)
          if (length(Sys.glob("fbCrawl.*"))>0){
            file.remove(Sys.glob("fbCrawl.*"))
          }
        }
      )
      
    }) 
  }
))

Thanks in advance
Capture.PNG
Reply all
Reply to author
Forward
0 new messages