Skip to content

pycensus.census.countries.spain.geography

Boundary fetching shared by every Spain census source.

Technical aspects

Boundaries come from Eurostat/GISCO's LAU (Local Administrative Units) dataset, verified 2026-08-19: https://gisco-services.ec.europa.eu/distribution/v2/lau/download/ref-lau-2021-01m.geojson.zip (~106 MB zip containing every EU/EFTA/candidate country's municipalities at 1:1,000,000 resolution; contains 8,131 Spanish features whose LAU_ID is exactly INE's 5-digit municipality code -- verified by cross-checking LAU_ID == "44001" (Ababuj, Teruel) against INE's own Tempus API (:mod:pycensus.spain.ine.loader), and by summing every Spanish feature's POP_2021 to get 47,385,107 -- matching INE table 2915's "Total Nacional" 2021 figure exactly).

Only municipality boundaries are downloaded directly. province/autonomous_community/nation boundaries are built by dissolving municipality geometries using the code prefixes/mapping in :mod:pycensus.spain.constants (INE municipality codes are {2-digit province code}{3-digit municipality code}, so the first two characters of LAU_ID are the province code).

section (seccion censal) boundaries come from a real, verified source found in this rebuild: INE's own OGC API Features service, documented at https://www.ine.es/geoserver/ogc/features/v1/collections?f=text/html and https://www.ine.es/geoserver/www/guias/OGC_API_Feature_Seccionado.pdf -- verified 2026-08-20 with a live request: GET https://www.ine.es/geoserver/ogc/features/v1/collections/WMS_INE_SECCIONES_G01:Secciones_2025/items?filter=CPRO='20' AND TIPO='SECCIONADO'&filter-lang=cql-text&f=json&limit=2000 returned 544 real MultiPolygon features for Gipuzkoa (province code "20"), each carrying a real CUSEC 10-digit code (CPRO 2 + CMUN 3 + CDIS 2 + CSEC 3, e.g. "2000101001") matching the format INE's own population-by-section files use for their Sección column. The collection also contains TIPO='DISTRITO' aggregate rows (CSEC='000') that are not real sections -- these are filtered out via the TIPO='SECCIONADO' CQL clause. The response's crs member is urn:ogc:def:crs:EPSG::4326, confirmed live.

district/block (distrito/manzana) are still not implemented -- INE's cartography service exposes district only as the TIPO='DISTRITO' aggregate rows within the same section collection (usable, but not yet wired up here), and no stable direct-download URL for block-level (manzana) cartography was verified in this session. Requesting those levels raises NotImplementedError rather than fabricating placeholder geometry (see :data:IMPLEMENTED_LEVELS).

Downloaded/derived files are cached under <data_dir>/raw/gisco_lau/ (municipality+) and <data_dir>/raw/ine_secciones/ (section, one parquet per province), both persistent across runs, independent of :mod:pycensus.cache's parquet cache.

Source

https://ec.europa.eu/eurostat/web/gisco/geodata/administrative-units/local-administrative-units

load_boundaries(level, region=None, year=2021, erase_water=None, data_dir=None)

Fetch boundaries for one Spain geography level.

Parameters:

Name Type Description Default
level str

Standard geometry level name, resolved through :data:pycensus.spain.constants.LEVEL_FALLBACK.

required
region Optional[str]

Region code -- an autonomous community code, province code, or municipality code, matching level. If None, returns every geometry at this level for the entire country. Required for level="section" (see below).

None
year int

Vintage year. GISCO LAU 2021 is the only vintage wired up for municipality+; INE's "Secciones_2025" collection is the only vintage wired up for sections. Kept as a parameter for interface parity with other countries.

2021
erase_water Optional[bool]

Whether to clip inland water bodies. Defaults to :data:ERASE_WATER for the resolved level. Not yet implemented (GISCO LAU / INE section geometries are land-only administrative boundaries already, so this is a no-op today).

None
data_dir Optional[str]

Root directory for the raw GISCO/INE cache. Defaults to :data:pycensus.spain.constants.DEFAULT_DATA_DIR.

None

Returns:

Type Description
GeoDataFrame

GeoDataFrame with a GEOID column, geometry, in EPSG:4326.

Raises:

Type Description
KeyError

If level does not resolve to a known geography level.

ValueError

If level resolves to "section" without a region (fetching every one of Spain's ~36,000 census sections in one call is not supported).

NotImplementedError

If level resolves to a level without a real, verified boundary source (district/block).

load_boundaries_multi(level, regions=None, year=2021, erase_water=None, aoi=None, data_dir=None)

Fetch and concatenate boundaries across one or more regions, optionally clipped to an AOI.

Parameters:

Name Type Description Default
level str

Standard geometry level name.

required
regions Optional[Union[str, List[str]]]

Region code(s). If None, returns every geometry at this level for the entire country.

None
year int

Vintage year (see :func:load_boundaries).

2021
erase_water Optional[bool]

Whether to clip inland water bodies.

None
aoi Optional[GeoDataFrame]

If given, the result is filtered to geometries intersecting it.

None
data_dir Optional[str]

Root directory for the raw GISCO cache.

None

Returns:

Type Description
GeoDataFrame

GeoDataFrame with a GEOID column, geometry, in EPSG:4326.

autonomous_communities_in_aoi(aoi, year=2021, data_dir=None)

Return the INE codes of every autonomous community intersecting aoi.

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 = the pycensus cache.

None

Returns:

Type Description
List[str]

Community codes.

provinces_in_aoi(aoi, year=2021, data_dir=None)

Return the INE codes of every province intersecting aoi.

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 = the pycensus cache.

None

Returns:

Type Description
List[str]

Province codes.

states_in_aoi(aoi, year=2021, data_dir=None)

Alias for :func:autonomous_communities_in_aoi (interface parity with usa).

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 = the pycensus cache.

None

Returns:

Type Description
List[str]

Community codes.