I applied a spatial join to two feature collections and am trying to extract specific properties from one in order to append them to another.

In order to retrieve a property from one of the features, I had to go through a convoluted process that I'm not sure I will be able to map accurately (see second block).

Is there a better way to do this?

I followed this link closely. For clarity, what I hope for is to add the HUC ID to the myPoints featureCollection as another property in the following code:

var HUC = ee.FeatureCollection("USGS/WBD/2017/HUC10");
var myPoints = ee.FeatureCollection("users/seancliffcarter/fishPrecenseNoNA");

//Define a filter where the points intersect the HUC
var distFilter = ee.Filter.intersects({
  leftField: '.geo', 
  rightField: '.geo', 
  maxError: 10

//Save the HUC object as a property named 'HUC'
var distSaveAll = ee.Join.saveAll({
  matchesKey: 'HUC',

//Apply the join
var spatialJoined = distSaveAll.apply(myPoints,HUC,distFilter);

This works great, except that each feature within spatialJoined now contains a HUC object as a property, but I'd like to extract the ID (or something similar) from each.

A basic sanity check starts to break down:

//Select an arbitrary feature to troubleshoot
var testFeature = spatialJoined.first();

//Select the HUC property within the feature
var testHUC = ee.Feature(testFeature.get('HUC'));

//This is where I'm struggling. Is testHUC a feature or a list? I casted it to a Feature, correct?

//Implies that testHUC is a "list" object:
print(testHUC,"Test HUC");

//Implies that testHUC is a "feature":

//If testHUC is a "feature", I should be able to use .get():

var testHUCName = testHUC.get('name');

//Gives Invalid type error

//Assuming it is a list, I can use the following code to extract a property. 
//First cast to list:
var featureList = ee.List(testFeature)

//Then retrieve first element and recast to a feature?:
var firstElement = ee.Feature(featureList.get(0))

//Finally get the ID property:

var hucID = firstElement.get('ID');

1 Answer 1


If you are confused with the data types of your objects, you can always use print. print(spatialJoined.first()); gave the following result:

enter image description here

Line marked 1 is a ee.Feature(), 2 is ee.List() and 3 is ee.Feature(). So just cast the datatypes in that order if you want to access the id property.

Lastly, id is actually stored as system:index for ee.Feature() objects. More details about that here.

So putting it all together, just replace your second code chunk with the following lines:

/// adding id property of HUC feature to root feature
var spatialJoined = spatialJoined.map(function(feature){
  var hucid = ee.Feature(ee.List(feature.get("HUC")).get(0)).get("system:index");
  var feature = feature.set('HUCid',hucid);
  return feature

This link has the complete working solution.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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