election-game/web/scripts/build-map-context.mjs

30 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 產生全台縣市界線(簡化),給開始畫面選縣市、以及各縣市地圖周邊的底圖用;不含任何選舉資料。
// 來源:後端全國資料包的鄉鎮界線(內政部國土測繪中心,政府資料開放授權條款)。
// 執行:node scripts/build-map-context.mjs
import { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const backend = fileURLToPath(new URL("../../backend/", import.meta.url));
const out = fileURLToPath(new URL("../public/maps/taiwan-counties.json", import.meta.url));
const NAMES = {
"63000": "臺北市", "64000": "高雄市", "65000": "新北市", "66000": "臺中市", "67000": "臺南市", "68000": "桃園市",
"10002": "宜蘭縣", "10004": "新竹縣", "10005": "苗栗縣", "10007": "彰化縣", "10008": "南投縣", "10009": "雲林縣",
"10010": "嘉義縣", "10013": "屏東縣", "10014": "臺東縣", "10015": "花蓮縣", "10016": "澎湖縣", "10017": "基隆市",
"10018": "新竹市", "10020": "嘉義市", "09007": "連江縣", "09020": "金門縣",
};
execFileSync(
"npx",
[
"mapshaper",
"-i", "data/build/packs/00000/topology.json",
"-target", "towns",
"-each", `county=townCode.slice(0,5), name=(${JSON.stringify(NAMES)})[townCode.slice(0,5)] || ''`,
"-filter", "name !== ''",
"-dissolve", "county", "copy-fields=name",
"-simplify", "2.5%", "keep-shapes",
"-o", out, "format=geojson", "precision=0.0002",
],
{ cwd: backend, stdio: "inherit" },
);