[GIS] How to open jp2 JPEG2000 files in R

gdaljpeg 2000r

I'm re-opening this question (Open JPEG2000 (Sentinel 2) in R). I'm working on a Mac OS X Yosemite 10.10.5.

I would like to be able to read jp2 format images with R without translating it to .tif format or anything of that kind.

I followed what have been explained in this question, but I'm still not able to open jp2 images.

library(rgdal)
Le chargement a nécessité le package : sp
rgdal: version: 1.2-7, (SVN revision 660)
Geospatial Data Abstraction Library extensions to R successfully loaded 
Loaded GDAL runtime: GDAL 2.1.2, released 2016/10/24
Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
Linking to sp version: 1.2-4

Then :

s04 <- readGDAL("/Users/martinmonziols/Documents/ENSAE/Stage TheGreenData/L2A_T30UYA_20170509T105621_B04_10m.jp2")
Error in .local(.Object, ...) : 

So, I checked the drivers

library(gdalUtils)
gdalDrivers()

It seems that I don't have any driver for jp2 files. But what is strange to me is that when running these lines in Terminal

MacBook-Air-de-Martin:~ martinmonziols$ gdalinfo --version
GDAL 2.1.3, released 2017/20/01

And

gdalinfo --formats

I have in particular this line in the table of all drivers:

JPEG2000 -raster,vector- (rwv): JPEG-2000 part 1 (ISO/IEC 15444-1), based on Jasper library

which means that I have somewhere a driver for jp2 format. But R is not speaking to this one since the versions differ and this driver does not appear in the list I have in R.

What am I missing here ? how can I force R to use the right gdal version instead of the older one ?

Best Answer

Martin, I had the same problem, very frustrating!

What worked for me was compiling my own version of the rgdal package using my local system's version of GDAL and PROJ4 as Loic Dutrieux's suggested. If you've installed GDAL using KyngChaos packages you should be able to do so by executing the following command in R:

install.packages("rgdal", type = "source", configure.args="--with-gdal-config=/Library/Frameworks/GDAL.framework/unix/bin/gdal-config --with-proj-include=/Library/Frameworks/PROJ.framework/unix/include --with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib --with-proj-share=/Library/Frameworks/PROJ.framework/unix/share/proj --with-proj-data=/Library/Frameworks/PROJ.framework/unix/share/proj --with-data-copy=yes")

If you do not yet have a separate install of GDAL on your system (check by running 'gdalinfo --version' in a MacOS terminal), you will have to download and install the 'GDAL Complete' framework from KyngChaos first: http://www.kyngchaos.com/software/frameworks

I ran into trouble with various versions of GDAL on my system and was unable to compile the package, so I ended up starting with a clean install of R, GDAL and all associated packages. Then it worked. Hopefully, this won't be needed in your case!

--

UPDATE: To ensure the home-build rgdal package works like the binaries on CRAN, the GDAL and PROJ4 data files need to be included. I have updated the line of code above with two additional arguments.

Related Question