Is there any way I can open Sentinel 2 JPEG2000 file (.jp2) using R? Or more specifically with the {raster} package in R? I keep getting the Error message that .jp2 is:

not recognised as a supported file format.

Do you know if this file format will be added to the raster package any time soon, or how to open it in R without converting it to .tif first?

3 Answers 3


You should use the rgdal package and ensure that your version of GDAL has the JPEG2000 Driver. For instance:

Loading required package: sp
rgdal: version: 1.1-10, (SVN revision 622)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
Path to GDAL shared files: /usr/share/gdal/1.11
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
Path to PROJ.4 shared files: (autodetected)
Linking to sp version: 1.2-3 

Then read in the jp2 file:

> s2a <- readGDAL('S2A_OPER_MSI_L1C_TL_SGS__20151221T173303_A002594_T29UMT_B12.jp2')

S2A_OPER_MSI_L1C_TL_SGS__20151221T173303_A002594_T29UMT_B12.jp2 has GDAL driver JPEG2000 
and has 5490 rows and 5490 columns

Check the output:


Object of class SpatialGridDataFrame
  min     max
x  399960  509760

y 5690220 5800020

Is projected: TRUE 

proj4string :

[+proj=utm +zone=29 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0]

Grid attributes: 
  cellcentre.offset cellsize cells.dim
x            399970       20      5490
y           5690230       20      5490

Data attributes:
 Min.   :    0  
 1st Qu.:  198  
 Median :  606  
 Mean   : 1080  
 3rd Qu.: 1430  
 Max.   :28000  
  • I have rgdal 1.1-10 and GDAL 2.0.1. However I still get the same error message with readGDAL(): not recognised as a supported file format
    – maRtin
    Sep 19, 2016 at 14:21
  • 2
    Is JPEG-2000 listed when you run gdalDrivers(), e.g.66 JPEG2000 JPEG-2000 part 1 (ISO/IEC 15444-1)
    – dmci
    Sep 19, 2016 at 14:22
  • No! Do I have to reinstall GDAL/rgdal?
    – maRtin
    Sep 19, 2016 at 14:23
  • 1
    Probably. You should check if JPEG2000 is listed when you run gdalinfo --formats
    – dmci
    Sep 19, 2016 at 14:35
  • 2
    I have the same problem. I did manage to run some operations with library(gdalUtils) gdal_chooseInstallation('JP2OpenJPEG')but working directly through gdal would be better. see also : stackoverflow.com/questions/40044403/…
    – unnic
    Oct 14, 2016 at 13:40

The provided solutions seem to work just fine. But I prefere working with the {raster} package, so I wrote a little function:


    jp2_to_raster <- function(jp2_path, ref_crs) {
      # read ".jp2" and convert to raster
      sen2_GDAL <- readGDAL(jp2_path)
      sen2_GDAL %>% 
        as.matrix() %>% 
        t() %>% 
               xmn=xmin(sen2_GDAL), xmx=xmax(sen2_GDAL), 
               ymn=ymin(sen2_GDAL), ymx=ymax(sen2_GDAL)) -> tmp_raster

      # get band number from file path
      strsplit(jp2_path, "_")[[1]][length(strsplit(jp2_path, "_")[[1]])] %>% 
        strsplit(".jp2") %>% 
        as.character() -> names(tmp_raster)


    file <- "T10UDV_20190630T190919_B01"
    crs <- "+proj=utm +zone=10 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"

    raster <- jp2_to_raster(file , crs(crs))

Or use lapply to convert the whole Sentinel2 scene to a raster::stack:

    SEN2_PATHS <- list.files(pattern = "*.jp2$", full.names = T)
    raster _stack <- raster::stack(lapply(SEN2_PATHS, jp2_to_raster, ref_crs = crs(crs)))
  • 2
    This is a bit unnecessary as readGDAL results in a SpatialPixelsDataFrame and there is a coercion method for raster or stack. You can simply do something like: raster(readGDAL("C:/myraster.jpg")) and skip the matrix coersion and transposition. Feb 29, 2020 at 4:53
  • 1
    Great this works perfectly. Thanks for the advice!
    – Sebastian
    Feb 29, 2020 at 20:12
  • This was helpful. The strsplit band number names did not work for me. I modified to: names(tmp_raster) <- as.character(regmatches(jp2_path,gregexpr("B0\\d", jp2_path))) Apr 28, 2022 at 2:18

You say "without converting to tif first", but it appears that many are struggling to make rgdal to understand that the OpenJPEG driver is actually included in the GDAL distribution. If you cannot manage to make the driver work in rgdal, you can translate the .jp2 file to GeoTIFF and open that file with rgdal. Translating can be done using the command line as explained here or the gdal_translate wrapper from the gdalUtils package:


gdal_translate("T33XVJ_20170803T125711_B01.jp2", "B01.tif")
b01 <- readGDAL("B01.tif")
# B01.tiff has GDAL driver GTiff 
# and has 1830 rows and 1830 columns

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.