@@ -172,18 +172,19 @@ def country_iso_geom(countries, shp_file):
172172 for info_idx , info in enumerate (list_records ):
173173 countries_shp [info .attributes ['ADMIN' ].title ()] = info_idx
174174
175- admin1_rec = shapereader .natural_earth (resolution = '10m' ,
176- category = 'cultural' ,
177- name = 'admin_1_states_provinces' )
178- admin1_rec = shapereader .Reader (admin1_rec )
179- admin1_rec = list (admin1_rec .records ())
180-
181175 num_codes = [iso3 for iso3 in wb .country_codes if len (iso3 ) == 3 ]
182176
183177 cntry_info = dict ()
184178 cntry_admin1 = dict ()
185179 if isinstance (countries , list ):
186180 countries = {cntry : [] for cntry in countries }
181+ admin1_rec = list ()
182+ else :
183+ admin1_rec = shapereader .natural_earth (resolution = '10m' ,
184+ category = 'cultural' ,
185+ name = 'admin_1_states_provinces' )
186+ admin1_rec = shapereader .Reader (admin1_rec )
187+ admin1_rec = list (admin1_rec .records ())
187188
188189 for country_name , prov_list in countries .items ():
189190 country_idx = countries_shp .get (country_name .title ())
@@ -336,13 +337,15 @@ def _fill_admin1_geom(iso3, admin1_rec, prov_list):
336337 list
337338 """
338339 prov_geom = list ()
340+
339341 for prov in prov_list :
340342 found = False
341343 for rec in admin1_rec :
342- if prov in rec .attributes ['name' ] and \
344+ if prov == rec .attributes ['name' ] and \
343345 rec .attributes ['adm0_a3' ] == iso3 :
344346 found = True
345347 prov_geom .append (rec .geometry )
348+ break
346349 if not found :
347350 options = [rec .attributes ['name' ] for rec in admin1_rec \
348351 if rec .attributes ['adm0_a3' ] == iso3 ]
@@ -372,14 +375,20 @@ def _get_income_group(cntry_info, ref_year, shp_file):
372375 attribute for every country.
373376 """
374377 # check if file with income groups exists in SYSTEM_DIR, download if not
375- fn_ig = os .path .join (os .path .abspath (SYSTEM_DIR ), 'OGHIST.xls' )
376- if not glob .glob (fn_ig ):
377- file_down = download_file (WORLD_BANK_INC_GRP )
378- shutil .move (file_down , fn_ig )
379- dfr_wb = pd .read_excel (fn_ig , 'Country Analytical History' , skiprows = 5 )
380- dfr_wb = dfr_wb .drop (dfr_wb .index [0 :5 ]).set_index ('Unnamed: 0' )
381- dfr_wb = dfr_wb .replace (INCOME_GRP_WB_TABLE .keys (),
382- INCOME_GRP_WB_TABLE .values ())
378+ try :
379+ fn_ig = os .path .join (os .path .abspath (SYSTEM_DIR ), 'OGHIST.xls' )
380+ if not glob .glob (fn_ig ):
381+ file_down = download_file (WORLD_BANK_INC_GRP )
382+ shutil .move (file_down , fn_ig )
383+ dfr_wb = pd .read_excel (fn_ig , 'Country Analytical History' , skiprows = 5 )
384+ dfr_wb = dfr_wb .drop (dfr_wb .index [0 :5 ]).set_index ('Unnamed: 0' )
385+ dfr_wb = dfr_wb .replace (INCOME_GRP_WB_TABLE .keys (),
386+ INCOME_GRP_WB_TABLE .values ())
387+ except (IOError , requests .exceptions .ConnectionError ):
388+ LOGGER .warning ('Internet connection failed while downloading ' +
389+ 'historical income groups.' )
390+ dfr_wb = pd .DataFrame ()
391+
383392 for cntry_iso , cntry_val in cntry_info .items ():
384393 try :
385394 cntry_dfr = dfr_wb .loc [cntry_iso ]
@@ -430,7 +439,11 @@ def _get_gdp(cntry_info, ref_year, shp_file):
430439 LOGGER .info ("GDP {} {:d}: {:.3e}." .format (cntry_iso , \
431440 int (close_gdp .iloc [0 ].name [1 ]), close_gdp_val ))
432441
433- except (ValueError , IndexError ):
442+ except (ValueError , IndexError , requests .exceptions .ConnectionError ) \
443+ as err :
444+ if isinstance (err , requests .exceptions .ConnectionError ):
445+ LOGGER .warning ('Internet connection failed while ' +
446+ 'retrieving GDPs.' )
434447 list_records = list (shp_file .records ())
435448 for info in list_records :
436449 if info .attributes ['ADM0_A3' ] == cntry_iso :
@@ -443,10 +456,6 @@ def _get_gdp(cntry_info, ref_year, shp_file):
443456 LOGGER .info ("GDP {} {:d}: {:.3e}." .format (cntry_iso , \
444457 close_gdp_year , close_gdp_val ))
445458
446- except requests .exceptions .ConnectionError :
447- LOGGER .error ('Connection error: check your internet connection.' )
448- raise ConnectionError
449-
450459 cntry_val .append (close_gdp_val )
451460
452461def _process_land (exp , geom , nightlight , coord_nl , res_fact , res_km ):
0 commit comments