list_postcodes_in_canton
Retrieve Swiss postal codes for any canton using canton codes or names. Access official Swiss geodata to find all postcodes in specific cantons.
Instructions
List all Swiss postcodes (PLZ) in a given canton. Accepts 2-letter canton codes (ZH, BE, GR…) or full names. Source: Swiss federal geodata (swisstopo).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| canton | Yes | Canton code (e.g. "ZH", "BE", "GR") or full name (e.g. "Zürich", "Bern", "Graubünden") |
Implementation Reference
- src/modules/post.ts:305-370 (handler)The handler function in the switch block that fetches and processes the postcode data for a specified canton.
case "list_postcodes_in_canton": { const cantonInput = String(args.canton ?? "").trim(); if (!cantonInput) { throw new Error("canton must not be empty."); } const cantonCode = resolveCantonCode(cantonInput); // 1. Get canton bounding box const cantonUrl = buildUrl(`${BASE}/rest/services/api/MapServer/find`, { layer: CANTON_LAYER, searchText: cantonCode, searchField: "ak", returnGeometry: true, sr: 4326, }); const cantonData = await fetchJSON<CantonFindResponse>(cantonUrl); if (!cantonData.results.length) { throw new Error(`Canton not found: "${cantonInput}"`); } const cantonResult = cantonData.results[0]; const [minX, minY, maxX, maxY] = cantonResult.bbox; const cantonAttr = cantonResult.attributes; const mapExtent = `${minX},${minY},${maxX},${maxY}`; // 2. Identify PLZ features within the canton bbox const identifyUrl = buildUrl(`${BASE}/rest/services/api/MapServer/identify`, { geometry: mapExtent, geometryType: "esriGeometryEnvelope", layers: `all:${PLZ_LAYER}`, mapExtent, imageDisplay: "1000,1000,96", tolerance: 0, sr: 4326, returnGeometry: false, }); const plzData = await fetchJSON<{ results: PlzFindResult[] }>(identifyUrl); const postcodes = plzData.results .map((r) => ({ postcode: r.attributes.plz, locality: r.attributes.langtext, })) .sort((a, b) => a.postcode - b.postcode); // Deduplicate by PLZ const seen = new Set<number>(); const unique = postcodes.filter((e) => { if (seen.has(e.postcode)) return false; seen.add(e.postcode); return true; }); return JSON.stringify({ canton: { code: cantonAttr.ak, name: cantonAttr.name }, count: unique.length, postcodes: unique, note: unique.length >= 200 ? "Results may be capped at 200 by the API. Cross-border PLZ entries near canton boundaries may be included." : undefined, source: "swisstopo — Amtliches Ortschaftenverzeichnis", }); } - src/modules/post.ts:170-185 (registration)The tool registration definition containing the name, description, and input schema for 'list_postcodes_in_canton'.
{ name: "list_postcodes_in_canton", description: "List all Swiss postcodes (PLZ) in a given canton. Accepts 2-letter canton codes (ZH, BE, GR…) or full names. Source: Swiss federal geodata (swisstopo).", inputSchema: { type: "object", required: ["canton"], properties: { canton: { type: "string", description: "Canton code (e.g. \"ZH\", \"BE\", \"GR\") or full name (e.g. \"Zürich\", \"Bern\", \"Graubünden\")", }, }, }, },