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 ?
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 withinstall.package('rgdal', type='source')
forrgdal
to dynamically bind with your system gdal installation. You may have to adjust theconfigure.args=
too. Also you can useraster()
directly to read the band, without having to usereadGDAL()
.configure.args=
? Usingraster
() returns the error message:Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.
rgdal
? Runningrgdal::gdalDrivers()$name
will give you the list of drivers available by yourrgdal
installation; it must include any ofJP2OpenJPEG
orSENTINEL2
for you to be able to read sentinel 2 data directly into R. The only difference betweenraster()
andreadGDAL()
is the class of object returned (RasterLayer
vsSpatialGridDataFrame
).They both usergdal
in the background and therefore require the mentioned drivers.rgdal
via the typical user interface. I also tried using the code you suggested withinstall.packages('rgdal', type='source')
. With both types of install, the list returned underrgdal::gdalDrivers()$name
does not includeJP2OpenJPEG
norJPEG2000
, onlySENTINEL2
andJPEG
. When I tryreadGDAL('filename.jp2')
, it returns the following error message:Error in .local(.Object, ...) :
. You had proposed something to do withconfigure.args=
but I don't know how to pursue that further.