def weatherUpdate(): #twitter consumer_key = "dHSopVEhYTGEnuFSO9ZOfMGp1" consumer_secret = "FAKeb5sLWElP9dksT3kjJsKO0vHPtixGW4O1oHrqrrj40r93cN" access_token = "909114160713289728-Akb2JL4JrmXZLQqGwp1loZQAPhnvNER" access_token_secret = "PInMGr6YidbTR4acUoKDtslQt7ksubqsXFWQef5nxvO8L" #weather api_key = "577f692bda463ed51e5e7f3632a90604" url = "http://api.openweathermap.org/data/2.5/weather?" units = "imperial" #authorization auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth, parser=tweepy.parsers.JSONParser()) lat = np.random.uniform(low=-90.0, high=90.0) lng = np.random.uniform(low=-180.0, high=180.0) try: city = citipy.nearest_city(lat, lng) cityname = city.city_name countryname = city.country_code query_url = url + "appid=" + api_key + "&units=" + units + "&q=" + cityname + "," + countryname response = req.get(query_url).json() temperature = response["main"]["temp"] #time = datetime.now().strftime("%I:%M %p") api.update_status("The current temperature in %s, %s is %sF" % (cityname, countryname, temperature)) print("Successful tweet!") except: print("Skipping this one!")
def WeatherTweet(): coordinates = [(rand.randint(-90, 90), rand.randint(-180, 180))] for coordinate_pair in coordinates: lat, lon = coordinate_pair nearest_city_obj = (citipy.nearest_city(lat, lon)) name = nearest_city_obj.city_name # Construct a Query URL url = "http://api.openweathermap.org/data/2.5/weather?" city = name units = "imperial" query_url = url + "appid=" + api_key + "&q=" + city + "&units=" + units # Perform the API weather_response = req.get(query_url) weather_json = weather_response.json() print(weather_json) # Twitter credentials auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth, parser=tweepy.parsers.JSONParser()) # Tweet the weather api.update_status(city + " Weather as of %s: %s F" % (datetime.datetime.now().strftime("%I:%M %p"), weather_json["main"]["temp"])) # Print success message print("Tweeted successfully, sir!")
def generte_city_datarame(): # Range of latitudes and longitudes lat_range = (-90, 90) lng_range = (-180, 180) # List for holding lat_lngs and cities lat_lngs = [] cities = [] weather_df = pd.DataFrame([]) # Create a set of random lat and lng combinations lats = np.random.uniform(low=-90.000, high=90.000, size=1500) lngs = np.random.uniform(low=-180.000, high=180.000, size=1500) lat_lngs = zip(lats, lngs) # Identify nearest city for each lat, lng combination for lat_lng in lat_lngs: city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name # If the city is unique, then add it to a our cities list if city not in cities: cities.append(city) weather_df = weather_df.append(pd.DataFrame( { "City": city, "latitude": lat_lng[0], "longitude": lat_lng[1] }, index=[0]), ignore_index=True) return weather_df
def get_weather_data(coords, time_between=1): """ Queries openweather API for data. Args: coords: A Pandas Dataframe with rows containing 'latitude' and 'longitude' columns. time_between: An integer specifying the sleep time in seconds between each API ping. Defaults to the OpenWeatherAPI's recommended limit of 1 request per second. Returns: A list of nested dicts (loaded JSON results). """ results = [] for ind, row in coords.iterrows(): lat, lon = row['latitude'], row['longitude'] query = f"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&units=imperial&appid={API_KEY}" print(query) clean_url = query.rpartition("&")[0] city = citipy.nearest_city(lat, lon) logger.info(f"Call {ind}: {city.city_name} {clean_url}") result = requests.get(query) results.append(result.json()) time.sleep(time_between) return results
def makeCitiesList(country_coords): # Range of latitudes and longitudes for the set confines lat_range = (country_coords['lat'][0], country_coords['lat'][1]) lng_range = (country_coords['lng'][0], country_coords['lng'][1]) # List for holding lat_lngs and cities lat_lngs = [] cities = [] # Create a set of random lat and lng combinations lats = np.random.uniform(low=lat_range[0], high=lat_range[1], size=1500) lngs = np.random.uniform(low=lng_range[0], high=lng_range[1], size=1500) lat_lngs = zip(lats, lngs) # Identify nearest city for each lat, lng combination for lat_lng in lat_lngs: city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name # If the city is unique, then add it to a our cities list if city not in cities: cities.append(city) # Print the city count to confirm sufficient count print(f'Cities in list : {len(cities)}') return (cities)
def get_nearest_city(coordinate): # takes a coordinate pair and uses citipy to return the name of the nearest city lat = coordinate[0] lng = coordinate[1] city = citipy.nearest_city(lat, lng).city_name return city
def generate_cities_list(num): city_list = [] for i in range(num): random_latitude = random.randrange(-90, 90) random_longitude = random.randrange(-180, 180) city = citipy.nearest_city(random_latitude, random_longitude).city_name city_list.append(city) return city_list
def citipyDistrict(df): latitude = df['latitude'].tolist() longitude = df['longitude'].tolist() district = [] for i in range(len(latitude)): city = citipy.nearest_city(latitude[i], longitude[i]) district.append(city.city_name) df['district'] = district pprint(df) return df
def get_city_coord(coordinates): cities = [] city_coord = [] # Identify the nearest city for each latitude and longitude combination. for coordinate in coordinates: city = citipy.nearest_city(coordinate[0], coordinate[1]).city_name # If the city is unique, then we will add it to the cities list. if city not in cities: cities.append(city) city_coord.append([city, coordinate[0], coordinate[1]]) # Print the city count to confirm sufficient count. city_count = len(cities) print(f"valid city count is {city_count}") return city_coord
def meteorites_data_retriever(): #Fecth the meteorite landings data meteorite_landings_data = fetch_meteorite_landings_full_dataset() meteorite_data_df = meteorite_landings_data.copy() #Delete the columns meteorite_data_df = meteorite_data_df.drop( columns=":@computed_region_cbhk_fwbd") meteorite_data_df = meteorite_data_df.drop( columns=":@computed_region_nnqa_25f4") #Use np.nan instaed of . "" as empty meteorite_data_df["country_code"] = np.nan meteorite_data_df["country_name"] = np.nan meteorite_data_df["city_name"] = np.nan meteorite_data_df["continent_name"] = np.nan #find the country name from country code and update to data Frame country_mapping_df = pd.read_csv( "./Resources/country_continent_mapping.csv", encoding='latin1') # Loop through the meteorite_data_df and find city name and coutry code for a lat/long start_time = time.time() for row in meteorite_data_df.itertuples(): try: latitude = float(row.reclat) longitude = float(row.reclong) city = citipy.nearest_city(latitude, longitude) city_name = city.city_name.capitalize() country_code = city.country_code.upper() meteorite_data_df["city_name"][row.Index] = city_name meteorite_data_df["country_code"][row.Index] = country_code except IndexError: print( f"Oops! That was no valid number. latitude:{latitude}, longitude:{longitude},city_name:{city_name} ,country_code:{country_code} , " ) print("Process completed : It took --- %s minutes ---" % ((time.time() - start_time) / 60)) meteorite_data_df.to_csv('./Resources/meteorites_landing_raw_data.csv') return meteorite_landings_data
def Load_Random_Cities_Locations(lattitude_low, latitude_high): """ Used to load random cities by generating samples of latitue and logitude values and finding nearby cities. Uses the citypy module Parameters ---------- lattitude_low : TYPE: int DESCRIPTION. Lowest latitude value to use in generating sample latitude_high : TYPE: int DESCRIPTION. Highest latitude value to use in generating sample Returns ------- TYPE: DataFrame DESCRIPTION. random cities within bounded latitude range """ # List for holding lat_lngs and cities lat_lngs = [] cities = [] # Create a set of random lat and lng combinations lats = np.random.uniform(low=lattitude_low, high=latitude_high, size=2000) lngs = np.random.uniform(low=-180.000, high=180.000, size=2000) lat_lngs = zip(lats, lngs) # Identify nearest city for each lat, lng combination for lat_lng in lat_lngs: city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name # If the city is unique, then add it to a our cities list if city not in cities: cities.append(city) # Print the city count to confirm sufficient count print(len(cities)) return pd.DataFrame(cities)
cloudiness = [] wind_speed = [] cities = [] countries = [] units = 'imperial' ''' generates lat and lon coordinates to two decimals and adds them to their respective lists ''' for number in range(num_cities): lat.append(round(random.uniform(-90, 90), 2)) lon.append(round(random.uniform(-180, 180), 2)) ''' ''' for number in range(num_cities): city = citipy.nearest_city(lat[number], lon[number]) city_name = city.city_name country_code = city.country_code print(f'{number+1}. {city_name.upper()}, {country_code.upper()}') request_url = url + f'q={city_name},{country_code}&units={units}&appid={token}' request = requests.get(request_url).json() try: temp.append(request['main']['temp']) humidity.append(request["main"]['humidity']) cloudiness.append(request['clouds']['all']) wind_speed.append(request['wind']['speed']) cities.append(city_name) countries.append(country_code) except KeyError: print('Error') temp.append('')
# create dupe checking set cityDupeChecker = set() # create counter i = 0 # create a list of 1500 possible cities (so even if a query fails still have good sample) while len(cityDupeChecker) < 1500: # set random lat and long latitude = rd.uniform(-90.0, 90.0) longitude = rd.uniform(-180.0, 180.0) # get city city = citipy.nearest_city(latitude, longitude).city_name country = citipy.nearest_city(latitude, longitude).country_code city_country_pair = f"{city}_{country}" if city_country_pair not in cityDupeChecker: cityDupeChecker.add(city_country_pair) # try to pull in a random value and add to dupe checker city_list.append([city, country]) # ----------------------------------------------------------------------------------- # Step 4: Pull city data from openweatherapi # ----------------------------------------------------------------------------------- for i in range(len(city_list)): # get current city and country
import random #Set up lists to collect data cities = [] temps = [] humid = [] wind = [] clouds = [] frames = [] #Randomly generate lat/lng and collect nearest city for i in range(5): lat = numpy.random.uniform(low=-90.00000, high=90.000000, size = 60) lng = numpy.random.uniform(low=-120.00000, high=120.000000, size = 60) for j in range(60): query = citipy.nearest_city(lat[j], lng[j]) #API request url = "api.openweathermap.org/data/2.5/weather?q=" + query + "&APPID=d77529b69cd743d50d241b93b6553ec5" response = req.get(url) print("Retrieving weather data for " + query) print(url) response_json = response.json() #Collect data from JSON temperature.append(response_json["main"]["temp"]) humidity.append(response_json["main"]["humidity"]) wind.append(response_json["wind"]["speed"]) clouds.append(response_json["clouds"]["all"]) cities.append(query) df = pd.DataFrame(data = {"Cities": cities, "Latitude": lat[j], "Temperature": temps, "Humidity": humid, "Wind Speed": wind, "Cloud Cover": clouds) frames.append(df)
lon_samples = [y + dec_lon for dec_lon in lons] cities_df = cities_df.append( pd.DataFrame.from_dict({ "Latitude": lat_samples, "Longitude": lon_samples })) cities_df = cities_df.reset_index(drop=True) cities_df.shape # In[116]: # For the selected coordinates, use citipy to associate them with nearest city. cities_df["Closest City name"] = "" cities_df["Closest Country code"] = "" for index, row in cities_df.iterrows(): city = citipy.nearest_city(row["Latitude"], row["Longitude"]) cities_df.set_value(index, "Closest City name", city.city_name) cities_df.set_value(index, "Closest Country code", city.country_code) # Drop random lat and lon samples from dataframe that don't match coordinates clean_cities_df = cities_df.drop(['Latitude', 'Longitude'], axis=1) clean_cities_df # Drop duplicate cities clean_cities_df = clean_cities_df.drop_duplicates() # Pick a sample of 500 cities for analysis select_cities = clean_cities_df.sample(500) select_cities = select_cities.reset_index(drop=True) # # Hello? OpenWeatherMap? You there?
lat_lngs = zip(lats, lngs) lat_lngs # %% # Add the latitudes and longitudes to a list. coordinates = list(lat_lngs) # %% from citipy import citipy # %% # Create a list for holding the cities. cities = [] # Identify the nearest city for each latitude and longitude combination. for coordinate in coordinates: city = citipy.nearest_city(coordinate[0], coordinate[1]).city_name # If the city is unique, then we will add it to the cities list. if city not in cities: cities.append(city) # Print the city count to confirm sufficient count. len(cities) # %% # Import the requests library. import requests from datetime import datetime # %% # Import the API key. from config import weather_api_key
units = 'imperial' query_url = url + "appid=" + api_key + "&units=" + units + "&q=" count = 0 lats = [] lngs = [] while count < 1200: lats.append(random.uniform(-90, 90)) lngs.append(random.uniform(-180, 180)) count = count + 1 cities = ['london'] countries = ['GB'] for lat, lng in zip(lats, lngs): cityid = citipy.nearest_city(lat, lng) city = cityid.city_name country = cityid.country_code cities.append(city) countries.append(country) temp_data = [] lon_data = [] temp_data = [] lat_data = [] humidity_data = [] cloud_data = [] wind_data = [] for city in cities: weather_json = req.get(query_url + city).json()
# define the range of latitudes and longitude and samplesize lat_range = (-90, 90) lng_range = (-180, 180) samplesize = 1500 # Create the amount of cities based on the maxsize for each round cities = [] i = 0 maxsize = 1000 while i < maxsize: lat_lngs, lats, lngs = randomcitycoor(lat_range, lng_range, samplesize, seed) for lat_lng in lat_lngs: citipy_obj = citipy.nearest_city(lat_lng[0], lat_lng[1]) cityname = citipy_obj.city_name countryname = citipy_obj.country_code citypluscountry = cityname + ', ' + countryname # if citypluscountry not in cities: cities.append(citypluscountry) i = len(cities) seed = seed + 1 print(i) # trim down to the target size len(cities) cities = list(np.unique(cities)) len(cities) cities
import requests as req from scipy.stats.stats import pearsonr import time API = "INSERT API KEY" owm = pyowm.OWM(API) url = "http://api.openweathermap.org/data/2.5/weather?q=" units = "&units=imperial" #get cities from coordinates cities = [] while len(cities) <= 1500: latitude = random.randint(-90.00, 90.00) longitude = random.randint(-180.00, 180.00) city = cp.nearest_city(latitude, longitude) if city not in cities: cities.append({ 'city code': city, 'city': city.city_name, 'country': city.country_code, 'latitude': latitude, 'longitude': longitude, }) else: continue #get weather info for each city weatherInfo = {} for x in cities: city = x['city']
def get_city_weather_df(n_rows=10, force_new=False): cache_data_file_path = '.cache.csv' coords_df = None # see if there is a cache file to use try: coords_df = pd.read_csv(cache_data_file_path, index_col=0) print("Data loaded from cache") except OSError as err: print("No cache file found") # If no cache file was found or a new one is forced # generate the data if (coords_df is None or force_new): # Generate latitudes(-90 to 90) and longitudes(-180 to 180) # using random number generator and multiply by 100 coords_df = pd.DataFrame({ "Lng": np.random.uniform(-180, 180, n_rows), # -180 | 180 "Lat": np.random.uniform(-90, 90, n_rows) # -90 | 90 }) # Now lets get the cities name and countries city_names = [] country_codes = [] temperature = [] humidity = [] cloudiness = [] wind_speed = [] date = [] for i in range(0, len(coords_df['Lat'])): lat = coords_df.iloc[i]['Lat'] lon = coords_df.iloc[i]["Lng"] # get the city data city = citipy.nearest_city(lat, lon) city_names.append(city.city_name) country_codes.append(city.country_code) # get the weather data weather = get_weather_by_coords(lat, lon) temperature.append(weather["main"]["temp_max"]) humidity.append(weather["main"]["humidity"]) cloudiness.append(weather["clouds"]["all"]) wind_speed.append(weather["wind"]["speed"]) date.append(weather["dt"]) # add values to the DataFrame coords_df['City']=city_names coords_df['Country']=country_codes coords_df['Max Temp']=temperature coords_df['Humidity']=humidity coords_df['Cloudiness']=cloudiness coords_df['Wind Speed']=wind_speed coords_df['Date']=date # cache the file for later... coords_df.to_csv(cache_data_file_path) # lets peek at our data frame return coords_df
api_key = '25bc90a1196e6f153eece0bc0b0fc9eb' units = 'Imperial' url = 'http://api.openweathermap.org/data/2.5/weather' # In[97]: #Cities Setup - random integers for latitude/longitude cities_dict = {'city':[],'country':[]} # In[98]: #Build City Dictionary for x in range(0,1500): x = cp.nearest_city(random.randint(-90,90), random.randint(-180,180)) if x.city_name not in cities_dict['city']: cities_dict['city'].append(x.city_name) cities_dict['country'].append(x.country_code) # In[99]: #Check that code is working for city in cities_dict['city']: print(city) len(cities_dict['city']) # In[100]:
locs = dict(zip(lats,lngs)) #print(len(lats), len(lngs)) type(locs) # In[82]: #Uses Citipy to identify the nearest city with country code locs_conv = [] weatherdata_df = pd.DataFrame(columns = ['City','Temp','Humidity','Clouds','Wind_Speed']) response_data = [] for key,value in locs.items(): city = citipy.nearest_city(key,value) locs_conv.append(city) #locs_conv city_counter = 0 print('Beginning Data Retrieval') print('-----------------------------') for location in locs_conv: print('Processing record '+str(city_counter)+' of '+ str(len(locs_conv))+"|"+location.city_name) city_counter +=1 try: params = { 'appid': api_key, 'units': units, 'q': location.city_name+","+location.country_code
import requests as req import json import pandas as pd from citipy import citipy import matplotlib.pyplot as plt lat = [] n = [0, 1, 2, 3, 4, 5] for row in n: # lat.append(row) # print("lat:" + str(row)) city = citipy.nearest_city(row, 101) lat.append(city.city_name) # # print(lat) url = 'http://api.openweathermap.org/data/2.5/weather/' api_key = "00e1e179e7275d087f40c8b9313ae8a8" params = {'appid': api_key, 'q': '', 'units': 'metric'} weather_data = [] lat_data = [] temp_data = [] # cities = ['Chennai', 'Berkeley', 'Chicago'] for city in lat: params['q'] = city response = req.get(url, params=params).json() weather_data.append(response) print(weather_data) for data in weather_data: lat_data.append(data['coord']['lat'])
def get_nearest_city(coor): city = citipy.nearest_city(coor['Latitude'], coor['Longitude']) return city.city_name + ',' + city.country_code
weatherList = [] Lat = [] temp = [] humid = [] cloud=[] windSpeed=[] #find cities with open('weatherReport.csv', 'w') as csvfile: dataWriter = csv.writer(csvfile, delimiter=',') dataWriter.writerow(['City','Country','Latitude','Longitude','Temperature','Humidity','Cloudiness','WindSpeed','URL']) cityCounter = 0 for lat in LatList: for lon in LonList: city = citipy.nearest_city(lat,lon) City=city.city_name Country=city.country_code CityCountrySTR= "{0},{1}".format(City,Country) if CityCountrySTR not in CityCountry: CityCountry.append(CityCountrySTR) URL="http://api.openweathermap.org/data/2.5/weather?q={0}&appid=4cc2a844092656dedc2d4db74f22bdb6".format(CityCountrySTR) # print(CityCountrySTR) r = requests.get(URL) data = r.json() if data['cod']==200: weatherList.append(data) Lat.append(data['coord']['lat']) temp.append(data['main']['temp']*(9/5) - 459.67) #Kelvin to Fahrenheit humid.append(data['main']['humidity']) cloud.append(data['clouds']['all'])
# List for holding lat_lngs and cities lat_lngs = [] new_lats = [] new_longs = [] cities = [] countries = [] temp = [] # Create a set of random lat and lng combinations lats = np.random.uniform(low=-90.000, high=90.000, size=1500) lngs = np.random.uniform(low=-180.000, high=180.000, size=1500) lat_lngs = zip(lats, lngs) # Identify nearest city for each lat, lng combination for lat_lng in lat_lngs: city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name country = citipy.nearest_city(lat_lng[0], lat_lng[1]).country_code #countries.append(country) # If the city is unique, then add it to a our cities list #if country not in countries: # countries.append(country) if city not in cities: cities.append(city) countries.append(country) new_lats.append(lat_lng[0]) new_longs.append(lat_lng[1]) # Print the city count to confirm sufficient count print(len(cities)) print(len(countries)) print(len(new_lats))
# #city = cp.nearest_city(22.99, 120.21) # #Yako, bf # #city = cp.nearest_city(12.95, -2.26) # #Wellington,? # #city = cp.nearest_city(-41.28, 174.77) # #San Francisco, CA, USA # #city = cp.nearest_city(37.77, -122.43) # #Sacramento, CA, USA # #city = cp.nearest_city(38.58, -121.48) # San Ramon, CA, USA city = cp.nearest_city(37.80,-121.98) print(city.city_name) print(city.country_code) # #---------------------------------------------------- # #Test: random.uniform coordinates and city lookup # from citipy import citipy as cp # from random import uniform # # Testing one geo coordinate pair # x,y = uniform(-90, 90),uniform(-180,-90) # print(x)
lat_lngs = [] cities = [] # Create a set of random lat and lng combinations lats = np.random.uniform(low=-90.000, high=90.000, size=1500) lngs = np.random.uniform(low=-180.000, high=180.000, size=1500) # lat_lngs = zip(lats, lngs) #************************************* coordinates = list(zip(lats, lngs)) cities = [] countries = [] for coordinate_pair in coordinates: lat, lon = coordinate_pair city = citipy.nearest_city(lat, lon).city_name country = citipy.nearest_city(lat, lon).country_code cities.append(city) countries.append(country) Cities_data = pd.DataFrame({"City": cities, "Country": countries}) # Drop any duplicate values Cities_data = Cities_data.drop_duplicates('City') # Visualize the data frame Cities_data.head() # ## Generate Cities List # In[230]:
lng_range = (-180, 180) # In[3]: # List for holding lat_lngs and cities lat_lngs = [] cities = [] # Create a set of random lat and lng combinations lats = np.random.uniform(lat_range[0], lat_range[1], size=1500) lngs = np.random.uniform(lng_range[0], lng_range[1], size=1500) lat_lngs = zip(lats, lngs) # Identify nearest city for each lat, lng combination for lat_lng in lat_lngs: city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name # If the city is unique, then add it to a our cities list if city not in cities: cities.append(city) # Print the city count to confirm sufficient count len(cities) # In[4]: #Lists to be filled with values url = "http://api.openweathermap.org/data/2.5/weather?units=Imperial&APPID=" + weather_api_key diffcity = [] Lat = [] Lng = []
import time from scipy.stats import linregress # %% # generate 1500 random latitude and longitude data as a list of tuples lats = np.random.uniform(-90.0, 90.0, size=1500) lngs = np.random.uniform(-180.0, 180.0, size=1500) lats_lngs = zip(lats, lngs) coordinates = list(lats_lngs) print(coordinates[:11]) # %% # use citipy module to get nearest city names cities = list() for coor in coordinates: cities_name = citipy.nearest_city(coor[0], coor[1]).city_name # ensure no any duplicate cities if cities_name not in cities: cities.append(cities_name) print(cities[:10], 'Generate', len(cities)) # %% # use OpenWeather API to request, get, parse JSON to retrieve weather data for each city. # initial counters for log and sets record_count = 1 set_count = 1 city_data = list() basic_url = "http://api.openweathermap.org/data/2.5/weather?units=imperial&APPID=" + weather_api_key