def tiger_url(year, summary_level, stusab=None): """ :param year: Vintage year :param summary_level: Summary level, in number format :param stusab: US State abbreviation :return: """ from geoid.censusnames import stusab as _stusab_map from geoid.core import names as _sl_names sl_name_map = { v:k for k,v in _sl_names.items() } stusab_map = { v:k for k,v in _stusab_map.items()} state = stusab_map.get(stusab.upper()) try: sl = sl_name_map[int(summary_level)].upper() except ValueError: sl_u = summary_level.upper() # ftp://ftp2.census.gov/geo/tiger/TIGER2016/TRACT/tl_2016_15_tract.zip base = f'shape+ftp://ftp2.census.gov/geo/tiger/TIGER{year}/{sl.upper()}' if sl=='COUNTY': return base+f'/tl_{year}_us_{sl.lower()}.zip' else: return base+f'/tl_{year}_{state:02}_{sl.lower()}.zip'
def sub_geoids(v): """Replace state abbreviations and state fips codes with state and national geoids""" from geoid.censusnames import stusab from geoid.acs import Us, State, AcsGeoid if len(v) == 2: v = v.upper() stmap = {v: k for k, v in stusab.items()} if v == 'US': return str(Us()) if v in stmap: return str(State(stmap[v])) # Maybe it is a state number try: v = int(v) return str(State(v)) except ValueError: pass return str(AcsGeoid.parse(v))
def states(self): """Return a dict of state names, numbers and abbreviations""" from geoid.core import names from geoid.censusnames import geo_names, stusab states = {} for state_no, stusab in stusab.items(): states[stusab] = { 'name': geo_names[(state_no, 0)], 'stusab': stusab, 'number': state_no } states['US'] = {'name': 'United States', 'stusab': 'US', 'number': 0} return states
def test_dump_states(self): import yaml, requests from geoid.censusnames import stusab r = requests.get('https://raw.githubusercontent.com/jasonong/List-of-US-States/master/states.yml') rows = [] states = {v:k for k, v in stusab.items()} for k, v in yaml.load(r.content).items(): try: rows.append((states[v['abbreviation']], v['abbreviation'], v['name'] )) except KeyError: print(k, v) print(rows)
def sub_geoids(v): """Replace state abbreviations with state and national geoids""" from geoid.censusnames import stusab from geoid.acs import Us, State if len(v) != 2: return v v = v.upper() stmap = {v: k for k, v in stusab.items()} if v == 'US': return str(Us()) if v not in stmap: return v return str(State(stmap[v]))
def tiger_url(year, summary_level, stusab=None): """ Construct a URL into the FTP server for census geometries Years 2020 and later, for summary level block, will return the 2020 blocks. Earler years will return 2010 blocks. :param year: Vintage year :param summary_level: Summary level, in number format :param stusab: US State abbreviation :return: """ from geoid.censusnames import stusab as _stusab_map from geoid.core import names as _sl_names sl_name_map = { v:k for k,v in _sl_names.items() } stusab_map = { v:k for k,v in _stusab_map.items()} state = stusab_map.get(stusab.upper()) try: # If it is an int, convert to the name sl = sl_name_map[int(summary_level)].upper() except ValueError: sl = summary_level # Example: # ftp://ftp2.census.gov/geo/tiger/TIGER2016/TRACT/tl_2016_15_tract.zip slu = sl.upper() sll = sl.lower() if slu == 'BLOCKGROUP': slu = 'BG' sll = 'bg' elif slu == 'BLOCK': if year < 2020: slu = 'TABBLOCK' sll = 'tabblock10' else: slu = 'TABBLOCK20' sll = 'tabblock20' elif slu == 'ZCTA': slu = 'ZCTA5' sll = 'zcta510' elif slu == 'PUMA': sll = 'puma10' base = f'shape+ftp://ftp2.census.gov/geo/tiger/TIGER{year}/{slu}' if sl in ('COUNTY', 'CBSA', 'CSA','STATE', 'ZCTA'): return base+f'/tl_{year}_us_{sll}.zip' else: assert state is not None, (year, sl) return base+f'/tl_{year}_{state:02}_{sll}.zip'