How Àgora finds your census section in the browser

Àgora has a page, Cómo votó tu barrio, where you type an address or press “use my location” and see how your census section voted in the general elections of 23 July 2023 (23J) and 10 November 2019 (10N). Next to it sit your municipality, your province and Spain as a whole. The next general election is on 29 November 2026.

There is no server behind it that answers the question. The site is static: the answer is computed in your browser from files that sit next to the page. This post follows a search from the point on the map to the table of percentages, says exactly what leaves your device (it depends on how you search) and ends with the cases where the answer is not quite your street. The page and its interface are in Spanish, Catalan, Basque and Galician; there is no English version.

What a census section is, and why it is the unit

A census section is the smallest area the Spanish statistics institute (INE) draws for the electoral census. Its code has ten digits: province (2), municipality (3), district (2) and section (3), as documented in src/features/sections/load.ts. The Ministry of the Interior publishes results per polling table, and the section is the smallest area that table results can be added up to.

The build script scripts/data/sections.mjs does that: it adds up the table files from Infoelectoral for each section, and takes the section boundaries from the INE map of each election year. It leaves out the CERA (votes of residents abroad), which belongs to no section. The output is two files per province and year, data/sections/<year>/<province>.topo.json for the shapes and <province>.results.json for the votes. With 52 provinces and two years, that is the 208 files in the repository.

Counted with a script over those files:

10N (2019)23J (2023)
Section shapes36,31736,462
Sections with their own results36,30236,460
Shapes with no results3049
Municipalities8,1318,131

The shapes are simplified with Visvalingam at 5 metres (SIMPLIFY_M in the script) and stored as TopoJSON, so each province is one small file. In 2023 they range from 20 KB (Melilla) to 1.45 MB (Madrid), uncompressed.

From an address to a point

There are two ways in, and they differ in what leaves your device.

Address. What you type goes straight from your browser to CartoCiudad, the geocoder of the Instituto Geográfico Nacional. Àgora does not receive it, store it or put it in the link. The page says so in a note next to the search box. The code that calls it is searchPlaces in src/features/myvote/logic.ts, with a 5-second limit (CARTO_TIMEOUT_MS). So an address search is not “entirely on your device”: a third-party service sees the text, as it would on its own website.

Your location. The button calls the browser’s geolocation (src/features/myvote/geolocate.ts) and nothing else. The coordinates are not sent anywhere: the only requests that follow are for map files that are the same for everybody. This is the path where the point stays in the browser.

Municipality. You type a town name and the page matches it against a local list of INE names (/geo/municipios-nombres.json). No geocoder is involved. The price is precision: you get the whole municipality and not your section.

Nothing is saved. The link you can copy carries ?sec=<code> or ?mun=<ine5>, never an address (parseQuery and queryString in logic.ts).

From a point to a section

Once there is a point, the rest is geometry in the browser. First it has to know which province file to download, because fetching all of them would be about 24 MB of shapes for each year (the 2023 and 2019 .topo.json files add up to 23.6 and 23.5 million bytes).

provinceCandidates tests the point against the province outlines, and also against any outline with a vertex within 0.1 degrees (about 11 km), up to three. The second part is there because the province outlines are simplified too, and a point on a border can fall just outside both neighbours. A point in the sea gives no province and nothing is downloaded.

The page then downloads that province’s shapes and results, builds the polygons with topojson-client and asks which one holds the point. This is the whole function in src/features/sections/load.ts:

export function locateSection(data: ProvinceSections, lon: number, lat: number): SectionFeature | null {
  return data.features.find((f) => geoContains(f, [lon, lat])) ?? null;
}

That is it: the first polygon that contains the point. Results for the section are then put in a table beside the municipality, province and Spain, as a percentage of valid votes, with turnout and electors. The rows are the parties that matter most in those columns (pickParties, at most seven), and everything else goes to “other and blank”.

How 2019 is matched

The section map is redrawn between elections. Comparing the section code across years would give the wrong neighbours for any section that was split or merged. Of the 36,317 section codes in the 2019 map, 36,067 also exist in 2023, and 250 do not.

So build in src/features/myvote/mount.ts does not look up the code. It takes the same point and runs locateSection on the 2019 file of that province. If the code found is the same, the page says so. If it is different, it says “the sections changed between 2019 and 2023, so these are not exactly the same neighbours”. If there is nothing, it shows the municipality, the province and Spain only.

A copied link with an old code is handled the other way round: if the code is not in the 2023 map but is in the 2019 one, the code takes an interior point of the 2019 shape (centroid, or a grid search if the centroid falls outside) and looks it up in 2023.

What it gets wrong

The results are the official ones, and the page does not rank, comment on or recommend anything. For how the rest of the site treats the same elections, see how Àgora checks the promises an AI extracted and from polls to seats.