import test from 'node:test';
import assert from 'node:assert/strict';
import { Window } from 'happy-dom';
import {
readGeoJsonFeatures,
readKmlFeatures,
rhumbPath,
} from './geoImportFeatures.js';
const { DOMParser } = new Window();
/** A KML document around `body`, parsed. */
const kml = (body) =>
new DOMParser().parseFromString(
'' +
'' +
`${body}`,
'application/xml',
);
const point = (coordinates = '1,2,0', extra = '') =>
`${extra}${coordinates}`;
test('a placemark takes its last inline style over its shared one, property by property', () => {
const { features } = readKmlFeatures(
kml(`
normal#shared
highlight#other
shared#map${point()}
inline#shared
${point()}
none${point()}
default${point()}`),
);
const [shared, inline, none, fallback] = features;
assert.equal(shared.image, 'data:image/png;base64,AAAA');
assert.equal(shared.scale, 2);
assert.deepEqual(shared.color, [1, 0, 0, 1], 'aabbggrr is red');
// Only the last inline style counts; the shared one fills what it leaves.
assert.equal(inline.scale, 0.5);
assert.deepEqual(inline.color, [1, 0, 0, 1]);
assert.equal(inline.image, 'data:image/png;base64,AAAA');
assert.equal(none.image, null, 'an empty Icon draws no icon');
assert.match(fallback.image.svg, /rgba\(255,255,0,1\)/, 'the yellow pin');
for (const f of features) {
assert.deepEqual(f.size, [32, 32]);
assert.deepEqual(f.scaleByDistance, {
near: 2414016,
nearValue: 1,
far: 1.6093e7,
farValue: 0.1,
});
assert.equal(f.offsetScalesByDistance, true, 'its offset shrinks too');
}
});
test('an icon hot spot and heading place and turn the icon', () => {
const { features } = readKmlFeatures(
kml(`
${point()}
${point()}`),
);
// Standing on its bottom centre: up by half its 25.6 px height.
assert.deepEqual(features[0].pixelOffset, { x: 0, y: -12.8 });
assert.equal(features[0].course, 90);
assert.deepEqual(features[1].pixelOffset, { x: 12, y: 8 });
assert.equal(features[1].course, undefined, 'heading 0 is no heading');
});
test('points, paths and polygons keep their altitude modes', () => {
const { features } = readKmlFeatures(
kml(`
${point('1,2,30')}
${point('1,2,30', 'relativeToGround')}
${point('1,2,30', 'absolute')}
#s1
1,2 3,4
#s1
absolute1,2,600 3,4,900
#s1
relativeToGround1,2,150 3,4,150
#s
0,0 1,0 1,1 0,0
0.1,0.1 0.2,0.1 0.2,0.2 0.1,0.1
1absolute
0,0,200 1,0,200 1,1,200 0,0,200
0,0 1,0 1,1 0,0`),
);
const [
clamped,
relative,
absolute,
draped,
inAir,
wall,
ground,
block,
bare,
] = features;
assert.deepEqual(
[clamped.mode, relative.mode, absolute.mode],
['clamp', 'relative', 'absolute'],
);
assert.equal(relative.height, 30);
assert.deepEqual(
[draped.mode, draped.width, draped.color],
['draped', 6, [1, 1, 0, 1]],
);
assert.deepEqual([inAir.mode, inAir.coords[1]], ['3d', [3, 4, 900]]);
assert.equal(wall.kind, 'wall');
assert.deepEqual(wall.fill, [0, 0, 1, 0x66 / 255]);
assert.deepEqual(wall.outline, [1, 1, 0, 1], 'outlined in its line colour');
assert.equal(wall.outlineWidth, 6);
assert.equal(ground.clamp, true);
assert.equal(ground.rings.length, 2, 'with its hole');
assert.deepEqual(ground.fill, [0, 0, 1, 0x66 / 255]);
assert.equal(block.clamp, false);
assert.equal(block.extrude, true);
assert.deepEqual(block.fill, [1, 1, 1, 1], 'white by default');
assert.deepEqual(block.outline, [1, 1, 1, 1]);
assert.equal(bare.fill, null);
assert.equal(bare.outline, null);
});
test('ground overlays are boxes turned by their rotation, or quads', () => {
const { features, drawable } = readKmlFeatures(
kml(`
boxblob:packed
absolute120
21200-10
20
80ff0000
0,0 1,0 1,1 0,1`),
);
const [box, quad] = features;
assert.equal(drawable, true);
assert.deepEqual(box.rect, { west: -10, south: 1, east: -160, north: 2 });
assert.ok(Math.abs(box.rotation - (20 * Math.PI) / 180) < 1e-12);
assert.equal(box.image, 'blob:packed');
assert.equal(box.color, undefined, 'an image is drawn untinted');
assert.equal(box.height, 120);
assert.equal(quad.quad.length, 4);
assert.deepEqual(quad.color, [0, 0, 1, 128 / 255]);
assert.equal(quad.height, undefined, 'on the ground');
});
test('map objects are counted as before: folders, placemarks and each part', () => {
const read = readKmlFeatures(
kml(`
f
${point()}1,2 3,4
${point()}
only the first geometry${point()}${point('5,6')}
nowhere`),
);
// Folder, two placemarks and the empty one, three parts and one nested.
assert.equal(read.entityCount, 8);
assert.deepEqual(
read.features.map((f) => f.kind),
['pin', 'line', 'pin', 'pin'],
);
assert.deepEqual([read.features[3].lon, read.features[3].lat], [1, 2]);
assert.equal(read.drawable, true);
assert.equal(
readKmlFeatures(
kml(
'',
),
).drawable,
false,
'a placemark with no icon and nowhere to stand draws nothing',
);
});
test('GeoJSON draws in one colour: pins on the ground, draped lines, quarter fills', () => {
const read = readGeoJsonFeatures(
{
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: { name: 'a' },
geometry: {
type: 'MultiPoint',
coordinates: [
[1, 2],
[3, 4],
],
},
},
{
type: 'Feature',
properties: {},
geometry: {
type: 'LineString',
coordinates: [
[1, 2],
[3, 4],
],
},
},
{
type: 'Feature',
properties: {},
geometry: {
type: 'MultiPolygon',
coordinates: [
[
[
[0, 0],
[1, 0],
[1, 1],
[0, 0],
],
],
[
[
[2, 2],
[3, 2],
[3, 3],
[2, 2],
],
],
],
},
},
{
type: 'Feature',
properties: {},
geometry: {
type: 'GeometryCollection',
geometries: [{ type: 'Point', coordinates: [5, 6] }],
},
},
{ type: 'Feature', properties: {}, geometry: null },
],
},
'#39d0ff',
);
assert.equal(read.entityCount, 7);
const [a, b, line, p1, p2, gc] = read.features;
assert.equal(a.name, 'a');
assert.deepEqual([a.mode, a.anchor, a.size], ['clamp', 'bottom', [48, 48]]);
assert.equal(a.image, b.image, 'one pin image for the file');
assert.deepEqual([line.mode, line.width], ['draped', 3]);
assert.deepEqual(line.color, [0x39 / 255, 0xd0 / 255, 1, 1]);
assert.deepEqual(p1.fill, [0x39 / 255, 0xd0 / 255, 1, 0.25]);
assert.equal(p1.outline, null);
assert.equal(p2.clamp, true);
assert.equal(gc.kind, 'pin');
assert.equal(read.drawable, true);
});
test('a ground overlay without bounds, or upside down, draws nowhere', () => {
const { features, drawable } = readKmlFeatures(
kml(`
ff00ff00
ff00ff00
1234`),
);
assert.deepEqual(
features.map((f) => [f.kind, f.rect, f.quad]),
[
['overlay', null, undefined],
['overlay', null, undefined],
],
);
assert.equal(drawable, true, 'counted as before');
});
test('a hot spot without units is a fraction', () => {
const { features } = readKmlFeatures(
kml(
`${point()}`,
),
);
assert.deepEqual(features[0].pixelOffset, { x: 0, y: -16 });
});
test('GeoJSON lines and rings follow rhumb lines, as they were drawn', () => {
const path = rhumbPath([
[0, 60, 0],
[10, 60, 0],
]);
assert.equal(path.length, 21, 'half a degree apart');
assert.ok(
path.every(([, lat]) => Math.abs(lat - 60) < 1e-9),
'along the parallel',
);
const across = rhumbPath([
[179, 0, 0],
[-179, 0, 0],
]);
assert.deepEqual(
across.map(([lon]) => lon),
[179, 179.5, 180, -179.5, -179],
'the short way over the antimeridian',
);
const short = rhumbPath([
[0, 0, 0],
[0.2, 0.2, 0],
]);
assert.equal(short.length, 2, 'short steps are left alone');
const { features } = readGeoJsonFeatures(
{
type: 'LineString',
coordinates: [
[0, 60],
[10, 60],
],
},
'#39d0ff',
);
assert.equal(features[0].coords.length, 21);
});
test('rhumb steps stop once a file has added as many points as it may hold', () => {
const budget = { left: 30 };
const zigzag = Array.from({ length: 6 }, (_, i) => [i % 2 ? 10 : 0, 0, 0]);
const path = rhumbPath(zigzag, budget);
// Each 10-degree step adds 19 points: one fits, the rest are drawn as given.
assert.equal(path.length, 6 + 19);
assert.equal(budget.left, 11);
});