Пример #1
0
        def map_multiple_outputs_points(directory: str, map_center=None, save_path=None):
            """
            Creates a folium map of single or multiple outputs from the LP model. Circle markers communicate both location of
            station and forecast demand (diameter).
            Good for outputs from LP model that outputs points - not hexes.
            """
            station_locations = open_station_locations()
            if not map_center:
                m_lat = station_locations[:1]['latitude']
                m_long = station_locations[:1]['longitude']

            else:
                m_lat = map_center[0]
                m_long = map_center[1]

            m = Map(
                location=[m_lat, m_long],
                tiles='cartodbpositron',
                zoom_start=9
            )

            colors = ['blue', 'crimson', 'violet', 'orange', 'yellow', 'black']
            cnt = 0

            for file in os.listdir(directory):
                open_path_1 = f'data/GIS/LP_outputs/{file}'
                print(file)
                name = file.rstrip('.csv')
                proposed_stations_1 = open_lp_proposed_stations(lp_proposed_stations_path=open_path_1)
                proposed_stations_1.set_index('id', inplace=True)
                proposed_stations_1 = join_dfs(station_locations, proposed_stations_1)

                feature_group = FeatureGroup(name=name)
                for i, row in proposed_stations_1.iterrows():
                    if row['demand'] == 0:
                        pass
                    else:
                        c = CircleMarker(
                            radius=row['demand'],
                            location=[row["latitude"], row["longitude"]],
                            tooltip=f"Station ID: {row.name} \n Demand: {row['demand']} kWh",
                            color=colors[cnt],
                            fill=True,
                            fill_color=colors[cnt]
                        )
                        c.add_to(feature_group)
                feature_group.add_to(m)
                cnt += 1

            LayerControl('bottomright', collapsed=False).add_to(m)
            if save_path:
                m.save(save_path)

            return m
Пример #2
0
def draw_score_markers(data, folium_map, congestions_toggle):
    cmap = plt.get_cmap('RdYlBu_r')

    # Sort by score to get higher score markers on top of the map
    data = sorted(data, key=lambda k: k['score'], reverse=congestions_toggle)
    norm = colors.Normalize(vmin=-1, vmax=1)
    placed_markers = set()
    try:
        for row in data:
            color = convert_to_hex(cmap(norm(row['score'])))
            tooltip = 'Location: {}<br />Attribute: {}<br />Time: {}<br />Score: {}'.format(
                row['location'].replace('{', '').replace('`', '').replace('}', ''),
                row['attribute_name'],
                row['time_point'],
                row['score'])
            # Only place a marker per location
            # TODO: change to max/min per location
            if row['location'] in placed_markers:
                continue

            if row['dataset'] == 'espiras':
                pmarker = CircleMarker(location=json.loads(row['location_coor']), radius=8, line_color=color,
                                       color=color,
                                       fill_color=color,
                                       tooltip=tooltip)
                pmarker.add_to(folium_map)
            else:
                if isinstance(row['location_coor'], dict):
                    row['location_coor'] = str(row['location_coor'])
                geojson = GeoJson(
                    row['location_coor'].replace("\'", "\""),
                    style_function=lambda f, color=color: {
                        'fillColor': color,
                        'color': color,
                        'weight': 4,
                        'fillOpacity': 0.7
                    },
                    tooltip=tooltip
                )
                geojson.add_to(folium_map)

            placed_markers.add(row['location'])
    except Exception as e:
        print(e)