Exemple #1
0
    def __init__(self,
                 locations,
                 color=None,
                 weight=None,
                 opacity=None,
                 latlon=True,
                 popup=None):
        super(PolyLine, self).__init__()
        self._name = 'PolyLine'
        self.data = (_locations_mirror(locations)
                     if not latlon else _locations_tolist(locations))
        self.color = color
        self.weight = weight
        self.opacity = opacity
        if isinstance(popup, text_type) or isinstance(popup, binary_type):
            self.add_child(Popup(popup))
        elif popup is not None:
            self.add_child(popup)

        self._template = Template(u"""
            {% macro script(this, kwargs) %}
                var {{this.get_name()}} = L.polyline(
                    {{this.data}},
                    {
                        {% if this.color != None %}color: '{{ this.color }}',{% endif %}
                        {% if this.weight != None %}weight: {{ this.weight }},{% endif %}
                        {% if this.opacity != None %}opacity: {{ this.opacity }},{% endif %}
                        });
                {{this._parent.get_name()}}.addLayer({{this.get_name()}});
            {% endmacro %}
            """)  # noqa
Exemple #2
0
    def __init__(self, locations, color=None, weight=None,
                 opacity=None, latlon=True, popup=None):
        super(MultiPolyLine, self).__init__()
        self._name = 'MultiPolyLine'
        self.data = (_locations_mirror(locations) if not latlon else
                     _locations_tolist(locations))
        self.color = color
        self.weight = weight
        self.opacity = opacity
        if isinstance(popup, text_type) or isinstance(popup, binary_type):
            self.add_child(Popup(popup))
        elif popup is not None:
            self.add_child(popup)

        self._template = Template(u"""
            {% macro script(this, kwargs) %}
                var {{this.get_name()}} = L.multiPolyline(
                    {{this.data}},
                    {
                        {% if this.color != None %}color: '{{ this.color }}',{% endif %}
                        {% if this.weight != None %}weight: {{ this.weight }},{% endif %}
                        {% if this.opacity != None %}opacity: {{ this.opacity }},{% endif %}
                        });
                {{this._parent.get_name()}}.addLayer({{this.get_name()}});
            {% endmacro %}
            """)  # noqa
Exemple #3
0
    def __init__(self, locations, color='black', weight=1, fill_color='black',
                 fill_opacity=0.6, popup=None, latlon=True):
        """
        Creates a PolygonMarker object for plotting on a Map.

        Parameters
        ----------
        locations: tuple or list, default None
            Latitude and Longitude of Polygon
        color: string, default ('black')
            Edge color of a polygon.
        weight: float, default (1)
            Edge line width of a polygon.
        fill_color: string, default ('black')
            Fill color of a polygon.
        fill_opacity: float, default (0.6)
            Fill opacity of a polygon.
        popup: string or folium.Popup, default None
            Input text or visualization for object.

        Returns
        -------
        folium.features.Polygon object

        Examples
        --------
        >>> locations = [[35.6762, 139.7795],
        ...              [35.6718, 139.7831],
        ...              [35.6767, 139.7868],
        ...              [35.6795, 139.7824],
        ...              [35.6787, 139.7791]]
        >>> Polygon(locations, color='blue', weight=10, fill_color='red',
        ...         fill_opacity=0.5, popup='Tokyo, Japan'))

        """
        super(PolygonMarker, self).__init__((
            _locations_mirror(locations) if not latlon else
            _locations_tolist(locations)), popup=popup
        )
        self._name = 'PolygonMarker'
        self.color = color
        self.weight = weight
        self.fill_color = fill_color
        self.fill_opacity = fill_opacity
        self._template = Template(u"""
            {% macro script(this, kwargs) %}
            var {{this.get_name()}} = L.polygon({{this.location}},
                {
                    color: '{{ this.color }}',
                    fillColor: '{{ this.fill_color }}',
                    fillOpacity: {{ this.fill_opacity }},
                    weight: {{ this.weight }}
                }).addTo({{this._parent.get_name()}});
            {% endmacro %}
            """)
Exemple #4
0
    def __init__(self, locations, color='black', weight=1, fill_color='black',
                 fill_opacity=0.6, popup=None, latlon=True):
        """
        Creates a PolygonMarker object for plotting on a Map.

        Parameters
        ----------
        locations: tuple or list, default None
            Latitude and Longitude of Polygon
        color: string, default ('black')
            Edge color of a polygon.
        weight: float, default (1)
            Edge line width of a polygon.
        fill_color: string, default ('black')
            Fill color of a polygon.
        fill_opacity: float, default (0.6)
            Fill opacity of a polygon.
        popup: string or folium.Popup, default None
            Input text or visualization for object.

        Returns
        -------
        folium.features.Polygon object

        Examples
        --------
        >>> locations = [[35.6762, 139.7795],
        ...              [35.6718, 139.7831],
        ...              [35.6767, 139.7868],
        ...              [35.6795, 139.7824],
        ...              [35.6787, 139.7791]]
        >>> Polygon(locations, color='blue', weight=10, fill_color='red',
        ...         fill_opacity=0.5, popup='Tokyo, Japan'))

        """
        super(PolygonMarker, self).__init__((
            _locations_mirror(locations) if not latlon else
            _locations_tolist(locations)), popup=popup
        )
        self._name = 'PolygonMarker'
        self.color = color
        self.weight = weight
        self.fill_color = fill_color
        self.fill_opacity = fill_opacity
        self._template = Template(u"""
            {% macro script(this, kwargs) %}
            var {{this.get_name()}} = L.polygon({{this.location}},
                {
                    color: '{{ this.color }}',
                    fillColor: '{{ this.fill_color }}',
                    fillOpacity: {{ this.fill_opacity }},
                    weight: {{ this.weight }}
                }).addTo({{this._parent.get_name()}});
            {% endmacro %}
            """)
def create_map(results, cities, statename, time):
    
    ## Gets the geoJSON for state and cities calling functions
    transformCities(cities)
    transformStates(statename)
    
    ## Creates the pup-up graph for the city results using vincent package
    ## looping over the results dictionary
    for cityid, df in results.iteritems():
        line = vincent.Line(df[['@realDonaldTrump', '@HillaryClinton']])
        line.axis_titles(x='date', y='normalized weighted composite score')
        line.legend(title='Queries')
        line.width=400
        line.height=200
        line.axes[0].properties = AxisProperties(
        labels=PropertySet(angle=ValueRef(value=45),
                           align=ValueRef(value='left')))
        line.colors(brew='Set1')
        line.to_json('data/'+cityid+time+'.json')

    ## Creates the map outline
    m = folium.Map([34.569728, -106.119447], tiles="Mapbox Bright", zoom_start=5, min_zoom=5)
    fg = folium.map.FeatureGroup().add_to(m)
    
    ## Adds the states looping over the transformed GeoJSON file
    ## and sets colour based on time string 'after' for states of
    ## California and Texas
    geo_json_states = json.load(open('data/us_states/us_states.json'))
    for feature in geo_json_states['features']:
        if time == "after" and feature['properties']['NAME'] == 'California':
            fg.add_child(MultiPolygon(_locations_mirror(feature['geometry']['coordinates']),
                                   color='blue', weight=0))
        elif time == "after" and feature['properties']['NAME'] == 'Texas':
            fg.add_child(MultiPolygon(_locations_mirror(feature['geometry']['coordinates']),
                                   color='red', weight=0))
        else:
            fg.add_child(MultiPolygon(_locations_mirror(feature['geometry']['coordinates']),
                                   color='grey', weight=0))
    
    ## Add the cities layer looping over the transformed GeoJSON file
    geo_json_cities = json.load(open('data/us_cities/us_cities.json'))
    for feature in geo_json_cities['features']:
        
        ## Sets colour variable based on most positive nwcs in the analysis
        ## timeframe
        places = open('data/' + cities, 'r')
        for line in places:
            attr = line.split(";")
            if attr[0] == feature['properties']['NAME10']:
                city_id = attr[1]
        
        if sum(results[city_id]['@realDonaldTrump']) > sum(results[city_id]['@HillaryClinton']):
            colour = 'red'
        else:
            colour = 'blue'
        
        ## Adds the polygon with the set colour
        fg.add_child(MultiPolygon(
        _locations_mirror(feature['geometry']['coordinates']),
        color=colour,
        weight=0,
        popup = folium.Popup(max_width=650).add_child(folium.Vega(json.load(open('data/'+city_id+time+'.json')),
                                                                  width=620, height=270)),))
    # Saves the map in html format using the string time in filename                                                            
    m.save('sentiment_'+time+'.html')