Monday, 29 August 2016

javascript - Exporting data with different data types in Google Earth Engine. How to make data consistent to download?


I am working with Sentinel-2 data in GEE and I added NDVI and a specific SAVI as new bands to an Image. I had an error downloading 6 bands (RGBI, and VIs) because the data type is not the same:



Error: Exported bands must have compatible data types; found inconsistent types: UInt16 and Float32.


Then I selected only the VIs (because I have already downloaded the other bands) and the error is still there but with a new data type:


Error: Exported bands must have compatible data types; found inconsistent types: Float32 and Float64.


I used the normal difference function for NDVI and a edited operation for SAVI. After that I used a operator for NDVI as well (to have 'same' result type) but the error is the same.


Do you know how to manage it to obtain the same data format to export?


Code sample:


// Parameters for Vegetation Index (NDVI-SAVI).
var nir = scene.select('B8');
var red = scene.select('B4');
var L = 0.38

var L1 = 1.38
var ndviParams = {min: -1, max: 1, palette: ['red', 'yellow', 'green']};

//Calculating VIs
var ndvi = scene.normalizedDifference(['B8', 'B4']).rename('NDVI');
var savi = nir.subtract(red).divide(nir.add(red).add(L)).multiply(L1).rename('SAVI');

//Selecting to download
var download = ee.Image(sorted.first()).addBands(savi).addBands(ndvi).select(['B4', 'B3', 'B2','B8','NDVI','SAVI']);

Answer




Exporting requires all bands to be of the same type, if you use the function bandTypes(), you will notice indeed differences:



  • B2: signed int16

  • B3: signed int16

  • ...

  • NDVI: float ∈ [-1, 1]


So to have all bands in the same type, you would usally cast the image into the least restrictive type, which is in this case float (indeed, casting integer to float does not loose data, while float to integer would). use the function toFloat() in this case.


Reproducible example


Your code was unfortunately not reproducible, see here a simple and dummy code, showing the issue with Landsat data:



var geometry = /* color: #0b4a8b */ee.Geometry.Polygon(
[[[-104.1558837890625, 42.950391774502876],
[-104.13665771484375, 42.83569550641452],
[-103.831787109375, 42.85784648372956],
[-103.84002685546875, 42.95441233121331]]]);

var imageCollection = ee.ImageCollection("LANDSAT/LE07/C01/T2_SR");
var image_one = ee.Image(imageCollection.first())




//Calculating VIs
var ndvi = image_one.normalizedDifference(['B3', 'B4']).rename('NDVI');
var image_one_withNDVI = image_one.addBands(ndvi)
print(image_one_withNDVI, "image_one_withNDVI")

First try: export without toFloat():


Export.image.toDrive({
image: image_one_withNDVI,
description: "test_task",

fileNamePrefix: "test",
region: geometry,
scale:30
})

This will return: Error: Exported bands must have compatible data types; found inconsistent types: Int16 and Byte.


Second try, use: ge_one_withNDVI.toFloat()


Export.image.toDrive({
image: image_one_withNDVI.toFloat(),
description: "test_task_OK",

fileNamePrefix: "test_OK",
region: geometry,
scale:30
})

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...