Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ public/vendor/leaflet/** -text
public/vendor/leaflet.heat/** -text
public/vendor/chart.js/** -text
public/vendor/swagger-ui-dist/** -text

# #1035: the vendored meshguide.dk files are hash-pinned (internal/scopeguide/meshguide/README.md).
internal/scopeguide/meshguide/** -text
5 changes: 5 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ jobs:
# above compile it but never run its tests, and it now holds the
# hashRegions/hashRegionsPath loader both processes share (#360).
(cd internal/regions && go test ./...)
# internal/scopeguide (#1035 M1): meshguide.dk's scope rules (vendored,
# pinned) as a per-postnummer table + a neutral verdict. Its own
# module; nothing imports it until M2, so its tests run here.
(cd internal/scopeguide && go vet ./... && go test -race ./...)
(cd internal/scopeguide && go test -count=1 -run 'TestRecommendCompare5kWithinBudget|TestRecommendCompareAllocs|TestPostnummerLookupConstantTime|TestGuideMemoryBounded|TestConformanceWithMeshguideEngine' -v . | grep -E '^(=== RUN|--- |ok|FAIL|PASS)|nodes:|target|allocs per call|lookup:|guide heap|points')

# internal/anomaly is its own module (not wired into any binary), so the
# server/ingestor steps above do not reach it. No -fuzz and no -bench:
Expand Down
3 changes: 3 additions & 0 deletions internal/scopeguide/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/meshcore-analyzer/scopeguide

go 1.22
11 changes: 11 additions & 0 deletions internal/scopeguide/meshguide/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2026 drkt <meshcore@drkt.eu>

Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
30 changes: 30 additions & 0 deletions internal/scopeguide/meshguide/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Vendored meshguide.dk scope engine (#1035)

These files are copied **unmodified** from
[github.com/twinoak/meshguide.dk](https://github.com/twinoak/meshguide.dk) at commit
`9a5d7f5fd210d863067f87581e75eaf562327d71` (2026-11-11). They are licensed under the
WTFPL v2 (see `LICENSE`, copied from the same commit).

| File | Upstream path | sha256 |
|---|---|---|
| `lib/scopes.js` | `lib/scopes.js` | `88d658b7ba928e960e1d7ce387b909ddab7d8e3243ff6e37241993711268a32c` |
| `regions.json` | `regions.json` | `1076248933dc1c78fe37832605ca61470cca8a0fbcbd207c655237d53ef78817` |
| `postnumre/index.json` | `postnumre/index.json` | `72586f3b62a5af8971cf093f8c7c9b34654a04de33121819b3e7f2b27646b624` |
| `postnumre/fyn.json` | `postnumre/fyn.json` | `8bc78645ab065da0a63b9c2611b91ee01dfa33078a5d0632ef0112d6f91da213` |
| `postnumre/sjaelland.json` | `postnumre/sjaelland.json` | `2a5ea0d9c708346ea3ef5924f413a80cd9b88c158c35ae3f926babe43265300e` |
| `LICENSE` | `LICENSE` | `997326d9ba812e87b8e6fad3c1dc7530f58624c9f4d90b9521f0938b5a03dc60` |

How they are used:

- `scripts/gen-scope-guide.mjs` imports `lib/scopes.js` and runs it offline over
`regions.json` and the two postnummer files. It writes
`internal/scopeguide/scopeguide-table.json` (the per-postnummer table) and
`internal/scopeguide/testdata/conformance.json` (meshguide's own answers for a grid of
points, which the Go code must reproduce). Run it by hand; the output is checked in.
- At runtime the Go package embeds `regions.json` (for the region hit test) and the
generated table. The postnummer files are only read by the generator.

`.gitattributes` marks this directory `-text`, so the bytes (and the hashes above)
survive checkout. `test-1035-scope-guide.js` and the Go tests check the hashes. To update,
replace the files with a newer upstream commit, update this README, rerun the generator and
review the diff of the table.
323 changes: 323 additions & 0 deletions internal/scopeguide/meshguide/lib/scopes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
// scopes.js - the scope engine for the MeshCore-DK map.
//
// Pure functions only: no DOM, no fetch, no Node APIs. The same module is
// imported by the browser (flow.js and app.js, on map click) and by the build step
// (tools/build-api.js, which writes the static /api JSON files), so there is
// exactly one source of the scope rules. If a rule changes (NEIGHBOR_DIST_M,
// the layer convention, regionDefLines) it changes HERE.
//
// A "dataset" is the decoded regions.json plus all postnumre/*.json files
// merged, with precomputed bounding boxes - see buildDataset(). The caller
// decides how the JSON gets loaded (fetch in the browser, fs in Node).

export const M_PER_DEG = 111320; // meters per degree latitude (and longitude at the equator)
export const NEIGHBOR_DIST_M = 2000; // a postal code is a neighbor if the border lies <= this
export const DEF_LIMIT = 160; // the repeater's serial line limit, in bytes

// The fixed top-level scopes that are not derived from geometry, with their
// parent in the scope tree: * -> {eu, europe} -> dk -> everything else. 'eu'
// and 'europe' are both in use and redundant (like #dk/#danmark), but both must
// exist. This is the only source of the fixed scopes - both the static
// scopes.json and the CLI blocks read from here.
export const FIXED_SCOPE_PARENTS = Object.freeze({
eu: "*",
europe: "*",
dk: "eu"
});

const POSTAL_KEY = /^dk(\d{4})$/;

// --- Geometry primitives ----------------------------------------------------

export function ringsOf(geom) {
if (!geom) return [];
if (geom.type === "Polygon") return geom.coordinates;
if (geom.type === "MultiPolygon") {
const out = [];
for (const poly of geom.coordinates) for (const ring of poly) out.push(ring);
return out;
}
return [];
}

// [minx, miny, maxx, maxy]; Infinity/-Infinity when the geometry has no points.
export function geomBBox(geom) {
let minx = Infinity, miny = Infinity, maxx = -Infinity, maxy = -Infinity;
for (const ring of ringsOf(geom)) {
for (const pt of ring) {
const x = pt[0], y = pt[1];
if (x < minx) minx = x;
if (x > maxx) maxx = x;
if (y < miny) miny = y;
if (y > maxy) maxy = y;
}
}
return [minx, miny, maxx, maxy];
}

// Ray-casting point-in-polygon. Point and rings in [lng, lat].
function pointInRing(x, y, ring) {
let inside = false;
const n = ring.length;
for (let i = 0, j = n - 1; i < n; j = i++) {
const xi = ring[i][0], yi = ring[i][1];
const xj = ring[j][0], yj = ring[j][1];
const intersect = ((yi > y) !== (yj > y)) &&
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}

function pointInPolygon(x, y, rings) {
if (!rings.length) return false;
if (!pointInRing(x, y, rings[0])) return false;
for (let i = 1; i < rings.length; i++) {
if (pointInRing(x, y, rings[i])) return false; // hole
}
return true;
}

export function pointInGeom(x, y, geom) {
if (!geom) return false;
if (geom.type === "Polygon") return pointInPolygon(x, y, geom.coordinates);
if (geom.type === "MultiPolygon") {
for (const poly of geom.coordinates) {
if (pointInPolygon(x, y, poly)) return true;
}
return false;
}
return false;
}

// Distance from point to line segment in meters (local equirectangular projection).
function segDistM(p, a, b, sx, sy) {
const px = p[0] * sx, py = p[1] * sy;
const ax = a[0] * sx, ay = a[1] * sy;
const bx = b[0] * sx, by = b[1] * sy;
const dx = bx - ax, dy = by - ay;
const len = dx * dx + dy * dy;
let t = len ? ((px - ax) * dx + (py - ay) * dy) / len : 0;
t = t < 0 ? 0 : (t > 1 ? 1 : t);
return Math.hypot(px - (ax + t * dx), py - (ay + t * dy));
}

// Smallest distance from g1's corners to g2's edges; short-circuits <= limit.
function boundaryDistM(g1, g2, sx, sy, limit) {
let best = Infinity;
const r2 = ringsOf(g2);
for (const ring of ringsOf(g1)) {
for (const p of ring) {
for (const ring2 of r2) {
const m = ring2.length;
for (let i = 0; i < m - 1; i++) {
const d = segDistM(p, ring2[i], ring2[i + 1], sx, sy);
if (d < best) {
best = d;
if (best <= limit) return best;
}
}
}
}
}
return best;
}

// --- Dataset ----------------------------------------------------------------

// regions: decoded regions.json. postnumreFiles: the decoded postnumre/*.json
// files in manifest order; on a duplicate key the first file wins.
export function buildDataset(regions, postnumreFiles) {
const postnumre = {};
for (const file of postnumreFiles) {
if (!file || typeof file !== "object") continue;
for (const [k, v] of Object.entries(file)) {
if (postnumre[k] === undefined) postnumre[k] = v;
}
}
const rbb = {};
for (const [k, v] of Object.entries(regions)) {
if (v && v.geometry) rbb[k] = geomBBox(v.geometry);
}
const pbb = {};
for (const [k, v] of Object.entries(postnumre)) {
if (v && v.geometry) pbb[k] = geomBBox(v.geometry);
}
return { regions, postnumre, rbb, pbb, neighborCache: new Map() };
}

// --- Scope derivation -------------------------------------------------------

// The layer convention for a 4-digit postal code: dk5230 -> dk5, dk52, dk523, dk5230.
export function postalLayers(digits) {
return ["dk" + digits[0], "dk" + digits.slice(0, 2), "dk" + digits.slice(0, 3), "dk" + digits];
}

// The distinct 2-digit prefixes (dkXY) for postal codes bordering key.
function neighborPrefixesFor(ds, key) {
const cached = ds.neighborCache.get(key);
if (cached) return cached;

const self = ds.postnumre[key];
if (!self || !self.geometry) {
ds.neighborCache.set(key, []);
return [];
}

const bb = ds.pbb[key] || geomBBox(self.geometry);
const refLat = (bb[1] + bb[3]) / 2;
const sx = M_PER_DEG * Math.cos(refLat * Math.PI / 180);
const sy = M_PER_DEG;
const padLon = NEIGHBOR_DIST_M / sx;
const padLat = NEIGHBOR_DIST_M / sy;

const seen = new Set();
for (const [k, e] of Object.entries(ds.postnumre)) {
if (k === key) continue;
const m = POSTAL_KEY.exec(k);
if (!m) continue;
if (!e || !e.geometry) continue;
const ob = ds.pbb[k] || geomBBox(e.geometry);
if (ob[0] > bb[2] + padLon || ob[2] < bb[0] - padLon ||
ob[1] > bb[3] + padLat || ob[3] < bb[1] - padLat) continue;
const d = Math.min(
boundaryDistM(self.geometry, e.geometry, sx, sy, NEIGHBOR_DIST_M),
boundaryDistM(e.geometry, self.geometry, sx, sy, NEIGHBOR_DIST_M)
);
if (d <= NEIGHBOR_DIST_M) seen.add("dk" + m[1].slice(0, 2));
}
const res = [...seen].sort();
ds.neighborCache.set(key, res);
return res;
}

// Expands a hit key into its full scope hierarchy. For postal code keys
// (dk####) the layer convention dk5 -> dk5x -> dk5xx -> dk5230 is followed; at
// dk5x the own 2-digit prefix PLUS the neighbors' are included, sorted.
export function scopesFor(ds, key) {
const m = POSTAL_KEY.exec(key);
if (!m) return [key];
const d = m[1];
const layer2 = [...new Set(["dk" + d.slice(0, 2), ...neighborPrefixesFor(ds, key)])].sort();
return ["dk" + d[0], ...layer2, "dk" + d.slice(0, 3), "dk" + d];
}

// The entire scope universe: the fixed top-level scopes (eu, europe, dk) plus
// every region key plus every postal code expanded to its prefix layers
// (dk5230 -> dk5, dk52, dk523, dk5230), flattened into one deduplicated,
// sorted list. Neighbor derivation is NOT included - it is point-specific and
// makes no sense for the whole dataset; but because a postal code like 5000
// exists, its 2-digit prefix dk50 shows up on its own.
export function allScopes(ds) {
const seen = new Set(Object.keys(FIXED_SCOPE_PARENTS));
for (const k of Object.keys(ds.regions)) seen.add(k);
for (const k of Object.keys(ds.postnumre)) {
const m = POSTAL_KEY.exec(k);
if (!m) { seen.add(k); continue; }
for (const s of postalLayers(m[1])) seen.add(s);
}
return [...seen].sort();
}

// Flat region tree: * -> {eu, europe} -> dk -> all other scopes.
export function parentScope(key) {
return Object.hasOwn(FIXED_SCOPE_PARENTS, key) ? FIXED_SCOPE_PARENTS[key] : "dk";
}

const utf8 = new TextEncoder();
function byteLength(s) {
return utf8.encode(s).length;
}

// Builds 'region def' lines (<= DEF_LIMIT bytes) for an ordered scope list
// (parent always before child). Each node is placed under the logical cursor;
// the form name|jump pops the cursor back up so siblings can be placed. If a
// split is needed, continuation lines lead with eu|<node> to reposition the
// cursor without changing the tree (eu's parent is really *, so re-putting it
// under the root is a no-op).
export function regionDefLines(scopes) {
const prefix = "region def ";
const lines = [];
const n = scopes.length;
let i = 0;
let lead = null;
while (i < n) {
const parts = lead !== null ? [lead] : [];
const minParts = parts.length;
while (i < n) {
const node = scopes[i];
let jump = null;
if (i < n - 1) {
const np = parentScope(scopes[i + 1]);
if (np !== node) jump = np;
}
const token = jump !== null ? node + "|" + jump : node;
const candidate = [...parts, token];
if (byteLength(prefix + candidate.join(" ")) > DEF_LIMIT && parts.length > minParts) break;
parts.push(token);
i++;
}
lines.push(prefix + parts.join(" "));
lead = i < n ? "eu|" + parentScope(scopes[i]) : null;
}
return lines;
}

// --- Point lookup -----------------------------------------------------------

// Everything for one point: the hit polygons (regions + postal codes), the
// expanded scope hierarchy, the finished CLI blocks for the repeater (two
// firmware variants; null when nothing is hit) and the geometry of the hit
// polygons so the caller can draw the highlight.
export function scopesForPoint(ds, lat, lon) {
// Hit test: regions first, then postal codes. The bbox pre-filter rejects
// distant polygons with four comparisons before the ray-cast.
const hits = [];
for (const [k, v] of Object.entries(ds.regions)) {
const bb = ds.rbb[k];
if (!bb || lon < bb[0] || lon > bb[2] || lat < bb[1] || lat > bb[3]) continue;
if (pointInGeom(lon, lat, v.geometry)) hits.push(k);
}
for (const [k, v] of Object.entries(ds.postnumre)) {
const bb = ds.pbb[k];
if (!bb || lon < bb[0] || lon > bb[2] || lat < bb[1] || lat > bb[3]) continue;
if (pointInGeom(lon, lat, v.geometry)) hits.push(k);
}

// Expand hits into the ordered, unique scope hierarchy.
const seen = new Set();
const scopes = [];
for (const k of hits) {
for (const s of scopesFor(ds, k)) {
if (!seen.has(s)) { seen.add(s); scopes.push(s); }
}
}

const features = [];
for (const k of hits) {
const entry = ds.regions[k] ?? ds.postnumre[k];
if (entry && entry.geometry) {
features.push({ type: "Feature", properties: { region: k }, geometry: entry.geometry });
}
}

let cli = null;
if (scopes.length) {
const all = [...Object.keys(FIXED_SCOPE_PARENTS), ...scopes];
const oldCli = all.map(s => "region put " + s + "\nregion allowf " + s).join("\n") + "\nregion save";
const newCli = regionDefLines(all).join("\n") + "\nregion save";
cli = {
firmware_1_16_0_plus: newCli,
firmware_1_12_0_to_1_15_0: oldCli
};
}

return {
lat,
lon,
hits,
scopes,
cli,
features: { type: "FeatureCollection", features }
};
}
Loading
Loading