Skip to content

Commit

Permalink
update world pop age sex group layer
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqi-tori committed Sep 25, 2024
1 parent a501beb commit c010e55
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 73 deletions.
1 change: 0 additions & 1 deletion city_metrix/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .landsat_collection_2 import LandsatCollection2
from .sentinel_2_level_2 import Sentinel2Level2
from .world_pop import WorldPop
from .world_pop_age_sex import WorldPopAgeSex
from .built_up_height import BuiltUpHeight
from .average_net_building_height import AverageNetBuildingHeight
from .open_buildings import OpenBuildings
Expand Down
39 changes: 27 additions & 12 deletions city_metrix/layers/world_pop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@

from .layer import Layer, get_utm_zone_epsg, get_image_collection


class WorldPop(Layer):
"""
Attributes:
agesex_classes: list of age-sex classes to retrieve
year: year used for data retrieval
spatial_resolution: raster resolution in meters (see https://github.com/stac-extensions/raster)
"""

def __init__(self, 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
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")
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
43 changes: 0 additions & 43 deletions city_metrix/layers/world_pop_age_sex.py

This file was deleted.

12 changes: 1 addition & 11 deletions tests/test_layer_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
TreeCanopyHeight,
TreeCover,
UrbanLandUse,
WorldPop,
WorldPopAgeSex
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 Expand Up @@ -176,15 +175,6 @@ def test_world_pop_downsampled_spatial_resolution(self):

assert pytest.approx(target_downsized_res, rel=RESOLUTION_TOLERANCE) == estimated_actual_res
assert downsizing_is_within_tolerances

def test_world_pop_age_sex_downsampled_spatial_resolution(self):
class_instance = WorldPopAgeSex()
default_res_data, downsized_res_data, target_downsized_res, estimated_actual_res = (
_get_sample_data(class_instance, BBOX, DOWNSIZE_FACTOR))
downsizing_is_within_tolerances = _evaluate_raster_value(default_res_data, downsized_res_data)

assert pytest.approx(target_downsized_res, rel=RESOLUTION_TOLERANCE) == estimated_actual_res
assert downsizing_is_within_tolerances

def test_halved_up_sampled_spatial_resolution(self):
class_instance = Albedo()
Expand Down
7 changes: 1 addition & 6 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
TreeCanopyHeight,
TreeCover,
UrbanLandUse,
WorldPop,
WorldPopAgeSex
WorldPop
)
from tests.resources.bbox_constants import BBOX_BRA_LAURO_DE_FREITAS_1

Expand Down Expand Up @@ -120,7 +119,3 @@ def test_urban_land_use():
def test_world_pop():
data = WorldPop().get_data(BBOX)
assert np.size(data) > 0

def test_world_pop_age_sex():
data = WorldPopAgeSex().get_data(BBOX)
assert np.size(data) > 0

0 comments on commit c010e55

Please sign in to comment.