I have a featureCollection that contains a string property called 'TaxOrder'. In total there are 12 different possible TaxOrders (['Alfisols', 'Andisols', 'Aridisols', ...]
).
I want to add the number of each TaxOrder (i.e. a number between 1 and 12) to each feature relative to the TaxOrder, e.g. if the TaxOrder is 'Alfisols', add {taxOrderNum: 1}
as a property.
I've figured out a very lengthy way of doing this, so I am thinking there must be a simpler way of doing it, using .remap()
for example (but without actually replacing the values of an existing property).
// Remove areas with null TaxOrder
USDA_Soils = USDA_Soils.filterMetadata('TaxOrder', 'not_equals', '');
print(USDA_Soils.limit(100));
// Function to add number corresponding to tax order
var addTaxOrderNum = function(feat) {
var result = ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Alfisols'),
trueCase: feat.set({taxOrderNum: 1}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Andisols'),
trueCase: feat.set({taxOrderNum: 2}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Aridisols'),
trueCase: feat.set({taxOrderNum: 3}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Entisols'),
trueCase: feat.set({taxOrderNum: 4}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Gelisols'),
trueCase: feat.set({taxOrderNum: 5}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Histosols'),
trueCase: feat.set({taxOrderNum: 6}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Inceptisols'),
trueCase: feat.set({taxOrderNum: 7}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Mollisols'),
trueCase: feat.set({taxOrderNum: 8}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Oxisols'),
trueCase: feat.set({taxOrderNum: 9}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Spodosols'),
trueCase: feat.set({taxOrderNum: 10}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Ultisols'),
trueCase: feat.set({taxOrderNum: 11}),
falseCase: ee.Algorithms.If({
condition: ee.String(feat.get('TaxOrder')).equals('Vertisols'),
trueCase: feat.set({taxOrderNum: 12}),
falseCase: feat
})
})
})
})
})
})
})
})
})
})
})
});
return result;
};
// Map function over featureCollection
var USDA_Soils_Final = USDA_Soils.map(addTaxOrderNum);
print(USDA_Soils_Final.limit(100));
https://code.earthengine.google.com/b00826b5d9ed0d12811389c7b511bd8d
Best Answer
A general rule for this kind of problem is to express your repetitive structure as data, not code. A mapping from one value to another is a kind of data that can be stored in a dictionary.
So, first define a dictionary. (This can be done more cleverly to not have to list the numbers explicitly, but let's keep things simple.)
Then, use that dictionary inside the feature mapping function.
https://code.earthengine.google.com/423c5b4f30b3928c57b8ec9413912bf2