TransWikia.com

Adding property while converting table into list in Google Earth Engine

Geographic Information Systems Asked on August 25, 2021

Is there a way to add a property when converting from a table to a list?

I have a table of lat and long points uploaded from a .csv (locTable), and found a GEE code that changed the format of my table so that I could use it as points for my ui.Chart input to make a timeseries graph. It converts the lat and long to geometries, but I want to add one of the table’s other columns (‘ID’) as a property when doing this conversion, so the lat and long points have an ID when looking at the timeseries chart.

Right now I have a function to add a property to a feature collection, and I call it in my function to reformat my table, but the geometries are in one list, and the IDs are in another.

Is there a way I can add the ID as a property when getting my geometries?

I tried .get() and .set() and neither of them worked.

When I hard code in a subset of lat/long points as a Feature Collection, I don’t need to go through all these steps, but when uploaded as a .csv file I do. I have a couple thousand points, so was hoping to be able to start with a .csv.

/// function to add a property to a feature in a feature collection
var addProperty = function(feature, property) {
  var properties = feature.propertyNames()
  var selectProperties = properties.filter(ee.Filter.eq('item', property))
  return feature.select(selectProperties)
}

//  Turn the location table (locTable) into a feature collection, necessary for ui.Chart input
  // Make feature collection a list and map over the elements (features)
var tabletoList = locTable.toList(table.size()).map(function(feature) {
  // Cast the input as a feature since it is coming from a list, get its
  // geometry, then split geometries into a list.
  var geomList = ee.Feature(feature).geometry().geometries()
  
// get the ID names from the feature collection and put into a list
  var idList = locTable.map(function(feature){
    return addProperty(feature, 'ID')
    })

  // Map over the geometry list.
  var featureList = geomList.map(function(geom) {
    // Return a feature representation of the geometry.
    return ee.Feature(ee.Geometry(geom))
  })
  // Return a list of all the features making up a potential multi-geometry,
  // this is a list. 
  return featureList;
})
// The result is a list of feature lists - flatten to a single list of features.
.flatten();



One Answer

In case any one runs into the same problem. You first extract the list of features you want to add, and then add a second function to map over that list and add the features as a property.

//  Turn the location table (locTable) into a feature collection, necessary for ui.Chart input
  // Make feature collection a list and map over the elements (features)
var tabletoList = locTable.toList(table.size()).map(function(feature) {
  // Cast the input as a feature since it is coming from a list, get its
  // geometry, then split geometries into a list.
var id = ee.Feature(feature).get('ID')
var geomList = ee.Feature(feature).geometry().geometries()
 
  // Map over the geometry list.
  var featureList = geomList.map(function(geom) {
    // Return a feature representation of the geometry.
    return ee.Feature(ee.Geometry(geom))
  })
// Map over the feature list, adding the IDs
 var IDlist = featureList.map(function(index){
    return ee.Feature(index).set('ID',id)
  })
  // Return a list of all the features making up a potential multi-geometry,
  // this is a list. 
  return IDList;
})
// The result is a list of feature lists - flatten to a single list of features.
.flatten();

Answered by fjellrev123 on August 25, 2021

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