GADM tools

cycles.gadm

read_gadm(path, country, level_str, *, conus=True)

Read a GADM layer and normalize its index.

Parameters:
  • path (str | Path) –

    Directory containing GADM shapefiles.

  • country (str) –

    Country code used in GADM file names.

  • level_str (str) –

    Administrative level name (country, state, county).

  • conus (bool, default: True ) –

    For USA state/county layers, exclude Alaska and Hawaii.

Returns:
  • GeoDataFrame

    GeoDataFrame indexed by GID.

Source code in cycles/gadm/gadm.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def read_gadm(path: str | Path, country: str, level_str: str, *, conus: bool=True) -> gpd.GeoDataFrame:
    """Read a GADM layer and normalize its index.

    Args:
        path: Directory containing GADM shapefiles.
        country: Country code used in GADM file names.
        level_str: Administrative level name (country, state, county).
        conus: For USA state/county layers, exclude Alaska and Hawaii.

    Returns:
        GeoDataFrame indexed by GID.
    """
    level = GADMLevel[level_str.upper()]
    gdf = gpd.read_file(_gadm_path(Path(path), country, level))

    if country != 'global':
        gdf.rename(columns={f'GID_{level.value}': 'GID'}, inplace=True)
    gdf.set_index('GID', inplace=True)

    if country == 'USA' and conus:
        gdf = gdf[~gdf['NAME_1'].isin(['Alaska', 'Hawaii'])]

    return gdf

state_abbreviation(*, state=None, gid=None, fips=None)

Look up state abbreviation by name, GID, or FIPS code.

Parameters:
  • state (str | None, default: None ) –

    Full state name.

  • gid (str | None, default: None ) –

    State GID string.

  • fips (int | None, default: None ) –

    Numeric state FIPS code.

Returns:
  • str

    Two-letter state abbreviation.

Raises:
  • KeyError

    If no matching state record is found.

Source code in cycles/gadm/gadm.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def state_abbreviation(*, state: str | None=None, gid: str | None=None, fips: int | None=None) -> str:
    """Look up state abbreviation by name, GID, or FIPS code.

    Args:
        state: Full state name.
        gid: State GID string.
        fips: Numeric state FIPS code.

    Returns:
        Two-letter state abbreviation.

    Raises:
        KeyError: If no matching state record is found.
    """
    return str(_find_representation(STATE_CSV, STATE_DTYPES, 'abbreviation', state=state, gid=gid, fips=fips))

state_fips(*, state=None, abbreviation=None, gid=None)

Look up state FIPS code by name, abbreviation, or GID.

Parameters:
  • state (str | None, default: None ) –

    Full state name.

  • abbreviation (str | None, default: None ) –

    Two-letter state abbreviation.

  • gid (str | None, default: None ) –

    State GID string.

Returns:
  • int

    Numeric state FIPS code.

Raises:
  • KeyError

    If no matching state record is found.

Source code in cycles/gadm/gadm.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def state_fips(*, state: str | None=None, abbreviation: str | None=None, gid: str | None=None) -> int:
    """Look up state FIPS code by name, abbreviation, or GID.

    Args:
        state: Full state name.
        abbreviation: Two-letter state abbreviation.
        gid: State GID string.

    Returns:
        Numeric state FIPS code.

    Raises:
        KeyError: If no matching state record is found.
    """
    return int(_find_representation(STATE_CSV, STATE_DTYPES, 'fips', state=state, abbreviation=abbreviation, gid=gid))

state_gid(*, state=None, abbreviation=None, fips=None)

Look up state GID by name, abbreviation, or FIPS code.

Parameters:
  • state (str | None, default: None ) –

    Full state name.

  • abbreviation (str | None, default: None ) –

    Two-letter state abbreviation.

  • fips (int | None, default: None ) –

    Numeric state FIPS code.

Returns:
  • str

    State GID string.

Raises:
  • KeyError

    If no matching state record is found.

Source code in cycles/gadm/gadm.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def state_gid(*, state: str | None=None, abbreviation: str | None=None, fips: int | None=None) -> str:
    """Look up state GID by name, abbreviation, or FIPS code.

    Args:
        state: Full state name.
        abbreviation: Two-letter state abbreviation.
        fips: Numeric state FIPS code.

    Returns:
        State GID string.

    Raises:
        KeyError: If no matching state record is found.
    """
    return str(_find_representation(STATE_CSV, STATE_DTYPES, 'gid', state=state, abbreviation=abbreviation, fips=fips))

state_name(*, abbreviation=None, gid=None, fips=None)

Look up state name by abbreviation, GID, or FIPS code.

Parameters:
  • abbreviation (str | None, default: None ) –

    Two-letter state abbreviation.

  • gid (str | None, default: None ) –

    State GID string.

  • fips (int | None, default: None ) –

    Numeric state FIPS code.

Returns:
  • str

    Full state name.

Raises:
  • KeyError

    If no matching state record is found.

Source code in cycles/gadm/gadm.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def state_name(*, abbreviation: str | None=None, gid: str | None=None, fips: int | None=None) -> str:
    """Look up state name by abbreviation, GID, or FIPS code.

    Args:
        abbreviation: Two-letter state abbreviation.
        gid: State GID string.
        fips: Numeric state FIPS code.

    Returns:
        Full state name.

    Raises:
        KeyError: If no matching state record is found.
    """
    return str(_find_representation(STATE_CSV, STATE_DTYPES, 'state', abbreviation=abbreviation, gid=gid, fips=fips))

county_fips(*, gid)

Look up county FIPS code by county GID.

Parameters:
  • gid (str) –

    County GID string.

Returns:
  • int

    Numeric county FIPS code.

Raises:
  • KeyError

    If no matching county record is found.

Source code in cycles/gadm/gadm.py
165
166
167
168
169
170
171
172
173
174
175
176
177
def county_fips(*, gid: str) -> int:
    """Look up county FIPS code by county GID.

    Args:
        gid: County GID string.

    Returns:
        Numeric county FIPS code.

    Raises:
        KeyError: If no matching county record is found.
    """
    return int(_find_representation(COUNTY_CSV, COUNTY_DTYPES, 'fips', gid=gid))

county_gid(*, fips)

Look up county GID by county FIPS code.

Parameters:
  • fips (int) –

    Numeric county FIPS code.

Returns:
  • str

    County GID string.

Raises:
  • KeyError

    If no matching county record is found.

Source code in cycles/gadm/gadm.py
150
151
152
153
154
155
156
157
158
159
160
161
162
def county_gid(*, fips: int) -> str:
    """Look up county GID by county FIPS code.

    Args:
        fips: Numeric county FIPS code.

    Returns:
        County GID string.

    Raises:
        KeyError: If no matching county record is found.
    """
    return str(_find_representation(COUNTY_CSV, COUNTY_DTYPES, 'gid', fips=fips))

county_name(*, gid=None, fips=None)

Look up county display name by GID or FIPS code.

Parameters:
  • gid (str | None, default: None ) –

    County GID string.

  • fips (int | None, default: None ) –

    Numeric county FIPS code.

Returns:
  • str

    County display name formatted as "County, State".

Raises:
  • KeyError

    If no matching county record is found.

Source code in cycles/gadm/gadm.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def county_name(*, gid: str | None=None, fips: int | None=None) -> str:
    """Look up county display name by GID or FIPS code.

    Args:
        gid: County GID string.
        fips: Numeric county FIPS code.

    Returns:
        County display name formatted as "County, State".

    Raises:
        KeyError: If no matching county record is found.
    """
    return str(_find_county_name(COUNTY_CSV, COUNTY_DTYPES, gid=gid, fips=fips))