3

I'm using Google Earth Engine in Python, but I'm guessing this problem is not specific to Python. I want to use ee.Filter.calendarRange on a feature collection. I was successful at using ee.Filter.calendarRange for an image collection from images stored on the server. I think I need to use ee.ImageCollection.getRegion to avoid doing computations on entire images. So, I am taking the output from getRegion and creating features from it that I'd like to do the computations on. The problem is that I don't know how to set feature dates in such a way that can be understood by ee.Filter.calendarRange. I get EEException: Collection.filter: Can't apply calendarRange filter to objects without a timestamp..

I have an example code that creates 10 dates from Jan 1 to Jan 10, 2010 with "data" values from 0-9. I would like to create a feature collection with just the feature from Jan 3.

def make_fts( val ):
    date = start_date.advance( ee.Number( val ), 'day' )
    ft = ee.Feature( None, {'system:time_start': date, 'data': val} )
    return ft

fake_vals = ee.List( [ee.Number( num ) for num in range(10)] )
start_date = ee.Date( '2010-01-01' )
fc = ee.FeatureCollection( fake_vals.map( make_fts ) )
day_ee_num = ee.Number( 3 )
day_filter = ee.Filter.calendarRange( day_ee_num, day_ee_num, 'day_of_month' ) 
day_three_fc = fc.filter( day_filter )

1 Answer 1

4

You need to store the date as an integer in milliseconds since 1970. You can convert it using .millis().

ft = ee.Feature( None, {'system:time_start': date.millis(), 'data': val} )

3
  • Thanks for the idea @JonasV. I'm getting the error as before though. It seems to me that dates are already internally stored as Epoch/Unix. E.g. "date = ee.Date( '2010-01-01' ); print(date.getInfo()) gives {'type': 'Date', 'value': 1262304000000}"
    – AF2k15
    Commented Mar 29, 2021 at 15:56
  • I tested your minimum example with my change and it works perfectly. Make sure in your original code, that you only save dates to system:time_start that have been converted with millis(). If my solution doesn't work, there has to be another problem in your original script that is not captured with your minimum example.
    – JonasV
    Commented Mar 29, 2021 at 16:05
  • Indeed. I had a typo. Thanks so much. I think this gets me unstuck.
    – AF2k15
    Commented Mar 29, 2021 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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