TigerFetch.jl

Download TIGER/Line shapefiles from the US Census Bureau
Log | Files | Refs | README | LICENSE

geotypes.jl (6351B)


      1 # ABOUTME: Type system for Census TIGER/Line geographies (state, county, tract, etc.)
      2 # ABOUTME: Defines abstract hierarchy (National/State/County) and concrete types with metadata
      3 
      4 # --------------------------------------------------------------------------------------------------
      5 # Abstract base type
      6 abstract type TigerGeography end
      7 
      8 # Abstract types for each scope
      9 abstract type NationalGeography <: TigerGeography end
     10 abstract type StateGeography <: TigerGeography end
     11 abstract type CountyGeography <: TigerGeography end
     12 # --------------------------------------------------------------------------------------------------
     13 
     14 
     15 # --------------------------------------------------------------------------------------------------
     16 # Concrete types with their metadata as constants
     17 struct State <: NationalGeography
     18     year::Int
     19 end
     20 const STATE_META = (tiger_name = "STATE", description = "State Boundaries")
     21 
     22 struct County <: NationalGeography
     23     year::Int
     24 end
     25 const COUNTY_META = (tiger_name = "COUNTY", description = "County Boundaries")
     26 
     27 struct ZipCode <: NationalGeography
     28     year::Int
     29 end
     30 const ZIP_META = (tiger_name = "ZCTA520", description = "2020 5-Digit ZIP Code Tabulation Area")
     31 
     32 struct UrbanArea <: NationalGeography
     33     year::Int
     34 end
     35 const URBANAREA_META = (tiger_name = "UAC20", description = "2020 Urban Area/Urban Cluster")
     36 
     37 struct PrimaryRoads <: NationalGeography
     38     year::Int
     39 end
     40 const PRIMARYROADS_META = (tiger_name = "PRIMARYROADS", description = "Primary Roads")
     41 
     42 struct Rails <: NationalGeography
     43     year::Int
     44 end
     45 const RAILS_META = (tiger_name = "RAILS", description = "Rails")
     46 
     47 struct CBSA <: NationalGeography
     48     year::Int
     49 end
     50 const CBSA_META = (tiger_name = "CBSA", description = "Core Based Statistical Area")
     51 
     52 struct CSA <: NationalGeography
     53     year::Int
     54 end
     55 const CSA_META = (tiger_name = "CSA", description = "Combined Statistical Area")
     56 
     57 struct METDIV <: NationalGeography
     58     year::Int
     59 end
     60 const METDIV_META = (tiger_name = "METDIV", description = "Metropolitan Division")
     61 # --------------------------------------------------------------------------------------------------
     62 
     63 
     64 # --------------------------------------------------------------------------------------------------
     65 struct CountySubdivision <: StateGeography
     66     year::Int
     67 end
     68 const COUSUB_META = (tiger_name = "COUSUB", description = "County Subdivision")
     69 
     70 struct Tract <: StateGeography
     71     year::Int
     72 end
     73 const TRACT_META = (tiger_name = "TRACT", description = "Census Tract")
     74 
     75 struct Place <: StateGeography
     76     year::Int
     77 end
     78 const PLACE_META = (tiger_name = "PLACE", description = "Place")
     79 
     80 struct PrimarySecondaryRoads <: StateGeography
     81     year::Int
     82 end
     83 const PSROADS_META = (tiger_name = "PRISECROADS", description = "Primary and Secondary Roads")
     84 
     85 struct ConCity <: StateGeography
     86     year::Int
     87 end
     88 const CONCITY_META = (tiger_name = "CONCITY", description = "Consolidated City")
     89 
     90 struct UNSD <: StateGeography
     91     year::Int
     92 end
     93 const UNSD_META = (tiger_name = "UNSD", description = "Unified School District")
     94 
     95 
     96 # --------------------------------------------------------------------------------------------------
     97 
     98 
     99 # --------------------------------------------------------------------------------------------------
    100 # --- county geographies
    101 struct AreaWater <: CountyGeography
    102     year::Int
    103 end
    104 const AREAWATER_META = (tiger_name = "AREAWATER", description = "Area Hydrography")
    105 
    106 struct LinearWater <: CountyGeography
    107     year::Int
    108 end
    109 const LINEARWATER_META = (tiger_name = "LINEARWATER", description = "Linear Hydrography")
    110 
    111 struct Roads <: CountyGeography
    112     year::Int
    113 end
    114 const ROADS_META = (tiger_name = "ROADS", description = "Roads")
    115 # --------------------------------------------------------------------------------------------------
    116 
    117 
    118 # --------------------------------------------------------------------------------------------------
    119 # Helper methods to access metadata
    120 tiger_name(::Type{State}) = STATE_META.tiger_name
    121 tiger_name(::Type{County}) = COUNTY_META.tiger_name
    122 tiger_name(::Type{ZipCode}) = ZIP_META.tiger_name
    123 tiger_name(::Type{UrbanArea}) = URBANAREA_META.tiger_name
    124 tiger_name(::Type{PrimaryRoads}) = PRIMARYROADS_META.tiger_name
    125 tiger_name(::Type{Rails}) = RAILS_META.tiger_name
    126 tiger_name(::Type{CBSA}) = CBSA_META.tiger_name
    127 tiger_name(::Type{CSA}) = CSA_META.tiger_name
    128 tiger_name(::Type{METDIV}) = METDIV_META.tiger_name
    129 
    130 tiger_name(::Type{CountySubdivision}) = COUSUB_META.tiger_name
    131 tiger_name(::Type{Tract}) = TRACT_META.tiger_name
    132 tiger_name(::Type{Place}) = PLACE_META.tiger_name
    133 tiger_name(::Type{ConCity}) = CONCITY_META.tiger_name
    134 tiger_name(::Type{UNSD}) = UNSD_META.tiger_name
    135 tiger_name(::Type{PrimarySecondaryRoads}) = PSROADS_META.tiger_name
    136 
    137 tiger_name(::Type{AreaWater}) = AREAWATER_META.tiger_name
    138 tiger_name(::Type{LinearWater}) = LINEARWATER_META.tiger_name
    139 tiger_name(::Type{Roads}) = ROADS_META.tiger_name
    140 
    141 tiger_name(x::T) where T <: TigerGeography = tiger_name(T)
    142 
    143 # -- description
    144 description(::Type{State}) = STATE_META.description
    145 description(::Type{County}) = COUNTY_META.description
    146 description(::Type{ZipCode}) = ZIP_META.description
    147 description(::Type{UrbanArea}) = URBANAREA_META.description
    148 description(::Type{PrimaryRoads}) = PRIMARYROADS_META.description
    149 description(::Type{Rails}) = RAILS_META.description
    150 description(::Type{CBSA}) = CBSA_META.description
    151 description(::Type{CSA}) = CSA_META.description
    152 description(::Type{METDIV}) = METDIV_META.description
    153 
    154 description(::Type{CountySubdivision}) = COUSUB_META.description
    155 description(::Type{Tract}) = TRACT_META.description
    156 description(::Type{Place}) = PLACE_META.description
    157 description(::Type{ConCity}) = CONCITY_META.description
    158 description(::Type{UNSD}) = UNSD_META.description
    159 description(::Type{PrimarySecondaryRoads}) = PSROADS_META.description
    160 
    161 description(::Type{AreaWater}) = AREAWATER_META.description
    162 description(::Type{LinearWater}) = LINEARWATER_META.description
    163 description(::Type{Roads}) = ROADS_META.description
    164 
    165 description(x::T) where T <: TigerGeography = description(T)
    166 
    167 # --
    168 # Helper methods now just reference the type hierarchy
    169 scope(::Type{T}) where {T <: NationalGeography} = National
    170 scope(::Type{T}) where {T <: StateGeography} = ByState
    171 scope(::Type{T}) where {T <: CountyGeography} = ByCounty
    172 # --------------------------------------------------------------------------------------------------