Reputation: 1300
I am having trouble converting some ee.Dictionary
(with null-valued keys) returned by sampleRegion
in Earth Engine
. I am trying to sample a multi-band image across multiple regions, then convert the resulting dictionary to ee.FeatureCollection
where the key/value pair (of the dictionary) will be a feature with null
geometry. I would like to keep all keys including those with null
values. Keys having null
values should be recoded to 9 or kept as null
, but I need them as feature in the final collection. I tried using ee.Algorithms.If
to handle these keys having null
values but I got stack with the following error:
FeatureCollection (Error) Error in map(ID=0): Element.geometry, argument 'feature': Invalid type. Expected type: Element. Actual type: String. Actual value: B3
Below is a reproducible example which can also be found here. Any hint will greatly help!
// Some features to use latter in sampleRegion
var roi1 =
/* color: #d63000 */
/* shown: false */
/* displayProperties: [
{
"type": "rectangle"
},
{
"type": "rectangle"
},
{
"type": "rectangle"
}
] */
ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Polygon(
[[[1.2850232278161755, 14.924433184708537],
[1.2850232278161755, 14.741234323298656],
[1.4882702981286755, 14.741234323298656],
[1.4882702981286755, 14.924433184708537]]], null, false),
{
"system:index": "0"
}),
ee.Feature(
ee.Geometry.Polygon(
[[[1.4772839700036755, 14.04155518401385],
[1.4772839700036755, 13.86296344675159],
[1.6393323098474255, 13.86296344675159],
[1.6393323098474255, 14.04155518401385]]], null, false),
{
"system:index": "1"
}),
ee.Feature(
ee.Geometry.Polygon(
[[[1.0817761575036755, 14.478114793660426],
[1.0817761575036755, 14.313173466470698],
[1.2767834817224255, 14.313173466470698],
[1.2767834817224255, 14.478114793660426]]], null, false),
{
"system:index": "2"
})]),
roi2 =
/* color: #98ff00 */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Polygon(
[[[1.6970105325036755, 14.448859913271122],
[1.6970105325036755, 14.25994066279539],
[1.9387097512536755, 14.25994066279539],
[1.9387097512536755, 14.448859913271122]]], null, false),
{
"system:index": "0"
})]),
roi3 =
/* color: #0b4a8b */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Polygon(
[[[1.7739148293786755, 14.38501773168985],
[1.7739148293786755, 14.29188185649032],
[1.8755383645349255, 14.29188185649032],
[1.8755383645349255, 14.38501773168985]]], null, false),
{
"rec": 3,
"system:index": "0"
})]);
// Getting the image of the region of interest
var roi = ee.Geometry.Point([1.864578244475683, 14.492292970253338]);
var image = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2019-01-01', '2019-01-31')
.filterBounds(roi)
.select(['B5', 'B4', 'B3'])
.toBands()
.rename(['B5', 'B4', 'B3']);
// Checking it out
print(image);
// Define the visualization parameters.
var vizParams = {
bands: ['B5', 'B4', 'B3'],
min: 0,
max: 0.5,
gamma: [0.95, 1.1, 1]
};
// Center the map and display the image.
Map.centerObject(image, 9);
Map.addLayer(image, vizParams, 'image');
// masking out some regions from the
// image, so that sampleRegion will return null in that region
var mask = ee.Image.constant(1).clip(roi2).mask().not()
var imageMasked = image.updateMask(mask);
// displaying the masked image
Map.addLayer(imageMasked, vizParams, 'imageMasked');
/////////// The actual problem start from here ///////////
// making a feature collection (masked + unmasked region)
var roi = roi1.merge(roi3);
var regionSamples = roi.map(function(x){
var out = imageMasked.reduceRegion({
reducer : ee.Reducer.mean().unweighted(),
geometry : x.geometry(),
scale : 30
})
// Getting the keys of the dictionary returned by sampleRegion
var keys = out.keys()
// mapping a function over the list of
// keys to latter extract their corresponding values
var keyVals = keys.map(function(y){
var proba = ee.Algorithms.If({
// test if the value corresponding to a key is null
condition: ee.Algorithms.IsEqual(out.get(y), null),
// if it the case, return a feature with property prob set to 9
trueCase: ee.Feature(null, {prob: 9}),
// if it not the case, return a feature with property prob
// set the value return by sampleRegion
falseCase: ee.Feature(null, {prob: out.get(y)})
})
return proba
})
return ee.FeatureCollection(keyVals)
})
print(regionSamples.flatten(), 'regional samples')
Upvotes: 0
Views: 1885
Reputation: 1300
I finally figured it out and I am posting the answer in case someone may be interested. I addressed the issues using the function below.
/////////// The actual problem start from here ///////////
/**
* Reduce multiple regions of an image to feature collection.
* @param {Image} The image to reduce.
* @param {FeatureCollection} roi The area/areas of interest.
* @param {Float} scale A nominal scale in meters of the projection to work in.
* @param {Float} nullKeyValue The value to use for keys where reduceRegion returns null.
* @return {FeatureCollection} A feature collection where dictionary keys returned by reduceRegion are converted to ee.Feature.
*/
function sampleFeatures(image, roi, nullKeyValue, scale){
var keyVals = roi.map(function(x){
var dictionary = image.reduceRegion({
reducer : ee.Reducer.mean().unweighted(),
geometry : x.geometry(),
scale : scale
})
var noNullDic = dictionary.map(function(key, val){
var dic = ee.Algorithms.If({
condition: ee.Algorithms.IsEqual(val, null),
trueCase: nullKeyValue,
falseCase: dictionary.get(key)
})
return dic;
});
var keys = noNullDic.keys()
var vals = keys.map(function(key){
var vl = ee.List([noNullDic.get(key)])
var ky = ee.List([key])
return ee.Feature(null, ee.Dictionary.fromLists(ky, vl))
})
return ee.FeatureCollection(vals)
})
return keyVals.flatten()
}
var test = sampleFeatures(imageMasked, roi, 999, 30)
print(test, 'test sampleFeatures')
Upvotes: 1