1

I'm trying to get a table from a 2d-array image. Basically, the Image (called segmentationInfo) looks like this:

The 2-D ‘LandTrendr’ annual segmentation array looks like this:

[
  [1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, ...] // Year list 
  [ 811,  809,  821,  813,  836,  834,  833,  818,  826,  820,  765,  827,  775, ...] // Source list
  [ 827,  825,  823,  821,  819,  817,  814,  812,  810,  808,  806,  804,  802, ...] // Fitted list
  [   1,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    1, ...] // Is Vertex list
]

How do I convert this to an exportable table?

My commented code is below.

// START INPUTS

var area = ee.Geometry.Polygon(
        [[[-64.77293019260956, -2.4770762036671767],
          [-64.77293019260956, -2.5250956006529575],
          [-64.71902852024628, -2.5250956006529575],
          [-64.71902852024628, -2.4770762036671767]]], null, false);

// define collection parameters
var startYear = 1986;
var endYear = 2020;
var startDay = '06-01';
var endDay = '12-31';
var index = 'B5';
var maskThese = ['cloud', 'shadow', 'water'];

// define landtrendr parameters
var runParams = { 
  maxSegments:            6,
  spikeThreshold:         0.6,
  vertexCountOvershoot:   3,
  preventOneYearRecovery: true, 
  recoveryThreshold:      0.25,
  pvalThreshold:          0.05,
  bestModelProportion:    0.75,
  minObservationsNeeded:  6
};

// define change parameters
var changeParams = {
  delta:  'loss',
  sort:   'greatest', //option: newtest, least, oldest, fastest, slowest
  year:   {checked:true, start:1986, end:2020},
  mag:    {checked:true, value:200,  operator:'>'}, //default 200
  dur:    {checked:true, value:4,    operator:'<'}, // default 4
  preval: {checked:true, value:300,  operator:'>'},
  mmu:    {checked:true, value:11},
  
};

// END INPUTS


// load the LandTrendr.js module
var ltgee = require('users/emaprlab/public:Modules/LandTrendr.js'); 

// add index to changeParams object
changeParams.index = index;

// run landtrendr
var lt = ltgee.runLT(startYear, endYear, startDay, endDay, area, index, [], runParams, maskThese);

print(lt, 'LandTrendr');

// selecting the LandTrendr band

var segmentationInfo = lt.select(['LandTrendr']); // subset the LandTrendr segmentation info
print(segmentationInfo, 'segmentInfo'); //It contains 4 rows and as many columns as there are annual observations for a given pixel through the time series.


2

Table export lets you export multidimensional arrays, so you just need to convert things into a collection of features.

If you really only have the 1 array, then it'd be something like this:

Export.table.toDrive(
    ee.FeatureCollection([
        ee.Feature(null, {segmentationInfo: segmentationInfo})
    ]))

But LandTrendr produces those arrays on a per-pixel basis and in your code, segmentInfo is an image, so you probably want to export a region of the image. You can do that with reduceRegion and a toCollection reducer to produce a table of pixel values (specify a scale smaller than 1000 if you want smaller pixels):

var table = segmentationInfo.reduceRegion(ee.Reducer.toCollection(["LandTrendr"]), area, 1000).get("LandTrendr")

If you want to split the arrays into 4 separate lists, then you can either map a function over the table to produce 4 values instead of 1 (or you could just split the input image into 4 bands before you run the reduceRegion).

table = ee.FeatureCollection(table).map(function(f) {
    var arr = ee.Array(f.get('LandTrendr'))
    return ee.Feature(null, {
        year: arr.cut([0, -1]),
        source: arr.cut([1, -1]),
        fitted: arr.cut([2, -1]),
        vertex: arr.cut([3, -1])
    })
})

All that said, because its computed on a per-pixel basis, and there are usually a lot of pixels, most people don't export the whole results of LandTrendr. More often you make a visualization of first/last/largest/count-of disturbance, and export that as an image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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