1

I'm trying to calculate the population within a specific radius within a specific country using Google Earth Engine. My plan is to use reduceToVectors to convert the population values into a featureCollection to then use a buffer. The final goal is to be able to use Inspector to click on a specific pixel and see the population within x km of the pixel as a value.

My code is below:

// Load country boundaries
var countryBoundaries = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level0");

// Filter country boundaries for specific country
var selectedCountry = countryBoundaries.filter(ee.Filter.eq('ADM0_NAME','Afghanistan'))

// Load population dataset
var popDataset = ee.ImageCollection("CIESIN/GPWv411/GPW_UNWPP-Adjusted_Population_Count")

// Retrieve the most recent population image
var recentPop = popDataset.limit(1, 'system:time_start', false).first();

// Round population values to integers
var popCount = recentPop.select('unwpp-adjusted_population_count').round();

// Clip population dataset to just the selected country, retain integer values
var clipped = popCount.clip(selectedCountry).toInt()

// Population vis params
var popVis = {
  min: 0,
  max: 200,
  palette: ['#B1F9E8','#8EE1D3','#6AC9BE','#47B0AA','#239895','#008080']
}

// Convert the clipped dataset to a feature collection
var vector = clipped.reduceToVectors(null, selectedCountry, 5100)

print(vector)

// Convert the feature collection to a feature to use a buffer
var feature = ee.Feature(vector)

// Create a buffer with a 50 km radius per point
var buffer = feature.buffer(50000)

// Add the population layer to the map
Map.addLayer(clipped, popVis, '2020 Population')

// Add the buffer layer to the map
Map.addLayer(buffer, popVis, 'buffered population')

My main issue is that I can't display the "buffered population" on the map because I get the following error: "Layer error: Feature.buffer: Unable to use a collection in an algorithm that requires a feature or image. This may happen when trying to use a collection of collections where a collection of features is expected; use flatten, or map a function to convert inner collections to features. Use clipToCollection (instead of clip) to clip an image to a collection."

Is there a way to make it so that when I click on a specific pixel, it shows the total population within the buffer radius?

1
  • Welcome to GIS SE. As a new user, please take the Tour, which emphasizes the importance of asking One question per Question. Bullet lists of questions will generate closure votes for lack of focus. Please pick the one question you want to ask and Edit the Question to focus on that.
    – Vince
    Aug 28, 2022 at 23:17

1 Answer 1

1

This is essentially a duplicate of your previous question, with the addition of handling clicks on the map? The following will show the total population within a radius of 200m of where you clicked. Adjust the radius as you want.

Map.style().set('cursor', 'crosshair')
Map.onClick(printPopulation)
Map.setOptions('HYBRID')

var population = ee.ImageCollection("CIESIN/GPWv411/GPW_UNWPP-Adjusted_Population_Count")
  .limit(1, 'system:time_start', false)
  .mosaic()
  .select(['unwpp-adjusted_population_count'], ['population']).round()

var populationSum = population.reduceNeighborhood({
  reducer: ee.Reducer.sum(), 
  kernel: ee.Kernel.circle(200, 'meters'),
  inputWeight: 'mask'
})

Map.addLayer(population, {min: 0, max: 300, palette: '#042333, #2c3395, #744992, #b15f82, #eb7958, #fbb43d, #e8fa5b'}, 'population', false)
Map.addLayer(populationSum, {min: 0, max: 5000, palette: '#042333, #2c3395, #744992, #b15f82, #eb7958, #fbb43d, #e8fa5b'}, 'populationSum', false)

function printPopulation(latLon) {
  var point = ee.Geometry.Point([latLon.lon, latLon.lat])
  var populationAroundPoint = populationSum.reduceRegion({
    reducer: ee.Reducer.first(), 
    geometry: point, 
    scale: 100, 
  }).getNumber('population_sum')
  
  print(populationAroundPoint)
}

https://code.earthengine.google.com/dd55de200c0a57244059db8e6cf06f34

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.