pycensus.census.countries.canada.geography¶
Boundary fetching shared by every Canada census source.
Technical aspects
Boundaries come from Statistics Canada's real 2021 Census cartographic boundary files, published at https://www12.statcan.gc.ca/census-recensement/2021/geo/sip-pis/boundary-limites/index2021-eng.cfm and verified live 2026-08-20 by actually downloading each zip and reading the shapefile inside it (not just checking the HTTP status):
- Province/territory (
lpr_000b21a_e.zip, 133,730,024 bytes,Content-Type: application/x-zip-compressed): 13 rows, columnsPRUID/DGUID/PRNAME/... in EPSG:3347.PRUIDvalues (10, 11, 12, ..., 62) matched exactly against :data:pycensus.canada.constants.PROVINCE_CODES. - Census subdivision / municipality (
lcsd000b21a_e.zip, 155,981,521 bytes): 5,161 rows, columnsCSDUID/DGUID/CSDNAME/CSDTYPE/PRUID/... in EPSG:3347. Toronto's row (CSDUID == "3520005") has bounds[-79.639, 43.579, -79.115, 43.855]-- squarely inside the real city of Toronto's extent, and itsCSDUIDmatches the DGUID (2021A00053520005) on Toronto's real population row in the Census Profile table (see :mod:pycensus.canada.api).
Unlike Mexico's per-state Marco Geoestadistico bundle, StatCan
publishes each level as a single national shapefile, so there is no
per-region download to parallelize/skip -- the whole national zip is
downloaded once (cached under <data_dir>/raw/statcan_boundaries/,
persistent across runs) and then filtered in memory per province.
Because the download is already national and can't be made cheaper by
narrowing first, :func:provinces_in_aoi does a real geometric
intersects test against the actual province boundaries instead of a
hand-set bounding-box heuristic (the pattern used by
mexico.geography.states_in_aoi, needed there specifically to avoid
32 separate expensive per-state downloads).
province, municipality (census subdivision), and
dissemination-area (DA -- StatCan's tract-equivalent, the finest
unit with published Census Profile population data) are implemented.
DA boundaries were verified live 2026-08-20 the same way as the other
two levels: lda_000b21a_e.zip (197,042,003 bytes, GET returns 200
/ application/x-zip-compressed -- note this server 404s plain HEAD
requests, so verification must use GET) contains a real shapefile,
57,932 rows, columns DAUID/DGUID/LANDAREA/PRUID in
EPSG:3347. Cross-check: DAs whose centroid falls within Toronto's CSD
polygon (CSDUID 3520005) sum to a 2021 population of 2,788,296 --
a 0.2% near-miss against the CSD-level total of 2,794,356, expected
from the centroid-in-polygon method missing a handful of edge DAs
rather than a data error (see :mod:pycensus.canada.statcan.loader
for the DA-level population source, PID 98-10-0015).
Dissemination block (DB) -- StatCan's actual finest geographic unit --
was verified live 2026-08-22 and is now implemented too:
ldb_000b21a_e.zip (317,391,964 bytes, GET-only like the DA file)
contains a real shapefile, 498,547 rows, columns
DBUID/DGUID/DBRPLAMX/DBRPLAMY/LANDAREA/PRUID,
EPSG:3347. Unlike the other three levels, DB attribute data (real
per-block population and dwelling counts -- not merely a
populated/unpopulated flag) does not come from a Full Table Download
cube; it comes from a separate flat file, the 2021 Census Geographic
Attribute File (catalogue 92-151-X, see
:data:pycensus.canada.constants.GEOGRAPHIC_ATTRIBUTE_FILE_URL and
:func:pycensus.canada.api.fetch_block_totals). Cross-check performed
2026-08-22: summing the GAF's real block-level population for every
one of Toronto's 3,743 real dissemination areas exactly reproduces PID
98-10-0015's own published DA-level population (0 mismatches), and the
grand total across all 13,322/13,323 matched Toronto blocks
(2,794,356) exactly matches the City of Toronto's published CSD
population -- see the module docstring of
:mod:pycensus.canada.statcan.loader for the full writeup. nation
has no single-row national boundary file verified either (StatCan
publishes the country outline as a byproduct of the province file,
which could be derived with dissolve(), but that hasn't been
wired up).
Source
https://www12.statcan.gc.ca/census-recensement/2021/geo/sip-pis/boundary-limites/index2021-eng.cfm
boundary_zip_available(level, data_dir=None)
¶
True when the national boundary zip of level is cached on disk or the file site answers (it 403s some networks).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Boundary level. |
required |
data_dir
|
Optional[str]
|
Folder of the raw downloads; |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if usable. |
provinces_in_aoi(aoi, year=2021, data_dir=None)
¶
Return the PRUIDs of every province/territory that actually intersects aoi.
Unlike mexico.geography.states_in_aoi's bounding-box heuristic,
this tests against the real national province boundary file (already
a single, already-cached national download -- see module docstring),
so the result is exact rather than approximate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aoi
|
GeoDataFrame
|
Area of interest (GeoDataFrame). |
required |
year
|
int
|
Boundary year. |
2021
|
data_dir
|
Optional[str]
|
Folder of the raw downloads; |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
PRUIDs. |
load_boundaries(level, province=None, year=2021, erase_water=None, data_dir=None)
¶
Fetch real StatCan 2021 boundaries for one Canada geography level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Standard geometry level name, resolved through
:data: |
required |
province
|
Optional[str]
|
Province code (PRUID), name, or postal abbreviation.
Required -- narrows the national shapefile to one province's
rows (a single row for |
None
|
year
|
int
|
Vintage year. Only 2021 is wired up. |
2021
|
erase_water
|
Optional[bool]
|
Accepted for interface parity with |
None
|
data_dir
|
Optional[str]
|
Base directory for the raw StatCan boundary zip cache.
Defaults to :data: |
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame with a |
GeoDataFrame
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
load_boundaries_multi(level, provinces=None, year=2021, erase_water=None, aoi=None, data_dir=None)
¶
Fetch and concatenate boundaries across one or more provinces, optionally clipped to an AOI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Standard geometry level name. |
required |
provinces
|
Optional[Union[str, List[str]]]
|
Province code(s)/name(s)/abbreviation(s). If |
None
|
year
|
int
|
Vintage year (see :func: |
2021
|
erase_water
|
Optional[bool]
|
Accepted for interface parity, not implemented. |
None
|
aoi
|
Optional[GeoDataFrame]
|
If given, the result is filtered to geometries intersecting it. |
None
|
data_dir
|
Optional[str]
|
Base directory for the raw StatCan boundary zip cache. |
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame with a |