@@ -556,7 +556,7 @@ def load_all_fewsnet_geographies(context: OpExecutionContext):
556556
557557 try :
558558 response = requests .get (
559- f"https://fdw.fews.net/api/feature/?format=geojson&unit_type=livelihood_zone&fnid={ fnid } &fields=with_population"
559+ f"https://fdw.fews.net/api/feature/?format=geojson&unit_type=livelihood_zone&fnid={ fnid } &fields=with_population&demography_year= { livelihood_zone_baseline . reference_year_end_date . year } "
560560 )
561561 response .raise_for_status ()
562562
@@ -583,7 +583,31 @@ def load_all_fewsnet_geographies(context: OpExecutionContext):
583583 if population_estimate is not None :
584584 try :
585585 livelihood_zone_baseline .population_estimate = int (population_estimate )
586- livelihood_zone_baseline .population_source = "Landscan 2020"
586+ # Population sources are in the geographicunit metadata url
587+ unit_id = geojson_data ["features" ][0 ].get ("id" )
588+ metadata_url = f"https://fdw.fews.net/api/geographicunit/metadata/?id={ unit_id } &format=json&fields=with_population"
589+ context .log .info (f"Fetching population metadata from { metadata_url } " )
590+ metadata_response = requests .get (metadata_url )
591+ metadata_response .raise_for_status ()
592+
593+ metadata_json = metadata_response .json ()
594+
595+ # Extract population source from metadata
596+ population_source = None
597+ if "metadata" in metadata_json and "Population" in metadata_json ["metadata" ]:
598+ population_list = metadata_json ["metadata" ]["Population" ]
599+ for pop_item in population_list :
600+ if pop_item .get ("Name" ) == "Population distribution" :
601+ population_source = pop_item .get ("Description" )
602+ context .log .info (f"Found population source: { population_source } " )
603+ break
604+
605+ if population_source :
606+ livelihood_zone_baseline .population_source = population_source
607+ else :
608+ context .log .warning (
609+ f"No population source found in metadata for { livelihood_zone_baseline } (fnid={ fnid } )"
610+ )
587611 except (ValueError , TypeError ) as e :
588612 context .log .warning (f"Invalid population data for { livelihood_zone_baseline } (fnid={ fnid } ): { e } " )
589613
0 commit comments