Skip to content

pycensus.census.countries.andorra.api

Thin ArcGIS REST client for Andorra's two real, keyless, hosted feature services.

Technical aspects

Andorra's national statistics office (Departament d'Estadistica, https://www.estadistica.ad/) has no documented REST/SDMX API for its human-facing dataset browser -- but that browser is itself built on Esri ArcGIS Hub (confirmed live 2026-08-20: the portal's page source references activation-engine/community-engine, the Ember modules Esri's Hub frontend ships, and https://www.estadistica.ad/portal/sharing/rest?f=json answers with a real ArcGIS Portal API version), and that portal's own search API (.../portal/sharing/rest/search) lists real, public, queryable ArcGIS Server FeatureServer items with no login required. Two of those are used here, both verified live by fetching them and inspecting the actual response content (not just HTTP 200):

  • Parish (comú) boundary polygons: https://www.estadistica.ad/server/rest/services/Estadistica/Cartografia_base/FeatureServer/3 (layer name Parroquies_Pol_codi). Returns exactly 7 polygons with real Nom/Codi fields matching Andorra's 7 parishes and their ISO 3166-2:AD codes (AD-01..AD-07), reprojected on request via outSR=4326; longitude/latitude bounds of the fetched geometry (1.409-1.787 E, 42.429-42.656 N) land exactly on Andorra's known extent.
  • Population by locality/year: https://sig.govern.ad/server/rest/services/Hosted/Poblaci%C3%B3_2010_2021/FeatureServer/0 (item title "Poblacio 2010-2025", found via the estadistica.ad portal search for "poblacio"). Returns 44 real localities ("pobles"), each tagged with its parish (parroquia) and one integer population column per year (pob_2003, pob_2010..pob_2025). Summed across all localities, pob_2023 gives exactly 85,101 -- matching Diari d'Andorra's reporting of the Departament d'Estadistica's own published 31 Dec 2023 population figure (https://www.diariandorra.ad/noticies/nacional/2023/11/21/andorra_guanya_500_habitants_any_cens_estadistica_suma_516_231842_1125.html) word for word (85,101).

Neither endpoint requires an API key or authentication token -- confirmed by live, unauthenticated queries against both hosts on 2026-08-20.

query_features(url, where='1=1', out_fields='*', return_geometry=True, out_sr=4326)

Fetch every feature matching where from an ArcGIS FeatureServer layer.

Both real services used by this module return their full result set (7 parishes, 44 localities) in a single unpaginated request -- verified live -- so no resultOffset pagination loop is needed here (unlike :mod:pycensus.israel.arcgis, which fetches much larger result sets).

Parameters:

Name Type Description Default
url str

Full FeatureServer layer URL.

required
where str

SQL WHERE clause (default "1=1", i.e. everything).

'1=1'
out_fields str

Comma-separated field list, or "*".

'*'
return_geometry bool

Whether to request geometry.

True
out_sr int

Output spatial reference. Both real services here accept server-side reprojection to 4326, verified live.

4326

Returns:

Type Description
List[dict]

List of raw Esri JSON feature dicts (``{"attributes": {...},

List[dict]

"geometry": {...}}``).

Raises:

Type Description
HTTPError

If the request fails.

RuntimeError

If the server returns an error payload.

query_group_count(url, group_field, where='1=1', count_field=None)

Run a groupBy/count statistics query against an ArcGIS FeatureServer layer.

Both real services used here (BUILDINGS_URL, BUSINESSES_URL) support outStatistics/groupByFieldsForStatistics -- verified live 2026-08-23.

Parameters:

Name Type Description Default
url str

Full FeatureServer layer URL.

required
group_field str

Field to group by (e.g. parish name/code field).

required
where str

SQL WHERE clause (default "1=1").

'1=1'

Returns:

Type Description
DataFrame

DataFrame with group_field and a count integer column.

Raises:

Type Description
HTTPError

If the request fails.

RuntimeError

If the server returns an error payload.

features_to_geodataframe(features)

Convert raw Esri JSON features (from :func:query_features) to a GeoDataFrame in EPSG:4326.

Parameters:

Name Type Description Default
features List[dict]

Esri JSON features.

required

Returns:

Type Description
GeoDataFrame

GeoDataFrame in EPSG:4326 (empty when there are no features).

fetch_parish_boundaries()

Fetch Andorra's 7 real parish (comú) boundary polygons, in EPSG:4326.

Returns:

Type Description
GeoDataFrame

GeoDataFrame with Nom (parish name), Codi (ISO 3166-2:AD

GeoDataFrame

code), and geometry columns.

fetch_population_by_locality()

Fetch every locality's ("poble") population by year, real values from estadistica.ad.

Returns:

Type Description
DataFrame

DataFrame with poblacio (locality name), parroquia (parish

DataFrame

name -- see :data:pycensus.andorra.constants.PARISH_NAME_ALIASES

DataFrame

for one known misspelling in this source), and one pob_<year>

DataFrame

integer column per year the source publishes (2003, 2010-2025).

fetch_building_counts_by_parish()

Fetch building-footprint counts per parish, real values from the 2019 Edificis layer.

Returns:

Type Description
DataFrame

DataFrame with parròquia (real parish name) and count

DataFrame

(integer number of building-footprint polygons in that parish).

fetch_active_business_counts_by_parish()

Fetch active-business counts per parish, real values from the REE business register.

Returns:

Type Description
DataFrame

DataFrame with PARROQUIA (integer 1-7, matching ISO 3166-2:AD

DataFrame

suffix) and count (integer number of active businesses, i.e.

DataFrame

rows with Es_Activa = 1, in that parish).

fetch_population_by_nationality_and_parish(url=NATIONALITY_BULLETIN_URL)

Fetch and parse the real "Poblacio per nacionalitat i parroquia" table.

Downloads the Departament d'Estadistica bulletin PDF at url (see :data:NATIONALITY_BULLETIN_URL), locates section "2.2 Poblacio per nacionalitat", and reconstructs its table via :func:_cluster_pdf_number_rows. The table's real, fixed row order (verified live 2026-08-23 against this exact document) is: Andorrana, Espanyola, Francesa, Portuguesa, Altres, Andorrana (repeated subtotal), Estrangera, Total -- 8 rows, each with one (n, %) pair per of 7 parishes plus a national Total column (16 numeric tokens/row).

This function cross-validates the parse before returning, rather than trusting row order alone: for every column, the single-nationality rows (Andorrana + Espanyola + Francesa + Portuguesa + Altres) must sum to the Total row, the repeated Andorrana row must equal the first Andorrana row, and Andorrana + Estrangera must equal Total. If a future bulletin's layout drifts and any of these fail, this raises rather than silently returning a wrong parse.

Returns:

Type Description
DataFrame

DataFrame with one row per parish code (GEOID in

DataFrame

data:pycensus.andorra.constants.PARISH_CODES plus "AD" for

DataFrame

the national total), an integer foreign_nationality column

DataFrame

(the real "Estrangera" count), and an integer population

DataFrame

column (the real "Total" count, for cross-checking against

DataFrame

func:fetch_population_by_locality).

Raises:

Type Description
HTTPError

If the PDF can't be downloaded.

RuntimeError

If the expected table structure/cross-checks aren't found in the parsed PDF (layout drift from a future bulletin).

Parameters:

Name Type Description Default
url str

URL of the statistics bulletin PDF.

NATIONALITY_BULLETIN_URL