Exemplo n.º 1
0
def test_map_colorado_like_readme():

    # Read congresisonal district data
    # Median household income estimate is "DP03_0062E", https://api.census.gov/data/2018/acs/acs1/profile/groups/DP03.html
    # https://api.census.gov/data/2018/acs/acs1/profile?get=group(DP03),NAME&for=congressional%20district:*&in=state:08
    data = census.load_json_file(
        data_dir / "DP03_08_cd.json", headers=["NAME", "GEO_ID", "DP03_0062E"]
    )

    # .shp GEOID uses last 4 digits
    data["GEOID"] = [geoid[-4:] for geoid in data["GEO_ID"]]

    # Create column of floats (to color by)
    data["Median Household Income"] = [float(x) for x in data["DP03_0062E"]]

    # Create column of nicely formatted strings (to display)
    data["Median Household Income (pretty format)"] = [
        "${:,.2f}".format(x) for x in data["Median Household Income"]
    ]

    # Make and save GIS GeoJson map object
    # Shapefile from: https://www.census.gov/cgi-bin/geo/shapefiles/index.php
    # Search for "Congressional Districts"
    gis.make_map(
        data_dir / "tl_2019_08_cd116",
        data,
        join_on="GEOID",
        color_by="Median Household Income",
        save_to=Path.cwd() / "user/map.html",
    )
Exemplo n.º 2
0
def test_map_nc():

    # Read race data per county subdivision
    # https://api.census.gov/data/2010/dec/sf1?get=group(H6),NAME&for=county:*&in=state:37
    data = census.load_json_file(
        data_dir / "nc_race_by_county.json",
        headers=[
            "NAME",
            "GEO_ID",
            "H006001",
            "H006002",
            "H006003",
            "H006004",
            "H006005",
            "H006006",
            "H006007",
            "H006008",
        ],
    )

    # .shp GEOID uses last 5 digits
    data["GEOID"] = [geoid[-5:] for geoid in data["GEO_ID"]]

    # Percentage of white people out of the total respondants (to color by)
    data["% White"] = [
        float(w) / float(t) if not t == 0 else 100
        for w, t in zip(data["H006002"], data["H006001"])
    ]

    # Percentage of white people (formatted nicely for display)
    data["% White (pretty format)"] = [
        f"{round(x * 100, 1)}%" for x in data["% White"]
    ]

    aliases = {
        "NAME": "Name",
        "GEO_ID": "GEOID",
        "H006001": "Total",
        "H006002": "White",
        "H006003": "Black",
        "H006004": "Native American",
        "H006005": "Asian",
        "H006006": "Pasific Islander",
        "H006007": "Other",
        "H006008": "Two or More",
        "% White (pretty format)": "Percent White",
    }

    # Make and save GIS GeoJson map object
    # Shapefile from: https://www.census.gov/cgi-bin/geo/shapefiles/index.php
    # Search for "County,"" then select "North Carolina"
    gis.make_map(
        data_dir / "tl_2019_37_county",
        data,
        join_on="GEOID",
        color_by="% White",
        include=aliases,
        save_to=Path.cwd() / "user/map.html",
    )
Exemplo n.º 3
0
def test_make_map_joins_properly(tmp_path):
    data = standard_data

    data_map = gis.make_map(
        shapefile_path,
        data,
        join_on="name",
    )

    geojson = data_map.data
    assert geojson["type"] == "FeatureCollection"

    features = geojson["features"]
    assert len(features) == 36

    feature0_props = features[0]["properties"]
    assert feature0_props["name"] == "Bay Colony, Port Royale"
    assert feature0_props["Demographic 1"] == "bay col dem1"
    assert feature0_props["Demographic 2"] == "bay col dem2"

    feature1_props = features[1]["properties"]
    assert feature1_props["name"] == "NE 48th"
    assert feature1_props["Demographic 1"] == "NE 48th dem1"
    assert feature1_props["Demographic 2"] == "NE 48th dem2"

    feature2_props = features[2]["properties"]
    assert feature2_props["name"] == "Linden Pointe Apartments"
    assert feature2_props["Demographic 1"] is None
    assert feature2_props["Demographic 2"] is None

    data_map.add_to(m)

    save_path = (tmp_path/"temp.html").resolve() #tmp_path will unlink automatically
    m.save(str(save_path))
Exemplo n.º 4
0
def test_make_map_joins_properly():
    data = standard_data

    data_map = gis.make_map(
        shapefile_path,
        data,
        join_on="name",
    )

    geojson = data_map.data
    assert geojson["type"] == "FeatureCollection"

    features = geojson["features"]
    assert len(features) == 36

    feature0_props = features[0]["properties"]
    assert feature0_props["name"] == "Bay Colony, Port Royale"
    assert feature0_props["Demographic 1"] == "bay col dem1"
    assert feature0_props["Demographic 2"] == "bay col dem2"

    feature1_props = features[1]["properties"]
    assert feature1_props["name"] == "NE 48th"
    assert feature1_props["Demographic 1"] == "NE 48th dem1"
    assert feature1_props["Demographic 2"] == "NE 48th dem2"

    feature2_props = features[2]["properties"]
    assert feature2_props["name"] == "Linden Pointe Apartments"
    assert feature2_props["Demographic 1"] is None
    assert feature2_props["Demographic 2"] is None

    data_map.add_to(m)

    _, save_path = tempfile.mkstemp(suffix=".html")
    m.save(save_path)
    Path(save_path).unlink()
Exemplo n.º 5
0
def test_make_map_tooltip_without_alias():
    data = standard_data

    data_map = gis.make_map(shapefile_path, data, join_on="name")

    children = data_map._children
    tooltip = children.popitem(last=False)[1]  # Get first tooltip

    assert tooltip.fields == ["Demographic 1", "Demographic 2"]
    assert tooltip.aliases == ["Demographic 1", "Demographic 2"]
Exemplo n.º 6
0
def test_make_map_tooltip_with_field_include():
    data = standard_data

    fields = list(data.keys())

    data_map = gis.make_map(shapefile_path, data, join_on="name", include=fields)

    children = data_map._children
    tooltip = children.popitem(last=False)[1]  # Get first tooltip

    assert tooltip.fields == ["name", "Demographic 1", "Demographic 2"]
    assert tooltip.aliases == ["name", "Demographic 1", "Demographic 2"]
Exemplo n.º 7
0
def test_make_map_tooltip_with_alias_include():
    data = standard_data

    aliases = {
        "name": "Name",
        "Demographic 1": "Population",
    }

    data_map = gis.make_map(shapefile_path, data, join_on="name", include=aliases)

    children = data_map._children
    tooltip = children.popitem(last=False)[1]  # Get first tooltip

    assert tooltip.fields == ["name", "Demographic 1"]
    assert tooltip.aliases == ["Name", "Population"]
Exemplo n.º 8
0
# Percentage of hispanic or latino origin people out of the total respondants (to color by)
data["% Hispanic or Latino Origin"] = [
    float(his) / float(tot) * 100 if not tot == 0 else None
    for tot, his in zip(data["B03003_001E"], data["B03003_003E"])
]

# Formatted nicely for display
data["pretty format"] = [f"{round(x, 1)}%" for x in data["% Hispanic or Latino Origin"]]

# What to include
aliases = {
    "NAME": "Name",
    "GEOID": "GEOID",
    "B03003_001E": "Total",
    "B03003_003E": "Hispanic or Latino Origin",
    "B03003_002E": "Not Hispanic or Latino Origin",
    "pretty format": "% Hispanic or Latino Origin",
}

# Make the map!
gis.make_map(
    shapefile_dir,
    data,
    join_on="GEOID",
    color_by="% Hispanic or Latino Origin",
    include=aliases,
    save_to=here / "tx-map.html",
    trim=True,
)
Exemplo n.º 9
0
    if not re.match(r"B02001_00[2-9]E", census_code):
        continue

    label = f"% {description}"

    valid_data[label] = [
        float(race) / float(tot) * 100 if not tot == "0" else None for tot,
        race in zip(valid_data["B02001_001E"], valid_data[census_code])
    ]
    percentage_labels.append(label)

# Log the percentage label so it shows up nicely in the tooltip
variables.update({label: label for label in percentage_labels})

# Make all the maps!
for census_code, description in variables.items():

    if not re.match(r"B02001_00[2-9]E", census_code):
        continue

    gis.make_map(
        shapefile_dir,
        valid_data,
        join_on="ZCTA5CE10",
        include=variables,
        color_by=f"% {description}",
        save_to=here / f"tx-zcta-map-{census_code}.html",
        trim=True,  # We definitely don't want all us zip codes on this map
    )
Exemplo n.º 10
0
def test_make_map_exception_for_value_not_list():
    data = standard_data.copy()
    data["Demographic 1"] = "I am not a list"

    with pytest.raises(ValueError):
        gis.make_map(shapefile_path, data, join_on="name")
Exemplo n.º 11
0
def test_make_map_exception_for_mismatched_data_size():
    data = standard_data.copy()
    data["name"] = data["name"] + ["extra element"]

    with pytest.raises(ValueError):
        gis.make_map(shapefile_path, data, join_on="name")
Exemplo n.º 12
0
def test_make_map_exception_for_bad_join_key():
    data = standard_data

    with pytest.raises(KeyError):
        gis.make_map(shapefile_path, data, join_on="Name")  # note caps
Exemplo n.º 13
0
#
# To make it possible to join the census data with the shapefile, we need to make
# a column that matches between the census data and the shapefile properties. For this
# example, we can simply combine the census data state, county, and tract
data["GEOID"] = [
    s + c + t for s, c, t in zip(data["state"], data["county"], data["tract"])
]

# Right now the median household income is stored as a string (e.g. "11456.84"). This
# is great for viewing, but makes it hard to do any numerical calculations with.
# Here we will create column of "Median Household Income" but as a float (a number,
# e.g. 11456.84). This will be used to generate the colormap.
data["Median Household Income"] = [float(x) for x in data["DP03_0062E"]]

# Just to make the map prettier, it would be nice to format the median household
# income with a "$" and commas between every third number (e.g. "$11,456.84").
# This will be shown in the map's tooltip.
data["Median Household Income (pretty format)"] = [
    "${:,.2f}".format(x) for x in data["Median Household Income"]
]

# Make and save GIS GeoJson map object of "Congressional Districts"
# Shapefile from: https://www.census.gov/cgi-bin/geo/shapefiles/index.php
gis.make_map(
    shapefile_path,
    data,
    join_on="GEOID",
    color_by="Median Household Income",
    save_to=here / "co-map.html",
)