I want to calculate the sum of the population living in the upstream area of stations using the hydro package by gena. I think I will have to loop over the stations (using the identifier "GEMS_Stati"). This is my (not very elegant) code to get the population for a single station:
var stations = ee.FeatureCollection('users/basins/stations');
//select only one station
var filter = ee.Filter.inList('GEMS_Stati', ['EGY00002']);
var FilteredStation = stations.filter(filter);
var PopCount2000= ee.Image('CIESIN/GPWv411/GPW_UNWPP-Adjusted_Population_Count/gpw_v4_population_count_adjusted_to_2015_unwpp_country_totals_rev11_2000_30_sec');
var hydro = require('users/gena/packages:hydro');
var catchments = hydro.getCatchments({level: 6});
var catchmentsSelected = hydro.getCatchments({outlet: FilteredStation, level: 6}) ;
var catchment_Pop2000 = PopCount2000.reduceRegions({
collection: catchmentsSelected,
reducer: ee.Reducer.sum(),
});
var sum_pop_upstream = catchment_Pop2000.reduceColumns({
reducer: ee.Reducer.sum(),
selectors: ['sum']
});
How can get a table that contains the station ID, "GEMS_Stati", and the respective value of "sum_pop_upstream" for each station?
Answer
You'll need to map over all your stations and based on that geometry (points) select the relevant hydro features. If you create a union of those features you'll have to apply a reduceRegion only 1 time.
var stations = ee.FeatureCollection('users/basins/stations');
var hydro = require('users/gena/packages:hydro');
var PopCount2000= ee.Image('CIESIN/GPWv411/GPW_UNWPP-Adjusted_Population_Count/gpw_v4_population_count_adjusted_to_2015_unwpp_country_totals_rev11_2000_30_sec');
//select only one station
var filter = ee.Filter.inList('GEMS_Stati', ['EGY00002']);
// var catchments = hydro.getCatchments({level: 6});
var stationSubset = stations.limit(2) // for now a subset for speed
print(stationSubset)
var test = stationSubset.map(function(feature){
var props = ee.Feature(feature).get('GEMS_Stati')
var geom = ee.Feature(feature).geometry()
var catchmentsSelected = hydro.getCatchments({outlet: geom, level: 6})
var union = catchmentsSelected.union(); // create a union of all the selected catchments
var catchment_Pop2000 = PopCount2000.reduceRegion({
geometry: catchmentsSelected,
reducer: ee.Reducer.sum(),
})//.values().get(0);
return ee.Feature(null, catchment_Pop2000).set('GEMS_Stati', props)
})
print(test, 'test')
No comments:
Post a Comment