Skip to main content

2026-11-29 · 8 min read

Turning Taiwan's election open data into an interactive map: five pitfalls

Data

Translated from the Chinese original: read it in Chinese

November 28, 2026 was Taiwan's nine-in-one local election. On counting night I wanted to build one thing: a clickable map of Taiwan covering past elections, where you can click into a county and see its townships, with a mobile version. Drawing the map turned out not to be the hard part; the time went into "is the data right". This post records where the data came from, how I verified it, and five pitfalls I really hit. The result is at /tools/election-map (a tool page in Chinese).

Find the data first: don't scrape pages, find the JSON behind them

The "election database" of Taiwan's Central Election Commission (CEC) is a single-page app, and the tables on screen are not HTML spat out by the server. Open the developer tools and you see it is actually reading a pile of static JSON, with URLs that follow fixed rules:

/static/elections/list/ELC_<code>.json                 which elections exist, each one's theme_id
/static/elections/data/tickets/ELC/<code>/00/<theme>/C/00_000_00_000_0000.json   candidate votes by county
/static/elections/data/tickets/ELC/<code>/00/<theme>/D/<province>_<city>_00_000_0000.json   candidate votes by township
/static/elections/data/profiles/ELC/...                 number of voters and votes cast (for turnout)

I got the codes wrong at first: C1 is the special-municipality mayors (in 2022 just the six big cities) and county and city mayors are C2; the president is P0, the legislature L0, special-municipality councilors T1, county and city councilors T2, village and neighborhood chiefs V0. Referendums are under a different path, /static/referendums/, with an extra case-type segment. At level C a single file gives all counties, while D (townships) needs one file per county.

The 2026 data was not yet in this database; it is on another site, the "115th-year election vote counting information site". That one has an HTML page per county, per township, even per village, with URLs like TC/63000000100000000.html. The mapping from pages to districts is hidden in a tree-structured JSON definition it loads (about 490 KB), and walking it lists 368 townships and 7,781 villages.

Check the numbers first, then draw

I gave myself a rule: before drawing any dataset, compare against one number I can verify elsewhere.

  • Mayors: township sums must equal county level; across all cycles, 622 candidate vote records matched exactly.
  • President 2024: Lai Ching-te 5,586,019, Hou Yu-ih 4,671,021, Ko Wen-je 3,690,466, consistent with the official figures.
  • Councilors 2022: 910 seats nationwide.
  • Referendums: 22 questions, with county sums matching the official county level on all 484 records.

These checks stopped two of the four later pitfalls while I was still writing the code.

Pitfall 1: old county codes counted the 2008 party votes twice

For the legislature's national party-list votes, my first approach was "fetch every county code that has ever appeared, and skip any that return 404". The map showed a 2008 national total of 11,708,998 votes and 50.87% for the KMT. That looked reasonable, but the official valid-vote count was 9,780,573 and the KMT 51.23%.

The cause is that county codes differ between cycles: from 1995 to 2001 the Taiwan Province counties were 03_xxx, and from 2004 they are 10_xxx; Taipei and Kaohsiung were sometimes 01 / 02 and sometimes 63 / 64. In 2008 both sets exist at the same time, so the same votes were fetched twice.

The fix is to not guess codes: each cycle's own level-C file already lists that cycle's county codes and names, so use that as the truth. After refetching, 2008 is 9,780,573 votes, KMT 51.23% and DPP 36.91%; 2024 is DPP 36.16%, KMT 34.58% and TPP 22.07%, all matching.

Pitfall 2: 2004, Yunlin and Taitung, a chunk of data is really missing

I counted district legislator seats as "people with an elected flag at district level". 1998 and 2001 gave 168 seats each, which is right, but 2004 gave 189. Digging in, Yunlin County alone had 24 seats.

In the raw file, the same candidate appears twice: once as a row for the whole district and once as rows for each township. After deduplicating by candidate ID, Yunlin was back to 6 seats and the nation back to 168.

The votes were worse: in 2004 Yunlin's township-level sum was 336,666 votes against 363,058 at district level; Taitung was even worse, with only 26,732 votes at township level against 97,244 at district level. The township data itself is incomplete and cannot be repaired. My handling: for these two counties in 2004 the township level is left blank and the county level uses the district sums, and the page says so.

What this pitfall taught me: sums I compute myself must be cross-checked against another level, and if they disagree, stop instead of drawing anyway.

Pitfall 3: one map object hiding two places

For township boundaries I used g0v's twgeojson. The raw file is 20 MB, and after simplifying with Douglas-Peucker (tolerance 0.001 degrees, about 100 meters) the township part is only 469 KB (about 100 KB compressed).

After simplification, Kaohsiung was missing a district: Namasia. It was in the data but not on the map. The raw file had merged the old Sanmin Township (now Namasia District) into the object for Sanmin District, so one MultiPolygon really holds two pieces, one in the city and one in the mountains. The fix was to split off, by position, the piece with longitude greater than 120.5 and name it separately.

Pitfall 4: selecting New Taipei covered Taipei

After the interaction was built, I clicked around myself and found: select New Taipei and Taipei disappears.

Taipei City is a "hole" in New Taipei City. I had only stored each polygon's outer outline and not the holes, so New Taipei's shape actually wrapped around the whole of Taipei. Normally that is fine because Taipei is drawn above New Taipei; but I move the selected county to the top layer (so its outline is visible), and once New Taipei moved up it covered Taipei, which could not even be clicked.

The fix was to store the holes in the path too and set fill-rule: evenodd in CSS. I also wrote a test: after selecting New Taipei, do elementFromPoint at Taipei's position and check that what is hit is Taipei.

Pitfall 5: one click and a big circle covered the whole map

Then something stranger: switch to "turnout" and click a county, and the whole map was covered by a big black circle.

At first I thought the turnout colors were miscomputed, but the data had no NaN and every county's fill was a normal color. Selecting a county programmatically also worked fine; it only broke with a real mouse click. In the end it turned out to be the browser's focus outline (outline: auto): I had given every path on the map a tabindex so the keyboard could operate it, the clicked county got focus, and the browser (I use a Chromium-based one) drew an enormous circular outline on the SVG that covered the entire map. After document.activeElement.blur() the circle vanished, which confirmed it.

The fix was to turn off the default outline and show my own stroke only for keyboard use (:focus-visible), and also to disable text selection on the map (even the two characters of a county name on the map were being highlighted, the same kind of problem). The test checks that the clicked element's outlineStyle is none, then clicks another county to confirm it can still be clicked.

License: what to write when you use someone else's data

The election database's license on the government open data platform is "Open Government Data License, version 1": commercial use and adaptation are allowed, but a credit statement in the specified format is mandatory, and without it you are treated as not licensed. I put the statement at the bottom of the page, and wrote that this is a personal hobby project, not official, and that the CEC's announcements are authoritative.

The harder part is two places I cannot fully confirm: the 2026 counting site's footer says only "© Central Election Commission" with no explicit open license that I could see; and although the map boundary package's author waived copyright, I could not find the license of the original boundary file. I wrote both points into the project's license document, and plan to wait until the CEC puts 2026 into the election database and to switch to the Ministry of the Interior's current boundary data.

Summary

What I learned building this map:

  1. To find data, look at the requests behind the page; there is often JSON far cleaner than scraping HTML.
  2. Before drawing each dataset, compare against one number you can verify elsewhere; sums at two levels must be cross-checked.
  3. Don't guess codes or names; use the list the data gives you.
  4. SVG holes, one object holding multiple places, focus outlines: these pitfalls all "look fine until a real person clicks", and need to be written as tests.
  5. Open data is not free of conditions: read the license terms in full, and put the credit statement in the specified format.

The map is at /tools/election-map (in Chinese). It covers mayors (1997–2026), president (1996–2024), legislature, councilors, village chiefs and national referendums, and only presents the data.

Type to search projects and posts too