0

Using Noel Gorelick's segmentation session script you end up with pixels that are assigned to a field. I made some modifications but ended with a similarly structured image included here in the script.

What I would like to do is summarise the information under these pixels (Area, NDVI, etc.) grouped by "labels". In the following example I just get a count of the number of pixels in each group. And try to sum them using an iterate algorithm.

var image = ee.Image("users/JASPR/Lotes/Lotes15M");


Map.centerObject(image,12)
Map.addLayer(image.randomVisualizer())


var imageOfOnes = ee.Image(1)

image = image.addBands(imageOfOnes)

var countOfPixels = image.reduceRegion({
  reducer: ee.Reducer.count().group({
    groupField: 0,
    groupName: 'labels',
  }),
  geometry: image.geometry(),
  scale: 15,
});

print("countOfPixels:",countOfPixels)
//Object (1 Property)
// property is a list called "groups"

var listOfFields = ee.List(ee.Dictionary(countOfPixels).get("groups"))

print("listOfFields:",listOfFields)
//List of 755 elements
// each element is an object with 2 properties

var fieldObject = listOfFields.get(0)

print("fieldObject:",fieldObject)
//Object with 2 Properties
// count and labels

var fieldCount = ee.Dictionary(fieldObject).get("count")

print("fieldCount", fieldCount)
// a number which the number of pixels inside the field


var addPixels = function(myList,sumCount){
    sumCount = sumCount + ee.Dictionary(myList).get("count")
    return sumCount
}




var sumOfPixels = listOfFields.iterate(addPixels,fieldCount)


print("Sum of Pixels", sumOfPixels)

result is an ee.ComputedObject that I can't figure out how to handle.

2
  • Hi Sean! I made code to extract "features" from segments, but as you will understand, you need the (source) image you have segmented. For example, if you want to get the mean NDVI, you'll need the NDVI source. Commented Jan 8, 2019 at 12:38
  • Hi Rodrigo, I shouldn't be using the features, as my regions are way to big to make them into features. I would quickly run out of space for it. My real problem is how to manage the "ee.ComputedObject" that results from my iteration. I'm still trying to get a handle on why this iteration gives me a format I can't handle. Commented Jan 28, 2019 at 14:25

2 Answers 2

1

You are using "client-side" code in iterate, but you can only use "server-side" code.

var addPixels = function(myList,sumCount){
  var count = ee.Dictionary(myList).get("count")
  sumCount = ee.Number(sumCount) 
  return sumCount.add(ee.Number(count))
}


var sumOfPixels = listOfFields.slice(1).iterate(addPixels, fieldCount)

replacing those 2 variables should do it.

3
  • Exactly right. I keep making that mistake! Thanks! Commented Jan 28, 2019 at 19:42
  • The listOfFields.slice(1) is so that it doesn't double count the first element which is already contained in fieldCount Commented Jan 28, 2019 at 19:51
  • I have to say yes in 15 characters.. exactly ;) Commented Jan 28, 2019 at 20:20
0

For this operation, I think best practice is to transform the output to a feature collection. This can be done easily with the reduceToVectors option on an image:

// make a feature collection
var featureCollection = image.reduceToVectors({scale: 15, labelProperty: 'labels',  geometryInNativeProjection: true});  
print(featureCollection);

If you would then like to add information like the area, you could perform operations like:

// an example of how to add statistics such as area and NDVI to the feature collection
var addArea = function(feature){
  var area = feature.area({maxError: 1});
  return feature.set({area_m2: area});
};
var featureCollection = featureCollection.map(addArea);

Or if you want to add the NDVI:

// add an random NDVI image for the example:
var NDVI = ee.ImageCollection("LANDSAT/LC8_L1T_32DAY_NDVI").filterBounds(image.geometry()).first();
var featureCollection = NDVI.reduceRegions(featureCollection, ee.Reducer.mean(), 30);

Here you can find a link to the full script. Below I added the option to make a feature collection from your grouped reducer, however I would recommend to option stated above to retain the geometry.

2
  • This would be a possible solution, if the geographical extent is relatively small. However, Noel Gorelick insisted that large amounts of data should be kept in the image space because of the limitations of moving to table space. I have used your solution previously, but it is not feasible once the number of possible polygons become much larger. (in the 10s of thousands) Commented Jan 7, 2019 at 12:35
  • I just tried to follow up on you approach, which uses reduceRegion and thus is moved outside the image space. You can add NDVI on a per-pixel way to your image using something like addBands. However, for statistics such as sum, mean etc. you also would like, I think what is left is exporting it as tables.
    – Kuik
    Commented Jan 7, 2019 at 13:36

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.