PageSourceSearch

https://sogn.dk/maps/js/sognegraenser/api.js

js sogn.dk collected 2026-10-02 07:41:37 UTC 1,799 bytes, 57 lines download raw bytes

1/*
2 * Server-kommunikation for sognegrænsekortet:
3 *  - XML-parsing (DOMParser) med fejl-detektion
4 *  - Opslag af nærmeste stift/provsti/sogn ud fra en koordinat
5 *    via /api/mobil/soeg.xml (erstatter v2's synkrone XHR).
6 */
7
8/** Kastes når et KML-endpoint svarer uden placemarks ("0 records", tom body m.v.). */
9export class EmptyLayerError extends Error {
10	constructor(url) {
11		super('Tomt kortlag: ' + url);
12		this.name = 'EmptyLayerError';
13	}
14}
15
16/** Parser XML-tekst. Returnerer Document, eller null hvis dokumentet er ugyldigt. */
17export function parseXml(text) {
18	const doc = new DOMParser().parseFromString(text, 'text/xml');
19	if (doc.getElementsByTagName('parsererror').length > 0) {
20		return null;
21	}
22	return doc;
23}
24
25/** Henter en URL og parser svaret som XML. Returnerer null ved HTTP-fejl eller ugyldig XML. */
26export async function fetchXml(url) {
27	const res = await fetch(url);
28	if (!res.ok) {
29		return null;
30	}
31	return parseXml(await res.text());
32}
33
34/**
35 * Slår nærmeste kirke op for en koordinat og returnerer de tilhørende id'er.
36 * Svarformat: <re cnt=".."><it sid="sogneId"><stid>stiftId</stid><pid>provstiId</pid>...</it></re>
37 * (fejl giver <err>-rod). Returnerer {stiftId, provstiId, sogneId} eller null.
38 */
39export async function resolveIdsAt(latLng) {
40	const url = '/api/mobil/soeg.xml?q=' + latLng.lat() + ',' + latLng.lng() + '&r=-1';
41	try {
42		const doc = await fetchXml(url);
43		const it = doc && doc.getElementsByTagName('it')[0];
44		if (!it || it.childNodes.length === 0) {
45			return null;
46		}
47		const stid = it.getElementsByTagName('stid')[0];
48		const pid = it.getElementsByTagName('pid')[0];
49		return {
50			stiftId: stid ? stid.textContent : 0,
51			provstiId: pid ? pid.textContent : 0,
52			sogneId: it.getAttribute('sid') || 0,
53		};
54	} catch {
55		return null;
56	}
57}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.