Misc584
Misc584

Reputation: 357

imageCollection.reduce() functions producing single pixel image when exported using Google Earth Engine Python API

I am trying to find ways to export a single image from an imageCollection and am currently looking at the imageCollection.reduce() functions. In particular, I want to create a single image from an image collection, where each pixel represents the mean value across of pixels in that location across images in the collection. According to this webpage (https://developers.google.com/earth-engine/reducers_image_collection), the imageCollection.reduce() functions should do just this. Looking at the example of the median function, it says "the output is computed pixel-wise, such that each pixel in the output is composed of the median value of all the images in the collection at that location."

However, whenever I try to use these functions, the output that gets exported to my Google Drive is a single pixel image.

I have been able to export and download an entire image for ee layers that only contain a single image. The below example shows my results for an elevation layer:

import ee
import rasterio as rio
import numpy as np
ee.Initialize()

elevation = ee.Image('JAXA/ALOS/AW3D30_V1_1').select('AVE')

geometry = ee.Geometry.Polygon([[33.8777, -13.4055],
                                [33.8777, -13.3157],
                                [33.9701, -13.3157],
                                [33.9701, -13.4055]])
geometry = geometry['coordinates'][0]

filename = "Example_File"

task_config = {
    'region': geometry,
    'min': 0.0,
    'max': 4000.0,
    'palette': ['0000ff', '00ffff', 'ffff00', 'ff0000', 'ffffff']
    }

task = ee.batch.Export.image(elevation, filename, task_config)

task.start()

Once the image has been exported and downloaded onto my computer, I get the following output:

rs = rio.open("C:\\Users\\miker\\Downloads\\Example_File.TIF")
rs.read(1)

#Output#
array([[1275, 1273, 1271, ..., 1152, 1163, 1178],
       [1275, 1273, 1271, ..., 1152, 1164, 1184],
       [1275, 1273, 1271, ..., 1158, 1169, 1187],
       ...,
       [1327, 1326, 1324, ..., 1393, 1396, 1397],
       [1328, 1326, 1325, ..., 1399, 1400, 1403],
       [1328, 1326, 1325, ..., 1402, 1404, 1407]], dtype=int16)

However, when I try to follow a similar procedure for an imageCollection layer, where the collection has been reduced to an image using the ee.Reducer.mean() function, I only get a single pixel array:

population = (ee.ImageCollection('WorldPop/POP')
              .select('population')
              .filter(ee.Filter.date('2015-01-01', '2017-12-31')))
population = population.reduce(ee.Reducer.mean())

File_Name = "Example_File2"

task_config = {
    'region': geometry,
    'min': 0.0,
    'max': 50.0,
    'palette': ['24126c', '1fff4f', 'd4ff50']
    }

task = ee.batch.Export.image(population, File_Name, task_config)

task.start()
rs = rio.open("C:\\Users\\miker\\Downloads\\Example_File2.TIF")
rs.read(1)

#Output#
array([[1.262935]], dtype=float32)

I have repeated this process for min(), max() and median() and got a similar result for each:

# mean:   array([[1.262935]], dtype=float32)
# median: array([[1.262935]], dtype=float32)
# min:    array([[1.2147448]], dtype=float32)
# max:    array([[1.3111253]], dtype=float32)

Does anyone know why this might be happening? My guess is that the reduce() functions are aggregating the entire collection into a single value, but I'm not sure why or what I can do to stop this.

Any help would be greatly appreciated.

Upvotes: 1

Views: 1222

Answers (1)

Kel Markert
Kel Markert

Reputation: 837

When you run task.start() without explicitly setting parameters the task will use defaults. As @blindjesse mentioned, you are not setting the parameters to you image export task correctly and it is giving you weird results with the defaults. Here is an example of export an the population image to your GDrive at 30m resolution:

population = (ee.ImageCollection('WorldPop/POP')
              .select('population')
              .filter(ee.Filter.date('2015-01-01', '2017-12-31')))
population = population.reduce(ee.Reducer.mean())

geometry = ee.Geometry.Polygon([[33.8777, -13.4055],
                                [33.8777, -13.3157],
                                [33.9701, -13.3157],
                                [33.9701, -13.4055]])

File_Name = "Example_File2"

task_config = {
    'region': geometry.coordinates().getInfo(), 
    'scale': 30,
    'description': File_Name
} 

task = ee.batch.Export.image.toDrive(population, **task_config)
task.start()

Upvotes: 1

Related Questions