7

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?

6

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

library(rgdal)
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:

summary(s2a)

Object of class SpatialGridDataFrame
Coordinates:
  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:
 band1      
 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 '16 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 '16 at 14:22
  • No! Do I have to reinstall GDAL/rgdal? – maRtin Sep 19 '16 at 14:23
  • 1
    Probably. You should check if JPEG2000 is listed when you run gdalinfo --formats – dmci Sep 19 '16 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 '16 at 13:40
0

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:

library(rgdal)
library(gdalUtils)

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, privacy policy and cookie policy

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