1616from findthatpostcode import db
1717from findthatpostcode .commands .codes import AREA_INDEX
1818
19+ IMD2025_URL = "https://assets.publishing.service.gov.uk/media/68ff5daabcb10f6bf9bef911/File_7_IoD2025_All_Ranks_Scores_Deciles_Population_Denominators.csv"
1920IMD2019_URL = "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/845345/File_7_-_All_IoD2019_Scores__Ranks__Deciles_and_Population_Denominators_3.csv"
2021IMD2015_URL = "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/467774/File_7_ID_2015_All_ranks__deciles_and_scores_for_the_Indices_of_Deprivation__and_population_denominators.csv"
2122
@@ -35,6 +36,10 @@ def parse_field(k, v):
3536IMD_FIELDS = {
3637 "LSOA code (2011)" : "lsoa_code" ,
3738 "LSOA name (2011)" : "lsoa_name" ,
39+ "LSOA code (2021)" : "lsoa_code" ,
40+ "LSOA name (2021)" : "lsoa_name" ,
41+ "Local Authority District code (2024)" : "la_code" ,
42+ "Local Authority District name (2024)" : "la_name" ,
3843 "Local Authority District code (2019)" : "la_code" ,
3944 "Local Authority District name (2019)" : "la_name" ,
4045 "Local Authority District code (2013)" : "la_code" ,
@@ -162,9 +167,63 @@ def parse_field(k, v):
162167 "Working age population 18-59/64: for use with Employment Deprivation Domain "
163168 "(excluding prisoners)"
164169 ): "population_workingage" ,
170+ "Total population: mid 2022" : "population_total" ,
171+ "Dependent Children aged 0-15: mid 2022" : "population_0_15" ,
172+ "Older population aged 60 and over: mid 2022" : "population_60_plus" ,
173+ "Working age population 18-66 (for use with Employment Deprivation Domain): mid 2022" : "population_workingage" ,
165174}
166175
167176
177+ @click .command ("imd2025" )
178+ @click .option ("--es-index" , default = AREA_INDEX )
179+ @click .option ("--url" , default = IMD2025_URL )
180+ @with_appcontext
181+ def import_imd2025 (url = IMD2025_URL , es_index = AREA_INDEX ):
182+ if current_app .config ["DEBUG" ]:
183+ requests_cache .install_cache ()
184+
185+ es = db .get_db ()
186+
187+ r = requests .get (url , stream = True )
188+
189+ reader = csv .DictReader (codecs .iterdecode (r .iter_lines (), "utf-8-sig" ))
190+ area_updates = []
191+ for k , area in tqdm .tqdm (enumerate (reader )):
192+ area = {
193+ IMD_FIELDS .get (k .strip (), k .strip ()): parse_field (
194+ IMD_FIELDS .get (k .strip (), k .strip ()), v
195+ )
196+ for k , v in area .items ()
197+ }
198+ area_update = {
199+ "_index" : es_index ,
200+ "_type" : "_doc" ,
201+ "_op_type" : "update" ,
202+ "_id" : area ["lsoa_code" ],
203+ "doc" : {
204+ "stats" : {
205+ "imd2025" : {k : v for k , v in area .items () if k .startswith ("imd_" )},
206+ "idaci2025" : {
207+ k : v for k , v in area .items () if k .startswith ("idaci_" )
208+ },
209+ "idaopi2025" : {
210+ k : v for k , v in area .items () if k .startswith ("idaopi_" )
211+ },
212+ "population2022" : {
213+ k : v for k , v in area .items () if k .startswith ("population_" )
214+ },
215+ }
216+ },
217+ }
218+ area_updates .append (area_update )
219+
220+ print ("[imd2025] Processed %s areas" % len (area_updates ))
221+ print ("[elasticsearch] %s areas to save" % len (area_updates ))
222+ results = bulk (es , area_updates )
223+ print ("[elasticsearch] saved %s areas to %s index" % (results [0 ], es_index ))
224+ print ("[elasticsearch] %s errors reported" % len (results [1 ]))
225+
226+
168227@click .command ("imd2019" )
169228@click .option ("--es-index" , default = AREA_INDEX )
170229@click .option ("--url" , default = IMD2019_URL )
0 commit comments