Skip to content

States

State

Source code in src\backend\us\states.py
class State:
    abbr: str
    ap_abbr: Optional[str]
    capital: Optional[str]
    capital_tz: Optional[str]
    fips: Optional[str]
    is_territory: bool
    is_obsolete: bool
    is_contiguous: bool
    is_continental: bool
    name: str
    name_metaphone: str
    statehood_year: Optional[int]
    time_zones: List[str]

    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)

    def __repr__(self) -> str:
        return f"<State:{self.name}>"

    def __str__(self) -> str:
        return self.name

    def shapefile_urls(self) -> Optional[Dict[str, str]]:
        """Shapefiles are available directly from the US Census Bureau:
        https://www.census.gov/cgi-bin/geo/shapefiles/index.php
        """

        fips = self.fips

        if not fips:
            return None

        base = "https://www2.census.gov/geo/tiger/TIGER2010/"
        urls = {
            "tract": urljoin(base, f"TRACT/2010/tl_2010_{fips}_tract10.zip"),
            "cd": urljoin(base, f"CD/111/tl_2010_{fips}_cd111.zip"),
            "county": urljoin(base, f"COUNTY/2010/tl_2010_{fips}_county10.zip"),
            "state": urljoin(base, f"STATE/2010/tl_2010_{fips}_state10.zip"),
            "zcta": urljoin(base, f"ZCTA5/2010/tl_2010_{fips}_zcta510.zip"),
            "block": urljoin(base, f"TABBLOCK/2010/tl_2010_{fips}_tabblock10.zip"),
            "blockgroup": urljoin(base, f"BG/2010/tl_2010_{fips}_bg10.zip"),
        }

        return urls

shapefile_urls()

Shapefiles are available directly from the US Census Bureau: https://www.census.gov/cgi-bin/geo/shapefiles/index.php

Source code in src\backend\us\states.py
def shapefile_urls(self) -> Optional[Dict[str, str]]:
    """Shapefiles are available directly from the US Census Bureau:
    https://www.census.gov/cgi-bin/geo/shapefiles/index.php
    """

    fips = self.fips

    if not fips:
        return None

    base = "https://www2.census.gov/geo/tiger/TIGER2010/"
    urls = {
        "tract": urljoin(base, f"TRACT/2010/tl_2010_{fips}_tract10.zip"),
        "cd": urljoin(base, f"CD/111/tl_2010_{fips}_cd111.zip"),
        "county": urljoin(base, f"COUNTY/2010/tl_2010_{fips}_county10.zip"),
        "state": urljoin(base, f"STATE/2010/tl_2010_{fips}_state10.zip"),
        "zcta": urljoin(base, f"ZCTA5/2010/tl_2010_{fips}_zcta510.zip"),
        "block": urljoin(base, f"TABBLOCK/2010/tl_2010_{fips}_tabblock10.zip"),
        "blockgroup": urljoin(base, f"BG/2010/tl_2010_{fips}_bg10.zip"),
    }

    return urls

lookup(val, field=None, use_cache=True)

State lookup. This method will make a best effort attempt at finding the state based on the lookup value provided.

  • two digits will search for FIPS code
  • two letters will search for state abbreviation

Exact matches can be done on any attribute on State objects by passing the field argument. This does an exact, case-sensitive comparison against the specified field.

This method caches non-None results, but can the cache can be bypassed with the use_cache=False argument.

Source code in src\backend\us\states.py
def lookup(val, field: Optional[str] = None, use_cache: bool = True) -> Optional[State]:
    """State lookup. This method will make a best effort
    attempt at finding the state based on the lookup value provided.

      * two digits will search for FIPS code
      * two letters will search for state abbreviation

    Exact matches can be done on any attribute on State objects by passing
    the `field` argument. This does an exact, case-sensitive comparison against
    the specified field.

    This method caches non-None results, but can the cache can be bypassed
    with the `use_cache=False` argument.
    """

    matched_state = None

    if field is None:
        if FIPS_RE.match(val):
            field = "fips"
        elif ABBR_RE.match(val):
            val = val.upper()
            field = "abbr"
        else:
            val = val.title()
            field = "name"

    # see if result is in cache
    cache_key = f"{field}:{val}"
    if use_cache and cache_key in _lookup_cache:
        matched_state = _lookup_cache[cache_key]

    for state in STATES_AND_TERRITORIES:
        if val == getattr(state, field):
            matched_state = state
            if use_cache:
                _lookup_cache[cache_key] = state

    return matched_state