Files
gods-eye-view/src/layers/perimeters/records.test.mjs
T
German Khokhlov 527068c126 feat(perimeters): colour fire perimeters by age
The Fire Perimeters layer now draws a snapshot of fires and the pieces of
their burned area, each piece coloured by its age at the snapshot's moment
on the WorldFX scale (under 6 h, 6-24 h, 1-3 days, older or unknown).
Containment stays on the card; the legend counts fires by last growth and
shows the source's note.

- One source method, getSnapshot, returning {at, fetchedAt, stale,
  revision, note, fires, pieces}; getSnapshotDetail is removed.
- WFIGS: one piece per polygon, born at poly_PolygonDateTime, else
  poly_DateCurrent; a future time is unknown.
- Outlines are drawn apart from fills, so cut pieces have no line along
  the cut; redraws happen only on a new revision or a change of age band.
- The card ages from `at` and adds "last growth ... ago".
- perimeterOptions: { inciweb: false } creates no InciWeb sources.
- get_fire_perimeters filters by pieces, places fires at their anchor and
  returns last_growth; spot conditions use the anchor.
2026-11-20 19:41:13 +04:00

324 lines
9.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
normalizeFirePerimeterSnapshot,
firePerimeterSnapshot,
} from './records.js';
const ring = [
[-108.1, 35.2],
[-108.0, 35.2],
[-108.0, 35.3],
[-108.1, 35.2],
];
function feature(overrides = {}) {
return {
id: 1,
geometry: { type: 'Polygon', coordinates: [ring] },
properties: {
poly_IncidentName: 'Fixture Fire',
attr_UniqueFireIdentifier: '2026-NMGNF-000123',
attr_IncidentSize: 512.5,
attr_PercentContained: 40,
attr_POOState: 'US-NM',
attr_IncidentTypeCategory: 'WF',
attr_FireDiscoveryDateTime: 1757900000000,
poly_DateCurrent: 1757950000000,
poly_PolygonDateTime: 1757940000000,
attr_FireCause: 'Natural',
attr_FireBehaviorGeneral: 'Active',
attr_TotalIncidentPersonnel: 380,
attr_POOCounty: 'Sandoval',
attr_EstimatedCostToDate: 4200000,
attr_IncidentComplexityLevel: 'Type 3 Incident',
attr_CpxName: 'ROWE CREEK COMPLEX',
...overrides.properties,
},
...overrides,
};
}
test('a valid snapshot yields one row per incident with polygons preserved', () => {
const rows = normalizeFirePerimeterSnapshot({
features: [
feature(),
feature({
id: 2,
geometry: {
type: 'MultiPolygon',
coordinates: [[ring], [ring]],
},
properties: { attr_UniqueFireIdentifier: '2026-AZASF-000456' },
}),
],
});
assert.equal(rows.length, 2);
assert.deepEqual(rows[0], {
stableId: '2026-NMGNF-000123',
name: 'Fixture Fire',
acres: 512.5,
containedPct: 40,
state: 'US-NM',
category: 'WF',
discoveredTime: 1757900000000,
updatedTime: 1757950000000,
polygonTime: 1757940000000,
cause: 'Natural',
behavior: 'Active',
personnel: 380,
county: 'Sandoval',
costToDate: 4200000,
complexity: 'Type 3 Incident',
complexName: 'ROWE CREEK COMPLEX',
polygons: [[ring]],
});
assert.equal(rows[1].polygons.length, 2);
});
test('a missing unique fire identifier falls back to the feature id', () => {
const rows = normalizeFirePerimeterSnapshot({
features: [feature({ properties: { attr_UniqueFireIdentifier: null } })],
});
assert.equal(rows[0].stableId, '1');
});
test('optional attributes normalize to null rather than leaking undefined', () => {
const rows = normalizeFirePerimeterSnapshot({
features: [
feature({
properties: {
poly_IncidentName: null,
attr_IncidentSize: null,
attr_PercentContained: null,
attr_POOState: null,
attr_IncidentTypeCategory: null,
attr_FireDiscoveryDateTime: null,
poly_DateCurrent: null,
poly_PolygonDateTime: null,
attr_FireCause: null,
attr_FireBehaviorGeneral: null,
attr_TotalIncidentPersonnel: null,
attr_POOCounty: null,
attr_EstimatedCostToDate: null,
attr_IncidentComplexityLevel: null,
attr_CpxName: null,
},
}),
],
});
assert.deepEqual(
{
name: rows[0].name,
acres: rows[0].acres,
containedPct: rows[0].containedPct,
state: rows[0].state,
category: rows[0].category,
discoveredTime: rows[0].discoveredTime,
updatedTime: rows[0].updatedTime,
polygonTime: rows[0].polygonTime,
cause: rows[0].cause,
behavior: rows[0].behavior,
personnel: rows[0].personnel,
county: rows[0].county,
costToDate: rows[0].costToDate,
complexity: rows[0].complexity,
complexName: rows[0].complexName,
},
{
name: null,
acres: null,
containedPct: null,
state: null,
category: null,
discoveredTime: null,
updatedTime: null,
polygonTime: null,
cause: null,
behavior: null,
personnel: null,
county: null,
costToDate: null,
complexity: null,
complexName: null,
},
);
});
test('a feed that is not a feature collection is rejected as a whole', () => {
for (const payload of [null, {}, { features: null }, { features: {} }]) {
assert.equal(normalizeFirePerimeterSnapshot(payload), null);
}
});
test('an invalid feature is skipped so one bad incident cannot blank the layer', () => {
const good = feature({
id: 2,
properties: { attr_UniqueFireIdentifier: 'good' },
});
for (const bad of [
feature({ geometry: null }),
feature({ geometry: { type: 'Point', coordinates: [0, 1] } }),
feature({
geometry: {
type: 'Polygon',
coordinates: [[[Infinity, 35.2], ...ring.slice(1)]],
},
}),
feature({
geometry: {
type: 'Polygon',
coordinates: [[[-200, 35.2], ...ring.slice(1)]],
},
}),
feature({ geometry: { type: 'Polygon', coordinates: [ring.slice(0, 2)] } }),
feature({ properties: null }),
feature({ id: null, properties: { attr_UniqueFireIdentifier: null } }),
]) {
const rows = normalizeFirePerimeterSnapshot({ features: [bad, good] });
assert.equal(rows.length, 1, 'bad feature must be skipped, not fatal');
assert.equal(rows[0].stableId, 'good');
}
});
test('a duplicate incident id keeps the first occurrence and skips the rest', () => {
const rows = normalizeFirePerimeterSnapshot({
features: [
feature(),
feature({
properties: {
attr_UniqueFireIdentifier: '2026-NMGNF-000123',
attr_IncidentSize: 999,
},
}),
],
});
assert.equal(rows.length, 1);
assert.equal(rows[0].acres, 512.5);
});
test('a perimeter with no rings is skipped rather than rendered empty', () => {
const rows = normalizeFirePerimeterSnapshot({
features: [
feature({ geometry: { type: 'Polygon', coordinates: [] } }),
feature({ id: 2, properties: { attr_UniqueFireIdentifier: 'other' } }),
],
});
assert.equal(rows.length, 1);
assert.equal(rows[0].stableId, 'other');
});
const NOW = 1758000000000;
const rowsOf = (...features) => normalizeFirePerimeterSnapshot({ features });
// feature() replaces all properties with the overrides' own: keep the rest.
const fixture = (id, properties) =>
feature({ id, properties: { ...feature().properties, ...properties } });
test('each WFIGS polygon becomes one piece of its fire, born when it was mapped', () => {
const hole = [
[-108.08, 35.22],
[-108.02, 35.22],
[-108.02, 35.25],
[-108.08, 35.22],
];
const snapshot = firePerimeterSnapshot(
rowsOf(
feature({
geometry: { type: 'MultiPolygon', coordinates: [[ring, hole], [ring]] },
}),
),
{ fetchedAt: NOW - 1000, now: NOW },
);
assert.equal(snapshot.at, null);
assert.equal(snapshot.stale, false);
assert.equal(snapshot.fetchedAt, NOW - 1000);
assert.equal(snapshot.note, 'Perimeters are simplified to about 100 m.');
assert.equal(snapshot.fires.length, 1);
const [fire] = snapshot.fires;
assert.equal(fire.id, '2026-NMGNF-000123');
assert.equal(fire.lastGrowthTime, 1757940000000);
assert.equal(fire.containedPct, 40);
assert.equal('stableId' in fire || 'polygons' in fire, false);
assert.ok(Math.abs(fire.anchor.lat - 35.2333) < 0.01);
assert.deepEqual(
snapshot.pieces.map(({ fireId, born, polygons, outline }) => [
fireId,
born,
polygons,
outline,
]),
[
['2026-NMGNF-000123', 1757940000000, [[ring, hole]], [ring, hole]],
['2026-NMGNF-000123', 1757940000000, [[ring]], [ring]],
],
'the outline is every ring, holes included',
);
});
test('a perimeter without its mapping time falls back to the last edit; a future time is unknown', () => {
const [fallback, future, futureEdit, none] = firePerimeterSnapshot(
rowsOf(
fixture(1, { poly_PolygonDateTime: null }),
fixture(2, {
attr_UniqueFireIdentifier: 'future',
poly_PolygonDateTime: NOW + 3600000,
}),
fixture(3, {
attr_UniqueFireIdentifier: 'future-edit',
poly_PolygonDateTime: null,
poly_DateCurrent: NOW + 3600000,
}),
fixture(4, {
attr_UniqueFireIdentifier: 'none',
poly_PolygonDateTime: null,
poly_DateCurrent: null,
}),
),
{ now: NOW },
).fires;
assert.equal(fallback.lastGrowthTime, 1757950000000);
assert.equal(future.lastGrowthTime, null);
assert.equal(futureEdit.lastGrowthTime, null);
assert.equal(none.lastGrowthTime, null);
// A clock a few minutes ahead of ours is not the future.
const skewed = firePerimeterSnapshot(
rowsOf(fixture(1, { poly_PolygonDateTime: NOW + 60000 })),
{ now: NOW },
);
assert.equal(skewed.fires[0].lastGrowthTime, NOW + 60000);
assert.equal(skewed.pieces[0].born, NOW + 60000);
});
test('the revision follows fire ids and update times, not feed order', () => {
const a = feature();
const b = fixture(2, { attr_UniqueFireIdentifier: 'b' });
const revision = (...features) =>
firePerimeterSnapshot(rowsOf(...features), { now: NOW }).revision;
assert.equal(revision(a, b), revision(b, a));
assert.notEqual(
revision(a, b),
revision(
a,
fixture(2, { attr_UniqueFireIdentifier: 'b', poly_DateCurrent: 1 }),
),
);
assert.notEqual(revision(a, b), revision(a));
});
test('the snapshot carries staleness and skips rows it cannot place', () => {
const snapshot = firePerimeterSnapshot(
[
{ stableId: 'empty', polygons: [] },
{ polygons: [[ring]] },
...rowsOf(feature()),
],
{ stale: true, fetchedAt: 'yesterday', now: NOW },
);
assert.equal(snapshot.stale, true);
assert.equal(snapshot.fetchedAt, null);
assert.deepEqual(
snapshot.fires.map(({ id }) => id),
['2026-NMGNF-000123'],
);
});