pycensus.census.countries.taiwan.geography¶
Boundary fetching shared by every Taiwan census source.
Technical aspects
Taiwan's own National Land Surveying and Mapping Center (NLSC) publishes
real, cataloged county/city and township/district boundary datasets
(https://data.gov.tw/dataset/7442, /7441 -- real catalog entries, real
field names COUNTYID/COUNTYCODE/COUNTYNAME/COUNTYENG),
but the actual download files are served from a TGOS host
(www.tgos.tw) that returns HTTP 403 to every plain HTTP client
(verified 2026-08-20, with and without a browser User-Agent/Referer, via
both curl and wget) -- a real, confirmed dead end, not an assumption.
county_city (the only level implemented here) instead uses
geoBoundaries.org's Taiwan ADM1 layer
(:data:pycensus.taiwan.constants.GEOBOUNDARIES_TWN_ADM1_URL): a real,
keyless, single-file GeoJSON download (~3.2MB, all 22 counties/cities in
one request, verified 2026-08-20 -- feature count, names, and ISO 3166-2
codes all cross-checked against
:data:pycensus.taiwan.constants.COUNTY_CODES, see
:data:pycensus.taiwan.constants.GEOBOUNDARIES_ISO_TO_COUNTY_CODE).
This is NOT an official NLSC file -- geoBoundaries' own catalog entry
for Taiwan says "boundarySource": "OpenStreetMap, Wambacher" -- but
it is real, verifiable, actively-maintained geometry, used here as the
practical alternative until TGOS's download gate is beaten (browser
automation, or a stable mirror of the actual NLSC file).
district (townships/districts) uses geoBoundaries' Taiwan ADM2
layer (:data:pycensus.taiwan.constants.GEOBOUNDARIES_TWN_ADM2_URL): a
real, keyless, single-file GeoJSON download (~10.5MB, 368 features,
verified 2026-08-20, same OpenStreetMap/Wambacher source as ADM1). Its
features carry no MOI-compatible code field, so each one was matched
to MOI RIS's 8-digit district_code prefix by spatial join (county
disambiguation) plus Chinese/Hanyu-Pinyin/Tongyong-Pinyin name matching
-- see :data:pycensus.taiwan.constants.GEOBOUNDARIES_ADM2_TO_DISTRICT_CODE
for the full derivation and verification method. All 368 ADM2 features
matched to all 368 MOI districts (1:1, nothing left over/guessed).
village (村里/li, finer than district) uses OpenStreetMap's Overpass
API: every admin_level=9/boundary=administrative relation inside
a county/city's OSM area carries a nat_ref tag that IS MOI's own
11-digit district_code directly -- no name/pinyin matching needed,
unlike district's ADM2 join. Verified live 2026-08-22: Taipei City
("63000") returns exactly 456 such relations, and that set of 456
nat_refs is an exact match (zero missing, zero extra) against the
456 unique village district_codes in MOI RIS's live
ODRP013/11407 response for Taipei -- see
:data:pycensus.taiwan.constants.OVERPASS_API_URL docstring for the
full verification (including New Taipei City's close-but-not-exact
1032/1039 match). :func:load_boundaries verifies this match itself,
live, on every fetch for a given county -- it raises loudly rather than
silently returning an unverified geometry set (see
:func:_download_village_geojson).
Real population data (:mod:pycensus.taiwan.moi.loader) is available
and verified independently of this module.
load_boundaries(level, region=None, year=None, erase_water=None, data_dir=None)
¶
Fetch real Taiwan county/city or district boundaries (see module docstring).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Standard geometry level name, resolved through
:data: |
required |
region
|
Optional[str]
|
A GEOID to filter down to (5-digit county/city code for
|
None
|
year
|
Optional[int]
|
Accepted for interface parity, not implemented -- geoBoundaries' TWN layers each have a single build vintage (2023). |
None
|
erase_water
|
Optional[bool]
|
Accepted for interface parity, not implemented. |
None
|
data_dir
|
Optional[str]
|
Base directory for the raw GeoJSON cache. Defaults to
:data: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
GeoDataFrame
|
GeoDataFrame with a |
|
GeoDataFrame
|
or 8-digit district code for |
|
EPSG |
GeoDataFrame
|
4326, for all 22 counties/cities or all 368 districts. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
load_boundaries_multi(level, regions=None, year=None, erase_water=None, aoi=None, data_dir=None)
¶
Fetch county/city boundaries, optionally filtered to regions/clipped to aoi.
All 22 counties/cities come back from a single :func:load_boundaries
call (see its docstring), so there's no per-region download loop here
-- regions/aoi only narrow the already-fetched result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Geography level. |
required |
regions
|
County codes to keep. |
None
|
|
year
|
Optional[int]
|
Boundary year. |
None
|
erase_water
|
Optional[bool]
|
Remove water areas. |
None
|
aoi
|
Optional[GeoDataFrame]
|
Area of interest (GeoDataFrame). |
None
|
data_dir
|
Optional[str]
|
Folder of the raw downloads; |
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame (EPSG:4326). |
counties_in_aoi(aoi, data_dir=None)
¶
Return the county/city codes whose real boundary intersects aoi.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aoi
|
GeoDataFrame
|
Area of interest (GeoDataFrame). |
required |
data_dir
|
Optional[str]
|
Folder of the raw downloads; |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
Codes. |
districts_in_aoi(aoi, data_dir=None)
¶
Return the 8-digit district codes whose real ADM2 boundary intersects aoi.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aoi
|
GeoDataFrame
|
Area of interest (GeoDataFrame). |
required |
data_dir
|
Optional[str]
|
Folder of the raw downloads; |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
Codes. |
villages_in_aoi(aoi, data_dir=None)
¶
Return the 11-digit village codes whose real OSM-sourced boundary intersects aoi.
Resolves aoi to the county/city codes it intersects first (via
:func:counties_in_aoi, real geoBoundaries ADM1 geometry), then fetches
village boundaries only for those counties -- avoiding a nationwide
Overpass fetch for a typically-small city-scale AOI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aoi
|
GeoDataFrame
|
Area of interest (GeoDataFrame). |
required |
data_dir
|
Optional[str]
|
Folder of the raw downloads; |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
Codes. |