1

I'm trying to open Sentinel-2 bands directly from the .zip L2A files with GDAL, but this only returns a None. Unzipping the folder prior to reading the file yield better results, but still randomly returns some None from time to time. I'm using gdal 2.2.2.

import numpy as np 
import osgeo.gdal as gdal
import zipfile, os

from libs.generic import filemanager as fm
os.environ['CPL_ZIP_ENCODING'] = 'UTF-8'

class s2img:

def __init__(self):
    return None

def readL2A(self, zippath):

    try:
        zipf = zipfile.ZipFile(zippath, 'r')
    except:
        raise IOError("Unable to open ZIP-file.")

    flist = zipf.namelist()
    zipf.close()

    bandnames = []
    bandnames.append( ['B02','B03','B04','B08'] )
    bandnames.append( ['B05','B06','B07','B8A','B11','B12','SCL'] )
    bandnames.append( ['B01','B09'] )

    bandres = ['10m','20m','60m']

    fnames = [f for f in flist if 'IMG_DATA' in f]
    fnames = [f for f in fnames if f.endswith('.jp2')]
    for r in range(len(bandnames)-1):
        res = bandres[r]
        resbnames = bandnames[r]
        fresnames = [f for f in fnames if res in f]

        for b in range(len(resbnames)):
            fname= [f for f in fresnames if resbnames[b] in f ][0]
            chn_fn = "/vsizip/%s/%s" % (zippath,fname)
            gobj = gdal.Open(chn_fn, gdal.GA_ReadOnly )

            # DO STUFF 
    return None
5
  • Could you give a link for getting some test data? Have you tried to read data from the zip with gdalinfo or gdal_translate?
    – user30184
    Mar 10, 2019 at 16:30
  • @user30184 here's an example file: link I use neither of the two options. I'd like to make a standalone python scipt and from what i found in the documentation, those methods can only be used through command line or with the C API, right?
    – C. Daniel
    Mar 11, 2019 at 15:46
  • It does not matter if your final aim is to do it with Python. If you test reading data from zip with gdalinfo or gdal_translate and test is successful then you'll know that it is possible and all that is left is to do it right with Python.
    – user30184
    Mar 11, 2019 at 16:00
  • I suggest to study the Python autotests in github.com/OSGeo/gdal/blob/master/autotest/gdrivers/…. I am not familiar with Sentinel2 format which seems a bit complicated. Gdalinfo behaves as supposed directly from zip with your example data.
    – user30184
    Mar 11, 2019 at 18:10
  • @user30184 thank you for your reply. After running without success some bits of the code you linked, I tried to simply read an unzipped jpeg2000 band, still without success. This is odd since on my other pc, with the same python modules/libraries this worked.
    – C. Daniel
    Mar 12, 2019 at 9:41

1 Answer 1

1

Found the problem. In order for gdal to be able to properly read the "sentinel-2.jp2" bands it needs supporting files. The location of the these can be specified by properly setting the "GDAL_DATA" environment variable: (https://trac.osgeo.org/gdal/wiki/FAQInstallationAndBuilding#HowtosetGDAL_DATAvariable)

If you're using Ananconda as the python library manager, then at the beginning of your script you should insert a line like this:

os.environ['GDAL_DATA'] = "C:/Users/B/Anaconda3/Library/share/gdal"

If you're using a virtual environment then the path should look something like this:

os.environ['GDAL_DATA'] = "C:/Users/B/Anaconda3/envs/geopy/Library/share/gdal/",

where "geopy" is the name of my environment.

However, this doesn't explain why on my other pc I didn't need to add this line to the code in order for the script to run properly on already unzipped images.

Your Answer

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

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