1

I'm trying to replicate this python script to calculate NDVI mean for a feature collection (Get results in an Earth Engine python script). It seems like the code calculates a result, but a cant see result. This is the code:

import datetime
import ee
ee.Initialize()

#Feature collection
fc = ee.FeatureCollection("ft:1x290vohty0Wgdn5jL3RlpzryK7dfOPtG6yY213e0");
fc_filtered = fc.filter(ee.Filter.equals('NUM_DECS', 1))
#Image collection
Sentinel_collection = (ee.ImageCollection('COPERNICUS/S2')
    .filterBounds(fc_filtered)
    .filterDate(ee.Date('2017-01-01'),ee.Date('2017-08-01')))

def GetSeries(feature):
  def NDVIcalc(img):
    red = img.select('B4')
    nir = img.select('B8')
    ndvi = nir.subtract(red).divide(nir.add(red)).rename(['NDVI'])
    return (feature
            .set(ndvi.reduceRegion(ee.Reducer.mean(), feature.geometry(), 10))
            .set('date', img.date().format("YYYYMMdd")))

  series = Sentinel_collection.map(NDVIcalc)
  list = series.reduceColumns(ee.Reducer.toList(2), ['date', 'NDVI']).get('list')
  return feature.set(ee.Dictionary(ee.List(list).flatten()))


result = fc_filtered.map(GetSeries)
print(result.getInfo())

Give me this result:

{'columns': {}, 'type': 'FeatureCollection', 'features': [], 'properties': {'name': 'kmltest', 'DocID': '1x290vohty0Wgdn5jL3RlpzryK7dfOPtG6yY213e0'}}

So it does not looks like something is coming out of it?

I havent yet tried the next where the following is included:

# Get all possible dates.
dates = ee.List(Sentinel_collection.map(function(img) {
      return ee.Feature(null, {'date': img.date().format("YYYYMMdd") })
}).aggregate_array('date'))

# Make a default value for every date.
header = ee.Feature(null, ee.Dictionary(dates, ee.List.repeat(-1, dates.size())))
output = header.merge(result)
ee.batch.Export.table.toDrive(...)

Any suggestions for what a do wrong? Will prefer to have the result in the feature collection or exported as a list.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
LAC
  • 107
  • 6

1 Answers1

0

As far as I know, you have to export the result to a CSV drive table. So after this line:

result = fc_filtered.map(GetSeries)

You have to write this:

task=ee.batch.Export.table.toDrive(collection=result,description='Name_csv_file', fileFormat='CSV')
task.start()

If you want to know if the export is finished, then check it with:

task.status()
rhal
  • 3
  • 1
  • 5