Esempio n. 1
0
def generateHeatmaps(df_date_list, date_columns):
    for date_values, date_col in zip(df_date_list, date_columns):
        base_map = Map(location=default_location,
                       control_scale=True,
                       zoom_start=default_zoom_start)
        heatmap = HeatMap(data=date_values,
                          name='Confirmed cases',
                          min_opacity=0.6,
                          radius=10,
                          max_zoom=13)
        heatmap.add_to(base_map)
        base_map.save(f"./heatmaps/heatmap_{date_col.replace('/', '_')}.html")
Esempio n. 2
0
 def add_traffic_heatmap(self, locations):
     """Generates a HeaMap using a bunch of traffic station points"""
     heat_map = HeatMap(locations,
                        name='Tráfico',
                        radius=14,
                        min_opacity=0.8,
                        gradient={
                            0.4: 'blue',
                            0.8: 'lime',
                            1: 'red'
                        })
     heat_map.add_to(self._map)
Esempio n. 3
0
    def create_heatmap(self, **kwargs):
        """Create a heatmap using the instance's coordinates.

        The following parameter description were taken directly from
        the folium.plugins.HeatMap documentation.
        
        Kwargs:
            name (str) --> None:
                The name of the Layer, as it will appear in
                LayerControls.
            min_opacity (int) --> 1;
                The minimum opacity the heat will start at.
            max_zoom (int) --> 18:
                Zoom level where the points reach maximum intensity (as
                intensity scales with zoom), equals maxZoom of the map
                by default.
            max_val (float) --> 1:
                Maximum point intensity.
            radius (int) --> 25:
                Radius of each "point" of the heatmap.
            blur (int) --> 15:
                Amount of blur.
            gradient (dict) --> None:
                Color gradient config.
                e.g. {0.4: 'blue', 0.65: 'lime', 1: 'red'}
            overlay (bool) -- True:
                Adds the layer as an optional overlay (True) or the
                base layer (False).
            control (bool) --> True:
                Whether the Layer will be included in LayerControls.
            show (bool) --> True:
                Whether the layer will be shown on opening (only for
                overlays).
        """

        # Raise an error if any invalid arguments are detected
        valid = [
            'name', 'min_opacity', 'max_zoom', 'max_val', 'radius', 'blur',
            'gradient', 'overlay', 'control', 'show'
        ]
        for kwarg in kwargs:
            if kwarg not in valid:
                raise ValueError('Invalid keyword argument.')

        # Instantiate a heatmap object
        heatmap = HeatMap(self._coordinates, **kwargs)
        heatmap.add_to(self)
Esempio n. 4
0
def create_heat_map(df, save_file, query):
    df = df.dropna(subset=['longitude', 'latitude', 'zip'])
    if query is not None:
        df = df.query(query)

    f_map = folium.Map(location=[55.748433, 10.563504],
                       zoom_start=7)  # center on denmark
    tuples = list(zip(df['latitude'], df['longitude']))

    h_map = HeatMap(tuples, min_opacity=0.3, radius=5, blur=3)
    h_map.add_to(f_map)

    save_dir = os.path.dirname(save_file)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir, 0o755)

    f_map.save(save_file)
    return f_map, h_map, df
Esempio n. 5
0
    def add_heatmap(self,
                    xycoords,
                    name=None,
                    radius=5,
                    blur=7,
                    overlay=True,
                    control=True,
                    **kwargs):

        # Generate heat map
        heatmap = HeatMap(xycoords,
                          name=name,
                          radius=radius,
                          blur=blur,
                          overlay=overlay,
                          control=control,
                          **kwargs)

        heatmap.add_to(self.map)
Esempio n. 6
0
def app(df_listings, df_attractions):
    html_temp = """
            <div><font color=\"#C8C8C8\" size=\"18\"><strong>Is there any connection between NY City's attraction and the Airbnb prices?</font></div><br>
            <div><font color=\"#C8C8C8\" size=\"6\">How many Airbnb listings are located in NY?</font></div>"""
    st.markdown(html_temp, unsafe_allow_html=True)

    st.markdown("This website will provide you an easy and fun set of visualizations and tools to search on the different available Airbnb accommodations in New York City. "
    "We know that in a city as big and expensive as New York, finding a suitable apartment can be a daunting task. "
    "In the left menu you can find different interactive tools that illustrate geographically the variation of the different listings characteristics \(such as price, ratings, etc.\), "
    "as well as some other helpful apps.")

    df_heatmap = df_listings.copy()
    df_heatmap['count'] = 1
    
    html_temp = """
            <div><font color=\"#C8C8C8\" size=\"6\">Heat Map of NY's Airbnb listings</font></div>"""
    st.markdown(html_temp, unsafe_allow_html=True)
    
    map_hooray = folium.Map([40.730610, -73.935242], zoom_start=11, tiles="OpenStreetMap")
    heatmap = HeatMap(data=df_heatmap[['latitude', 'longitude', 'count']].groupby(
        ['latitude', 'longitude']).sum().reset_index().values.tolist(), radius=8, max_zoom=13)

    heatmap.add_to(map_hooray)

    popup = []

    for i in range(len(png)):
        encoded = base64.b64encode(open(png[i], 'rb').read()).decode()
        building_name = df_attractions.Attraction[i]
        html = building_name
        html += '<img src="data:image/png[i];base64,{}" width="100" height="100">'.format(encoded)
        iframe = IFrame(html, width=130, height=150)
        popup.append(folium.Popup(iframe, max_width=130))

    for i in range(len(png)):
        folium.Marker([df_attractions.latitude[i], df_attractions.longitude[i]], popup=popup[i],
                      icon=folium.Icon(color='blue', icon_color='white', icon='globe')).add_to(map_hooray)

    folium_static(map_hooray, width=1000, height=600)
Esempio n. 7
0
hmap = folium.Map(
    location=[36.1699, -115.1398],
    zoom_start=10,
)

#%% Vegas low and high star heat maps
hm_wide = HeatMap(
    list(zip(vegas_low['latitude'].values, vegas_low['longitude'].values)),
    min_opacity=0.2,
    max_val=1,
    radius=10,
    blur=18,
    max_zoom=1,
)
hm_wide.add_to(hmap)
#hmap.save('maps/heatmap_vagas.html')
hmap.save('maps/heatmap_Vegas_Stars_low.html')

hm_wide = HeatMap(
    list(zip(vegas_high['latitude'].values, vegas_high['longitude'].values)),
    min_opacity=0.2,
    max_val=1,
    radius=10,
    blur=18,
    max_zoom=1,
)
hm_wide.add_to(hmap)
#hmap.save('maps/heatmap_vagas.html')
hmap.save('maps/heatmap_Vegas_Stars_high.html')
driver_m = HeatMap(data=driver_map[['LATITUDE', 'LONGITUDE', 'ADDRESS_TYPE_CD']].groupby(['LATITUDE','LONGITUDE']).count().reset_index().values.tolist(),  
                   gradient={.2: '#74B7FA', 1: 'black', .4: '#0256A9'},  radius = 8, max_zoom=10)

origin_van_m = HeatMap(data=origin_van_map[['LATITUDE_NB', 'LONGITUDE_NB', 'VisitCount']].groupby(['LATITUDE_NB','LONGITUDE_NB']).sum().reset_index().values.tolist(),  
                   gradient={.4: '#B87AFE', 1: 'black', .65: '#5403B0'},  radius = 8, max_zoom=10)

destination_van_m = HeatMap(data=destination_van_map[['LATITUDE_NB', 'LONGITUDE_NB', 'VisitCount']].groupby(['LATITUDE_NB','LONGITUDE_NB']).sum().reset_index().values.tolist(),  
                        gradient={.4: '#FECF9E', 1: 'black', .65: '#FC8A13'},  radius = 8, max_zoom=10)

origin_ded_m = HeatMap(data=origin_ded_map[['LATITUDE_NB', 'LONGITUDE_NB', 'VisitCount']].groupby(['LATITUDE_NB','LONGITUDE_NB']).sum().reset_index().values.tolist(),  
                   gradient={.4: '#B87AFE', 1: 'black', .65: '#5403B0'},  radius = 8, max_zoom=10)

destination_ded_m = HeatMap(data=destination_ded_map[['LATITUDE_NB', 'LONGITUDE_NB', 'VisitCount']].groupby(['LATITUDE_NB','LONGITUDE_NB']).sum().reset_index().values.tolist(),  
                        gradient={.4: '#FECF9E', 1: 'black', .65: '#FC8A13'},  radius = 8, max_zoom=10)

conway_m.add_to(folium.FeatureGroup(name = 'Conway Investment in Millions', show = False).add_to(base_map))

driver_m.add_to(folium.FeatureGroup(name = 'Driver Domiciles Count', show = False).add_to(base_map))


origin_van_m.add_to(folium.FeatureGroup(name = 'Van Origin Trip Count', show = False).add_to(base_map))

destination_van_m.add_to(folium.FeatureGroup(name = 'Van Destination Trip Count', show = False).add_to(base_map))

origin_ded_m.add_to(folium.FeatureGroup(name = 'Dedicated Origin Trip Count', show = False).add_to(base_map))

destination_ded_m.add_to(folium.FeatureGroup(name = 'Dedicated Destination Trip Count', show = False).add_to(base_map))

werner_m.add_to(folium.FeatureGroup(name = 'Werner Trip Count', show = False).add_to(base_map))

# legend = folium.raster_layers.ImageOverlay(name = 'Legend',
Esempio n. 9
0
    np.savetxt(dir + '\LA_data.txt', data, delimiter=',')

    ############################################################
    # data ready, now inserting into mapping process with image output
    ############################################################
    out_fn = 'LA_heatmap.html'
    generate_new = False

    m = folium.Map(location=location,
                   control_scale=True,
                   tiles='Mapbox Bright',
                   zoom_start=11)

    radius = 10
    hm = HeatMap(data, radius=radius, blur=int(2 * radius))
    hm.add_to(m)

    m.save(out_fn)
    print('View the map with a browser by opening {}.'.format(out_fn))

    ############################################################
    # map image output
    ############################################################

    from selenium import webdriver
    driver = webdriver.Chrome(
        executable_path=
        'C:/Users/canth/Dropbox/UCLA/A2 Neural Networks/project/chromedriver.exe'
    )

    driver.set_window_size(1600, 1200)  # choose a resolution
Esempio n. 10
0
    def generate_heatmap(self,
                         source='internal',
                         coordinate_data=None,
                         latitude_column=None,
                         longitude_column=None,
                         output='heatmap.html',
                         open_html=False):
        """Generate a heatmap from coordinate data.
        
        Kwargs:
            source (str) --> 'internal':
                Potential sources are one of: 'internal', 'data', and
                'csv'. 'Internal' using the instance's stored
                coordinates, 'data' uses data that is passed in via the
                coordinate_data argument, and 'csv' reads data from a 
                csv file.
            coordinate_data (None/list/str) --> None:
                Type of data passed in via this parameter is dependent
                on the string that was passed into the source
                parameter. Value should be None if 'internal' was
                chosen as the source, a list of coordinate tuples if
                the source is 'data', and a string/filepath if the
                source is 'csv'.
            latitude_column (int) --> None:
                The column of the data that latitude values are stored
                in. Only required when the chosen source is 'data' or
                'csv'.
            longitude_column (int) --> None:
                The column of the data that longitude values are stored
                in. Only required when the chosen source is 'data' or
                'csv'.
            output (str) --> 'heatmap.html':
                Filepath of the output html file.
            open_html (bool) --> False:
                Open the heatmap in a browser once it has been created.
        """

        if source == 'internal':
            data = self.pull_coordinates(include_timestamp=False)
            latitudes = [datum[0] for datum in data if None not in datum]
            longitudes = [datum[1] for datum in data if None not in datum]

        elif source == 'data':
            latitudes = [
                datum[latitude_column - 1] for datum in coordinate_data
            ]
            longitudes = [
                datum[longitude_column - 1] for datum in coordinate_data
            ]

        elif source == 'csv':
            data = pd.read_csv(coordinate_data)
            latitudes = data.iloc[:, latitude_column - 1]
            longitudes = data.iloc[:, longitude_column - 1]

        # Need to find a better way to make the heatmap more customizable
        # when being generated through the GeoPhotos class

        heatmap = folium.Map(location=[43.1065, -76.2177], zoom_start=14)

        heatmap_wide = HeatMap(list(zip(latitudes, longitudes)), )

        heatmap_wide.add_to(heatmap)

        heatmap.save(output)

        if open_html:
            webbrowser.open(output)
Esempio n. 11
0
def main():
    # set to True to generate a new heatmap for a city
    data_fn = 'data/data.pkl'
    out_fn = 'atlanta_heatmap.html'
    generate_new = False

    if generate_new:
        node_map = defaultdict(list)
        all_nodes = set()
        node_data = dict()
        msg = ''
        city = 'Atlanta'
        state = 'GA'
        print('City: {}'.format(city))
        n_iter = 0
        max_iter = np.inf
        size_target = 10000

        # get initial results to seed the graph
        homes = get_search_results(address=city,
                                   city_state='{city}+{state}'.format(
                                       city=city, state=state))
        if len(homes) == 0:
            raise ValueError('No results found for city {}.'.format(city))

        for home in homes:
            node_data[home.zpid] = home

        done = False
        while len(homes) > 0 and not done:
            n_iter += 1

            home = homes.pop(-1)

            comps = get_comps(home.zpid)
            for comp in comps:

                if comp.zpid not in all_nodes:

                    if meets_criteria(comp):
                        # add to set of all nodes
                        all_nodes.add(comp.zpid)

                        # add to the queue
                        homes.append(comp)

                        # add to the id -> home info mapping
                        node_data[comp.zpid] = comp

                # add to the id -> comps mapping
                node_map[home.zpid].append(comp.zpid)

            # iterate until we have reached the desired condition
            if len(node_data) >= size_target:
                msg = 'Desired # elements reached'
                done = True
            elif n_iter == max_iter:
                msg = 'Maximum # iterations reached'
                done = True
            elif len(homes) == 0:
                msg = 'All homes traversed/dead end reached.'
                done = True
            # randomly print out how many homes we've found
            elif np.random.normal() < -2:
                print('# of homes found: [}'.format(len(node_data)))

            if done:
                print(msg)
                print('Found {} homes.'.format(len(homes)))

        with open(data_fn, 'wb') as fp:
            pickle.dump({'node_data': node_data, 'node_map': node_map}, fp)
    else:
        with open(data_fn, 'rb') as fp:
            data = pickle.load(fp)
        node_data, node_map = data['node_data'], data['node_map']

    print('# houses: {}'.format(len(node_data)))

    data = np.array([[home_info.lat, home_info.lon, home_info.value]
                     for home_info in node_data.values()])

    m = folium.Map(location=data[:, :2].mean(axis=0),
                   control_scale=True,
                   zoom_start=11)

    radius = 10
    hm = HeatMap(data, radius=radius, blur=int(2 * radius))
    hm.add_to(m)

    m.save(out_fn)
    print('View the map with a browser by opening {}.'.format(out_fn))
Esempio n. 12
0
df = get_data(api_params)
basemap = folium.Map(
    location=[map_params['center_lat'], map_params['center_long']],
    zoom_start=map_params['zoom'],
    control_scale=True,
    zoom_control=True,
)
tiles = folium.raster_layers.TileLayer(tiles=map_params['basemap_url'],
                                       attr=map_params['basemap_attr'],
                                       name=map_params['basemap_name'],
                                       show=True)
tiles.add_to(basemap)

obs_coords = list(zip(df['latitude'], df['longitude']))
heatmap = HeatMap(obs_coords, min_opacity=0.2, radius=25)

fg_hm = folium.FeatureGroup(name='NNZD heatmap', show=False)
basemap.add_child(fg_hm)
heatmap.add_to(fg_hm)

fg = folium.FeatureGroup(name='NNZD single observations', show=True)
basemap.add_child(fg)

for coord in obs_coords:
    folium.Marker(coord).add_to(fg)

folium.LayerControl().add_to(basemap)

basemap.save(params['output_map'])
Esempio n. 13
0
    def draw(self):
        """ Gera um mapa de calor a partir das opções enviadas """
        # http://localhost:5000/charts/choropleth?from_viewconf=S&au=2927408&card_id=mapa_pib_brasil&dimension=socialeconomico&as_image=S
        analysis_unit = self.options.get('au')
        chart_options = self.options.get('chart_options')

        result = self.pre_draw(self.get_tooltip_data())

        centroide = None
        cols = [
            chart_options.get('lat', 'lat'),
            chart_options.get('long', 'long')
        ]
        if 'value_field' in chart_options:
            cols.append(chart_options.get('value_field'))

        # Get group names from headers
        group_names = ViewConfReader.get_layers_names(
            self.options.get('headers'))
        grouped = self.dataframe.groupby(
            chart_options.get('layer_id', 'cd_indicador'))
        show = True  # Shows only the first
        for group_id, group in grouped:
            if 'timeseries' not in chart_options:
                chart = HeatMap(group[cols].values.tolist(),
                                name=group_names.get(group_id),
                                show=show)
            else:
                t_grouped = group.groupby(chart_options.get('timeseries'))
                t_data = []
                t_index = []
                for t_group_id, t_group in t_grouped:
                    t_data.append(t_group[cols].values.tolist())
                    t_index.append(t_group_id)
                chart = HeatMapWithTime(t_data,
                                        index=t_index,
                                        auto_play=True,
                                        name=group_names.get(group_id),
                                        show=show)
            chart.add_to(result)
            show = False

        # Adding marker to current analysis unit
        if np.issubdtype(self.dataframe.index.dtype, np.number):
            analysis_unit = int(analysis_unit)

        df = self.dataframe.pivot_table(index=[
            chart_options.get('id_field', 'cd_mun_ibge'),
            chart_options.get('name_field', 'nm_municipio'),
            chart_options.get('lat', 'latitude'),
            chart_options.get('long', 'longitude')
        ],
                                        columns='cd_indicador',
                                        values=chart_options.get(
                                            'value_field',
                                            'vl_indicador')).reset_index()

        if 'idx' in df.columns:
            df.set_index('idx', inplace=True)
        else:
            df.set_index(chart_options.get('id_field', 'cd_mun_ibge'),
                         inplace=True)

        if analysis_unit in df.index:
            au_row = df.loc[analysis_unit].to_dict()

            if chart_options.get('lat', 'latitude') in list(df.columns):
                centroide = [
                    au_row.get(chart_options.get('lat', 'latitude')),
                    au_row.get(chart_options.get('long', 'longitude'))
                ]

            if centroide:
                marker_layer = folium.map.FeatureGroup(name=self.get_au_title(
                    au_row, self.options.get('headers')))
                folium.map.Marker(
                    centroide,
                    tooltip=self.tooltip_gen(au_row,
                                             self.options.get('headers')),
                    icon=folium.Icon(color=ViewConfReader.get_marker_color(
                        self.options))).add_to(marker_layer)
                marker_layer.add_to(result)

        return self.post_adjustments(result)