rgdal failing on Windows with a URL specifying GeoJSON - r

devtools::install_github("SeaAroundUs/rseaaroundus")
library("seaaroundus")
regionmap("eez")
#> Error in ogrListLayers(url): Cannot open data source
Internally regionmap() is using the url http://api.seaaroundus.org/api/v1/eez/?geojson=true in a call to readOGR(dsn=url, layer=ogrListLayers(url))
ogrListLayers fails, but even if you get they layer (OGRGeoJSON) specified manually, readOGR() fails with Cannot open file
Any help greatly appreciated. I'm not a windows user, so not sure where things are going wrong.
Using: rgdal v1.1.1, GDAL v.1.11.2, R v.3.2.3, on Windows XP
There's no problem on *nix systems

Related

How to bypass repos error when installing repository on internet disconnected machine?

I've found a number of different methods that can be used to install packages on a machine that isn't connected to the internet. This post offers a fairly straightforward method to download the packages, transfer to the disconnected machine, and then to point the R installation at this custom repository.
After transferring the files, I ran the following command on the machine not connected to the internet:
update.packages(repos="C:/Users/username/Documents/R/R Repository/3.4",repos = NULL,type = "source")
After running the above line, I am getting the following error:
Error in update.packages(repos = "C:/Users/username/Documents/R/R Repository/3.4", :
formal argument "repos" matched by multiple actual arguments
Another thing I noticed is that the downloaded packages are all ".tar.gz" files, and this is a windows machine (as was the machine in the linked post above). Could this be part of the problem?
Any help would be much appreciated, thank you!
It looks like you have "repos =" twice in your call. Take out "repos = NULL" and try it again.

Error reading a .nc4 file in R (ncdf4 package)

I am trying to use a data set of .nc4 files downloaded from NASA.
The format NCDF4 is confirmed by this source.
I used download .file in R to get the database and then a simple nc_open (ncdf4 package) to test the file. Unfortunately the result is an "Unknown file format" error.
Here my replication file and my script:
download.file (url=http://hydro1.gesdisc.eosdis.nasa.gov/.../url, destfile=destination_folder/file.nc4)
All fine till this point, but when testing the files:
library(ncdf4)
setwd('destination_folder')
data <- nc_open('file.nc4')
Error in R_nc4_open: NetCDF: Unknown file format
Error in nc_open("file.nc4") :
Error in nc_open trying to open file file.nc4
Am I missing something?
Thank you.
I do not know what is wrong, but I can add the information that the problem resides in the Windows implementation of the ncdf4 package. With the following statement:
catlg<-nc_open("http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/waterbase/concentration_of_suspended_matter_in_water/catalog.nc")
I have the same problem as described in the question. However, it works perfectly in R under Linux
The file server is an OpenDAP server strictly following netcdf 4 conventions, but maybe some features are not correctly implemented in the ncdf4 package under Windows
for some reason I get the same error using [64-bit] C:\Program Files\R\R-3.4.2), but when using [64-bit] C:\Program Files\R\R-3.3.3 the ncdf4 package works fine.
not that this solves the problem, but it provides an easy work around for the time being.

Warning message In download.file: download had nonzero exit status

I am downloading data from data.gov website and I get following two types of errors in the process:
fileUrl <- "http://catalog.data.gov/dataset/expenditures-on-children-by-families"
download.file(fileUrl,destfile=".data/studentdata.csv",method="curl")
Warning message:
In download.file(fileUrl, destfile = ".data/studentdata.csv", method = "curl") :
download had nonzero exit status
I tried to remove the method="curl" as suggested in other forum, but again I get this new error
download.file(fileUrl,destfile=".data/studentdata.csv")
Error in download.file(fileUrl, destfile = ".data/studentdata.csv") :
cannot open destfile '.data/studentdata.csv', reason 'No such file or directory'
I think there are two major factors why your curl doesn't work well.
First, the problem is on your URL. fileUrl <- "http://catalog.data.gov/dataset/expenditures-on-children-by-families". In your URL, it is not referred to a csv file. So, they won't work even if you set the destination into a csv file such as destfile = ".data/studentdata.csv"
I have an example of getting a csv dataset using the same code (different dataset):
DataURL<- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD" (This link refers to a rows.csv file)
download.file(DataURL, destfile="./data/rows.csv", method="curl") (The method is quite same, using curl)
Second, previously I had the same problem that the curl does not work, even I used a proper URL that refers to a csv file. However, when I diagnosed a bit deeper, I found something interesting fact about why my curl method cannot work properly. It was my R session program. I used a 32-bit R, in which the error occurs. Later then, I tried to change the session into a 64-bit R. Amazingly, and the download status was running at that time. To see your R session architecture (whether you are using 32-bit or 64-bit), type in your R:
sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-ming32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
You have to switch your R, from 32-bit to 64-bit to avoid 'curl' call had nonzero exit status. You go to your R directory folder, and then you run a 64-bit R.
If you are using a Windows OS and installing the R in a default path folder, you can run this C:\Program Files\R\R-3.5.3\bin\x64\R.exe. (I used a version of 3.5.3, so it may be different with your version)
If you are using R-studio, you can switch the R session on the menubar Tools -> Global Options -> R version -> Change -> Use your machine's default version of R64 (64-bit) -> OK. Then restart your R-studio.
However, it depends on your OS architecture. If you are using a 32-bit OS, hence you have to find another way to solve this.
So looking at the code for download.file(...), if you specify method="curl" the function tries to use the curl shell command. If this command does not exist on your system, you will get the error above.
If you do not specify a method, the default is to use an internal R method to download, which evidently works on your system. In that case, the function is trying to put the file in .data/studentdata.csv but evidently there is not .data directory. Try taking out the ..
When this download works, you will get a text/html file, not a csv file. Your url points to a web page, not a download link. That page does have a download link, but unfortunately it is a pdf, not a csv.
Finally, if your goal is to have the data in R (is it?), and if the link actually produces a csv file, you could more easily use
df <- read.csv(fileUrl)
If I'm not very much mistaken you just have a simple typo here. I suspect you have a "data" directory, not a ".data" directory - in which case your only problem is that your destfile string needs to begin "./data", not ".data".
I was having the same problem.
Then I realized that I forget to create the "data" directory!
So try adding this above your fileURL line to create the directory first.
if(!file.exists("data")){
dir.create("data")
}
Also, if you are running a Mac, then you want to keep method="curl" when downloading a https file. I don't believe Windows has that problem hence the suggestions to remove it.
Try this:
file<-'http://catalog.data.gov/dataset/expenditures-on-children-by-families'
file<- read.csv(file)

rgdal error when connecting to postgres

I'm working with a Mac Os 10.9.2 and a R version 3.0.2.
I used dbDriver() and dbConnect() to initiate the connection to my database. Next, I tried to connect to my postgres database using
c = readOGR("PG:dbname=OB", layer="geo.countries")
This does not work, and always returns a "Cannot open file" error.
I understood from https://stat.ethz.ch/pipermail/r-sig-geo/2010-January/007519.html that the reason for this is the absence of a driver for PostgreSQL. As can be seen by using the command ogrDrivers()
Does anybody can help me on how to install the driver? Or how I can make this work? Any help is much appreciated!
Thanks!
In the absence of the right driver, gdal/ogr usually throws and error like
Unable to find driver PostgreSQL
First, make sure that the database and layer exist. If it's true that the driver for Postgres isn't installed, you'll have to re-install gdal. Using homebrew:
brew uninstall gdal
brew install gdal --with-postgresql
See also this question.
If you are really sure that gdal is properly installed, make sure that your dns is fully specified (it has more parameters than dbname)
dsn="PG:dbname=DB host=HOST user=USER password=PSSWD port=5432"
Moreover, if your table contains more than one spatial columns (layers), you have to specify the one you want:
layer = "DB.TABLE(YOUR_SPATIAL_COLUMN)"
Took me a while to find out. But it was obvious after calling function
ogrListLayers()
It works for me. Just one issue, if you have some raster column in you table, it will not be excluded (as all other layers/spatial columns). Instead, it will be loaded into spatialobject#data dataframe as text column. Quite annoying in case of big rasters. I have posted question for this.

R function zip(utils) doesn't work on my machine

I would like to use the zip(utils) function, unzip function works fine, however, call zip e.g. zip ("out", "file.txt") does not return the file ("out.zip") to the working directory and R does not return any message at all - any error message and no massage about successful compression - if compression is successful R returns info such as "deflate - 40%." I checked various R versions and files - zip still doesn't work. On another computers, everything works fine. I have Windows XP. Furthermore, when I type the wrong name of the zip file to compress e.g. zip("out", "this_file_doesnt_exist.txt") R does not return any error at all ! I don't know much about computers, what should I check ? what could be problem in my case ? I turned off antivirus that didn't help.
You can use gzip from the package R.utils
library(R.utils)
df <- data.frame(x=10)
write.csv(df, file="x1.csv")
gzip("x1.csv")
> dir()
[1] "x1.csv.gz"
For those reading this on 2020:
Install Rtools (https://cran.r-project.org/bin/windows/Rtools/) and follow the documentation steps.
Then Zip command will wokr on Windows 10 machines.
For Windows 10 Users: While the abovementioned Rtools approach did not work for me in the context of zip() {utils}, package {zip} (package description) works fine without installing Rtools and without the need to change your existing code (as the {zip} package masks the functions zip() and unzip() of {utils}).

Resources