'Python Google-Earth-Engine: print list of Landsat scenes over ROI to export them on Drive
I have a Java script on GEE that allows me to grab all the Landsat images over my ROI for a specific time-period. The issue is that transferring those images from the GEE code editor to my Google Drive takes a very long time (2h per image, and I have at least 50 of them), and I have to manually click on a button to do it.
I have been exploring the Python API for GEE and I use a code to transfer an image on my Google Drive (see code below):
# Download a specific Landsat Scene
landsat = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_123032_20140515')\
.select(['B4', 'B3', 'B2'])
# Create a geometry representing an export region.
geometry = ee.Geometry.Rectangle([116.2621, 39.8412, 116.4849, 40.01236])
# Export the image, specifying scale and region.
task = ee.batch.Export.image.toDrive(**{
'image': landsat,
'description': 'Test',
'folder':'Folder',
'scale': 100,
'region': geometry.getInfo()['coordinates']
})
task.start()
What I would like to do is create a list gathering all my Landsat images' names, then iterate over that list with the code above so I can automatically save and export the images to my drive.
My Java code to export the Landsat images from GEE code editor is below. Every example I found deals with names of images already found, but in my case I need to get the names of images depending on: the time at which they were taken, the cloud cover, their coverage over the ROI. The Java script does all of that.
The way I look at it I have two solutions:
Export a list gathering the names of my images from the Java script on GEE Editor, then open that list on Python and iterate through that
Have the Java script working on Python that does the exact same thing.
I have no experience with Java and I can't manage to export a list as .csv or .ASCII or .txt. The issue with the 2nd option is that I can't find any code that does what I do on Java.
How could I solve my problem ?
/**** Start of imports. If edited, may not auto-convert in the playground. ****/
var geometry =
/* color: #98ff00 */
/* shown: false */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.Geometry.Polygon(
[[[-141.1474025430237, 60.163560184011736],
[-141.1474025430237, 59.70120024412181],
[-139.9938380898987, 59.70120024412181],
[-139.9938380898987, 60.163560184011736]]], null, false),
aoi = /* color: #d63000 */ee.Geometry.Point([-140.62080375708652, 59.944917067323274]);
/***** End of imports. If edited, may not auto-convert in the playground. *****/
var cloudMaskL457 = function(image) {
var qa = image.select('pixel_qa');
// If the cloud bit (5) is set and the cloud confidence (7) is high
// or the cloud shadow bit is set (3), then it's a bad pixel.
var cloud = qa.bitwiseAnd(1 << 5)
.and(qa.bitwiseAnd(1 << 7))
.or(qa.bitwiseAnd(1 << 3));
// Remove edge pixels that don't occur in all bands
var mask2 = image.mask().reduce(ee.Reducer.min());
return image.updateMask(cloud.not()).updateMask(mask2);
};
function maskL8sr(image) {
// Bits 3 and 5 are cloud shadow and cloud, respectively.
var cloudShadowBitMask = (1 << 3);
var cloudsBitMask = (1 << 5);
// Get the pixel QA band.
var qa = image.select('pixel_qa');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask);
}
var l5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
.filterDate('1984-01-01', '2012-05-05')
.map(cloudMaskL457)
.filterBounds(aoi)
.map(function(a){
return a.set('year', ee.Image(a).date().get('year'))
})
/*
var l7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
.filterDate('1997-01-01', '2021-05-12')
.map(cloudMaskL457)
.filterBounds(aoi)
.map(function(a){
return a.set('year', ee.Image(a).date().get('year'))
})
*/
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate('2013-04-11', '2021-05-05')
.map(maskL8sr)
.filterBounds(aoi)
.map(function(a){
return a.set('year', ee.Image(a).date().get('year'))
})
var list = ee.List([])
for (var a=1984; a < 2022; a++) {
if(a < 2012) {
var filL5 = l5.filterMetadata('year', 'equals', a).median()
var l5final = filL5.set('year', a)
.set('product', 'L5')
list = list.add(l5final)
} else {
var filL8 = l8.filterMetadata('year', 'equals', a).median()
var l8final = filL8.set('year', a)
.set('product', 'L8')
list = list.add(l8final)
}
}
print(list)
var finalCol = ee.ImageCollection(list).map(function(a){
return a.set('bands', ee.Image(a).bandNames().length())
})
.filterMetadata('bands','greater_than',10)
print(finalCol)
for(var a = 1984; a < 2022; a++){
Map.addLayer(finalCol.filterMetadata('year', 'equals', a), {}, ' '+a, false)
}
var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate('2016-01-01', '2016-12-31')
.map(maskL8sr);
var visParams = {
bands: ['B4', 'B3', 'B2'],
min: 0,
max: 3000,
gamma: 1.4,
};
Map.addLayer(dataset.median(), visParams);
var n = finalCol.size().getInfo()
var i = 0
for(var a = 1984; a < 2022; a++){
//put an actual angled bracket here
var img = ee.Image(list.get(i))
var i = i + 1
var rgb = img.select('B3','B2', 'B1').float()
Map.addLayer(img, {bands: ['B3', 'B2', 'B1'], max: 10000}, 'Image_'+a)
//export
Export.image.toDrive({
image: rgb,
description: 'Landsat5_NC'+ a,
scale: 10,
region: geometry,
folder: 'Malaspina_Landsat5'
})
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
