Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create data layer for worldpop_age_sex #64

Merged
merged 14 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment n possible list of values here just for documentation: [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]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading