Skip to content

Add Blockgroups level fix #73 #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions cenpy/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,23 @@ def _preprocess_variables(self, columns):
@property
def _layer_lookup(self):
"""
The lookup table relating the layers in the WMS service and the levels
supported by this API product.
Create lookup table to translate cenpy levels ('county', 'block',
'tract') to layer id. Dynamically create to handle year to year id
changes (naming changes will break)
"""
pass
supported_levels = {
'Counties': 'county',
'Census Blocks': 'block',
'Census Block Groups': 'blockgroup',
'Census Tracts': 'tract',
}

return {
supported_levels[l._name]: l._id
for l in self._api.mapservice.layers if
l._name in supported_levels.keys()
}

@_layer_lookup.getter
def _layer_lookup(self):
raise NotImplementedError(
"This must be implemented on children " "of this class!"
)

def from_place(
self,
Expand Down Expand Up @@ -285,6 +292,10 @@ def chunked_query(elements_in_chunk):
geo_unit = "block:*"
geo_filter["tract"] = ",".join(elements_in_chunk)
geo_filter["county"] = county
elif level == "blockgroup":
geo_unit = "block group:*"
geo_filter["tract"] = ",".join(elements_in_chunk)
geo_filter["county"] = county
elif level == "tract":
geo_unit = "tract:{}".format(",".join(elements_in_chunk))
geo_filter["county"] = county
Expand Down Expand Up @@ -480,7 +491,8 @@ def check_match(
class Decennial2010(_Product):
"""The 2010 Decennial Census from the Census Bureau"""

_layer_lookup = {"county": 100, "tract": 14, "block": 18}
#_layer_lookup = {"county": 100, "tract": 14, "blockgroup": 16, "block": 18}
#2020: {"county": 82, "tract": 6, "blockgroup": 8, "block": 10}

def __init__(self):
super(Decennial2010, self).__init__()
Expand Down Expand Up @@ -673,17 +685,23 @@ def crosstab_tables(self):
class ACS(_Product):
"""The American Community Survey (5-year vintages) from the Census Bueau"""

_layer_lookup = {"county": 84, "tract": 8}

#_layer_lookup = {
# 2017: {'county': 84, 'tract': 8, "blockgroup": 10},
# 2018: {'county': 86, 'tract': 8, "blockgroup": 10},
# 2019: {'county': 86, 'tract': 8, "blockgroup": 10},
# 2020: {'county': 78, 'tract': 6, "blockgroup": 8}
#}

def __init__(self, year="latest"):
self._cache = dict()
if year == "latest":
year = 2019
if year not in list(range(2017,2020)):
year = 2021
if year not in list(range(2017, 2022)):
raise NotImplementedError(
"The requested year ({}) is too early/late. "
"Only 2017, 2018, or 2019 are supported.".format(year)
"Only 2017, 2018, 2019, 2020 or 2021 are supported.".format(year)
)
self._year = year
self._api = APIConnection("ACSDT{}Y{}".format(5, year))
self._api.set_mapservice("tigerWMS_ACS{}".format(year))

Expand Down
6 changes: 5 additions & 1 deletion cenpy/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,9 @@ def set_mapservice(self, key):
if isinstance(key, tig.TigerConnection):
self.mapservice = key
elif isinstance(key, str):
self.mapservice = tig.TigerConnection(name=key)
if key == 'tigerWMS_ACS2020':
# No unique layer use census 2020 geographies
self.mapservice = tig.TigerConnection(name='tigerWMS_Census2020')
else:
self.mapservice = tig.TigerConnection(name=key)
return self
2 changes: 1 addition & 1 deletion cenpy/tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def test_all():

# In[30]:

conn2 = c.remote.APIConnection("CBP2012")
conn2 = c.remote.APIConnection("CBP2011")

# Alright, let's look at the available columns:

Expand Down
8 changes: 8 additions & 0 deletions cenpy/tests/test_functional_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def test_all():
# In[6]:

la = dectest.from_county("Los Angeles, CA", level="tract", variables=["^P004"])

# Test block group
la_bg = dectest.from_county("Los Angeles, CA", level="blockgroup", variables=["^P004"])


# And, making a pretty plot of the Hispanic population in LA:

Expand Down Expand Up @@ -144,6 +148,10 @@ def test_all():
sf = products.ACS(2017).from_place(
"San Francisco, CA", level="tract", variables=["B00002*", "B01002H_001E"]
)

sf_bg = products.ACS(2017).from_place(
"San Francisco, CA", level="blockgroup", variables=["B00002*", "B01002H_001E"]
)

# In[19]:

Expand Down