Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google_maps:refer issue #38 #61

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

Tanvi-Jain01
Copy link

@nipunbatra @patel-zeel

This PR solves issue #38 also adds snakecase and typehints

BEFORE:

CODE:

import numpy as np
import pandas as pd
np.random.seed(42)  

start_date = pd.to_datetime('2022-01-01')
end_date = pd.to_datetime('2022-12-31')

dates = pd.date_range(start_date, end_date)

pm25_values = np.random.rand(365)  # Generate 365 random values
lat_values = np.random.rand(365)
lon_values = np.random.rand(365)


df = pd.DataFrame({
    'date': dates,
    'pm25': pm25_values,
    'latitude': lat_values,
    'longitude': lon_values,
    'station':stn_values
})

df['date'] = df['date'].dt.strftime('%Y-%m-%d')  # Convert date format to 'YYYY-MM-DD'

print(df)
from vayu.googleMaps  import googleMaps
googleMaps(df, 'latitude', 'longitude', 'pm25', '2022-02-23')

OUTPUT:

KeyError                                  Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3802, in Index.get_loc(self, key, method, tolerance)
   3801 try:
-> 3802     return self._engine.get_loc(casted_key)
   3803 except KeyError as err:

File ~\anaconda3\lib\site-packages\pandas\_libs\index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()

File ~\anaconda3\lib\site-packages\pandas\_libs\index.pyx:165, in pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:5745, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:5753, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Parameter Name'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[5], line 4
      1 from vayu.googleMaps  import googleMaps
      2 #print(df)
----> 4 googleMaps(df, 'latitude', 'longitude', 'pm25', '2022-02-23')

File ~\anaconda3\lib\site-packages\vayu\googleMaps.py:39, in googleMaps(df, lat, long, pollutant, dataLoc)
     34 # =============================================================================
     35 # df = pd.read_csv('interpolData.csv')
     36 # =============================================================================
     38 some_value = pollutant
---> 39 df = df.loc[df["Parameter Name"] == some_value]
     41 some_value = "2018-05-07"
     42 df = df.loc[df["Date Local"] == some_value]

File ~\anaconda3\lib\site-packages\pandas\core\frame.py:3807, in DataFrame.__getitem__(self, key)
   3805 if self.columns.nlevels > 1:
   3806     return self._getitem_multilevel(key)
-> 3807 indexer = self.columns.get_loc(key)
   3808 if is_integer(indexer):
   3809     indexer = [indexer]

File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3804, in Index.get_loc(self, key, method, tolerance)
   3802     return self._engine.get_loc(casted_key)
   3803 except KeyError as err:
-> 3804     raise KeyError(key) from err
   3805 except TypeError:
   3806     # If we have a listlike key, _check_indexing_error will raise
   3807     #  InvalidIndexError. Otherwise we fall through and re-raise
   3808     #  the TypeError.
   3809     self._check_indexing_error(key)

KeyError: 'Parameter Name'

AFTER

CODE:

import folium
import webbrowser
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
    
def google_maps(df:pd.DataFrame, lat:str, long:str, pollutant:str, date:str, markersize:int,zoom:int):
    """Plots a geographical plot.

    Plots a folium plot of longitude and latitude points 
    provided with information about each point when clicked 
    on.

    Parameters
    ----------
    df: pandas.DataFrame
        minimally containing date and values of pollutant, city,
        longitude, latitude, and AQI
    lat: str
        Name of column in df of where latitude points are
    long: str
        Name of column in df of where longitude points are
    pollutant: str
        Name of pollutant where values of that pollutant is stored.
    date: str
        visualizing the pollutant of a specific date.
    markersize: int
        The int by which the value of pollutant will be multiplied.
    zoom: int
        The int by which you want to zoom in the plot

    """
   
    df1 = df[df['date'] == date]

    lat= df1[lat].values[0] 
    long=df1[long].values[0] 
    my_map4 = folium.Map(location = [lat, long], zoom_start = zoom)

    for lat,long,pol,st in zip(df['latitude'],df['longitude'],df[pollutant],df['station']):
        folium.CircleMarker([lat, long],radius=markersize * pol, popup=(str(st).capitalize()+"<br>"+ str(round(pol, 3))), fill=True, fill_opacity=0.7, color = 'red').add_to(my_map4)

    my_map4.save("googleMaps.html")
    print('your map has been saved')
    return my_map4

USAGE:

map_obj = google_maps(df, 'latitude', 'longitude', 'pm25', '2022-02-23', 5,10)
map_obj

OUTPUT:
The map will be shown in Notebook and will also be saved in html format.
HTML
googlemaps1

Jupyter Notebook
googlemaps2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant