Skip to content

Commit

Permalink
Merge pull request #64 from wri/add_worldpop_agesex_classes
Browse files Browse the repository at this point in the history
create data layer for worldpop_age_sex
  • Loading branch information
weiqi-tori authored Oct 24, 2024
2 parents cbb4325 + 8bd08bb commit fcb6570
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
44 changes: 32 additions & 12 deletions city_metrix/layers/world_pop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,45 @@

from .layer import Layer, get_utm_zone_epsg, get_image_collection


class WorldPop(Layer):
"""
Attributes:
agesex_classes: list of age-sex classes to retrieve (see https://airtable.com/appDWCVIQlVnLLaW2/tblYpXsxxuaOk3PaZ/viwExxAgTQKZnRfWU/recFjH7WngjltFMGi?blocks=hide)
year: year used for data retrieval
spatial_resolution: raster resolution in meters (see https://github.com/stac-extensions/raster)
"""

def __init__(self, spatial_resolution=100, **kwargs):
def __init__(self, agesex_classes=[], year=2020, spatial_resolution=100, **kwargs):
super().__init__(**kwargs)
# agesex_classes options:
# M_0, M_1, M_5, M_10, M_15, M_20, M_25, M_30, M_35, M_40, M_45, M_50, M_55, M_60, M_65, M_70, M_75, M_80
# F_0, F_1, F_5, F_10, F_15, F_20, F_25, F_30, F_35, F_40, F_45, F_50, F_55, F_60, F_65, F_70, F_75, F_80
self.agesex_classes = agesex_classes
self.year = year
self.spatial_resolution = spatial_resolution

def get_data(self, bbox):
# load population
dataset = ee.ImageCollection('WorldPop/GP/100m/pop')
world_pop = ee.ImageCollection(dataset
.filterBounds(ee.Geometry.BBox(*bbox))
.filter(ee.Filter.inList('year', [2020]))
.select('population')
.mean()
)

data = get_image_collection(world_pop, bbox, self.spatial_resolution, "world pop")
return data.population
if not self.agesex_classes:
# total population
dataset = ee.ImageCollection('WorldPop/GP/100m/pop')
world_pop = ee.ImageCollection(dataset
.filterBounds(ee.Geometry.BBox(*bbox))
.filter(ee.Filter.inList('year', [self.year]))
.select('population')
.mean()
)

data = get_image_collection(world_pop, bbox, self.spatial_resolution, "world pop").population

else:
# sum population for selected age-sex groups
dataset = ee.ImageCollection('WorldPop/GP/100m/pop_age_sex')
world_pop = dataset.filterBounds(ee.Geometry.BBox(*bbox))\
.filter(ee.Filter.inList('year', [self.year]))\
.select(self.agesex_classes)\
.mean()
world_pop = ee.ImageCollection(world_pop.reduce(ee.Reducer.sum()).rename('sum_age_sex_group'))
data = get_image_collection(world_pop, bbox, self.spatial_resolution, "world pop age sex").sum_age_sex_group

return data
6 changes: 4 additions & 2 deletions tests/test_layer_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
AlosDSM,
AverageNetBuildingHeight,
BuiltUpHeight,
EsaWorldCover, EsaWorldCoverClass,
EsaWorldCover,
EsaWorldCoverClass,
HighLandSurfaceTemperature,
ImperviousSurface,
LandSurfaceTemperature,
NasaDEM,
NaturalAreas,
NdviSentinel2,
OpenBuildings,
OpenStreetMap,
TreeCanopyHeight,
TreeCover,
UrbanLandUse,
WorldPop, OpenBuildings
WorldPop
)
from tests.resources.bbox_constants import BBOX_BRA_LAURO_DE_FREITAS_1
from tests.tools.general_tools import get_class_from_instance, get_class_default_spatial_resolution
Expand Down

0 comments on commit fcb6570

Please sign in to comment.