From e9592c5562469b88dc52ed9908f5c063aa5e3974 Mon Sep 17 00:00:00 2001 From: Ted Wong Date: Mon, 23 Sep 2024 12:57:22 -0400 Subject: [PATCH] Total pop is sum over list of agesex classes --- city_metrix/layers/world_pop_age_sex.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/city_metrix/layers/world_pop_age_sex.py b/city_metrix/layers/world_pop_age_sex.py index ec3eda1..860642c 100644 --- a/city_metrix/layers/world_pop_age_sex.py +++ b/city_metrix/layers/world_pop_age_sex.py @@ -4,6 +4,7 @@ import ee from .layer import Layer, get_utm_zone_epsg, get_image_collection +from city_metrix.layers import WorldPop class WorldPopAgeSex(Layer): @@ -14,19 +15,27 @@ class WorldPopAgeSex(Layer): spatial_resolution: raster resolution in meters (see https://github.com/stac-extensions/raster) """ - def __init__(self, agesex_classes=['M_70'], year=2020, spatial_resolution=100, **kwargs): + def __init__(self, agesex_classes=[], year=2020, spatial_resolution=100, **kwargs): super().__init__(**kwargs) self.agesex_classes = agesex_classes self.year = year self.spatial_resolution = spatial_resolution def get_data(self, bbox): - # load population + # If no agesex classes requested, return plain WorldPop + if not self.agesex_classes: + return WorldPop().get_data(bbox) + dataset = ee.ImageCollection('WorldPop/GP/100m/pop_age_sex') - world_pop = dataset.filterBounds(ee.Geometry.BBox(*bbox))\ + + # Sum over agesex classes + world_pop = ee.Image(0) + for agesex_class in self.agesex_classes: + tmp = dataset.filterBounds(ee.Geometry.BBox(*bbox))\ .filter(ee.Filter.inList('year', [self.year]))\ - .select(self.agesex_classes)\ + .select(agesex_class)\ .mean() + world_pop = world_pop.add(tmp) world_pop = ee.ImageCollection(world_pop.reduce(ee.Reducer.sum()).rename('sum_age_sex_group'))