def update_geo(self, max_tries=3): ''' Updates the new missing geo_locations ''' country = 'Peru' # Loads cases cases = self.build_cases() # loads geo df_geo = geo.get_geo(country) # Finds missing locations merged = cases.merge(df_geo, on=['geo_id'], how='left') missing = merged.loc[ merged.lon.isna(), ['geo_id', 'departement', 'province']].drop_duplicates() #Updates if missing.shape[0] == 0: print(self.ident + 'Found zero new places.') else: print(self.ident + 'Found {} new places. Updating'.format(missing.shape[0])) print('') for ind, row in missing.iterrows(): print(self.ident + ' Finding: {}, {}, {} ({})'.format( country, row.departement, row.province, row.geo_id)) tries = 0 while tries < max_tries: try: lon, lat = geo.geolocate(country, row.departement, row.province) print(' Done! Location: {},{}'.format(lat, lon)) geo.save_new_location(row.geo_id, country, row.departement, row.province, lon, lat) break except: time.sleep(15) tries += 1 if tries == max_tries: print( self.ident + ' Could not find location. Please enter it manually' ) print()
def update_geo(self, max_tries=3): ''' Updates the new missing geo_locations ''' country = 'Mexico' geo_file_location = os.path.join(self.raw_folder, 'geo', self.get('geo_file_name')) # loads geo df_geo = geo.get_geo(country) df_geo["geo_id"] = df_geo["geo_id"].astype(int) # Loads cases cases = self.build_cases() cases["geo_id"] = cases["geo_id"].astype(int) # Finds missing locations merged = cases.merge(df_geo, on=['geo_id'], how='left') missing = merged.loc[merged.lon.isna(), ['geo_id']].drop_duplicates() # Reads the CSV df_geo_names = pd.read_csv(geo_file_location) df_geo_names.set_index(['geo_id']) df_geo_names = df_geo_names.rename(columns={'name': 'geo_name'}) # merges missing = missing.merge(df_geo_names, on='geo_id') #Updates if missing.shape[0] == 0: print(self.ident + 'Found zero new places.') else: print(self.ident + 'Found {} new places. Updating'.format(missing.shape[0])) print('') for ind, row in missing.iterrows(): print(self.ident + ' Finding: {}, {}, {} ({})'.format( country, row.state, row.geo_name, row.geo_id)) tries = 0 while tries < max_tries: try: lon, lat = geo.geolocate(country, row.state, row.geo_name) print(' Done! Location: {},{}'.format(lat, lon)) geo.save_new_location(row.geo_id, country, row.state, row.geo_name, lon, lat) break except: time.sleep(15) tries += 1 if tries == max_tries: print( self.ident + ' Could not find location. Please enter it manually' ) print()
def update_geo(self, max_tries=3): ''' Updates the new missing geo_locations ''' country = 'Germany' geo_file_location = os.path.join(self.raw_folder, 'geo', self.get('geo_file_name')) # loads geo df_geo = geo.get_geo(country) # Loads cases cases = self.build_cases() # Finds missing locations merged = cases.merge(df_geo, on=['geo_id'], how='left') missing = merged.loc[merged.lon.isna(), ['geo_id']].drop_duplicates() # Reads the JSON with open(geo_file_location) as json_file: geo_names = json.load(json_file) df_geo_names = pd.DataFrame.from_dict( geo_names, orient='index').reset_index().drop( ['note'], axis=1).rename(columns={ 'index': 'geo_id', 'name': 'geo_name' }) df_geo_names.geo_name = df_geo_names.geo_name.apply(clean_name) # merges missing = missing.merge(df_geo_names, on='geo_id') #Updates if missing.shape[0] == 0: print(self.ident + 'Found zero new places.') else: print(self.ident + 'Found {} new places. Updating'.format(missing.shape[0])) print('') for ind, row in missing.iterrows(): print(self.ident + ' Finding: {}, {}, {} ({})'.format( country, row.state, row.geo_name, row.geo_id)) tries = 0 while tries < max_tries: try: lon, lat = geo.geolocate(country, row.state, row.geo_name) print(' Done! Location: {},{}'.format(lat, lon)) geo.save_new_location(row.geo_id, country, row.state, row.geo_name, lon, lat) break except: time.sleep(15) tries += 1 if tries == max_tries: print( self.ident + ' Could not find location. Please enter it manually' ) print()