0

I want to reproject an Image Collection with .map(). This is what I tried:

import ee
ee.Authenticate()
ee.Initialize()

import geetools

# Outline of lower 48 U.S.
usbound = ee.Image("Q:My Drive/temp/us_bounds.tif");
# The TIFF can be retrieved at
# https://code.earthengine.google.com/?asset=users/japolo/us_bounds

usb = usbound.geometry();

#oeel = require('users/OEEL/lib:loadAll');

# I have to create this to clip the collection
def clip_img(img):
    return ee.Image(img).clip(usb)
    
# Set up Daymet collection from 2009 to 2020
daymetb = ee.ImageCollection("NASA/ORNL/DAYMET_V4").filterDate('2009-01-01', '2009-12-31').map(clip_img);

# Set up projection for reproject of Daymet
proj = ee.Projection('EPSG:4326');

def imreproj(img):
    return ee.Image(img).reproject(crs='EPSG:4326', crsTransform='null', scale=1000)

# Reproject Daymet
daymet = daymetb.map(imreproj(img))

The error is

Traceback (most recent call last):
  File "<stdin>", line 1, in
<module> NameError: name 'img' is not defined

I also tried:

daymet = daymetb.map(ee.Image.reproject(crs='EPSG:4326', crsTransform='null', scale=1000))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\japolo\code\gee_env\lib\site-packages\ee\apifunction.py", line 205, in <lambda>
    return lambda *args, **kwargs: func.call(*args, **kwargs)  # pylint: disable=unnecessary-lambda
  File "C:\Users\japolo\code\gee_env\lib\site-packages\ee\function.py", line 67, in call
    return self.apply(self.nameArgs(args, kwargs))
  File "C:\Users\japolo\code\gee_env\lib\site-packages\ee\function.py", line 80, in apply
    result = computedobject.ComputedObject(self, self.promoteArgs(named_args))
  File "C:\Users\japolo\code\gee_env\lib\site-packages\ee\function.py", line 109, in promoteArgs
    raise ee_exception.EEException(
ee.ee_exception.EEException: Required argument (image) missing to function: Force an image to be computed in a given projection and resolution.

Args:
  image: The image to reproject.
...

How should I set up the .map(<reproject>) for the Python API?

EDIT after answer from Daniel: I changed:

def imreproj(img):
    return ee.Image(0).reproject(crs='EPSG:4326', scale = 1000)

daymet = daymetb.map(imreproj(img)) 

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'img' is not defined

# and tried

def imreproj():
    return ee.Image(0).reproject(crs='EPSG:4326', scale = 1000)
daymet = daymetb.map(imreproj)
TypeError: imreproj() takes 0 positional arguments but 1 was given

also tried

daymet = daymetb.map(ee.Image(0).reproject(crs='EPSG:4326', scale = 1000))

TypeError: 'Image' object is not callable

I'm dense... and I don't know much Python. I don't know how the .map() works in Python.

1 Answer 1

0

The crsTransform argument cannot be the string 'null'. You either leave it out completely, set it to None, or specify a proper value.

# Just a collection with 10 random images as a dummy setup
collection = ee.ImageCollection( 
  ee.List.sequence(1, 10).map(lambda i: ee.Image.random(i))
)

def imreproj(image):
    return image.reproject(crs='EPSG:4326', scale=1000)

reprojected = collection.map(imreproj)
4
  • Thanks. My question was more about how to write the reproject to use with .map when I'm using the Python API. I edited the question to add more examples of how I my method was failing. I did incorporate your suggestion, thanks.
    – John Polo
    Mar 23, 2022 at 16:37
  • I updated my answer with an example on how to map over a collection and reproject each image. The function you map over must have exactly one argument, with the image. Mar 23, 2022 at 16:57
  • Thanks. It seems part of my misunderstanding comes from not knowing when to use "image" or "ee.Image" in defining a function.
    – John Polo
    Mar 23, 2022 at 17:23
  • 1
    You use ‘ee.Image()’ to create a new image (or cast an element to an image). Here you get passed ‘image’, that you can use directly. Think of that function as being invoked for each image in the collection. (It actually isn’t, but that’s a different story) Mar 23, 2022 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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