Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ee.Image.index raises AttributeError on median-reduced image #42

Open
aazuspan opened this issue Aug 19, 2021 · 2 comments
Open

ee.Image.index raises AttributeError on median-reduced image #42

aazuspan opened this issue Aug 19, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@aazuspan
Copy link
Contributor

Hi @davemlz! I don't think this is really an eemont bug, but I'm wondering if you know of a good workaround or if maybe there's an eemont feature that could help with this.

Describe the bug
If you use a reducer other than ee.Reducer.first on an ImageCollection and then try to use the index method, an attribute error is thrown. I think this is because Earth Engine reducers like ee.Reducer.median don't preserve image metadata that's needed for _get_platform_STAC to work.

I'm not sure exactly what metadata _get_platform_STAC needs, but maybe I could fix this by setting or copying some properties after running median?

To Reproduce

import ee, eemont
ee.Initialize()

s2 = ee.ImageCollection("COPERNICUS/S2_SR")
pt = ee.Geometry.Point([-122.276437, 44.803428])
img = (s2
    .filterBounds(pt)
    .filterDate("2020-01-01", "2020-01-30")
    # Using .first() instead would fix the problem
    .median()
)

img = img.index("NDSI")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_14264/966081019.py in <module>
     10 )
     11 
---> 12 img = img.index("NDSI")

~\anaconda3\envs\salvage\lib\site-packages\eemont\image.py in index(self, index, G, C1, C2, L, cexp, nexp, alpha, slope, intercept, kernel, sigma, p, c, online)
    746     )
    747 
--> 748     return _index(
    749         self,
    750         index,

~\anaconda3\envs\salvage\lib\site-packages\eemont\common.py in _index(self, index, G, C1, C2, L, cexp, nexp, alpha, slope, intercept, kernel, sigma, p, c, online)
    417         Image (or Image Collection) with the computed spectral index, or indices, as new bands.
    418     """
--> 419     platformDict = _get_platform_STAC(self)
    420 
    421     if isinstance(sigma, int) or isinstance(sigma, float):

~\anaconda3\envs\salvage\lib\site-packages\eemont\common.py in _get_platform_STAC(args)
     46             args, ee.image.Image
     47         ):
---> 48             pltID = "/".join(ID.split("/")[:-1])
     49         elif eeDict[platform]["gee:type"] == "image" and isinstance(
     50             args, ee.imagecollection.ImageCollection

AttributeError: 'NoneType' object has no attribute 'split'

Setup (please complete the following information):

  • OS: Windows 10
  • python version 3.9.6
  • eemont version 0.2.5
  • earthengine-api version 0.1.279

Additional context
Thanks!

@aazuspan aazuspan added the bug Something isn't working label Aug 19, 2021
@davemlz
Copy link
Owner

davemlz commented Aug 19, 2021

Hi, @aazuspan!

You're totally right. The problem here is that after reducing an image it loses all its properties. Most eemont methods depend on the system:id property, so it is necessary to have it. Given this, here is a workaround:

s2 = ee.ImageCollection("COPERNICUS/S2_SR")
pt = ee.Geometry.Point([-122.276437, 44.803428])
img = (s2
    .filterBounds(pt)
    .filterDate("2020-01-01", "2020-01-30")
    # Using .first() instead would fix the problem
    .median()
    .copyProperties(s2.first(),["system:id"])
)

img = ee.Image(img).scaleAndOffset().spectralIndices("NDSI")

I will leave this issue open, maybe I can find a way to fix this in the future :)

Cheers!

@aazuspan
Copy link
Contributor Author

Awesome, thanks so much for the quick workaround! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants