0

I would like to unzip a .zip file in R. After unzipping, I would like to read the file .shp, so I did the following:

library(rgdal)

temp <- tempfile() 
download.file("https://github.com/JovaniSouza/JovaniSouza5/blob/ac61a729b3856112bcb100f4dfb2c900565fdb54/gasoduto1.zip",temp) 
con <- readOGR(unz(temp, "gasoduto_SIRGAS_2000.shp"))

The following error appears:

Error in readOGR(unz(temp, "gasoduto_SIRGAS_2000.shp")) : 
  is.character(dsn) is not TRUE
Antonio
  • 1,091
  • 7
  • 24
  • https://stackoverflow.com/questions/3053833/using-r-to-download-zipped-data-file-extract-and-import-data – dcsuka Jul 19 '22 at 00:12
  • What have you attempted? `download.file(.)` and then `unzip(.)` should work. If you didn't know that a function named `unzip` exists in R's `utils` package (installed by default), then you can search for packages/functions with `??unzip` (two question marks) and it'll inform you of what it finds among your installed packages. – r2evans Jul 19 '22 at 00:17
  • Thanks @r2evans! I tried the following: `temp <- tempfile() download.file("https://github.com/JovaniSouza/JovaniSouza5/blob/ac61a729b3856112bcb100f4dfb2c900565fdb54/gasoduto1.zip",temp) con <- readOGR(unz(temp, "gasoduto1/gasoduto_SIRGAS_2000.shp"))`. But it did not work. I'd like to unzip, then read the file. shp. – Antonio Jul 19 '22 at 02:13
  • @Antonio If yu did a search (and one always does a search before posting) and you found some answers, then you should have posted the code and the results of your efforts. If the nominated duplicate doesn't address the question, then post what you coded and the problems you see. – IRTFM Jul 19 '22 at 02:31
  • Sorry @IRTFM, you right! I adjusted the question. – Antonio Jul 19 '22 at 04:13
  • (1) Try that URL but in a regular web browser. (2) Note that it takes you to an HTML page (which, if you check your `temp` file, is what it is: HTML). (3) Click on the `Download` button. If you want to have a URL that works without needing the brower, right-click and "Copy link". (4) Run `unzip(temp, list=TRUE)` and see that the filenames have no leading path. (5) Try `readOGR(unz(temp, "gasoduto_SIRGAS_2000.shp"))` and see that `readOGR` doesn't like file connections. I don't use `readOGR` so I don't know how best to continue (I tried extracting the file and opening normally, failed). – r2evans Jul 19 '22 at 11:37

0 Answers0