Download correctly ndvi data in format .tiff

Hello, i wanted to download the NDWI from a ROI in .tiff format. I used the next code, but the outfile shows me values in true color (R, G and B) and not values between -1 and 1. The ROI is a area most little into the bbox.

The next code is in a function

def save_sentinel_patch(height, data_folder):

    bbox = BBox(bbox=[(-7223582,-3839077), (-7142680,-3897777)], crs=CRS.POP_WEB)
    ROI = """MULTIPOLYGON(((-7179178.2313 -3859329.8432,
    -7161416.6336 -3865178.1741,-7168239.6864 -3881965.0500,
    -7187409.2156 -3876658.2312,-7179178.2313 -3859329.8432)))"""
    
    layer = 'MOISTURE_INDEX'
    wms_bands_request = WmsRequest(data_folder=data_folder,
                                   layer=layer,
                                   bbox=bbox, 
                                   time='2017-12-06',
                                   height=height,
                                   #image_format=MimeType.TIFF_d32f,
                                   image_format=MimeType.TIFF_d16,
                                   
                                   instance_id=INSTANCE_ID,
                                   custom_url_params={CustomUrlParam.ATMFILTER: 'ATMCOR',
                                                       CustomUrlParam.TRANSPARENT: True,
                                                       CustomUrlParam.GEOMETRY: ROI,
                                                       CustomUrlParam.SHOWLOGO: False})
    
    wms_img = wms_bands_request.get_data(save_data=True)
    
    if not wms_img: # Image extraction Failed.
        return False
    else:
        return True            # Image succesfully extracted.
    
height = 3721
data_folder = '/Example/test_dir' 
save_sentinel_patch(height, data_folder)

I wonder how can i download the roi in format .tiff with those values?

Thanks

The layer, which you are using, is defined for visualization rather than values.

If you take a look at our Custom script repository, you will find moisture index defined here:

The part related to value is:
if (B8A == 0 || B11 == 0){
return [0,0,0];
} else {
var val = (B8A - B11)/(B8A + B11);

You should create a new layer in Configuration utility as described here:
https://www.sentinel-hub.com/faq/how-create-eo-product

The layer MOISTURE_INDEX you are using is configured for visualization.

You should create a new layer in configuration utility as described here:
https://www.sentinel-hub.com/faq/how-create-eo-product

Check this FAQ on how to get actual value rather than visualization:
https://www.sentinel-hub.com/faq/how-do-i-get-actual-ndvi-values

You will find Moisture index configuration in our Custom script repository:

The part related to Moisture index value is:
if (B8A == 0 || B11 == 0){
return [0,0,0];
} else {
var val = (B8A - B11)/(B8A + B11)
return [vall];

If you let us know first part of the Instance ID you are using, we can help you configure it.

Thanks for you attention, I resolved the download, I think I have a problem with the .tiff format, specifically with the 32 bit format, but now I can’t download only ROI (region of interest) in one area (BBox). I think that the problem is in the parameters in custom_url_params. How could I solve the problem?

The code and the instance that I use is the following:

def save_sentinel_patch(height, data_folder):
INSTANCE_ID = '691eadb2-2dcc-4f23-ac65-MASKED'
bbox = BBox(bbox=[(-7223582,-3839077), (-7142680,-3897777)], crs=CRS.POP_WEB)
ROI = """MULTIPOLYGON(((-7179178.2313 -3859329.8432,
-7161416.6336 -3865178.1741,-7168239.6864 -3881965.0500,
-7187409.2156 -3876658.2312,-7179178.2313 -3859329.8432)))
"""
#layer = 'MOISTURE_INDEX'
layer = 'NDVI'


wms_bands_request = WmsRequest(data_folder=data_folder,
                               data_source=DataSource.SENTINEL2_L1C,
                               layer=layer,
                               bbox=bbox, 
                               time='2017-12-06',
                               height=height,
                               #image_format=MimeType.TIFF_d32f,
                               image_format=MimeType.TIFF_d16,
                               #image_format=MimeType.TIFF_d8,
                               
                               instance_id=INSTANCE_ID,
                               custom_url_params={CustomUrlParam.ATMFILTER: 'ATMCOR',
                                               CustomUrlParam.TRANSPARENT: True,
                                               CustomUrlParam.GEOMETRY: ROI,
                                               #CustomUrlParam.EVALSCRIPT: ScaleColor,
                                               CustomUrlParam.SHOWLOGO: False})

wms_img = wms_bands_request.get_data(save_data=True)

if not wms_img: # Image extraction Failed.
    return False
else:
    return True            # Image succesfully extracted.  

height = 3721
data_folder = '/Example/test_dir' 
save_sentinel_patch(height, data_folder)

The error that is shown is:

C:\Users\car\Anaconda3\lib\site-packages\sentinelhub\download.py:127: UserWarning: File path examples\wms_TRUE_COLOR_EPSG3857_-7223582.0_-3897777.0_-7142680.0_-3839077.0_2019-01-20T14-21-53_1150X856_Geometry_MULTIPOLYGON(((-7179178.2313-3859329.8432,-7161416.6336-3865178.1741,-7168239.6864-3881965.0500,-7187409.2156-3876658.2312,-7179178.2313-3859329..png is longer than 255 character which might cause an error while saving on disk
  'disk'.format(self.file_path))
Traceback (most recent call last):

  File "<ipython-input-123-91796a54fbef>", line 1, in <module>
    runfile('C:/Users/car/Documents/Example/save_png.py', wdir='C:/Users/carli/Documents/Example')

  File "C:\Users\car\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "C:\Users\car\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/car/Documents/Example/save_png.py", line 42, in <module>
    custom_wms_data = custom_wms_request.get_data(save_data=True)

  File "C:\Users\car\Anaconda3\lib\site-packages\sentinelhub\data_request.py", line 104, in get_data
    data_list = self._execute_data_download(data_filter, redownload, max_threads, raise_download_errors)

  File "C:\Users\car\Anaconda3\lib\site-packages\sentinelhub\data_request.py", line 159, in _execute_data_download
    data_list.append(future.result(timeout=SHConfig().download_timeout_seconds))

  File "C:\Users\car\Anaconda3\lib\concurrent\futures\_base.py", line 425, in result
    return self.__get_result()

  File "C:\Users\car\Anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
    raise self._exception

  File "C:\Users\car\Anaconda3\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)

  File "C:\Users\car\Anaconda3\lib\site-packages\sentinelhub\download.py", line 265, in execute_download_request
    _save_if_needed(request, response_content)

  File "C:\Users\car\Anaconda3\lib\site-packages\sentinelhub\download.py", line 392, in _save_if_needed
    with open(file_path, 'wb') as file:

FileNotFoundError: [Errno 2] No such file or directory: 'examples\wms_TRUE_COLOR_EPSG3857_-7223582.0_-3897777.0_-7142680.0_-3839077.0_2019-01-20T14-21-53_1150X856_Geometry_MULTIPOLYGON(((-7179178.2313-3859329.8432,-7161416.6336-3865178.1741,-7168239.6864-3881965.0500,-7187409.2156-3876658.2312,-7179178.2313-3859329..tiff'

Thanks

Looking at the error you get (“FileNotFoundError: [Errno 2] No such file or directory: 'examples\wms_TRUE_COLOR_…”) it seems to me the error is in how you handle saving the data on your local storage.
Try shortening the name of the file, e…g avoiding the BBOX and GEOMETRY parameters in there.

If I am not mistaken the problem is that by default on Windows the entire file path can be at most 255 characters long. Anything longer than that will not be saved correctly.
On the other hand on Linux only the name of each file and folder is limited to 255 characters and not the entire path combined.

In sentinelub-py the filename is created automatically and we try to make it as unique as possible and at the same time human-readable. If it is longer than 255 characters it is shortened. Hence this doesn’t cause problems on Linux machines but it is still not good enough solution for Windows.

Possible solutions at the moment:

  • I am not Windows expert but I think you can file path change limitations to the ones that Linux has. Even when you install Python I think you can choose an option to reconfigure your file system setting and change this exact limitations. (Never tried it though)

  • At the moment the package doesn’t offer a nice option how to manually change names. We plan to add this soon but at the moment you have to do something like this:

wms_bands_request = WmsRequest(...)

for download_request in wms_bands_request.get_download_list():
    current_filename = download_request.filename

    download_request.set_filename('your new filename ...')

wms_img = wms_bands_request.get_data(save_data=True)

Improving how filenames are generated is also on our TODO list. Should be implemented in one of our future releases.

1 Like