Beispiel #1
0
def make_map(features):
    #print('> Making map...')
    m = f.Map()
    a = {}
    z = 0.3 * len(d)
    y = int(z)
    for i in range(0, y):
        z1 = d.iloc[i]['latitude']
        z2 = d.iloc[i]['longitude']
        try:
            loc.append((float(z1), float(z2)))
            #a={i:{d.iloc[i]['tweetCreated']:{loc}}}
        except:
            pass

    plugins.MarkerCluster(loc).add_to(m)

    TimestampedGeoJson({
        'type': 'FeatureCollection',
        'features': features
    },
                       period='P1M',
                       add_last_point=True,
                       auto_play=False,
                       loop=False,
                       max_speed=1,
                       loop_button=True,
                       date_options='YYYY/MM',
                       time_slider_drag_update=True).add_to(m)
    #print('> Done.')
    return pollution_map
    def make_map(features):
        """ Returns a HTML file with time interactive scatter plot
    """
        print("Plotting the map...")
        pollution_map = folium.Map(
            width=width,
            height=height,
            location=location,
            zoom_start=zoom_start,
            control_scale=True,
            tiles=tiles,
            min_lat=min_lat,
            max_lat=max_lat,
            min_lon=min_lon,
            max_lon=max_lon,
        )

        colormap.add_to(pollution_map)

        TimestampedGeoJson(
            {"type": "FeatureCollection", "features": features},
            period=period,
            add_last_point=True,
            auto_play=False,
            loop=False,
            max_speed=1,
            loop_button=True,
            date_options=date_options,
            time_slider_drag_update=True,
        ).add_to(pollution_map)
        print("Saved the map.")
        return pollution_map
    def createPredictiveMap(self, features, areaId, cmap, outputPath,
                            period_str):
        if period_str == 40000:
            period_set = 'PT4H'
        elif period_str == 6000:
            period_set = 'PT1H'
        elif period_str == 3000:
            period_set = 'PT30M'
        htmlFile = outputPath + 'map_congested_' + str(areaId) + '.html'
        m = self.createBaseMap(areaId)
        TimestampedGeoJson({
            'type': 'FeatureCollection',
            'features': features
        },
                           transition_time=1000,
                           period=period_set,
                           add_last_point=True,
                           auto_play=True,
                           loop=True,
                           max_speed=1,
                           loop_button=True,
                           date_options='YYYY-MM-DD HH:mm:ss',
                           time_slider_drag_update=False).add_to(m)
        m.add_child(cmap)
        m.save(htmlFile)

        self.beautifyDisplay(htmlFile)
def make_map(features, center_name, coverage):
    print('> Making map...')
    init_location = [customer.loc[0, 'latitude'], customer.loc[0, 'longitude']]
    time_map = folium.Map(location=init_location, control_scale=True, zoom_start=11)
    center[center.name == center_name].apply(lambda row: folium.Marker(location=[row['latitude'], row['longitude']],
                                                                       icon=Icon(color='black', icon='fa-wrench',
                                                                                 prefix='fa')).add_to(time_map), axis=1)
    time_map.choropleth(
        geo_data=coverage,
        fill_color='yellow',
        fill_opacity=0.1
    )
    TimestampedGeoJson(
        data={'type': 'Feature',
              'features': features},
        period='P1D',
        duration='P1D',
        auto_play=False,
        loop=False,
        max_speed=10,
        loop_button=True,
        date_options='YYYY/MM/DD',
        time_slider_drag_update=True).add_to(time_map)
    print('> Done.')
    return time_map
Beispiel #5
0
def get_data():

    data = pre.read_data()

    variables = data['Variable'].unique().tolist()[:5]  # Probando solo con 5
    maps_data = {}

    for var in variables:
        print(var)
        df_variable = pre.df_variable(data, var)
        # add color column
        df_variable['color'] = [
            RdYlBu[11][val] for val in pd.cut(
                x=df_variable['Concentración'], bins=11, labels=False)
        ]

        geo_features = create_geojson_features(df_variable.reset_index())
        maps_data[var] = TimestampedGeoJson(
            {
                'type': 'FeatureCollection',
                'features': geo_features
            },
            period='P1D',
            add_last_point=True,
            auto_play=False,
            loop=False,
            max_speed=10,
            loop_button=True,
            date_options='YYYY/MM',
            duration='P1D',
            time_slider_drag_update=True)
    return maps_data, variables
Beispiel #6
0
def plotPointMap(values, date_range):
    base_map = folium.Map(location=[-31.420082, -64.188774])
    TimestampedGeoJson(getGeojsonFeatures(values, date_range),
                       date_options='YYYY/MM/DD',
                       auto_play=True).add_to(base_map)
    filename = tempfile.NamedTemporaryFile(prefix='pointmap_').name + '.html'
    base_map.save(filename)
    webbrowser.open(filename)
def value_map(pollutant):
    """
    Returns a map with mesures values and their location for each instant

    Args:
        pollutant (string): name of the pollutant to display

    Returns:
        Map: folium map with time mesures
    """
    folium_map = folium.Map(location=[46, 2.291705], zoom_start=7)
    client = DBClient("database")
    ordered_mesures = client.get_mesures_by_time(pollutant)
    global_max = get_global_max(ordered_mesures)

    encoder = JSONEncoder(global_max)
    features = []
    for time in ordered_mesures:
        features += [
            encoder.encode_as_geoJSON(mesure)
            for mesure in ordered_mesures[time]
        ]

    tsjson = TimestampedGeoJson(
        {
            'type': 'FeatureCollection',
            'features': features
        },
        period='PT1H',
        add_last_point=True,
        auto_play=False,
        loop=False,
        max_speed=1,
        loop_button=True,
        time_slider_drag_update=True)
    tsjson.options["position"] = "topright"
    tsjson.add_to(folium_map)
    encoder.cm.caption = f"{pollutant} concentration in ug.m-3"
    folium_map.add_children(encoder.cm)
    return folium_map
Beispiel #8
0
def make_map(features):
    coords = [44.981041, -93.232149]
    this_map = folium.Map(location=coords, control_scale=True, zoom_start=10)
    folium.TileLayer('cartodbpositron').add_to(this_map)

    temp = TimestampedGeoJson(
        {
            'type': 'FeatureCollection',
            'features': features
        },
        period='P1D',
        add_last_point=True,
        auto_play=True,
        loop=True,
        max_speed=.7,
        loop_button=True,
        date_options='YYYY/MM',
        time_slider_drag_update=True)

    temp.add_to(this_map)

    return this_map
Beispiel #9
0
 def add_traffic_timestamped_map(self, gjson_timestamped):
     TimestampedGeoJson(
         {
             'type': 'FeatureCollection',
             'features': gjson_timestamped
         },
         period='PT1H',
         add_last_point=True,
         auto_play=False,
         loop=False,
         max_speed=1,
         loop_button=True,
         date_options='YYYY/MM/DD HH',
         time_slider_drag_update=True).add_to(self._map)
Beispiel #10
0
def visualize_traj(traj,
                   fmap=None,
                   animation=True,
                   color='blue',
                   weight=3,
                   icon='circle',
                   transition_time=100,
                   period='PT2M',
                   duration=None):
    if fmap is None:
        fmap = create_map()

    if animation:
        TimestampedGeoJson(
            {
                'type':
                'FeatureCollection',
                'features': [{
                    'type': 'Feature',
                    'geometry': {
                        'type':
                        'LineString',
                        'coordinates':
                        list(map(lambda x: [x[1], x[0]], traj['coordinates']))
                    },
                    'properties': {
                        'times': traj['times'],
                        'style': {
                            'color': color,
                            'weight': weight
                        },
                        'icon': icon
                    }
                }]
            },
            transition_time=transition_time,
            period=period,
            duration=duration).add_to(fmap)
    else:
        fl.PolyLine(traj['coordinates'], color=color,
                    weight=weight).add_to(fmap)
        fl.Circle(traj['coordinates'][-1],
                  color=color,
                  weight=weight,
                  radius=600).add_to(fmap)

    return fmap
def __make_map(features, initial_coords):
    coords = initial_coords  #[52.24, 21.02]
    map_ = folium.Map(location=coords, control_scale=True, zoom_start=8)

    TimestampedGeoJson({
        'type': 'FeatureCollection',
        'features': features
    },
                       period='PT15M',
                       duration='PT1M',
                       add_last_point=True,
                       auto_play=False,
                       loop=False,
                       loop_button=True,
                       time_slider_drag_update=True).add_to(map_)

    return map_
Beispiel #12
0
def setup_business_time_series_map(business_data):
    features = business_time_series_map_features_util(business_data)
    business_time_series_map = folium.Map(location=seattle_coords,
                                          min_zoom=13,
                                          max_bounds=True)
    business_time_series_map.add_child(
        TimestampedGeoJson({
            'type': 'FeatureCollection',
            'features': features
        },
                           period='P1D',
                           add_last_point=True,
                           auto_play=False,
                           loop=False,
                           loop_button=True,
                           date_options='MM/DD/YYYY',
                           duration="P1D"))
    business_time_series_map.save("business_time_series_map.html")
Beispiel #13
0
def make_map(features):
    print('> Making map...')
    coords_belgium=[50.5039, 4.4699]
    pollution_map = folium.Map(location=coords_belgium, control_scale=True, zoom_start=8)

    TimestampedGeoJson(
        {'type': 'FeatureCollection',
        'features': features}
        , period='P1M'
        , add_last_point=True
        , auto_play=False
        , loop=False
        , max_speed=1
        , loop_button=True
        , date_options='YYYY/MM'
        , time_slider_drag_update=True
    ).add_to(pollution_map)
    print('> Done.')
    return pollution_map
Beispiel #14
0
def make_map2(features):
    print('> Making map...')
    coords=[37.0902,-95.7129]
    m = f.Map(location=coords, tiles='cartodbpositron', control_scale=True, zoom_start=3)
    TimestampedGeoJson(
        {'type': 'FeatureCollection',
        'features': features}
        , period='P1D'
        , add_last_point=True
        , auto_play=False
        , loop=False
        , max_speed=2
        , loop_button=True
        , date_options='MM/DD'
        , time_slider_drag_update=True
        , duration='P0D' #prevents new data from layering on top of old
    ).add_to(m)
    print("Done!")
    return m
Beispiel #15
0
def make_map(df):
    print('> Making map...')
    bay_area_cord = [37.739417, -122.254747]
    pollution_map = folium.Map(location = bay_area_cord, control_scale=True, zoom_start=10, tiles = 'Stamen Terrain')
    for x,y,labels in zip(coords['Lat_DD'], coords['Long_DD'], coords['Station label']):
        pollution_map.add_child(folium.CircleMarker([x,y], color = '#99b3cc', radius = 15, tooltip = labels))
    features = create_geojson_features(df)
    
    TimestampedGeoJson(
        {'type': 'FeatureCollection',
        'features': features}
        , period='P1M'
        , add_last_point=True
        , auto_play=False
        , loop=True
        , max_speed=10
        , loop_button=True
        , date_options='YYYY/MM'
        , time_slider_drag_update=True
    ).add_to(pollution_map)
    print('> Done.')
    return pollution_map
Beispiel #16
0
def make_map(features, legend_range):
    m = folium.Map(location=[20, 0],
                   height='100%',
                   width='100%',
                   control_scale=True,
                   zoom_start=1.8)

    TimestampedGeoJson({
        'type': 'FeatureCollection',
        'features': features
    },
                       period='P1D',
                       add_last_point=True,
                       auto_play=False,
                       loop=False,
                       duration='P1D',
                       max_speed=10,
                       loop_button=True,
                       date_options='YYYY/MM/DD',
                       time_slider_drag_update=True).add_to(m)

    color_range = np.round(
        np.linspace(legend_range[0], legend_range[1], 10) / 1000).astype(int)
    colormap = branca.colormap.LinearColormap(color_palette(),
                                              index=color_range,
                                              vmin=min(color_range),
                                              vmax=max(color_range),
                                              caption='Deaths (thousands)')
    colormap.add_to(m)

    caption = """<table>
  <tr>
    <td>Data sources: <a href="https://systems.jhu.edu/research/public-health/ncov/">John Hopkins University</a> & <a href="https://github.com/CSSEGISandData/COVID-19">GitHub</a></td> 
  </tr>
</table>"""

    m.get_root().html.add_child(folium.Element(caption))

    return m
Beispiel #17
0
def device_metric_map(self, channel, start_date, end_date, options=dict()):
    '''
    Creates a folium map showing the evolution of a metric dynamically with colors
    Parameters
    -------
    channel: String
        The channel to make the map from
    start_date, end_date: String
        Date convertible string
    options: dict()
        Possible keys are (default otherwise)
            location: list
                [41.400818, 2.1825157]
                Center map location
            tiles: (String)
                'Stamen Toner'
                Tiles for the folium.Map
            zoom: (float)
                2.5
                Zoom to start with in folium.Map
            period: 'String'
                '1W'
                Period for 'dynamic' map
            radius: float
                10
                Circle radius for icon
            fillOpacity: float
                1
                (<1) Fill opacity for the icon
            stroke: 'String'
                'false'
                'true' or 'false'. For icon's stroke
            icon: 'String'
                'circle'
                A valid folium.Map icon style
    Returns
    -------
        Folium.Map object
    '''

    # Set defaults
    options = dict_fmerge(config._map_def_opt, options)

    # Make date range
    date_r = date_range(start=start_date,
                        end=end_date,
                        normalize=True,
                        freq=options['period']).strftime('%Y-%m-%d')
    date_l = list()
    for item in date_r.values:
        date_l.append(str(item))

    # Get bins
    for bname in config._channel_bins.keys():
        if bname in channel:
            bins = config._channel_bins[bname]
            break

    # Make features
    features = []
    for device in self.devices:

        # Get lat, long
        try:
            self.devices[str(device)].api_device.get_device_lat_long()
            _lat = self.devices[str(device)].api_device.lat
            _long = self.devices[str(device)].api_device.long
        except AttributeError:
            std_out(f'Cannot retrieve [lat, long] from device {device}',
                    'WARNING')
            pass
            continue

        if _lat is None or _long is None: continue

        # Resample
        try:
            dfc = self.devices[str(device)].readings.resample(
                options['period']).mean()
        except:
            pass
            continue

        if channel not in dfc.columns: continue
        # Make color column
        dfc['color'] = cut(dfc[channel],
                           bins,
                           labels=config._map_colors_palette)

        # Add point for each date
        for date in date_l:
            if date not in dfc.index: continue
            if date_l.index(date) > len(date_l) - 2: continue
            features.append({
                'type': 'Feature',
                'geometry': {
                    'type': 'LineString',
                    'coordinates': [[str(_long), str(_lat)]] * 2,
                    'popup': str(device),
                },
                'properties': {
                    'times': [date, date_l[date_l.index(date) + 1]],
                    'icon': options['icon'],
                    'iconstyle': {
                        'fillColor': str(dfc.loc[date, 'color']),
                        'fillOpacity': options['fillOpacity'],
                        'stroke': options['stroke'],
                        'radius': options['radius']
                    },
                    'style': {
                        'weight': '0'
                    },
                    'id': 'man'
                }
            })

    # Make map
    m = Map(
        location=options['location'],
        tiles=options['tiles'],
        zoom_start=options['zoom'],
    )

    TimestampedGeoJson(
        {
            'type': 'FeatureCollection',
            'features': features
        },
        period='P' + convert_rollup(options['period']),
        add_last_point=True,
        auto_play=False,
        loop=False,
        max_speed=5,
        loop_button=True,
        # date_options='YYYY/MM/DD',
        time_slider_drag_update=True,
        duration='P' + options['period']).add_to(m)

    return m
                    'style': {
                        'color': row['Color']
                    }
                }
            }
            features.append(feature)
            if row["id"] in ["DNK", "FRA", "ITA", "GBR", "GRC"]:
                for geometry in compl.get(row["id"]).get("geometries"):
                    feature_new = dict(feature)
                    feature_new["geometry"] = geometry
                    features.append(feature_new)
    new_map = folium.Map([50.736455, 17.666], zoom_start=4.5)

    TimestampedGeoJson({
        'type': 'FeatureCollection',
        'features': features
    },
                       period='P1M',
                       duration='P1M',
                       add_last_point=True,
                       auto_play=False,
                       loop=False,
                       max_speed=1,
                       loop_button=True,
                       date_options='YYYY/MM',
                       time_slider_drag_update=True).add_to(new_map)
    colormap = LinearColormap(color_scale, vmin=0, vmax=bin_edges[-1])
    colormap.caption = "Monthly average " + pollutant + " (in µg/m3)"
    colormap.add_to(new_map)
    new_map.save("../maps/avg_" + pollutant + ".html")
Beispiel #19
0
    def layer_gen(self, group, group_id, show):
        """ Generates a bubbles layer """
        # Get the color of the bubble according to layer definitions
        color = 'blue'
        chart_options = self.options.get('chart_options', {})
        for ind_index in range(len(chart_options.get('indicadores', []))):
            if chart_options.get('indicadores')[ind_index] == group_id:
                color = chart_options.get('colorArray')[ind_index]
                break

        # Adding circle radius to group, if it's not present in dataframe group
        if 'radius' not in group.columns:
            group['radius'] = self.assess_radius(group)

        if 'timeseries' not in chart_options:
            # Creating a layer for the group
            layer = FeatureGroup(
                name=ViewConfReader.get_layers_names(self.options.get('headers')).get(group_id),
                show=show
            )

            # Check if popup data is present
            has_tooltip = 'tooltip' in group.columns

            # Generating circles
            for _row_index, row in group.iterrows():
                tooltip_data = None
                if has_tooltip:
                    tooltip_data = row['tooltip']

                CircleMarker(
                    location=[
                        row[chart_options.get('lat', 'latitude')],
                        row[chart_options.get('long', 'longitude')]
                    ],
                    radius=row['radius'],
                    popup=tooltip_data,
                    color=color,
                    fill=True,
                    fill_color=color
                ).add_to(layer)

            # Adding layer to map
            return layer
        else:
            features = []
            for _row_index, row in group.iterrows():
                features.append({
                    'type': 'Feature',
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [
                            row[chart_options.get('long', 'longitude')],
                            row[chart_options.get('lat', 'latitude')]
                        ]
                    },
                    'properties': {
                        'time': pd.to_datetime(
                            row[chart_options.get('timeseries', 'nu_competencia')],
                            format='%Y'
                        ).__str__(),
                        'style': {'color': color},
                        'icon': 'circle',
                        'iconstyle': {
                            'fillColor': color,
                            'fillOpacity': 0.8,
                            'stroke': 'true',
                            'radius': row['radius']
                        }
                    }
                })

            return TimestampedGeoJson(
                features,
                period='P1Y',
                duration='P1Y',
                date_options='YYYY',
                transition_time=1000,
                auto_play=True
            )
Beispiel #20
0
def device_history_map(map_type='dynamic', dataframe=None, options=dict()):
    '''
    Creates a folium map with either location of devices or their "existence period"
    -------
    Parameters:
    map_type: String
        'dynamic'
        'dynamic' or 'static'. Whether is a dinamic map or not
    dataframe: Pandas Dataframe
        None
        Contains information about when the devices started posting data, ids, location.
        It follows the format of world_map in api device
    options: dict
        dict()

    Returns:
        Folium.Map object
    '''
    def coordinates(x):
        return [x['latitude'], x['longitude']]

    def color(x):
        iSCAPE_IDs = [19, 20, 21, 28]
        making_sense_IDs = [11, 14]
        SCK_21_IDs = [26]

        color = '#0019ff'

        try:
            if x['kit_id'] in iSCAPE_IDs: color = '#7dbd4c'
            elif x['kit_id'] in making_sense_IDs: color = '#f88027'
            elif x['kit_id'] in SCK_21_IDs: color = '#ffb500'
        except:
            print_exc()
            pass

        return color

    def validate(x):

        if x['last_reading_at'] is None: return False
        if x['added_at'] is None: return False
        if any(x['coordinates']) is None or any(
            [isnan(item) for item in x['coordinates']]):
            return False
        if map_type == 'dynamic':
            if x['date_list'] == []: return False

        return True

    def range_list(x):

        date_r = date_range(start=x['added_at'],
                            end=x['last_reading_at'],
                            normalize=True,
                            freq=options['period']).strftime('%Y-%m-%d')
        date_l = list()
        for item in date_r.values:
            date_l.append(str(item))

        return date_l

    dataframe['color'] = dataframe.apply(lambda x: color(x), axis=1)
    dataframe['coordinates'] = dataframe.apply(lambda x: coordinates(x),
                                               axis=1)

    options = dict_fmerge(config._map_def_opt, options)

    # Make map
    m = Map(
        location=options['location'],
        tiles=options['tiles'],
        zoom_start=options['zoom'],
    )

    if map_type == 'dynamic':
        dataframe['date_list'] = dataframe.apply(lambda x: range_list(x),
                                                 axis=1)
        dataframe['valid'] = dataframe.apply(lambda x: validate(x), axis=1)

        dataframe = dataframe[(dataframe['valid'] == True)]
        features = list()

        for sensor in dataframe.index:
            features.append({
                'type': 'Feature',
                'geometry': {
                    'type':
                    'LineString',
                    'coordinates':
                    [dataframe.loc[sensor, 'coordinates'][::-1]] *
                    len(dataframe.loc[sensor, 'date_list']),
                    'popup':
                    f'<a href="http://smartcitizen.me/kits/{sensor}">{sensor}</a>',
                },
                'properties': {
                    'times': dataframe.loc[sensor, 'date_list'],
                    'icon': options['icon'],
                    'iconstyle': {
                        'fillOpacity': options['fillOpacity'],
                        'fillColor': dataframe.loc[sensor, 'color'],
                        'stroke': options['stroke'],
                        'radius': options['radius']
                    },
                    'style': {
                        'weight': '0'
                    },
                    'id': 'man'
                }
            })

        TimestampedGeoJson(
            {
                'type': 'FeatureCollection',
                'features': features
            },
            period='P' + convert_rollup(options['period']),
            add_last_point=True,
            auto_play=False,
            loop=False,
            max_speed=options['max_speed'],
            loop_button=True,
            # date_options='YYYY/MM/DD',
            time_slider_drag_update=True,
            duration='P' + options['period']).add_to(m)

    elif map_type == 'static':

        dataframe['valid'] = dataframe.apply(lambda x: validate(x), axis=1)
        dataframe = dataframe[(dataframe['valid'] == True)]

        for sensor in dataframe.index:
            Circle(
                location=dataframe.loc[sensor, 'coordinates'],
                radius=options['radius'],
                color=dataframe.loc[sensor, 'color'],
                fill=True,
                fillOpacity=options['fillOpacity'],
                fillColor=dataframe.loc[sensor, 'color'],
                popup=
                f'<a href="http://smartcitizen.me/kits/{sensor}">{sensor}</a>'
            ).add_to(m)

    return m
Beispiel #21
0
        51, 50, 31, 32, 4, 38, 30, 26, 13, 49, 40, 6, 29, 20, 10, 41, 3, 58, 7
    ]
    routes_gj = get_routes_geojson(bus_ids)
    grid_gj = get_grid_geojson(bus_ids, ('weekday', '00:00', '23:59'),
                               stations_source='here')
    m = folium.Map(location=[55.6795535, 12.542231],
                   zoom_start=13,
                   tiles='OpenStreetMap')
    routes = folium.GeoJson(routes_gj,
                            name='routes',
                            style_function=lambda feature: {'color': 'blue'})

    grid = TimestampedGeoJson(grid_gj,
                              period='PT1H',
                              duration='PT1M',
                              date_options='HH:mm',
                              auto_play=False,
                              max_speed=1,
                              loop=False,
                              time_slider_drag_update=True,
                              loop_button=True)

    routes.add_to(m)
    grid.add_to(m)

    folium.features.GeoJsonTooltip(fields=['bus_id', 'name'],
                                   aliases=['id', 'bus']).add_to(routes)
    folium.LayerControl().add_to(m)

    print(m)
    lon = lon_init + (i+1) * 0.01 * random()
    coordinates.append([lon, lat])
    time.append(time_stamp + i * 60000)
print(coordinates)
print(time)

poly_line = TimestampedGeoJson(data={
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": coordinates,
            },
            "properties": {
                "times": time,
                "style": {
                    "color": "red",
                    "weight": 2
                }
            }
        }
    ]
}, period="PT1M")

poly_line.add_to(map)

# Create a tool to measure distance and area
tool_measure = MeasureControl()
tool_measure.add_to(map)
Beispiel #23
0
start.loc[start['count']<1, 'fillColor'] = '#e37222'

def create_geojson_features(df):
    features = []
    for _, row in df.iterrows():
        feature = { 'type': 'Feature', 'geometry': { 'type':'Point', 'coordinates':[row['lon'],row['lat']]},
            'properties': {'time': pd.to_datetime(row['hour'], unit='h').__str__(), 'style': {'color' : ' '}, 'icon': 'circle',
                'iconstyle':{  'color': row['fillColor'], 'fillColor': row['fillColor'], 'fillOpacity': 0.8, 'stroke': 'true',
                 'fill':'true', 'radius': row['count'] + 1 }}}
        features.append(feature)
    return features

start_geojson = create_geojson_features(start)
nyc_map = folium.Map(location = [40.744916, -73.989830], tiles = "CartoDB dark_matter", zoom_start = 12.5)

TimestampedGeoJson(start_geojson, period = 'PT1H', duration = 'PT1M', transition_time = 800, auto_play = True).add_to(nyc_map)
nyc_map.save('LiveMap.html')

nyc1= nyc.reset_index().set_index('stoptime')
end = nyc1.pivot_table('tripduration',  index = ['end station id', 'end station latitude', 'end station longitude', nyc1.index.hour],
                     columns = 'type', aggfunc='count').reset_index()
end['station'] = end['station']/days
end.columns = ['station_id', 'lat', 'lon', 'hour', 'count']
end['fillColor'] = '#e64c4e'
end.loc[end['count']<1, 'fillColor'] = '#586065'
end_geojson = create_geojson_features(end)

df_hour_list = []
hours = pd.Series(nyc.index.hour.unique().sort_values())

def create_list(hour):
Beispiel #24
0
                'type': "MultiPoint",
                'coordinates': [all_truths[id]['lonlats'][time]]
            }
        })

# %%
# The Results
# ~~~~~~~~~~~

from folium.plugins import TimestampedGeoJson, Fullscreen

Fullscreen().add_to(m)

TimestampedGeoJson(data=geo_json,
                   transition_time=200,
                   auto_play=True,
                   add_last_point=False,
                   period='PT10S',
                   duration='PT0S').add_to(m)

# %%

# sphinx_gallery_thumbnail_path = '_static/sphinx_gallery/OpenSky_thumb.png'

m

# %%
# References
# ----------
# .. [#] The OpenSky Network, http://www.opensky-network.org
# .. [#] Bringing up OpenSky: A large-scale ADS-B sensor network for research
#    Matthias Schäfer, Martin Strohmeier, Vincent Lenders, Ivan Martinovic, Matthias Wilhelm
def GenStaticTimemap(Datadic,val,ave,titlename,infos): 
    """
    Generate a static time series make for Date in a Datadirectory, the location come from the Datadic keys
    needed functions Staticsitedatetime, colormap
    Features:basics popups
    
    
    Created:08/06/2019
    #updated for RPI3 use
    """

    
    for k , info in infos.items():
            locs=infos[k]['Location:']
            Lat=float(locs[2])
            Lon=float(locs[3])
            print(Lat,Lon)
   
    #generate base map

    m=folium.Map([Lat,Lon],
             zoom_start=10,
             tiles='OpenStreetMap')
    
    #add colar bard
    if "pm" in val:
        index=[0,5,10,15,20,25,30,40,50]
        colormap(m,index,"Mass concentration ug/m^3")
    styledict=[] #Stlyed dict for the time stamped features to be added to map
    
    #loop through data dic, getting the location and putting the data as geojseon data under style
    for k, item in Datadic.items():
    #    print(item.head(4),loc)
        if val.upper()=="PM2.5" or val.upper()=="PM10" and "SDS" in k.upper():
            val="sds-"+val
        item[val]=item[val].dropna()
        style,df=Staticsitedatetime(item,infos[k],val,m) #Add the time stamed geoJson
        styledict=styledict+style 
    #Read the styledict, puting the time onto the map  
    if ave.upper()=="RAW":
        interval="1T"
    else:
        i=ave.find("T")
        interval=ave[0:i]
       # print(interval)
    TimestampedGeoJson(
                {
                        'type': 'FeatureCollection',
                        'features': styledict,
                        },
          period='PT'+interval+'M'
        , add_last_point=True
        , auto_play=False
        , loop=False
        , max_speed=10
        , loop_button=True
        , time_slider_drag_update=True).add_to(m)
       
    #save map     
    
    m.save(titlename+".html")
    print("完成")
Beispiel #26
0
def generate_location_map(reportfolderbase, legend_title):
    KML_path = os.path.join(reportfolderbase, iLEAPP_KMLs)
    if not os.path.isdir(KML_path) or not os.listdir(KML_path):
        return

    location_path = os.path.join(reportfolderbase, 'LOCATIONS')
    os.makedirs(location_path, exist_ok=True)
    db = sqlite3.connect(os.path.join(KML_path, "_latlong.db"))
    df = pd.read_sql_query(
        "SELECT key as Name, Activity as Description, latitude, longitude FROM data ;",
        db)
    df["Point"] = df.apply(
        lambda row: Point(float(row['longitude']), float(row['latitude']), .0),
        axis=1)

    #sorting is needed for correct display
    df.sort_values(by=['Name'], inplace=True)

    #Parse geo data and add to Folium Map
    data_names = df[~df.Description.str.contains('Photos')].Description.unique(
    )
    featuresProp = {}

    for c, d in zip(colors, data_names):
        descFilter = d
        if 'ZRT' in d:
            fType = 'LineString'
            icon = 'marker'
            iconUrl = defaultIconUrl.format(c)
            shadowUrl = defaultShadowUrl
        else:
            fType = 'MultiPoint'
            icon = 'circle'
            iconUrl = ''
            shadowUrl = ''

        color = c

        featuresProp[d] = {
            'fType': fType,
            'color': c,
            'icon': icon,
            'iconUrl': iconUrl,
            'shadowUrl': defaultShadowUrl,
        }

    location_map = folium.Map([df.iloc[0].Point.y, df.iloc[0].Point.x],
                              prefer_canvas=True,
                              zoom_start=6)
    bounds = (
        df[~df.Description.str.contains('Photos')]['longitude'].min(),
        df[~df.Description.str.contains('Photos')]['latitude'].min(),
        df[~df.Description.str.contains('Photos')]['longitude'].max(),
        df[~df.Description.str.contains('Photos')]['latitude'].max(),
    )
    location_map.fit_bounds([
        (bounds[1], bounds[0]),
        (bounds[3], bounds[2]),
    ])

    tsGeo = TimestampedGeoJson(
        {
            'type': 'FeatureCollection',
            'features':
            [geodfToFeatures(df, f, featuresProp) for f in data_names]
        },
        period="PT1M",
        duration="PT1H",
        loop=False,
        transition_time=50,
        time_slider_drag_update=True,
        add_last_point=True,
        max_speed=200).add_to(location_map)

    #legend
    legend = '\n'.join([
        legend_tag.format(featuresProp[f]['color'], htmlencode(f))
        for f in data_names
    ])
    template = '\n'.join([
        template_part1,
        legend_title_tag.format(htmlencode(legend_title)),
        legend_div.format(legend), template_part2
    ])

    macro = MacroElement()
    macro._template = Template(template)

    location_map.get_root().add_child(macro)

    location_map.save(os.path.join(location_path, "Locations_Map.html"))

    report = ArtifactHtmlReport('Locations Map')
    report.start_artifact_report(location_path, 'Locations Map',
                                 'Map plotting all locations')
    report.write_raw_html(
        open(os.path.join(location_path, "Locations_Map.html")).read())
    report.end_artifact_report()
def GenStaticTimemap(Datadic, val, ave, titlename, infos):
    """
    Generate a static time series make for Date in a Datadirectory, the location come from the Datadic keys
    needed functions Staticsitedatetime, colormap
    Features:basics popups
    
    
    Created:08/06/2019
    #updated for RPI3 use
    """

    print("Datadic")
    print(Datadic)
    print("")
    #Set initital Lat / Lon to center map
    for k, data in Datadic.items():
        print("k")
        print(k)
        Lat = data["lat"].iloc[0]
        Lon = data["lon"].iloc[0]

    print("Lat = ", Lat)
    print("Lon = ", Lon)

    #generate base map

    m = folium.Map([Lat, Lon], zoom_start=15, tiles='OpenStreetMap')

    #add colar bard
    if "pm" in val:
        #index=[0,5,10,15,20,25,30,40,50]
        #index=[0,0.5,1,1.5,2,2.5,3,3.5,4]
        #colorbar_index=[0,1,2,3,4,5,6,7,8]
        colormap(m, colorbar_values, "Mass concentration ug/m^3")
        styledict = [
        ]  #Stlyed dict for the time stamped features to be added to map

    #loop through data dic, getting the location and putting the data as geojseon data under style
    for k, item in Datadic.items():
        print("k = ", k)
        print("item = ", item)
        print("*****")
        print("*****")
        print("*****")
        print("*****")
        #    print(item.head(4),loc)
        if val.upper(
        ) == "PM2.5" or val.upper() == "PM10" and "SDS" in k.upper():
            val = "sds-" + val
            print("val = ", val)
        item = item.dropna()
        style, df = Staticsitedatetime(item, infos[k], val,
                                       m)  #Add the time stamed geoJson
        styledict = styledict + style
    #Read the styledict, puting the time onto the map
    if ave.upper() == "RAW":
        interval = "1T"
    else:
        i = ave.find("T")
        interval = ave[0:i]
        print(interval)

    print("interval = ", interval)
    print("*******")
    interval = "1"

    TimestampedGeoJson({
        'type': 'FeatureCollection',
        'features': styledict,
    },
                       period='PT' + interval + 'M',
                       add_last_point=True,
                       auto_play=False,
                       loop=False,
                       max_speed=10,
                       loop_button=True,
                       time_slider_drag_update=True).add_to(m)

    m.save(titlename + ".html")
    features = [
        {
            'type': 'Feature',
            'geometry': {
                'type': 'LineString',
                'coordinates': [each[1]['longitude'], each[1]['latitude']],
            },
            'properties': {
                'times': each[1]['acq_time'],
                'style': {
                    # 'color': line['color'],
                    # 'weight': line['weight'] if 'weight' in line else 5
                }
            }
        } for each in wildfireFile[0:searchLimit].iterrows()
    ]

    TimestampedGeoJson({
        'type': 'FeatureCollection',
        'features': features,
    },
                       period='PT1M',
                       add_last_point=True).add_to(m)

# WEB BROWSER PART ------
# m.add_child(markerCluster)
MousePosition().add_to(m)
m.save('index.html')
url = 'index.html'
webbrowser.open(url, new=1)  # open in new tab
Beispiel #29
0
def path_plot(self,
              channel=None,
              map_type='dynamic',
              devices='all',
              start_date=None,
              end_date=None,
              options=dict()):
    '''
    Creates a folium map showing a path
    Parameters
    -------
    channel: String
        None
        If None, shows path, otherwise, colored path with channel mapping
    map_type: String
        'dynamic'
        'dynamic' or 'static'. Whether is a dinamic map or not
    devices: list or 'all'
        List of devices to include, or 'all' from self.devices
    channel: String
        The channel to make the map from
    start_date, end_date: String
        Date convertible string
    options: dict()
        Possible keys are (default otherwise)
            location: list
                [41.400818, 2.1825157]
                Center map location
            tiles: (String)
                'Stamen Toner'
                Tiles for the folium.Map
            zoom: (float)
                2.5
                Zoom to start with in folium.Map
            period: 'String'
                '1W'
                Period for 'dynamic' map
            radius: float
                10
                Circle radius for icon
            fillOpacity: float
                1
                (<1) Fill opacity for the icon
            stroke: 'String'
                'false'
                'true' or 'false'. For icon's stroke
            icon: 'String'
                'circle'
                A valid folium.Map icon style
    Returns
    -------
        Folium.Map object
    '''

    # Set defaults
    options = dict_fmerge(config._map_def_opt, options)

    # Make features
    features = []
    if devices == 'all':
        mdev = self.devices
    else:
        mdev = list()
        for device in devices:
            if device in self.devices: mdev.append(device)
            else: std_out(f'Device {device} not found, ignoring', 'WARNING')

    if len(mdev) == 0:
        std_out('Requested devices not in test', 'ERROR')
        return None

    for device in mdev:
        chs = ['GPS_LAT', 'GPS_LONG']
        if channel is not None:
            if channel not in self.devices[str(device)].readings.columns:
                std_out(
                    f'Channel {channel} not in columns: {self.devices[str(device)].readings.columns}',
                    'ERROR')
                return None

            # Get bins
            minmax = False
            if not options['minmax']:
                if all([key not in channel for key in config._channel_bins]):
                    std_out(
                        f'Requested channel {channel} not in config mapped bins {config._channel_bins.keys()}.Using min/max mapping',
                        'WARNING')
                    minmax = True
            else:
                minmax = True

            if minmax:
                bins = linspace(
                    self.devices[str(device)].readings[channel].min(),
                    self.devices[str(device)].readings[channel].max(),
                    config._channel_bin_n)
            else:
                for bname in config._channel_bins.keys():
                    if bname in channel:
                        bins = config._channel_bins[bname]
                        break
            chs.append(channel)

        # Create copy
        dfc = self.devices[str(device)].readings[chs].copy()
        # Resample and cleanup
        # TODO THIS CAN INPUT SOME MADE UP READINGS
        dfc = clean(dfc.resample(options['period']).mean(), 'fill')

        # Make color column
        legend_labels = None
        if channel is not None:
            dfc['COLOR'] = cut(dfc[channel], bins, labels =\
                config._map_colors_palette)

            # Make legend labels
            legend_labels = {}
            for ibin in range(len(bins) - 1):
                legend_labels[f'{round(bins[ibin],2)} : {round(bins[ibin+1],2)}'] =\
                    config._map_colors_palette[ibin]
        else:
            dfc['COLOR'] = config._map_colors_palette[0]

        if start_date is not None:
            dfc = dfc[dfc.index > start_date]
        if end_date is not None:
            dfc = dfc[dfc.index < end_date]

        # Add point for each date
        for date in dfc.index:
            if date == dfc.index[-1]: break
            times = []

            color = str(dfc.loc[date, 'COLOR'])
            if color == 'nan' or isnan(dfc.loc[date, 'GPS_LONG'])\
            or isnan(dfc.loc[date, 'GPS_LAT']):
                std_out(f'Skipping point {date}', 'WARNING')
                continue

            geometry = {
                'type':
                'LineString',
                'coordinates':
                [[dfc.loc[date, 'GPS_LONG'], dfc.loc[date, 'GPS_LAT']],
                 [
                     dfc.loc[date + dfc.index.freq, 'GPS_LONG'],
                     dfc.loc[date + dfc.index.freq, 'GPS_LAT']
                 ]],
            }

            properties = {
                'icon':
                options['icon'],
                'iconstyle': {
                    'fillColor': color,
                    'fillOpacity': options['fillOpacity'],
                    'stroke': options['stroke'],
                    'radius': options['radius']
                },
                'device':
                device,
                'timestamp':
                date.strftime('%Y-%m-%dT%H:%M:%S'),
                "coordinates": [
                    dfc.loc[date + dfc.index.freq, 'GPS_LAT'],
                    dfc.loc[date + dfc.index.freq, 'GPS_LONG']
                ],
                'style': {
                    'color': color,
                    'stroke-width': options['stroke-width'],
                    'fillOpacity': options['fillOpacity']
                }
            }

            # Add reading to tooltip
            if channel is not None:
                properties['channel'] = channel
                properties['value'] = dfc.loc[date, channel]

            if map_type == 'dynamic':
                properties['times'] = [
                    date.strftime('%Y-%m-%dT%H:%M:%S'),
                    (date + dfc.index.freq).strftime('%Y-%m-%dT%H:%M:%S')
                ]

            features.append({
                'type': 'Feature',
                'geometry': geometry,
                'properties': properties
            })

    featurecol = {'type': 'FeatureCollection', 'features': features}

    # Make map
    if options['location'] == 'average':
        avg_long = dfc['GPS_LONG'].mean()
        avg_lat = dfc['GPS_LAT'].mean()
        loc = [avg_lat, avg_long]
    else:
        loc = options['location']

    m = Map(
        location=loc,
        tiles=options['tiles'],
        zoom_start=options['zoom'],
    )

    if map_type == 'static':
        # TODO WORKAROUND UNTIL GEOJSON ACCEPTS MARKERS
        if options['markers']:
            for feature in features:
                Circle(location=[
                    feature['geometry']['coordinates'][0][1],
                    feature['geometry']['coordinates'][0][0]
                ],
                       fill='true',
                       radius=feature['properties']['iconstyle']['radius'],
                       color=feature['properties']['iconstyle']['fillColor'],
                       fill_opacity=feature['properties']['iconstyle']
                       ['fillOpacity']).add_to(m)

        if channel is not None:
            fields = ["device", "channel", "timestamp", "coordinates", "value"]
            aliases = [
                "Device:", "Sensor:", "Timestamp:", "Coordinates:", "Reading:"
            ]
        else:
            fields = ["device", "timestamp", "coordinates"]
            aliases = ["Device:", "Timestamp:", "Coordinates:"]

        popup = GeoJsonPopup(
            fields=fields,
            aliases=aliases,
            localize=True,
            labels=True,
            max_width=800,
        )

        tooltip = GeoJsonTooltip(
            fields=fields,
            aliases=aliases,
            localize=True,
            sticky=True,
            labels=True,
            style="""
                background-color: #F0EFEF;
                border: 1px solid gray;
                border-radius: 1px;
                box-shadow: 2px;
            """,
            max_width=800,
        )

        GeoJson(
            featurecol,
            tooltip=tooltip,
            popup=popup,
            style_function=lambda x: {
                'color': x['properties']['style']['color'],
                'weight': x['properties']['style']['stroke-width'],
                'fillOpacity': x['properties']['style']['fillOpacity']
            },
        ).add_to(m)

    elif map_type == 'dynamic':
        TimestampedGeoJson(featurecol,
                           period='PT' + convert_rollup(options['period']),
                           add_last_point=True,
                           auto_play=False,
                           loop=False,
                           max_speed=options['max_speed'],
                           loop_button=True,
                           time_slider_drag_update=True).add_to(m)

    else:
        std_out(f'Not supported map type {map_type}', 'ERROR')
        return None

    if options['minimap']:
        minimap = MiniMap(toggle_display=True, tile_layer=options['tiles'])
        minimap.add_to(m)

    if options['legend'] and not legend_labels is None:

        templateLoader = FileSystemLoader(searchpath=join(dirname(__file__),\
            'templates'))
        templateEnv = Environment(loader=templateLoader)
        template = templateEnv.get_template("map_legend.html")

        filled_map_legend = template.render(legend_labels=legend_labels)

        map_legend_html = '{% macro html(this, kwargs) %}'+\
            filled_map_legend+\
            '{% endmacro %}'

        legend = element.MacroElement()
        legend._template = element.Template(map_legend_html)

        m.get_root().add_child(legend)

    return m