def get_city(address): """get the nearest city to an address to find the weather forecast (forecast only has cities)""" geolocator = Nominatim(user_agent="specify_your_app_name_here") while True: try: location = geolocator.geocode(address) break except Exception: None city = citipy.nearest_city(location.latitude, location.longitude) return [city.city_name.title(), city.country_code.title()]
def test_Quimper(self): city = citipy.nearest_city(48.0, -4.1) self.assertEqual('quimper', city.city_name)
def test_tainan(self): city = citipy.nearest_city(22.99, 120.21) self.assertEqual('tainan', city.city_name)
def test_Wellington(self): city = citipy.nearest_city(-41.28, 174.77) self.assertEqual('wellington', city.city_name)
def test_NewYork(self): city = citipy.nearest_city(40.78, -73.96) self.assertEqual('edgewater', city.city_name)
def test_Yako(self): city = citipy.nearest_city(12.95, -2.26) self.assertEqual('yako', city.city_name)
def use_this(): import citipy city = citipy.nearest_city(22.99, 120.21) print(city, city.city_name, city.country_code)
final_city = [] final_country = [] for i in range(600): # Generate random lat/longs using random functions new_lat = random.randint(-90, 90) + random.random() new_long = random.randint(-180, 180) + random.random() init_lat.append(new_lat) init_long.append(new_long) # Use citypy function of nearest_city to identify the nearest cities associated to the random lat/longs # Append the final_city and final_country lists new_city = city.nearest_city(new_lat, new_long) final_city.append(new_city.city_name) final_country.append(new_city.country_code) #print(init_lat) #print(init_long) #print(final_city) #print(final_country) # Create a new DataFrame to store the needed data of city and country world_cities_sample_df = pd.DataFrame(final_city) world_cities_sample_df = world_cities_sample_df.rename(columns={0: 'City'}) world_cities_sample_df["Country"] = final_country print(world_cities_sample_df.head(10))