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');