Skip to content

Commit

Permalink
fix sum bands issue
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqi-tori committed Sep 18, 2024
1 parent 9a4e3fd commit ea890e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 5 additions & 3 deletions city_metrix/layers/world_pop.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
class WorldPop(Layer):
"""
Attributes:
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, year=2020, spatial_resolution=100, **kwargs):
super().__init__(**kwargs)
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]))
.filter(ee.Filter.inList('year', [self.year]))
.select('population')
.sum()
.mean()
)

data = get_image_collection(world_pop, bbox, self.spatial_resolution, "world pop")
Expand Down
14 changes: 7 additions & 7 deletions city_metrix/layers/world_pop_age_sex.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def __init__(self, agesex_classes=['M_70'], year=2020, spatial_resolution=100, *
def get_data(self, bbox):
# load population
dataset = ee.ImageCollection('WorldPop/GP/100m/pop_age_sex')
world_pop = ee.ImageCollection(dataset
.filterBounds(ee.Geometry.BBox(*bbox))
.filter(ee.Filter.inList('year', [self.year]))
.select(self.agesex_classes)
.sum()
)
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")
return data.population
return data.sum_age_sex_group

0 comments on commit ea890e4

Please sign in to comment.