def add_tweets(geo_tweets, cluster, tweets_map): """Add all geo enabled tweets on the map.""" # Define subgroups for LayerControl coords_count = sum(1 for t in geo_tweets if t.coordinates) place_count = len(geo_tweets) - coords_count coords_subgroup = FeatureGroupSubGroup( cluster, f"Tweets with coordinates ({coords_count})" ) place_subgroup = FeatureGroupSubGroup( cluster, f"Tweets with place only ({place_count})" ) # Load popup html template popup_template = display.get_template("popup.html.j2") # Map tweets for tweet in geo_tweets: subgroup = coords_subgroup if tweet.coordinates else place_subgroup add_marker(tweet=tweet, subgroup=subgroup, popup_template=popup_template) # Add subgroups to map for subgroup in [coords_subgroup, place_subgroup]: tweets_map.add_child(subgroup)
def create_potential_duplicates_map(base_map, near_duplicates: pd.DataFrame): if near_duplicates is None: return marker_cluster = MarkerCluster(control=False) marker_cluster.add_to(base_map) sub_group = FeatureGroupSubGroup(marker_cluster, name='Near-Duplicate Locations', control=True, show=True) # # add markers # ['LocationA', 'NameA', 'coordinatesA', 'LocationB', 'NameB', 'coordinatesB', 'dist_m'] for ix, row in near_duplicates.iterrows(): for is_row_a in [True, False]: dup_location_marker(row, is_row_a).add_to(sub_group) # add lines folium.PolyLine((row.coordinatesA, row.coordinatesB), color="red", weight=2.5, opacity=1).add_to(sub_group) sub_group.add_to(base_map) return None
def create_center_markers(base_map, centers_df): # cluster_table has columns ['locId', 'latitude', 'longitude', 'cluster_label', 'GeoName'] if centers_df is None: # No pseudo-sectors return None marker_cluster = MarkerCluster(control=False) marker_cluster.add_to(base_map) sub_group = FeatureGroupSubGroup(marker_cluster, name='Pseudo-sector Centers', control=True, show=True) for index, row in centers_df.iterrows(): hname = row['GeoName'] # marker_color = marker_colors.get('Yellow') # marker_size = 10 xpopup = folium.Popup(row.to_frame().to_html()) # options = { # 'isAlphaNumericIcon': True, # 'borderColor': '#00ABDC', # 'textColor': '#00ABDC', # 'innerIconStyle': 'margin-top:0;' # } marker = folium.Marker( location=(row['latitude'], row['longitude']), popup=xpopup, tooltip=hname, icon=folium.plugins.BeautifyIcon( icon='circle', borderColor='#00ABDC', textColor='#00ABDC', innerIconStyle='margin-top:0;' # icon=L.BeautifyIcon.icon(options), # icon_shape='circle-dot', # text_color=srgb_black, # actually icon color # background_color=marker_color, # border_width=marker_size )) marker.add_to(sub_group) sub_group.add_to(base_map) return None
def create_location_markers(base_map, locations_df): # hotspots_df has columns ['locid', 'lat', 'lng', 'name', 'num', 'marker_size'] marker_cluster = MarkerCluster(control=False) marker_cluster.add_to(base_map) sub_group = FeatureGroupSubGroup(marker_cluster, name='Observations', control=True, show=True) marker_colors_by_name = assign_marker_colors(locations_df) color_by_sectors = 'cluster_label' in locations_df.columns for index, row in locations_df.iterrows(): hname = row['Name'] marker_color = marker_color_for_cluster(row['cluster_label']) if color_by_sectors else \ marker_colors_by_name.get(hname) marker_size = 1 xpopup = folium.Popup(make_observation_popup_html(row), max_width=600) # pixels marker = folium.Marker( location=[row['latitude'], row['longitude']], popup=xpopup, tooltip=hname, icon=folium.plugins.BeautifyIcon( prefix='fa', icon='binoculars', # icon_shape='rectangle', # icon='twitter', icon_shape='marker', # icon_size=Point(), text_color=marker_colors.get('Yellow', 'HotPink'), # actually icon color background_color=marker_color, border_width=marker_size, border_color=marker_colors.get('Silver', 'HotPink'))) marker.add_to(sub_group) sub_group.add_to(base_map) return None
def make_map(data_df, loc_df): OK_COORDINATES = (26.43, 127.8) # base map my_map = Map(location=OK_COORDINATES, zoom_start=10.12, min_zoom=8, max_zoom=13, tiles='cartodb positron') # tiles = [”OpenStreetMap”, ”Stamen Terrain”, “Stamen Toner”, “Stamen Watercolor”, ”CartoDB positron”, “CartoDB dark_matter”, ”Mapbox Bright”, “Mapbox Control Room” ] tile_options = ['cartodb positron', 'stamen terrain'] tile_names = ["simple map", "terrain map"] for name, opt in zip(tile_names, tile_options): TileLayer(opt).add_to(my_map, name=name) # cluster object cluster = MarkerCluster(control=False) my_map.add_child(cluster) group_list = [] for age_id in range(10): if age_id == 9: age_str = 'N/A' else: age_str = '%d0s' % (age_id + 1) group = FeatureGroupSubGroup(cluster, age_str) my_map.add_child(group) group_list.append(group) # add a marker for every record in the filtered data, use a clustered view for ind, d in data_df.iterrows(): # make pop-up string place = d['place'] age = d['age'] gender = d['gender'] conf_date = d['confirm_date'] status = d['status'] if age == 'N/A': age_id = 9 else: age_id = int(age[0]) - 1 pop_str = 'date:%s, age:%s' % (conf_date, age) if gender == 'Male': icon = Icon(color='blue', icon='male', prefix='fa') elif gender == 'Female': icon = Icon(color='red', icon='female', prefix='fa') else: icon = Icon(color='black', icon='question-circle', prefix='fa') # get coordinates try: lat_value = loc_df[loc_df.place == place].lat.values[0] lng_value = loc_df[loc_df.place == place].lng.values[0] except: lat_value = loc_df[loc_df.place == '県外'].lat.values[0] lng_value = loc_df[loc_df.place == '県外'].lng.values[0] pop_str = 'date:%s, age:%s, place=%s' % (conf_date, age, 'outside Okinawa') #print(place, lat_value, lng_value) # create marker with coordinates & pop-up info marker = Marker(location=[lat_value, lng_value], popup=pop_str, icon=icon) # add marker to gorup group_list[age_id].add_child(marker) LayerControl().add_to(my_map) my_map.save("covid19_okinawa_map.html") #display(my_map) return my_map
color = 'red' elif event == 'Health': color = '#1AFAFF' elif event == 'Literacy': color = 'green' return color events = ['AQI', 'Health', 'Literacy'] for event in events: cluster = folium.plugins.MarkerCluster(control=False).add_to(m) subgroup = FeatureGroupSubGroup(cluster, name=event, control=True, show=True) for place, lat, lan, aqi, health, populationMale, populationFemale, literacy, sexRatio, childPopulation in zip( data['Place'], data['Latitude'], data['Longitude'], data['AQI'], data['Health'], data['Population-Male'], data['Population-Female'], data['Litracy Rate'], data['Sex Ratio'], data['Child Population']): if event == 'AQI': folium.Circle(location=[lat, lan], popup="AQI : " + str(aqi), tooltip='<strong>Click to view AQI of ' + str(place), radius=500, color=colorDecide(event), fill_color=colorDecide(event), line_color=colorDecide(event), name=event,