Can't transforme sf feature on shinyapps.io - r

In my shinyapp I try to transform a sf feature (with EPSG:4326) sf::st_transform(sf_feature, crs = sf::st_crs(4839)), but when deploying on shinyapps.io an error occurs:
GDAL Error 1: No PROJ.4 translation for source SRS, coordinate transformation initialization has failed.
On the shinyapps.io server the geolibraries are as follows:
GEOS 3.5.1, GDAL 2.2.2, PROJ 4.9.2

With a free account shinyapps.io blocks connections to other servers. As far as I know the sf package depends on GEOS, GDAS and other services to provide support for certain features (see the web site). That is most likely the reason, why it does not work on a free shinyapps.io account.
Solutions are obvious:
use other packages
get a paid account
Aside from that I do not think there is much you can do.

Related

Use R inside a docker container (docker run) with packages already imported

I use R within a docker container to run some R-scripts.
Using docker run, it works quite well and fast besides the import of the packages which always take quite a while.
Especially importing the sf package takes 10 times the runtime of the actual R-script:
library("sf")
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
Is there any possibility to fasten up the import of the packages, for example importing the files in the docker build process?
Yes, you can build your docker image with {sf} installed at build time.
Note that it may be somewhat tricky, as {sf} has dependencies (e.g. GDAL, GEOS, PROJ) at system level, which in turn have their dependencies.
For an example of Dockerfile that installs a specific version of {sf} dependencies, consider for inspiration this one: https://github.com/jlacko/RCzechia/blob/master/data-raw/Dockerfile
What it does is:
it pulls R 4.1.0 version Rocker image with R Studio
compiles the big three dependencies from source as my use case requires a specific version; this may be overkill in yours and takes about forever - you may want to replace this step with an install of complied binaries as in next step
installs system level (ubuntu) dependency binaries required for R packages
installs several R packages from time versioned CRAN mirror - note how Microsoft, of all companies, helpfully provides daily snapshots of CRAN. This is very helpful for reproducibility.
Your use case will likely call for a different mix of R packages, but I hope this gives you an inspiration for going forward.

How can I start a Docker container and tell R to use a specific version of a system dependency being run in said container?

I'm wanting to use the R package MODIStsp to download and process MODIS data, but currently there is an issue with GDAL's HDF4 driver package in my version of Linux (Manjaro). I've contacted the package maintainer, but in the mean time, I'd like to use a Dockerized version of GDAL on the backend to complete the task.
I know from within R it is possible to set the version of GDAL used ala
gdalUtils::gdal_setInstallation("/path/to/gdal/here")
My question is this: after I've pulled the osgeo/gdal image, how I can run the container in such a way to make it available to be used in R? And then how would the path get constructed in the function above?
I would turn this around and look into setting up a Docker container that has the versions of gdal (and then MODIStsp) that work for you.
I just updated a machine this morning running Ubuntu 20.04 with the usual PPA for CRAN packages (over 5000 of them) which relies on the underlying UbuntuGIS-stable PPA. That gave me, inter alia, GDAL 3.28 and rgdal 1.5-27 -- and I could just install MODIStsp 2.0.6 as a binary too.
I also have a couple of base container you could use for this, and could lend a hand if that is of interest.

Install sf for r using gdal from postgresapp

I am on MacOSX 11.4 Big Sur.
I have just installed on my computer https://postgresapp.com, which bundles PostgreSQL together with the needed libraries for PostGIS (proj, geos, and gdal).
I am able to use command line functions like ogr2ogr, gdalinfo etc. from my terminal, indicating that gdal is successfully installed.
I now want to install the "sf" package for R version 4.1.0. I am able to successfully install "sf", but the PostgreSQL driver is missing when I use the native installation
install.packages("sf")
According to the sf documentation and things I have read elsewhere on stack, this is a known issue for MacOSx, and the recommended workaround for this problem is to separately install gdal using homebrew or the kyngchaos binaries. However, I would like to use the gdal installation used by the postgresapp binary mentioned earlier so that there is only one installation on my computer..
According to the postgresapp installation documentation (https://postgresapp.com/documentation/install.html), the relevant paths are:
Binaries: /Applications/Postgres.app/Contents/Versions/latest/bin
Headers: /Applications/Postgres.app/Contents/Versions/latest/include
Libraries: /Applications/Postgres.app/Contents/Versions/latest/lib
Therefore, when combined with the "sf" documentation my assumption is that the following should work.
install.packages("sf", configure.args = "--with-gdal-lib=/Applications/Postgres.app/Contents/Versions/latest/lib/"
Although I receive no errors or warnings, this does not fix the problem for me as sf::st_drivers() still does not list the PostgreSQL driver as an option. Can someone please help me figure out what additional configuration arguments, if any, are needed to get sf to recognise the Postgres driver?

How to read JPEG2000 of Sentinel-2 data in R

I am trying to use the rgdal package to open multiple Sentinel-2 data of JPEG2000 format.
The issue I am running into is:
Error in .local(.Object, ...) :
d:/data/T35SMD_20161227T091402_B01.jp2' not recognised as a supported file format.
Although, I have updated my installation of the rgdal package and use updated gdal binaries that support JPEG2000, which had support introduced in v1.9.0.
rgdal: version: 1.2-5, (SVN revision 648)
Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
When I perform gdalDrivers() command, the entry for JPG2000 is not listed there among other drivers.
Can anyone please tell me how could I solve this problem? I have plenty of S-2 data and converting them to tiff in QGIS or any similar program is no option for me.
I have also found few similar questions on this theme (c.f. open jpeg2000 sentinel 2 in r), however it seems there is no straight answer yet.
Use the command-line utility gdal_translate, as documented here: http://www.gdal.org/frmt_sentinel2.html. You will have a lot more flexibility, and likely better speed and memory handling. Example that is given there is:
gdal_translate SENTINEL2_L1C:S2A_OPER_MTD_SAFL1C_PDMC_20150818T101440_R022_V20150813T102406_20150813T102406.xml:10m:EPSG_32632 10m.tif -co TILED=YES --config GDAL_CACHEMAX 1000 --config GDAL_NUM_THREADS 2
There's not really a solid argument for doing that much heavy lifting in R, especially through something that is essentially a wrapper.

cannot install rgdal in windows 64-bit

I have problems installing rgdal in windows 8.1, i installed OSGeo4W (is it mandatory btw?), understood that I have to install proj4, but do not really understand what to install on page, I read some documentation, but I feel really ignorant and can't get it to work properly.
I want to access rgdal through R-studio through rgdal, but how can i tell r-studio where to look for gdal and proj4?
I am really confused, clearly need some documentation but do not understand where to look for it, because all I read did not seem useful.
rgdal is installed according to R but when I use spTransform it says
Error in spTransform(locations, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")) :
load package rgdal for spTransform methods
moreover in the system requirements GDAL and proj4 are needed I know that because I already installed all this in Ubuntu, with a lot of effort.
EDIT: I re-started R, now doing library(rgdal) I get this:
Carico il pacchetto richiesto: sp
rgdal: version: 0.8-16, (SVN revision 498)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.11.0, released 2014/04/16
Path to GDAL shared files: C:/R/i686-pc-linux-gnu-library/3.0/rgdal/gdal
GDAL does not use iconv for recoding strings.
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
Path to PROJ.4 shared files: C:/R/i686-pc-linux-gnu-library/3.0/rgdal/proj
Warning message:
package ‘rgdal’ was built under R version 3.0.3
is it all ok?
EDIT 2: I updated R, just to see if that could have been the reason. Absolutely no changes in the error message.
Any help is appreciated :)
The problem solved itself after updating R and turning the pc on and off twice. no idea why but it works.

Resources