TransWikia.com

Extracting NDVI scores over time for defined field boundaries

Geographic Information Systems Asked by Ryan Huffman on February 8, 2021

I am trying to do something similar to this post with the dataset "users/ryanhuffman/Training_Dataset_Trunc" and keep getting hung up on an error with the dictionary section of the code. For simplicity I renamed my column headers and kept variable names the same to streamline retro-fitting code.

Link below for context.

var Akdarya = ee.FeatureCollection("users/ryanhuffman/Training_Dataset_Trunc")

Map.centerObject(Akdarya)
Map.addLayer(Akdarya)

var S2 = ee.ImageCollection('COPERNICUS/S2')
  .filterDate('2018-05-01', '2018-09-30')
  .filterBounds(Akdarya)
  .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 20)

var maskcloud = function(image) {
  var QA60 = image.select(['QA60']);
  return image.updateMask(QA60.lt(1))
}

var NDVI = function(image) {
  return image.expression(
    '(NIR - RED) / (NIR + RED)', {
      'NIR': image.select('B8'),
      'RED': image.select('B4'),
    }).rename('NDVI').copyProperties(image, image.propertyNames());
}

var NDVIcol = S2.map(maskcloud)
  .map(NDVI)

var ts = NDVIcol.map(calculateFarmersNDVI)

function calculateFarmersNDVI(image) {
  var properties = ee.Dictionary(
    Akdarya.iterate(function(region, acc) {
      return ee.Dictionary(acc)
        .combine(
          ndviForArea(image, ee.Feature(region))
        )
    }, ee.Dictionary())
  )
  return ee.Feature(null, properties
    .set('Year', image.date().get('year'))
    .set('Month', image.date().get('month'))
    .set('Day', image.date().get('day'))
  )
}

function ndviForArea(image, region) {
  var ndvi = image.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: region.geometry(),
    scale: 30
  }).get('NDVI')

  var farmers = region.getString('Farmers')


  var ndviOrBlank = ee.Algorithms.If(ndvi, ndvi, '')
  return ee.Dictionary().set(farmers, ndviOrBlank)
}

var listOfFarmers = ee.List(Akdarya.aggregate_array('Farmers'))
  .map(function (farmers) { 
    return ee.String(farmers)
  })
ee.List(['Year', 'Month', 'Day'])
  .cat(listOfFarmers)  

  .evaluate(function (selectors) {
    Export.table.toDrive({
      collection: ts,
      description: 'NDVI_test3',
      fileFormat: 'CSV',
      selectors: selectors
    })    
  })

Exporting NDVI result data as a table using Earth Engine

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