I want to use random forests in R to classify an image. My image is in jp2 format. I read Open JPEG2000 (Sentinel 2) in R and updated my rdal:

rgdal: version: 1.2-5, (SVN revision 648)
 Geospatial Data Abstraction Library extensions to R successfully loaded
 Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15

Still, I see only JPEG not JPER2000 when I run gdalDrivers(). Translation into tiff is not an option for me as my image is ~800 Gb if uncompressed (more than my c drive) and gdal is not implementing random forests.

I also tried
gdal_chooseInstallation('JP2OpenJPEG') library(rgdal) myimage <- readGDAL('filename.jp2').

But even after exhaustive googling I can't figure out how gdal_chooseInstallation('JP2OpenJPEG') works. All I am getting is Error in .local(.Object, ...) :

How to I read jp2 image in R without translating it other formats so I can apply random forests?


This is an old topic, but I have the same problem today. I tried to open a JPG2000 image (sentinel-2) without success. Since GDAL is supporting this driver a long time ago, was weird couldn't open it.

What I tried:

  • Update rgdal
  • Update gdal (I'm a Mac OS user, so I tried with brew install gdal, pip install gdal, KyngChaos installation files, etc)

All this with failure.

I surfed into rgdal installation folder seeing gdal binaries inside, so that was a surprise for me.

enter image description here

I deleted rgdal folder and reinstalled the library. Now opening S2 images works:

## Loading required package: sp
## rgdal: version: 1.4-4, (SVN revision 833)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.4.2, released 2019/06/28
## Path to GDAL shared files: /usr/local/Cellar/gdal/2.4.2/share/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ.4 runtime: Rel. 6.1.1, July 1st, 2019, [PJ_VERSION: 611]
## Path to PROJ.4 shared files: (autodetected)
## Linking to sp version: 1.3-1 


 ## class       : RasterLayer 
 ## dimensions  : 1830, 1830, 3348900  (nrow, ncol, ncell)
 ## resolution  : 60, 60  (x, y)
 ## extent      : 199980, 309780, 6590200, 6700000  (xmin, xmax, ymin, ymax)
 ## coord. ref. : +proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
 ## data source : /Users/aldotapia/Downloads/test/T19JBG_20190822T143759_B01.jp2 
 ## names       : T19JBG_20190822T143759_B01 
 ## values      : 0, 65535  (min, max)

Paths of R libraries in Mac OS are in /Library/Frameworks/R.framework/Versions/Current/Resources/library and Path to GDAL shared files: gave me the clue of my problem (loading rgdal library)


You can use gdalUtils to convert it to a different format and then read it. It may not keep the data in a jp2 format but, it does keep you in R.


gdalUtils::gdal_translate("band01.jp2", "band01.tif")
  b01 <- raster::raster("Band01.tif")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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