diff --git a/cenpy/products.py b/cenpy/products.py index dc928b0..25a5fc8 100644 --- a/cenpy/products.py +++ b/cenpy/products.py @@ -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, @@ -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 @@ -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__() @@ -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)) diff --git a/cenpy/remote.py b/cenpy/remote.py index 4371735..046fa5c 100644 --- a/cenpy/remote.py +++ b/cenpy/remote.py @@ -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 diff --git a/cenpy/tests/test_functional.py b/cenpy/tests/test_functional.py index d1ae661..9ec7dba 100644 --- a/cenpy/tests/test_functional.py +++ b/cenpy/tests/test_functional.py @@ -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: diff --git a/cenpy/tests/test_functional_products.py b/cenpy/tests/test_functional_products.py index 5297b84..2c562bb 100644 --- a/cenpy/tests/test_functional_products.py +++ b/cenpy/tests/test_functional_products.py @@ -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: @@ -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]: