|
5 | 5 |
|
6 | 6 | from .layer import Layer, get_utm_zone_epsg, get_image_collection
|
7 | 7 |
|
| 8 | + |
8 | 9 | class WorldPop(Layer):
|
9 | 10 | """
|
10 | 11 | Attributes:
|
| 12 | + agesex_classes: list of age-sex classes to retrieve |
11 | 13 | year: year used for data retrieval
|
12 | 14 | spatial_resolution: raster resolution in meters (see https://github.com/stac-extensions/raster)
|
13 | 15 | """
|
14 | 16 |
|
15 |
| - def __init__(self, year=2020, spatial_resolution=100, **kwargs): |
| 17 | + def __init__(self, agesex_classes=[], year=2020, spatial_resolution=100, **kwargs): |
16 | 18 | super().__init__(**kwargs)
|
| 19 | + self.agesex_classes = agesex_classes |
17 | 20 | self.year = year
|
18 | 21 | self.spatial_resolution = spatial_resolution
|
19 | 22 |
|
20 | 23 | def get_data(self, bbox):
|
21 |
| - # load population |
22 |
| - dataset = ee.ImageCollection('WorldPop/GP/100m/pop') |
23 |
| - world_pop = ee.ImageCollection(dataset |
24 |
| - .filterBounds(ee.Geometry.BBox(*bbox)) |
25 |
| - .filter(ee.Filter.inList('year', [self.year])) |
26 |
| - .select('population') |
27 |
| - .mean() |
28 |
| - ) |
29 |
| - |
30 |
| - data = get_image_collection(world_pop, bbox, self.spatial_resolution, "world pop") |
31 |
| - return data.population |
| 24 | + if not self.agesex_classes: |
| 25 | + # total population |
| 26 | + dataset = ee.ImageCollection('WorldPop/GP/100m/pop') |
| 27 | + world_pop = ee.ImageCollection(dataset |
| 28 | + .filterBounds(ee.Geometry.BBox(*bbox)) |
| 29 | + .filter(ee.Filter.inList('year', [self.year])) |
| 30 | + .select('population') |
| 31 | + .mean() |
| 32 | + ) |
| 33 | + |
| 34 | + data = get_image_collection(world_pop, bbox, self.spatial_resolution, "world pop").population |
| 35 | + |
| 36 | + else: |
| 37 | + # sum population for selected age-sex groups |
| 38 | + dataset = ee.ImageCollection('WorldPop/GP/100m/pop_age_sex') |
| 39 | + world_pop = dataset.filterBounds(ee.Geometry.BBox(*bbox))\ |
| 40 | + .filter(ee.Filter.inList('year', [self.year]))\ |
| 41 | + .select(self.agesex_classes)\ |
| 42 | + .mean() |
| 43 | + world_pop = ee.ImageCollection(world_pop.reduce(ee.Reducer.sum()).rename('sum_age_sex_group')) |
| 44 | + data = get_image_collection(world_pop, bbox, self.spatial_resolution, "world pop age sex").sum_age_sex_group |
| 45 | + |
| 46 | + return data |
0 commit comments