Example #1
0
    def test_choropleth_geopandas_numeric(self):
        """Test to make sure that Choropleth function does complete the lookup
        between a GeoJSON generated from a GeoDataFrame and data from the GeoDataFrame itself.

        key_on field is dtype = str, while column 0 is dtype = int
        All geometries have matching values (no nan_fill_color allowed)
        """
        self.setup()

        with open(os.path.join(rootpath, 'geo_grid.json')) as f:
            geo_data = json.load(f)

        geo_data_frame = gpd.GeoDataFrame.from_features(geo_data['features'])
        geo_data_frame.crs = {'init': 'epsg:4326'}
        fill_color = 'BuPu'
        key_on = 'feature.id'

        Choropleth(geo_data=geo_data_frame.geometry,
                   data=geo_data_frame.value,
                   key_on=key_on,
                   fill_color=fill_color,
                   fill_opacity=0.543212345,
                   nan_fill_color='a_random_color',
                   nan_fill_opacity=0.123454321).add_to(self.m)

        out = self.m._parent.render()
        out_str = ''.join(out.split())

        assert '"fillColor":"a_random_color","fillOpacity":0.123454321' not in out_str
        assert '"fillOpacity":0.543212345' in out_str
Example #2
0
    def test_choropleth_key_on(self):
        """Test to make sure that Choropleth function doesn't raises
        a ValueError when the 'key_on' field is set to a column that might
        have 0 as a value.
        """
        with open(os.path.join(rootpath, 'geo_grid.json')) as f:
            geo_data = json.load(f)
        data = pd.DataFrame({
            'idx': {
                '0': 0,
                '1': 1,
                '2': 2,
                '3': 3,
                '4': 4,
                '5': 5
            },
            'value': {
                '0': 78.0,
                '1': 39.0,
                '2': 0.0,
                '3': 81.0,
                '4': 42.0,
                '5': 68.0
            }
        })
        fill_color = 'BuPu'
        columns = ['idx', 'value']
        key_on = 'feature.properties.idx'

        Choropleth(geo_data=geo_data,
                   data=data,
                   key_on=key_on,
                   fill_color=fill_color,
                   columns=columns)
Example #3
0
def choroplethMap(filename, kwargs):
    def agg(dfFile):
        data = pd.read_csv(dfFile)
        aggData = data.groupby('State')['Venue'].count()

        return aggData

    foliumMap = folium.Map(**kwargs)

    data2016agg = agg('data/2016Campaign/data2016.csv')
    dataPostagg = agg('data/postElection/dataPost.csv')

    bothAgg = data2016agg.combine(dataPostagg,
                                  lambda x, y: x + y,
                                  fill_value=0)

    with open('data/us-states.json') as f:
        states = json.load(f)

    Choropleth(
        geo_data=states,
        # data=bothAggdf, columns=['State', 'Venue'],
        data=bothAgg,
        key_on='feature.properties.name',
        fill_color='YlGn',
        fill_opacity=.6,
        legend_name='Number of Rallies').add_to(foliumMap)

    folium.LayerControl().add_to(foliumMap)

    foliumMap.save(filename)
Example #4
0
    def choropleth(self, *args, **kwargs):
        """Call the Choropleth class with the same arguments.

        This method may be deleted after a year from now (Nov 2018).
        """
        warnings.warn(
            'The choropleth  method has been deprecated. Instead use the new '
            'Choropleth class, which has the same arguments. See the example '
            'notebook \'GeoJSON_and_choropleth\' for how to do this.',
            FutureWarning)
        from folium.features import Choropleth
        self.add_child(Choropleth(*args, **kwargs))
Example #5
0
    def test_choropleth_features(self):
        """Test to make sure that Choropleth function doesn't allow
        values outside of the domain defined by bins.

        It also tests that all parameters work as expected regarding
        nan and missing values.
        """
        self.setup()

        with open(os.path.join(rootpath, 'us-counties.json')) as f:
            geo_data = json.load(f)
        data = {'1001': -1}
        fill_color = 'BuPu'
        key_on = 'id'

        with pytest.raises(ValueError):
            Choropleth(geo_data=geo_data,
                       data=data,
                       key_on=key_on,
                       fill_color=fill_color,
                       bins=[0, 1, 2, 3]).add_to(self.m)
            self.m._parent.render()

        Choropleth(geo_data=geo_data,
                   data={
                       '1001': 1,
                       '1003': float('nan')
                   },
                   key_on=key_on,
                   fill_color=fill_color,
                   fill_opacity=0.543212345,
                   nan_fill_color='a_random_color',
                   nan_fill_opacity=0.123454321).add_to(self.m)

        out = self.m._parent.render()
        out_str = ''.join(out.split())
        assert '"fillColor":"a_random_color","fillOpacity":0.123454321' in out_str
        assert '"fillOpacity":0.543212345' in out_str
Example #6
0
    def test_choropleth_geopandas_str(self):
        """Test to make sure that Choropleth function does complete the lookup
        between a GeoJSON generated from a GeoDataFrame and data from a DataFrame.

        key_on field and column 0 from data are both strings.
        All geometries have matching values (no nan_fill_color allowed)
        """
        self.setup()

        with open(os.path.join(rootpath, 'geo_grid.json')) as f:
            geo_data = json.load(f)

        geo_data_frame = gpd.GeoDataFrame.from_features(geo_data['features'])
        geo_data_frame.crs = {'init': 'epsg:4326'}
        data = pd.DataFrame({
            'idx': {
                '0': '0',
                '1': '1',
                '2': '2',
                '3': '3',
                '4': '4',
                '5': '5'
            },
            'value': {
                '0': 78.0,
                '1': 39.0,
                '2': 0.0,
                '3': 81.0,
                '4': 42.0,
                '5': 68.0
            }
        })
        fill_color = 'BuPu'
        columns = ['idx', 'value']
        key_on = 'feature.id'

        Choropleth(geo_data=geo_data_frame.geometry,
                   data=data,
                   key_on=key_on,
                   columns=columns,
                   fill_color=fill_color,
                   fill_opacity=0.543212345,
                   nan_fill_color='a_random_color',
                   nan_fill_opacity=0.123454321).add_to(self.m)

        out = self.m._parent.render()
        out_str = ''.join(out.split())

        assert '"fillColor":"a_random_color","fillOpacity":0.123454321' not in out_str
        assert '"fillOpacity":0.543212345' in out_str
Example #7
0
    def test_topo_json_smooth_factor(self):
        """Test topojson smooth factor method."""
        self.m = folium.Map([43, -100], zoom_start=4)

        # Adding TopoJSON as additional layer.
        with open(os.path.join(rootpath, 'or_counties_topo.json')) as f:
            choropleth = Choropleth(f, topojson='objects.or_counties_geo',
                                    smooth_factor=0.5).add_to(self.m)

        out = self.m._parent.render()

        # Verify TopoJson
        topo_json = choropleth.geojson
        topojson_str = topo_json._template.module.script(topo_json)
        assert ''.join(topojson_str.split())[:-1] in ''.join(out.split())
Example #8
0
    './', 'measles_disease_data.csv')
measles_disease_datam = pd.read_csv(measles_disease_path)


# probably start loop here !!!!!!!!!!!!!!!!!!!!!!
for x in range(2011, 2020):
    year = "" + x.__str__()
    # print(year)
    measles_disease_bins = list(
        measles_disease_datam[year].quantile([0, 0.25, 0.5, 0.75, 1]))
    Choropleth(
        geo_data=worldmap,
        name=year,
        data=measles_disease_datam,
        columns=['ISO3', year],
        key_on='feature.id',
        fill_color='YlGn',
        fill_opacity=0.7,
        line_opacity=0.2,
        # legend_name='Measle Occurence \# of people',
        bins=measles_disease_bins
    ).add_to(m.m2)


measles_vaccine_path = os.path.join('./', 'measles_vaccine_data.csv')
measles_vaccine_datam = pd.read_csv(
    measles_vaccine_path, encoding='iso-8859-1')


for x in range(1980, 2018):
    year = "" + x.__str__()
    measles_vaccine_bins = list(