4

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 ?

5
  • I don't have a mac but I think rgdal comes with its own gdal binaries on that platform, and these binaries apparently don't include the jp2 driver. Try re-installing the package with install.package('rgdal', type='source') for rgdal to dynamically bind with your system gdal installation. You may have to adjust the configure.args= too. Also you can use raster() directly to read the band, without having to use readGDAL(). Commented Jun 6, 2017 at 16:03
  • Could you provide more details? How is it possible to adjust with configure.args=? Using raster() returns the error message: Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.
    – kl-higgins
    Commented Oct 16, 2017 at 19:45
  • @LoïcDutrieux Could you please elaborate on your comment-answer?
    – kl-higgins
    Commented Oct 18, 2017 at 20:38
  • @user3386170 How did you install rgdal? Running rgdal::gdalDrivers()$name will give you the list of drivers available by your rgdal installation; it must include any of JP2OpenJPEG or SENTINEL2 for you to be able to read sentinel 2 data directly into R. The only difference between raster() and readGDAL() is the class of object returned (RasterLayer vs SpatialGridDataFrame).They both use rgdal in the background and therefore require the mentioned drivers. Commented Oct 31, 2017 at 23:02
  • 1
    @LoïcDutrieux I initially installed rgdal via the typical user interface. I also tried using the code you suggested with install.packages('rgdal', type='source'). With both types of install, the list returned under rgdal::gdalDrivers()$name does not include JP2OpenJPEG nor JPEG2000, only SENTINEL2 and JPEG. When I try readGDAL('filename.jp2'), it returns the following error message: Error in .local(.Object, ...) : . You had proposed something to do with configure.args= but I don't know how to pursue that further.
    – kl-higgins
    Commented Nov 1, 2017 at 15:34

1 Answer 1

5

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.

5
  • Great workaround! I understand the headache of this issue. Thanks!
    – aldo_tapia
    Commented Nov 2, 2017 at 11:14
  • I realize that the original question was about Mac OS but could you provide some hints on how to adapt this solution to Windows. I don't have GDAL installed (unless it is part of QGIS... or dnrgps?).
    – kl-higgins
    Commented Nov 2, 2017 at 18:37
  • 1
    @user3386170 While it is theoretically possible to install rgdal from source on windows too, it is far from easy and for that reason usually not recommended. See this r-sig-geo thread about compiling rgdal Commented Nov 3, 2017 at 5:27
  • @LoïcDutrieux Essentially, you are saying that, for a beginner user on Windows, it is difficult to make R be able to read .jp2... ? I can give up and just use QGIS to convert from .jp2 to .tiff (I just find it more elegant and more efficient to run all my code in R) if that is a more doable option.
    – kl-higgins
    Commented Nov 3, 2017 at 14:44
  • 1
    @user3386170 That's right, on windows you're pretty much bound to using the gdal binaries that come statically linked to rgdal and they do not include all drivers (e.g.: jp2, hdf4). An alternative is to use gdalUtils::gdal_translate() to generate a tiff from the jp2 and raster() to read the geotTiff into R. This works because the gdalUtils calls the gdal that comes with QGIS (or another one, but QGIS is the most common option) via system(). This is of course a lot less efficient than reading the jp2 files directly, but afraid it's the only option on windows Commented Nov 3, 2017 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.