1

I have been using a function to get the centroid of each polygon (building) in my dataset.

var buildings = ee.FeatureCollection('GOOGLE/Research/open-buildings/v1/polygons')
Map.addLayer(buildings, {color: 'FF0000'}, 'buildings');
// This function creates a new feature from the centroid of the geometry.
var getCentroid = function(feature) {
  var centroid = feature.geometry().centroid();
  return ee.Feature(centroid)
};
// Map the centroid getting function over the features.
var centroids = buildings.map(getCentroid);
// Display the results.
Map.addLayer(centroids, {color: 'FF0000'}, 'centroids');

However, I noticed that one of the properties of each building is 'longitude_latitude' which already includes the coordinates of the centroid.

print(buildings.first())
print(centroids.first())

Is there any way that I can get the centroids as features from that?

For context: I then try to count the centroids with the code:

// ***************************************************************************
// Change to Image
// ***************************************************************************

// add dummy property to use for reduceToImage
var points = ee.FeatureCollection(centroids).map(function(feature){
  return feature.set('dummy',1)
});
Map.addLayer(points, {color: 'FF0000'}, 'points'); //Confirmed same as centroids
print(points.first())

// specify scale in meters to project point Image to
var ptScale = ee.Number(1000);

// convert featureCollection to Image and add geoinformation
var ptImg = points
  .reduceToImage({
    properties: ['dummy'],
   reducer: ee.Reducer.sum(),
}).reproject('epsg:4326',null,ptScale);

// ***************************************************************************
// Count buildings and display
// ***************************************************************************

// specify output scale, in this case 1km
var outScale = ee.Number(1000);
// reduce resolution of Image and reproject from ptScale to outScale 
var countImg = ptImg.reduceResolution(ee.Reducer.mean(),false,60000)
  .reproject('epsg:4326',null,outScale)
  .multiply(outScale.pow(2)).divide(ptScale.pow(2)).round().int();
// display results!   
Map.addLayer(countImg,{min:0,max:3},'Point Counts');
1
  • You could, but the resulting function would actually be a lot more complicated and cumbersome that the one that you wrote. It would entail extracting the property of "longitude_latitude", reading the resulting dictionary, placing the coordinates in separate variables and then re-building as coordinates for the geometry that you would then give to the feature as a return of your function. Aug 10, 2021 at 19:35

1 Answer 1

0

You still have to map a function over the collection so it's only saving you the centroid() call:

var centroids = buildings.map(function(f) { 
    return ee.Feature(f.get("longitude_latitude"), {}) 
})

What are you going to do with the centroids? There might be a way to short-circuit a little of the work.

4
  • Thanks! I'm trying to count them (with the code I added now to the question) - but keep getting the error: "centroids: Tile error: Computation timed out."
    – Emma
    Aug 10, 2021 at 20:43
  • That's definitely the hard way. Why are you summing a dummy value instead of just counting the original polygons directly in reduceToImage? code.earthengine.google.com/5a63f2448bb4d66400a7c576bbce6c1a How important is using the centroid vs just using the whole building? (The polygons are indexed, the centroids aren't so it'll be vastly slower and you'll almost certainly have to run it as an export). Aug 12, 2021 at 8:46
  • That works way better! The only problem now is that I'm getting memory problems or 'Request payload size exceeds the limit' when I try to scale it via exporting: code.earthengine.google.com/36d7aa0a95b096b7a9510c7763e16e0f Do you know how to overcome this?
    – Emma
    Aug 15, 2021 at 10:42
  • Updated link (which runs longer but still gives memory error): code.earthengine.google.com/c6f709cebe6f4de716261c7ed71a75ca
    – Emma
    Aug 16, 2021 at 8: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.