TransWikia.com

GEE NDVI time series extraction does not populate CSV file columns

Geographic Information Systems Asked on January 31, 2021

I am trying to extract a time series of sentinel NDVI data. This script (below) takes a feature collection of points, buffers around them, and collects all available imagery 30 days before each feature date.

The output should be a CSV file with two additional columns: "NDVIs" and "dates." Each row in these columns will be an array of NDVI and unix dates that correspond to each feature date.

However, the task does not fill the columns. Instead the resulting CSV has ‘[]’ in both columns. See here:

resulting CSV file

Any idea what could be going wrong here?

########################################################################################
#################            NDVI at locations                ##########################
########################################################################################

import ee
#ee.Authenticate()
ee.Initialize()
l8 = ee.ImageCollection("COPERNICUS/S2")

table = ee.FeatureCollection("users/ddlawton/LocustDat_500")

def locational(feature):

  end = ee.Number(feature.get('unixdates'))
  start = end.subtract(2592000000) #subtract 30 days (in milliseconds)
##initialize lat and long
  lat = ee.Number(feature.get('latitude'))
  longi = ee.Number(feature.get('longitude'))

##initialize an EE geometry point with lat and long and buffer 
  geom = ee.Geometry.Point([longi,lat])
  buffed = geom.buffer(500)

  #start = ee.Date(1496793600000)
  #end = ee.Date(1528329600000)

  #end = ee.Date('2019-06-01')
  #start = ee.Date('2019-05-01')
  
  ########define date range of interest
  
  fCollectp = l8.filterBounds(buffed)
  fCollect = fCollectp.filter(ee.Filter.date(start, end))
  #filterdate range
  
  

  def addDates(image):
    imagep = ee.Image(image)
    setted = None
    nir = imagep.select('B5');
    red = imagep.select('B4');
    ndviImage = nir.subtract(red).divide(nir.add(red)).rename('NDVI');
    NDVIavgp = ndviImage.reduceRegion(ee.Reducer.mean(),buffed,scale=30,maxPixels=1e9)
    NDVIavg = NDVIavgp.get('NDVI')
    NDVIdateCap = ee.String(image.get('GENERATION_TIME'))

    tempA = ee.List([NDVIavg,-9999])
    tempB = tempA.reduce(ee.Reducer.firstNonNull())#this will filter out a null if the image was not actually over the gauge
    


    NDVISetted = ee.Image(image.set('date',NDVIdateCap).set('NDVI',tempB))


    return NDVISetted
      
  mapC = fCollect.map(addDates)
  agg = mapC.aggregate_array('date')
  aggb = mapC.aggregate_array('NDVI')
  agFeat = feature.set('dates',agg).set('NDVIs', aggb)
  return agFeat


mapF = table.map(locational)


task = ee.batch.Export.table.toDrive(
  collection=mapF,
  description='Locust_outbreaks_jan_23',
  fileFormat='CSV'
)

task.start()

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP