Draw imported files through the draw port, off Cesium

Import no longer loads KML, KMZ, GeoJSON and GPX through Cesium's data
source loaders. geoImportFeatures.js reads the sanitized file into plain
pins, lines, walls, polygons and ground overlays with the looks those
loaders gave them: icon, scale, colour, heading and hot spot, the shared
and inline style precedence, line and polygon colours and widths,
extruded and clamped geometry, rotated boxes and quads, and GeoJSON's
48 px pins, draped lines along rhumb lines and quarter-opacity fills.
geoImportPins.js draws them in three draw sets per file, places ground
pins through the height service (0 on the bare ellipsoid, whose coarse
tiles sag below it; the mesh nearby on photoreal maps), follows a
double-clicked pin, frames Fly-to, and shows, hides and removes them.

The Cesium adapter now draws two members the contract already had: an
icon's pixel offset and the wall shape. Image fills draw in the
translucent pass, so an overlay's own alpha blends as it did.

The Cesium allow-list is now empty; the ratchet test checks that its
scan still sees each way of reaching Cesium. The QA scripts read what a
file drew through the import's drawn(id), and the large-file check
compares it with Cesium's own reading of the same KML. A file the
renderer refuses to draw is refused by itself, without stopping its
batch. Two translations no longer used are dropped.
This commit is contained in:
Sameh Khamis committed 2026-11-09 15:50:53 -08:00
1 parent c073064507
commit 6c1f91014c
33 files changed
+2608 -1177

No files matched your search

+30 -34
View File
@@ -1,10 +1,7 @@
/** Session-local geodata loading with bounded geometry and local-only file references. */
import { createImportImageBudget } from './geoImportImages.js';
import {
loadImportedGeoJson,
loadImportedKml,
settleImportedSource,
} from './geoImportPins.js';
import { readGeoJsonFeatures, readKmlFeatures } from './geoImportFeatures.js';
import { settleImportedDrawing } from './geoImportPins.js';
import {
GeoImportError,
MAX_IMPORT_ENTITIES,
@@ -204,8 +201,8 @@ export function countGeoJsonGeometry(
MultiPolygon: 3,
}[node.type];
if (depth !== undefined) {
// Multipart geometries create one Cesium entity per component, even
// when the document wraps them in just one Feature.
// Multipart geometries are one map object per component, even when
// the document wraps them in just one Feature.
entities += node.type.startsWith('Multi')
? Array.isArray(node.coordinates)
? node.coordinates.length
@@ -285,7 +282,7 @@ function overLimitError({ over, features }) {
}
// The import uses WGS84 ground footprints and a short name only. Remove
// loader-sensitive metadata before Cesium allocates properties or entity IDs.
// everything else before the features are read.
function prepareGeoJson(geojson) {
const stack = [geojson];
while (stack.length) {
@@ -512,7 +509,7 @@ export function kmzImageResolver(
* Strip everything from a parsed KML document that would make the page reach
* outside the file. `resolveLocal(href)` returns a URL for an image packed in
* the same KMZ (or null); every other href is removed, so icons fall back to
* Cesium's default pin and a ground overlay without a local image is dropped.
* the default pin and a ground overlay without a local image is dropped.
* @param {Document} doc
* @param {(href: string) => string|null} [resolveLocal]
* @returns {{removed: number}} How many elements were taken out.
@@ -526,8 +523,8 @@ export function sanitizeKmlDocument(doc, resolveLocal = () => null) {
}
};
// One snapshot and pass, rather than rescanning the whole XML tree for
// every discarded element type. Strip unused metadata before the loader
// constructs descriptions, availability intervals and camera objects.
// every discarded element type. Strip unused metadata (descriptions,
// times and cameras) before the features are read.
for (const el of [...doc.getElementsByTagName('*')])
if (
KML_REMOVED_ELEMENTS.has(el.localName) ||
@@ -549,8 +546,8 @@ export function sanitizeKmlDocument(doc, resolveLocal = () => null) {
drop(overlay);
continue;
}
// An empty Icon explicitly hides the billboard in Cesium. Remove the
// blocked icon definition so the loader supplies its local default pin.
// An empty Icon explicitly hides the icon. Remove the blocked icon
// definition so the placemark gets the local default pin.
// Authored empty Icons (with no blocked href) still mean no marker.
drop(
icon?.localName === 'Icon' && icon.parentNode?.localName === 'IconStyle'
@@ -571,9 +568,8 @@ export function sanitizeKmlDocument(doc, resolveLocal = () => null) {
!childrenNamed(pair, 'Style').length)
)
drop(pair);
// Draw recorded KML paths as their original static vertices. Letting the
// loader build time-dynamic tracks would expand each into thousands of
// interpolated points later and exceed the checked geometry budget.
// Draw recorded KML paths as their original static vertices, the
// geometry the budget checked, never as time-dynamic tracks.
for (const track of elementsNamed(doc, 'Track')) {
const line = doc.createElementNS(
'http://www.opengis.net/kml/2.2',
@@ -639,11 +635,11 @@ const childText = (el, localName) =>
(childrenNamed(el, localName)[0]?.textContent || '').trim();
/**
* Share identical styles before the loader builds their graphics. Inline
* Share identical styles before the features are read. Inline
* overrides stay inline StyleMaps, so an existing styleUrl still supplies
* properties the override leaves unset. Named references are redirected.
* Random-color styles remain unchanged; duplicate IDs
* keep the loader's original precedence instead of being rewritten.
* keep their original precedence instead of being rewritten.
* @param {Document} doc Sanitized KML.
* @param {{serializeStyle?: Function, maxStyles?: number}} [options]
* @returns {number} Number of original style blocks replaced.
@@ -746,8 +742,8 @@ export function deduplicateKmlStyles(
}
/**
* Count placemarks and vertices in a parsed KML document before Cesium reads
* it: every `<coordinates>` tuple and every `gx:coord`, plus normalized
* Count placemarks and vertices in a parsed KML document before it is read:
* every `<coordinates>` tuple and every `gx:coord`, plus normalized
* style definitions and distinct image `<href>` values (`links`).
* Inline StyleMaps are references, not additional shared style definitions.
* @param {Document} doc
@@ -797,8 +793,8 @@ export function countKmlGeometry(
)
throw new GeoImportError(msg('That KML has invalid geometry bounds.'));
}
// Cesium creates entities for folders, a parent for every placemark, and
// a child for each geometry in a MultiGeometry, including nested containers.
// A map object for each folder, a parent for every placemark, and a
// child for each geometry in a MultiGeometry, including nested containers.
if (
name === 'Placemark' ||
name === 'Folder' ||
@@ -1040,7 +1036,7 @@ export function repairKmlRootNamespaces(value, what = 'KML') {
/** Parse KML or GPX with the shared namespace repairs and XML validation. */
export function parseXml(text, what) {
// Match the namespace repairs Cesium applies to ordinary exported KML.
// The namespace repairs ordinary exported KML needs.
if (what !== 'GPX') text = repairKmlRootNamespaces(text, what);
const doc = new DOMParser().parseFromString(
String(text).replace(/^/, ''),
@@ -1099,11 +1095,11 @@ function checkLocalImages(doc, entries = new Map(), main = '') {
}
/**
* Load a ground footprint, retaining explicit KML altitude modes. Call mount after
* adding the source; revoke owns pin and blob cleanup.
* Load a ground footprint, retaining explicit KML altitude modes. Call mount
* to draw it; revoke owns the drawing and blob cleanup.
* @param {File|Blob & {name?: string}} file
* @param {{viewer: object, color?: string}} options `color` is a CSS color.
* @returns {Promise<{dataSource: object, name: string,
* @returns {Promise<{drawing: object, name: string,
* format: string, entityCount: number, removed: number, mount: Function, revoke: Function}>}
*/
export async function loadGeoFile(
@@ -1131,11 +1127,11 @@ export async function loadGeoFile(
const urls = [];
let drawn;
const revoke = () => {
drawn?.pins.destroy();
drawn?.drawing.destroy();
urls.splice(0).forEach((url) => URL.revokeObjectURL(url));
};
const name = importDisplayName(file.name);
let dataSource;
let read;
let removed = 0;
try {
if (format === 'geojson' || format === 'gpx') {
@@ -1151,7 +1147,7 @@ export async function loadGeoFile(
throw new GeoImportError(msg('That file has nothing to draw.'));
prepareGeoJson(geojson);
bytes = null;
dataSource = await loadImportedGeoJson(geojson, { color, signal });
read = readGeoJsonFeatures(geojson, color);
} else {
let text;
let entries;
@@ -1180,7 +1176,7 @@ export async function loadGeoFile(
deduplicateKmlStyles(doc);
const normalizedSize = countKmlGeometry(doc);
if (normalizedSize.over) throw overLimitError(normalizedSize);
dataSource = await loadImportedKml(doc, viewer, signal);
read = readKmlFeatures(doc);
}
} catch (error) {
revoke();
@@ -1192,21 +1188,21 @@ export async function loadGeoFile(
);
}
try {
drawn = await settleImportedSource(dataSource, viewer, signal);
drawn = await settleImportedDrawing(read, viewer, signal);
} catch (error) {
revoke();
throw error;
}
// The display name travels beside the data source (the file name, not
// The display name travels beside the drawing (the file name, not
// anything the document says about itself).
return {
dataSource,
drawing: drawn.drawing,
name,
format,
entityCount: drawn.entityCount,
removed,
revoke,
mount: () => drawn.pins.mount(),
mount: () => drawn.drawing.mount(),
refreshGround: drawn.refreshGround,
};
}
+200 -284
View File
@@ -2,7 +2,6 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import { deflateRawSync } from 'node:zlib';
import { spawnSync } from 'node:child_process';
import * as Cesium from 'cesium';
import {
GeoImportError,
repairKmlRootNamespaces,
@@ -24,106 +23,125 @@ import {
readZipEntries,
resolveKmzPath,
} from './geoFileImport.js';
import { detachImportedTime, scrubImportedEntities } from './geoImportPins.js';
import { readKmlFeatures } from './geoImportFeatures.js';
import { bindFakeGroundRenderer } from '../testSupport/fakeGroundRenderer.mjs';
import { fromDegrees, toLonLatH } from '../geo/ellipsoid.js';
/** The point `at` (lon, lat) at `height`, ECEF. */
const fromDegreesHeight = (at, height) => fromDegrees(at.lon, at.lat, height);
import { createImportImageBudget, importImageSize } from './geoImportImages.js';
test('real loader grounds XYZ GeoJSON and strips loader-sensitive metadata before allocation', async () => {
const previous = Cesium.PinBuilder.prototype.fromColor;
Cesium.PinBuilder.prototype.fromColor = () => ({
toDataURL: () => 'data:image/png;base64,local',
tagName: 'CANVAS',
/**
* A viewer bound to a renderer double for the import: draw sets kept,
* terrain heights from `terrain` (the globe shown) or from `hidden` (the
* globe hidden over a mesh), mesh clamps from `clamp`, and a camera
* `eyeHeight` up looking at `view`.
*/
function importViewer({
terrain = () => null,
hidden = null,
clamp = null,
eyeHeight = 1_000_000,
view = { west: -180, south: -90, east: 180, north: 90 },
} = {}) {
const viewer = {};
const ground = bindFakeGroundRenderer(viewer, {
sampleHeight: Boolean(clamp),
eye: { lon: 0, lat: 0, height: eyeHeight },
});
try {
const object = {
type: 'FeatureCollection',
crs: { type: 'name', properties: { name: 'unsupported' } },
features: [
{
type: 'Feature',
id: 'kartaview-owned',
properties: { name: 'Test', addProperty: 1, 'marker-symbol': 'rail' },
geometry: { type: 'Point', coordinates: [1, 2, 999] },
},
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[1, 2, 0],
[2, 2, 0],
[2, 3, 0],
[1, 2, 0],
],
],
},
},
],
};
const loaded = await loadGeoFile(
new File([JSON.stringify(object)], 'xyz.geojson'),
ground.globe.show = !hidden;
ground.globe.terrain = terrain;
const query = ground.renderer.query;
const sampleHeightNow = query.sampleHeightNow;
query.sampleHeightNow = (point, options = {}) =>
hidden && options.against === 'terrain'
? hidden(point)
: sampleHeightNow(point, options);
if (clamp) query.clampToSurface = async (points) => clamp(points);
ground.renderer.camera.viewRectangle = () => view;
return { viewer, camera: ground.renderer.camera, ...ground };
}
/** The live import set of a kind (`pins`, `lines`, `shapes`). */
const importSet = (ground, kind) =>
[...ground.draw.sets.values()].find(
(record) =>
!record.disposed &&
record.spec.id.startsWith('geo-import:') &&
record.spec.id.endsWith(`:${kind}`),
);
test('GeoJSON points stand on the ground and lose everything but a name', async () => {
const object = {
type: 'FeatureCollection',
crs: { type: 'name', properties: { name: 'unsupported' } },
features: [
{
viewer: {
scene: {
frameState: { mode: Cesium.SceneMode.SCENE3D },
primitives: new Cesium.PrimitiveCollection(),
globe: { getHeight: () => 1234 },
},
type: 'Feature',
id: 'kartaview-owned',
properties: { name: 'Test', addProperty: 1, 'marker-symbol': 'rail' },
geometry: { type: 'Point', coordinates: [1, 2, 999] },
},
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[1, 2, 0],
[2, 2, 0],
[2, 3, 0],
[1, 2, 0],
],
],
},
},
],
};
const t = importViewer({ terrain: () => 1234 });
try {
const loaded = await loadGeoFile(
new File([JSON.stringify(object)], 'xyz.geojson'),
{ viewer: t.viewer, color: '#ff0000' },
);
const [point, polygon] =
loaded.dataSource.importedEntities || loaded.dataSource.entities.values;
assert.notEqual(point.id, 'kartaview-owned');
assert.equal(point.name, 'Test');
assert.equal(point.properties, undefined);
assert.equal(
point.billboard.heightReference.getValue(),
Cesium.HeightReference.NONE,
);
assert.equal(point.billboard.disableDepthTestDistance.getValue(), Infinity);
assert.ok(
Math.abs(
Cesium.Cartographic.fromCartesian(
point.position.getValue(Cesium.JulianDate.now()),
).height - 1234,
) < 1e-6,
);
assert.notEqual(polygon.polygon.perPositionHeight?.getValue(), true);
assert.equal(loaded.dataSource.clock, undefined);
assert.equal(
loaded.dataSource.entities.values.length,
1,
'only polygon geometry remains in the per-frame entity collection',
);
assert.equal(loaded.dataSource.importedBillboards.length, 1);
const update = Cesium.BillboardCollection.prototype.update;
let passes = 0;
Cesium.BillboardCollection.prototype.update = () => passes++;
try {
const collection = loaded.dataSource.importedBillboards;
collection.update({ passes: { pick: true, offscreen: true } });
assert.equal(passes, 0, 'height picks ignore imported markers');
collection.update({ passes: { pick: true, offscreen: false } });
collection.update({ passes: { render: true, offscreen: true } });
assert.equal(passes, 2, 'ordinary picks and screenshots include markers');
} finally {
Cesium.BillboardCollection.prototype.update = update;
}
assert.equal(importSet(t, 'pins'), undefined, 'nothing drawn before mount');
loaded.mount();
loaded.dataSource.show = false;
assert.equal(loaded.dataSource.importedBillboards.show, false);
loaded.dataSource.show = true;
const [pin] = importSet(t, 'pins').items.values();
assert.equal(pin.meta.name, 'Test');
assert.deepEqual(Object.keys(pin.meta), ['name']);
assert.equal(pin.height, 'absolute');
assert.equal(pin.pos.height, 1234, 'on the terrain, not at 999 m');
assert.equal(pin.disableDepthTest, Infinity);
assert.equal(pin.anchor, 'bottom');
assert.deepEqual(pin.size, [48, 48]);
assert.match(pin.image.svg, /rgba\(255,0,0,1\)/);
assert.equal(
importSet(t, 'pins').spec.includeInHeightQueries,
undefined,
'height queries never see imported markers',
);
const [polygon] = importSet(t, 'shapes').items.values();
assert.equal(polygon.height, 'clampGround');
assert.deepEqual(polygon.fill, [1, 0, 0, 0.25]);
assert.equal(polygon.outline, undefined, 'no outline on the ground');
assert.equal(loaded.entityCount, 2);
loaded.drawing.show = false;
assert.equal(importSet(t, 'pins').show, false);
assert.equal(importSet(t, 'shapes').show, false);
loaded.drawing.show = true;
t.globe.terrain = () => 50;
await loaded.refreshGround();
assert.equal(
point.billboard.heightReference.getValue(),
Cesium.HeightReference.NONE,
[...importSet(t, 'pins').items.values()][0].pos.height,
50,
'a refresh moves the pin onto the ground now under it',
);
loaded.revoke();
assert.ok(loaded.dataSource.importedBillboards.isDestroyed());
assert.equal(importSet(t, 'pins'), undefined);
assert.equal(importSet(t, 'shapes'), undefined);
} finally {
Cesium.PinBuilder.prototype.fromColor = previous;
t.unbind();
}
});
@@ -240,81 +258,54 @@ test('pending file reads can be cancelled without waiting for the loader timeout
});
test('a hidden coarse globe never seeds a mesh-clamp ray below the surface', async () => {
const previous = Cesium.PinBuilder.prototype.fromColor;
Cesium.PinBuilder.prototype.fromColor = () => ({
tagName: 'CANVAS',
toDataURL: () => 'data:image/png;base64,local',
});
const starts = [];
const t = importViewer({
eyeHeight: 100_000,
hidden: () => -12311,
clamp: (points) =>
points.map((p) => {
const at = toLonLatH(p);
starts.push(at.height);
return at.height < -9000
? null
: { ...p, ...fromDegreesHeight(at, 2000) };
}),
});
try {
for (const terrainProvider of [new Cesium.EllipsoidTerrainProvider(), {}]) {
const viewer = {
terrainProvider,
camera: {
positionCartographic: { height: 100000 },
computeViewRectangle: () => Cesium.Rectangle.MAX_VALUE,
},
scene: {
frameState: { mode: Cesium.SceneMode.SCENE3D },
globe: { show: false, getHeight: () => -12311 },
clampToHeightSupported: true,
clampToHeightMostDetailed: async (points) =>
points.map((p) => {
const c = Cesium.Cartographic.fromCartesian(p);
starts.push(c.height);
return c.height < -9000
? undefined
: Cesium.Cartesian3.fromRadians(c.longitude, c.latitude, 2000);
}),
},
};
const loaded = await loadGeoFile(
new File(
[JSON.stringify({ type: 'Point', coordinates: [1, 2] })],
'surface.geojson',
),
{ viewer },
);
assert.ok(
Math.abs(starts.at(-1)) < 1e-6,
`ray started at ${starts.at(-1)}`,
);
assert.ok(
Math.abs(
Cesium.Cartographic.fromCartesian(
loaded.dataSource.importedBillboards.get(0).position,
).height - 2000,
) < 1e-6,
);
const sampled = starts.length;
viewer.camera.computeViewRectangle = () => undefined;
await loaded.refreshGround();
assert.equal(
starts.length,
sampled,
'distant points do not start a detailed mesh sweep',
);
assert.ok(
Math.abs(
Cesium.Cartographic.fromCartesian(
loaded.dataSource.importedBillboards.get(0).position,
).height - 2000,
) < 1e-6,
'the last valid mesh height survives outside the camera footprint',
);
const point = loaded.dataSource.importedEntities[0];
const other = new Cesium.Entity({ id: 'unrelated-aircraft' });
viewer.trackedEntity = starts.length === 1 ? other : point;
viewer.selectedEntity = point;
loaded.revoke();
assert.equal(viewer.selectedEntity, undefined);
assert.equal(
viewer.trackedEntity,
starts.length === 1 ? other : undefined,
);
}
const loaded = await loadGeoFile(
new File(
[JSON.stringify({ type: 'Point', coordinates: [1, 2] })],
'surface.geojson',
),
{ viewer: t.viewer },
);
loaded.mount();
const height = () => [...importSet(t, 'pins').items.values()][0].pos.height;
assert.ok(
Math.abs(starts.at(-1)) < 1e-6,
`ray started at ${starts.at(-1)}`,
);
assert.ok(Math.abs(height() - 2000) < 1e-6);
const sampled = starts.length;
t.camera.viewRectangle = () => ({
west: 10,
south: 10,
east: 11,
north: 11,
});
await loaded.refreshGround();
assert.equal(
starts.length,
sampled,
'distant points do not start a detailed mesh sweep',
);
assert.ok(
Math.abs(height() - 2000) < 1e-6,
'the last valid mesh height survives outside the camera footprint',
);
loaded.revoke();
} finally {
Cesium.PinBuilder.prototype.fromColor = previous;
t.unbind();
}
});
@@ -708,7 +699,7 @@ test('distinct style definitions and named StyleMaps still have a pre-loader bud
assert.equal(countKmlGeometry(maps, { maxLinks: 1 }).over, 'links');
});
test('KML extended metadata is removed before the document reaches the loader', () => {
test('KML extended metadata is removed before the document is read for drawing', () => {
const point = xmlEl('Point', {}, [xmlEl('coordinates', {}, '1,2,0')]);
const metadata = xmlEl('ExtendedData', {}, [
xmlEl('Data', { name: 'note' }, [xmlEl('value', {}, 'ordinary metadata')]),
@@ -809,7 +800,7 @@ test('GPX keeps each track segment as its own line, names as text, no times', ()
assert.throws(() => gpxToGeoJson(xmlDoc(xmlEl('kml'))), GeoImportError);
});
test('GPX past the point limit is refused while reading, before Cesium', () => {
test('GPX past the point limit is refused while reading, before anything is drawn', () => {
const seg = xmlEl(
'trkseg',
{},
@@ -966,7 +957,7 @@ test('KML vertex counting recognizes the Unicode whitespace Cesium accepts', ()
}
});
test('KML multipart geometries count every Cesium entity before loading', () => {
test('KML multipart geometries count every map object before drawing', () => {
const doc = xmlDoc(
xmlEl('kml', {}, [
xmlEl('Placemark', {}, [
@@ -1049,69 +1040,47 @@ test('a KMZ image gets one blob URL however many hrefs name it', () => {
assert.deepEqual(kept, ['blob:test/1', 'blob:test/2']);
});
test('imported entities keep only a short plain-text name: no description, label or properties', () => {
const ds = new Cesium.CustomDataSource('t');
const e = ds.entities.add({
name: 'Trail\u0007 <b>head</b>\n' + 'x'.repeat(300),
description: '<img src="https://example.invalid/x.png" onerror="alert(1)">',
label: { text: 'Trail head' },
properties: { note: '<script>alert(1)</script>' },
position: Cesium.Cartesian3.fromDegrees(1, 2),
});
e.kml = { extendedData: { note: { value: '<iframe src=//x>' } } };
ds.entities.add({ name: 42, position: Cesium.Cartesian3.fromDegrees(1, 2) });
assert.equal(scrubImportedEntities(ds), 2);
assert.equal(e.description, undefined);
assert.equal(e.label, undefined);
assert.equal(e.properties, undefined);
assert.equal(e.kml, undefined);
assert.ok(e.name.startsWith('Trail <b>head</b> x'));
assert.ok(e.name.length <= 120 && !/[\u0000-\u001f]/.test(e.name));
assert.equal(ds.entities.values[1].name, undefined);
test('imported features keep only a short plain-text name', () => {
const placemark = (name) =>
xmlEl('Placemark', {}, [
xmlEl('name', {}, name),
xmlEl('Point', {}, [xmlEl('coordinates', {}, '1,2')]),
]);
const doc = xmlDoc(
xmlEl('kml', {}, [
xmlEl('Document', {}, [
placemark('Trail\u0007 <b>head</b>\n' + 'x'.repeat(300)),
placemark(' '),
]),
]),
);
const { features } = readKmlFeatures(doc);
assert.ok(features[0].name.startsWith('Trail <b>head</b> x'));
assert.ok(
features[0].name.length <= 120 && !/[\u0000-\u001f]/.test(features[0].name),
);
assert.equal(features[1].name, undefined);
assert.equal(importFeatureName(' '), undefined);
});
test('generated pin canvases are encoded once and shared without changing authored image URLs', () => {
const ds = new Cesium.CustomDataSource('pins');
let encoded = 0;
const canvas = {
tagName: 'CANVAS',
toDataURL: () => {
encoded++;
return 'data:image/png;base64,shared-pin';
},
};
for (let i = 0; i < 1000; i++)
ds.entities.add({
billboard: { image: canvas },
label: { text: 'discard' },
properties: { discard: true },
});
const authored = ds.entities.add({
billboard: { image: 'blob:packed-image' },
});
const dynamicImage = new Cesium.CallbackProperty(() => canvas, false);
const dynamic = ds.entities.add({ billboard: { image: dynamicImage } });
let notifications = 0;
ds.entities.collectionChanged.addEventListener(() => notifications++);
scrubImportedEntities(ds);
assert.equal(encoded, 1);
assert.equal(
notifications,
1,
'normalization publishes one complete collection change',
);
for (const e of ds.entities.values.slice(0, 1000))
assert.equal(
e.billboard.image.getValue(),
'data:image/png;base64,shared-pin',
);
assert.equal(authored.billboard.image.getValue(), 'blob:packed-image');
assert.equal(
dynamic.billboard.image,
dynamicImage,
'time-dependent image properties stay intact',
test('placemarks without an icon of their own share one default pin', () => {
const doc = xmlDoc(
xmlEl('kml', {}, [
xmlEl(
'Document',
{},
Array.from({ length: 1000 }, () =>
xmlEl('Placemark', {}, [
xmlEl('Point', {}, [xmlEl('coordinates', {}, '1,2')]),
]),
),
),
]),
);
const { features } = readKmlFeatures(doc);
assert.equal(features.length, 1000);
assert.ok(features.every((f) => f.image === features[0].image));
assert.match(features[0].image.svg, /rgba\(255,255,0,1\)/);
});
test('unused KML time and camera metadata is removed before loading without changing geometry', () => {
@@ -1132,47 +1101,6 @@ test('unused KML time and camera metadata is removed before loading without chan
assert.equal(coordinates.textContent, '1,2,0 3,4,0');
});
test('a dated import exposes no clock and draws for all time', () => {
// A data source with its own clock, as KmlDataSource has for a dated file.
class DatedSource extends Cesium.CustomDataSource {
get clock() {
return this._testClock;
}
}
const ds = new DatedSource('dated');
ds._testClock = new Cesium.DataSourceClock();
const start = Cesium.JulianDate.fromIso8601('2020-01-01T00:00:00Z');
const stop = Cesium.JulianDate.fromIso8601('2020-01-01T01:00:00Z');
const span = new Cesium.TimeIntervalCollection([
new Cesium.TimeInterval({ start, stop }),
]);
// KML tracks become their original static vertices before Cesium loading.
const track = ds.entities.add({
availability: span,
polyline: {
positions: Cesium.Cartesian3.fromDegreesArray([10, 10, 10.1, 10.1]),
clampToGround: true,
},
});
const recorded = track.polyline.positions;
const pin = ds.entities.add({
availability: span,
position: Cesium.Cartesian3.fromDegrees(1, 1),
});
assert.ok(ds.clock);
detachImportedTime(ds);
assert.equal(ds.clock, undefined);
// The viewer reads `clock` from every added source; setting it is ignored.
ds.clock = new Cesium.DataSourceClock();
assert.equal(ds.clock, undefined);
assert.equal(pin.availability, undefined);
assert.ok(pin.isAvailable(Cesium.JulianDate.now()));
assert.equal(track.availability, undefined);
assert.equal(track.polyline.positions, recorded);
assert.equal(recorded.getValue(Cesium.JulianDate.now()).length, 2);
assert.ok(track.polyline.clampToGround.getValue());
});
test('KML root namespace repair respects quoted delimiters and preserves the document tail', () => {
const text =
'<KML note="a > b" xmlns:x="first" xmlns:x="second"><Placemark xsi:type="test"/></KML>';
@@ -1263,38 +1191,26 @@ test('KML namespace repair ignores declaration text inside quoted attribute valu
});
test('a failed mesh clamp fails the import instead of leaving pins at sea level', async () => {
const previous = Cesium.PinBuilder.prototype.fromColor;
Cesium.PinBuilder.prototype.fromColor = () => ({
tagName: 'CANVAS',
toDataURL: () => 'data:image/png;base64,local',
const t = importViewer({
eyeHeight: 100_000,
hidden: () => 0,
clamp: () => {
throw new Error('tile request failed');
},
});
try {
const viewer = {
terrainProvider: new Cesium.EllipsoidTerrainProvider(),
camera: {
positionCartographic: { height: 100000 },
computeViewRectangle: () => Cesium.Rectangle.MAX_VALUE,
},
scene: {
frameState: { mode: Cesium.SceneMode.SCENE3D },
globe: { show: false, getHeight: () => 0 },
clampToHeightSupported: true,
clampToHeightMostDetailed: async () => {
throw new Error('tile request failed');
},
},
};
await assert.rejects(
loadGeoFile(
new File(
[JSON.stringify({ type: 'Point', coordinates: [1, 2] })],
'surface.geojson',
),
{ viewer },
{ viewer: t.viewer },
),
/Ground heights could not be read/,
);
assert.equal(importSet(t, 'pins'), undefined);
} finally {
Cesium.PinBuilder.prototype.fromColor = previous;
t.unbind();
}
});
+684
View File
@@ -0,0 +1,684 @@
/**
* What an imported file draws, read from its parsed KML document or its
* prepared GeoJSON as the default renderer's KML and GeoJSON loaders read
* them: placemark pins with their icons, colours and anchors, paths,
* polygons, walls and ground overlays, each with its altitude mode. Plain
* data in degrees and metres, drawn by `geoImportPins.js`.
*
* `entityCount` counts what those loaders made of the file (folders,
* placemarks, each part of a multi-geometry), the number the import limit
* is checked against. `drawable` says whether any of it can be drawn.
*/
import { plainPin } from './geojsonPins.js';
import { importFeatureName } from './geoImportShared.js';
import { cssColor } from '../render/color.js';
/** KML icons are drawn this many pixels square, times their scale. */
const KML_ICON_SIZE = 32;
/** KML icons shrink with distance: full size near, a tenth far. */
const KML_ICON_SCALE_BY_DISTANCE = Object.freeze({
near: 2414016,
nearValue: 1,
far: 1.6093e7,
farValue: 0.1,
});
const WHITE = Object.freeze([1, 1, 1, 1]);
const BLACK = Object.freeze([0, 0, 0, 1]);
/** A placemark without an icon of its own: a yellow pin. */
const DEFAULT_KML_PIN = plainPin([1, 1, 0, 1], 64);
/** GeoJSON markers are pins this many pixels square, standing on the point. */
const GEOJSON_PIN_SIZE = 48;
const GX = 'http://www.google.com/kml/ext/2.2';
/** The namespaces KML elements come in: none, or a KML version's. */
const KML_NAMESPACES = new Set([
null,
undefined,
'',
'http://www.opengis.net/kml/2.2',
'http://earth.google.com/kml/2.2',
'http://earth.google.com/kml/2.1',
'http://earth.google.com/kml/2.0',
]);
const isGx = (el) => el.namespaceURI === GX;
const isKml = (el) => KML_NAMESPACES.has(el.namespaceURI);
/** Element children of `el` with this local name, KML (not gx) unless `gx`. */
function children(el, name, gx = false) {
const out = [];
const kids = el?.childNodes;
if (!kids) return out;
for (let i = 0; i < kids.length; i++) {
const kid = kids[i];
if (
kid.nodeType === 1 &&
kid.localName === name &&
(gx ? isGx(kid) : isKml(kid))
)
out.push(kid);
}
return out;
}
const child = (el, name, gx) => children(el, name, gx)[0];
const text = (el, name, gx) => child(el, name, gx)?.textContent.trim();
function number(el, name, gx) {
const value = Number.parseFloat(child(el, name, gx)?.textContent);
return Number.isNaN(value) ? undefined : value;
}
function flag(el, name) {
const value = text(el, name);
return value === undefined
? undefined
: value === '1' || /^true$/i.test(value);
}
function attribute(el, name) {
const value = el?.getAttribute(name);
if (value === null || value === undefined) return undefined;
const parsed = Number.parseFloat(value);
return Number.isNaN(parsed) ? undefined : parsed;
}
/**
* A KML `aabbggrr` colour as `[r, g, b, a]`; with `colorMode` random, each
* channel drawn at random up to its own value.
*/
function kmlColor(el, name = 'color') {
let value = text(el, name);
if (!value) return undefined;
if (value[0] === '#') value = value.slice(1);
const channel = (at) => Number.parseInt(value.slice(at, at + 2), 16) / 255;
const alpha = channel(0);
const rgb = [channel(6), channel(4), channel(2)];
if (text(el, 'colorMode') === 'random')
for (let i = 0; i < 3; i++)
rgb[i] = rgb[i] > 0 ? Math.random() * rgb[i] : 0;
return [...rgb, alpha];
}
/** One `lon,lat[,height]` tuple; missing or unreadable numbers are 0. */
function readCoordinate(value) {
const digits = value?.match(/[^\s,\n]+/g);
const at = (i) => {
const n = Number.parseFloat(digits?.[i]);
return Number.isNaN(n) ? 0 : n;
};
return [at(0), at(1), at(2)];
}
function readCoordinates(el) {
const tuples = el?.textContent.match(/[^\s\n]+/g);
return tuples ? tuples.map(readCoordinate) : undefined;
}
/** Copy each graphic's unset properties from `from`, as styles merge. */
function merge(into, from) {
for (const kind of ['billboard', 'polyline', 'polygon']) {
if (!from?.[kind]) continue;
if (!into[kind]) into[kind] = { ...from[kind] };
else
for (const key of Object.keys(from[kind]))
if (into[kind][key] === undefined) into[kind][key] = from[kind][key];
}
return into;
}
const defaultBillboard = () => ({});
/** The icon, line and polygon look a `<Style>` gives, merged into `style`. */
function applyStyle(styleEl, style) {
if (!styleEl) return style;
for (const node of styleEl.childNodes) {
if (node.nodeType !== 1 || !isKml(node)) continue;
if (node.localName === 'IconStyle') {
const billboard = (style.billboard ??= defaultBillboard());
const icon = child(node, 'Icon');
const href = text(icon, 'href');
// An empty <Icon> draws no icon; none at all, the default pin.
billboard.image = href ? href : icon ? false : undefined;
const scale = number(node, 'scale');
billboard.scale = scale;
billboard.color = kmlColor(node);
const heading = number(node, 'heading');
// Heading 0 is no heading; north is 360.
if (heading !== undefined && heading !== 0) billboard.course = heading;
const spot = child(node, 'hotSpot');
const x = attribute(spot, 'x');
const y = attribute(spot, 'y');
const s = scale ?? 1;
let dx;
let dy;
if (x !== undefined) {
// Units left out are a fraction, as KML has it.
const units = spot.getAttribute('xunits') || 'fraction';
if (units === 'pixels') dx = -x * s;
else if (units === 'insetPixels') dx = (x - KML_ICON_SIZE) * s;
else if (units === 'fraction') dx = -x * KML_ICON_SIZE * s;
if (dx !== undefined) dx += KML_ICON_SIZE * 0.5 * s;
else dx = Number.NaN;
}
if (y !== undefined) {
const units = spot.getAttribute('yunits') || 'fraction';
if (units === 'pixels') dy = y * s;
else if (units === 'insetPixels') dy = (-y + KML_ICON_SIZE) * s;
else if (units === 'fraction') dy = y * KML_ICON_SIZE * s;
if (dy !== undefined) dy -= KML_ICON_SIZE * 0.5 * s;
else dy = Number.NaN;
}
if (dx !== undefined || dy !== undefined)
billboard.pixelOffset = {
x: Number.isFinite(dx) ? dx : 0,
y: Number.isFinite(dy) ? dy : 0,
};
} else if (node.localName === 'LineStyle') {
const polyline = (style.polyline ??= {});
polyline.width = number(node, 'width');
polyline.color = kmlColor(node);
} else if (node.localName === 'PolyStyle') {
const polygon = (style.polygon ??= { outline: true });
polygon.color = kmlColor(node) ?? polygon.color;
polygon.fill = flag(node, 'fill') ?? polygon.fill;
polygon.outline = flag(node, 'outline') ?? polygon.outline;
}
}
return style;
}
/**
* The document's shared styles by `#id`: each `<Style>` with an id, then
* each `<StyleMap>` as its normal pair; the first of an id wins.
*/
function sharedStyles(doc) {
const styles = new Map();
for (const el of doc.getElementsByTagName('*')) {
if (el.localName !== 'Style' || !isKml(el)) continue;
const id = el.getAttribute('id');
if (id && !styles.has(`#${id}`)) styles.set(`#${id}`, applyStyle(el, {}));
}
for (const el of doc.getElementsByTagName('*')) {
if (el.localName !== 'StyleMap' || !isKml(el)) continue;
const id = el.getAttribute('id');
if (!id || styles.has(`#${id}`)) continue;
for (const pair of children(el, 'Pair')) {
if (text(pair, 'key') !== 'normal') continue;
const style = {};
let url = text(pair, 'styleUrl');
if (url !== undefined) {
if (url[0] !== '#') url = `#${url}`;
merge(style, styles.get(url));
} else applyStyle(child(pair, 'Style'), style);
styles.set(`#${id}`, style);
break;
}
}
return styles;
}
/** A feature's own style: its last inline Style or StyleMap, then its styleUrl. */
function finalStyle(feature, styles) {
const style = {};
const lookup = (url) => styles.get(url) ?? styles.get(`#${url}`);
let inline;
for (const node of feature.childNodes)
if (
node.nodeType === 1 &&
isKml(node) &&
(node.localName === 'Style' || node.localName === 'StyleMap')
)
inline = node;
if (inline?.localName === 'Style') applyStyle(inline, style);
else if (inline)
for (const pair of children(inline, 'Pair')) {
if (text(pair, 'key') !== 'normal') continue;
const url = text(pair, 'styleUrl');
if (url !== undefined) merge(style, lookup(url));
else applyStyle(child(pair, 'Style'), style);
}
const url = text(feature, 'styleUrl');
if (url !== undefined) merge(style, lookup(url));
return style;
}
const extrudable = (mode, gxMode) =>
mode === 'absolute' ||
mode === 'relativeToGround' ||
gxMode === 'relativeToSeaFloor';
/** How a point stands: on the ground, above it, or at its own height. */
function pinHeightMode(mode, gxMode) {
if ((!mode && !gxMode) || mode === 'clampToGround') return 'clamp';
if (mode === 'relativeToGround') return 'relative';
if (mode === 'absolute') return 'absolute';
if (gxMode === 'relativeToSeaFloor') return 'relative';
return 'clamp';
}
/** A placemark pin as a KML point draws it. */
function kmlPin(name, style, at, mode) {
const billboard = style.billboard ?? defaultBillboard();
const image =
billboard.image === undefined
? DEFAULT_KML_PIN
: billboard.image === false
? null
: billboard.image;
return {
kind: 'pin',
name,
lon: at?.[0],
lat: at?.[1],
height: at?.[2] ?? 0,
mode,
image,
size: [KML_ICON_SIZE, KML_ICON_SIZE],
scale: billboard.scale,
color: billboard.color,
course: billboard.course,
pixelOffset: billboard.pixelOffset,
scaleByDistance: KML_ICON_SCALE_BY_DISTANCE,
};
}
/** One KML geometry element of a placemark as features; false for none. */
function kmlGeometry(el, name, style, out) {
const mode = text(el, 'altitudeMode');
const gxMode = text(el, 'altitudeMode', true);
const canExtrude = extrudable(mode, gxMode);
const extrude = flag(el, 'extrude');
switch (el.localName) {
case 'Point': {
out.push(
kmlPin(
name,
style,
readCoordinate(text(el, 'coordinates')),
pinHeightMode(mode, gxMode),
),
);
return true;
}
case 'LineString':
case 'LinearRing': {
const coords = readCoordinates(child(el, 'coordinates'));
const line = style.polyline;
if (canExtrude && extrude) {
const polygon = style.polygon;
out.push({
kind: 'wall',
name,
coords,
fill: polygon?.fill === false ? null : (polygon?.color ?? WHITE),
outline: line
? (line.color ?? WHITE)
: polygon
? (polygon.color ?? WHITE)
: BLACK,
outlineWidth: line?.width,
});
} else if (!canExtrude && flag(el, 'tessellate')) {
out.push({
kind: 'line',
name,
coords,
mode: 'draped',
color: line?.color ?? WHITE,
width: line?.width ?? 1,
});
} else {
// Straight in space: at the heights given where the altitude mode
// allows, otherwise on the ellipsoid (a clamped line the import did
// not tessellate, such as one with an explicit tessellate 0).
out.push({
kind: 'line',
name,
coords: canExtrude
? coords
: coords?.map(([lon, lat]) => [lon, lat, 0]),
mode: '3d',
color: line?.color ?? WHITE,
width: line?.width ?? 1,
});
}
return true;
}
case 'Polygon': {
const ring = (boundary) =>
readCoordinates(child(child(boundary, 'LinearRing'), 'coordinates'));
const outer = ring(child(el, 'outerBoundaryIs'));
const holes = [];
for (const inner of children(el, 'innerBoundaryIs'))
for (const linear of children(inner, 'LinearRing')) {
const coords = readCoordinates(child(linear, 'coordinates'));
if (coords) holes.push(coords);
}
const polygon = style.polygon ?? { outline: true };
const line = style.polyline;
out.push({
kind: 'polygon',
name,
rings: outer ? [outer, ...holes] : [],
clamp: !canExtrude,
extrude: canExtrude && extrude === true,
fill: polygon.fill === false ? null : (polygon.color ?? WHITE),
outline:
polygon.outline === false
? null
: line
? (line.color ?? WHITE)
: WHITE,
outlineWidth: line?.width,
});
return true;
}
case 'MultiGeometry': {
let any = false;
for (const part of el.childNodes) {
if (part.nodeType !== 1 || !KML_GEOMETRIES.has(part.localName))
continue;
// Each part is a map object of its own, named as its placemark.
out.entities += 1;
if (kmlGeometry(part, name, style, out)) any = true;
}
return any;
}
default:
return false;
}
}
const KML_GEOMETRIES = new Set([
'Point',
'LineString',
'LinearRing',
'Polygon',
'MultiGeometry',
'Model',
'Track',
'MultiTrack',
]);
/** A ground overlay: an image (or colour) on a box or a quad. */
function kmlOverlay(el, name, out) {
const quad = readCoordinates(child(el, 'LatLonQuad', true));
const href = text(child(el, 'Icon'), 'href') || null;
const color = kmlColor(el);
const mode = text(el, 'altitudeMode');
const gxMode = text(el, 'altitudeMode', true);
const height =
mode === 'absolute' || (!mode && gxMode === 'relativeToSeaFloor')
? number(el, 'altitude')
: undefined;
let rect = null;
let rotation;
const box = quad ? null : child(el, 'LatLonBox');
// Neither box nor quad: nowhere to draw it, as before.
if (box) {
// Missing bounds are 0; longitudes wrap into [-180, 180].
const wrap = (lon = 0) =>
lon >= -180 && lon <= 180
? lon
: ((((lon + 180) % 360) + 360) % 360) - 180;
const clampLat = (lat = 0) => Math.max(-90, Math.min(90, lat));
rect = {
west: wrap(number(box, 'west')),
south: clampLat(number(box, 'south')),
east: wrap(number(box, 'east')),
north: clampLat(number(box, 'north')),
};
// A box upside down has no area to draw (Cesium refused it too).
if (rect.north < rect.south) rect = null;
const turn = number(box, 'rotation');
if (turn !== undefined) rotation = (turn * Math.PI) / 180;
}
out.push({
kind: 'overlay',
name,
rect,
quad,
rotation,
image: href,
// Without an image, the overlay's colour; an image is drawn untinted.
color: href ? undefined : (color ?? WHITE),
height,
});
}
/** A feature's short plain-text name, or undefined. */
function featureName(el) {
try {
return importFeatureName(text(el, 'name'));
} catch {
return undefined;
}
}
/**
* The features of a sanitized KML document.
* @param {Document} doc
* @returns {{features: object[], entityCount: number, drawable: boolean}}
*/
export function readKmlFeatures(doc) {
const styles = sharedStyles(doc);
const out = [];
out.entities = 0;
let positionless = false;
const visit = (node) => {
switch (node.localName) {
case 'Document':
for (const kid of node.childNodes) if (kid.nodeType === 1) visit(kid);
return;
case 'Folder':
out.entities += 1;
for (const kid of node.childNodes) if (kid.nodeType === 1) visit(kid);
return;
case 'Placemark': {
out.entities += 1;
const name = featureName(node);
const style = finalStyle(node, styles);
// Only the first geometry of a placemark is drawn.
const geometry = [...node.childNodes].find(
(kid) => kid.nodeType === 1 && KML_GEOMETRIES.has(kid.localName),
);
if (geometry) kmlGeometry(geometry, name, style, out);
else {
// No geometry: an icon with nowhere to stand.
const pin = kmlPin(name, style, null, 'absolute');
if (pin.image) positionless = true;
}
return;
}
case 'GroundOverlay':
out.entities += 1;
kmlOverlay(node, featureName(node), out);
return;
default:
}
};
let root = doc.documentElement;
if (root?.localName === 'kml')
root = [...root.childNodes].find(
(kid) =>
kid.nodeType === 1 &&
['Document', 'Folder', 'Placemark', 'GroundOverlay'].includes(
kid.localName,
),
);
if (root) visit(root);
const entityCount = out.entities;
const features = [...out];
return {
features,
entityCount,
drawable: positionless || features.some(drawable),
};
}
/** The longest step, in degrees, of a rhumb line drawn as a path. */
const RHUMB_STEP = 0.5;
/**
* The most points the steps may add to one file, as many as it may hold
* (`MAX_IMPORT_POSITIONS`); past it, the rest are drawn as they are.
*/
const RHUMB_BUDGET = 250_000;
const mercatorY = (lat) =>
Math.log(Math.tan(Math.PI / 4 + (lat * Math.PI) / 360));
const fromMercatorY = (y) => (Math.atan(Math.sinh(y)) * 180) / Math.PI;
/**
* A path along rhumb lines, as GeoJSON is drawn: each long step split at
* most `RHUMB_STEP` degrees apart along the line of constant bearing (the
* draw port joins points the short way over the globe), the short way
* round in longitude. Steps are added while `budget.left` allows.
* @param {number[][]} coords `[lon, lat, height]`
* @param {{left: number}} [budget] Points it may still add, shared by a file.
* @returns {number[][]}
*/
export function rhumbPath(coords, budget = { left: RHUMB_BUDGET }) {
const out = [];
for (let i = 0; i < coords.length; i++) {
const b = coords[i];
const a = coords[i - 1];
if (a) {
const dLon = ((((b[0] - a[0] + 180) % 360) + 360) % 360) - 180;
const steps = Math.ceil(
Math.max(Math.abs(dLon), Math.abs(b[1] - a[1])) / RHUMB_STEP,
);
if (
steps > 1 &&
steps - 1 <= budget.left &&
Math.abs(a[1]) < 89.9 &&
Math.abs(b[1]) < 89.9
) {
budget.left -= steps - 1;
const ya = mercatorY(a[1]);
const yb = mercatorY(b[1]);
for (let k = 1; k < steps; k++) {
const f = k / steps;
let lon = a[0] + dLon * f;
if (lon > 180) lon -= 360;
else if (lon < -180) lon += 360;
out.push([
lon,
fromMercatorY(ya + (yb - ya) * f),
a[2] + (b[2] - a[2]) * f,
]);
}
}
}
out.push(b);
}
return out;
}
/** Whether a feature draws anything, as the loaders' output is checked. */
function drawable(feature) {
switch (feature.kind) {
case 'pin':
return Boolean(feature.image);
case 'line':
case 'wall':
return (feature.coords?.length ?? 0) >= 2;
case 'polygon':
return (feature.rings[0]?.length ?? 0) >= 3;
case 'overlay':
// Counted as before even without bounds, though it then draws nothing.
return true;
default:
return false;
}
}
/**
* The features of prepared GeoJSON (names only, two-dimensional
* coordinates), drawn in one colour: pins in it standing on the ground,
* lines draped in it three pixels wide, polygons filled with it at a
* quarter opacity.
* @param {object} geojson
* @param {string} color A CSS colour.
* @returns {{features: object[], entityCount: number, drawable: boolean}}
*/
export function readGeoJsonFeatures(geojson, color) {
const rgba = cssColor(color) ?? [...WHITE];
const fill = [rgba[0], rgba[1], rgba[2], 0.25];
const pin = plainPin(rgba, GEOJSON_PIN_SIZE);
const features = [];
let entityCount = 0;
const at = (c) => [c[0], c[1], c[2] ?? 0];
const geometry = (g, name) => {
switch (g?.type) {
case 'Point':
entityCount += 1;
features.push(geoPin(name, g.coordinates));
return;
case 'MultiPoint':
for (const c of g.coordinates) {
entityCount += 1;
features.push(geoPin(name, c));
}
return;
case 'LineString':
entityCount += 1;
features.push(geoLine(name, g.coordinates));
return;
case 'MultiLineString':
for (const c of g.coordinates) {
entityCount += 1;
features.push(geoLine(name, c));
}
return;
case 'Polygon':
entityCount += 1;
features.push(geoPolygon(name, g.coordinates));
return;
case 'MultiPolygon':
for (const c of g.coordinates) {
entityCount += 1;
features.push(geoPolygon(name, c));
}
return;
case 'GeometryCollection':
for (const part of g.geometries) geometry(part, name);
return;
default:
}
};
const geoPin = (name, c) => ({
kind: 'pin',
name,
lon: c[0],
lat: c[1],
height: c[2] ?? 0,
mode: c.length > 2 ? 'absolute' : 'clamp',
image: pin,
size: [GEOJSON_PIN_SIZE, GEOJSON_PIN_SIZE],
anchor: 'bottom',
});
const budget = { left: RHUMB_BUDGET };
const geoLine = (name, coords) => ({
kind: 'line',
name,
coords: rhumbPath(coords.map(at), budget),
mode: 'draped',
color: rgba,
width: 3,
});
const geoPolygon = (name, rings) => ({
kind: 'polygon',
name,
rings: rings.map((ring) => rhumbPath(ring.map(at), budget)),
clamp: true,
extrude: false,
fill,
outline: null,
});
const feature = (f) => {
if (f.geometry === null) {
// A feature without geometry is a map object that draws nothing.
entityCount += 1;
return;
}
geometry(f.geometry, f.properties?.name || undefined);
};
if (geojson?.type === 'FeatureCollection')
for (const f of geojson.features) feature(f);
else if (geojson?.type === 'Feature') feature(geojson);
else geometry(geojson, undefined);
return { features, entityCount, drawable: features.some(drawable) };
}
+341
View File
@@ -0,0 +1,341 @@
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(
'<?xml version="1.0" encoding="UTF-8"?>' +
'<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">' +
`<Document>${body}</Document></kml>`,
'application/xml',
);
const point = (coordinates = '1,2,0', extra = '') =>
`<Point>${extra}<coordinates>${coordinates}</coordinates></Point>`;
test('a placemark takes its last inline style over its shared one, property by property', () => {
const { features } = readKmlFeatures(
kml(`
<Style id="shared">
<IconStyle><scale>2</scale><color>ff0000ff</color>
<Icon><href>data:image/png;base64,AAAA</href></Icon></IconStyle>
</Style>
<StyleMap id="map">
<Pair><key>normal</key><styleUrl>#shared</styleUrl></Pair>
<Pair><key>highlight</key><styleUrl>#other</styleUrl></Pair>
</StyleMap>
<Placemark><name>shared</name><styleUrl>#map</styleUrl>${point()}</Placemark>
<Placemark><name>inline</name><styleUrl>#shared</styleUrl>
<Style><IconStyle><color>ff00ff00</color></IconStyle></Style>
<Style><IconStyle><scale>0.5</scale></IconStyle></Style>
${point()}</Placemark>
<Placemark><name>none</name><Style><IconStyle><Icon/></IconStyle></Style>${point()}</Placemark>
<Placemark><name>default</name>${point()}</Placemark>`),
);
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,
});
}
});
test('an icon hot spot and heading place and turn the icon', () => {
const { features } = readKmlFeatures(
kml(`
<Placemark><Style><IconStyle><scale>0.8</scale><heading>90</heading>
<hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
</IconStyle></Style>${point()}</Placemark>
<Placemark><Style><IconStyle><heading>0</heading>
<hotSpot x="4" y="8" xunits="pixels" yunits="insetPixels"/>
</IconStyle></Style>${point()}</Placemark>`),
);
// 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(`
<Style id="s"><LineStyle><color>ff00ffff</color><width>6</width></LineStyle>
<PolyStyle><color>66ff0000</color></PolyStyle></Style>
<Placemark>${point('1,2,30')}</Placemark>
<Placemark>${point('1,2,30', '<altitudeMode>relativeToGround</altitudeMode>')}</Placemark>
<Placemark>${point('1,2,30', '<altitudeMode>absolute</altitudeMode>')}</Placemark>
<Placemark><styleUrl>#s</styleUrl><LineString><tessellate>1</tessellate>
<coordinates>1,2 3,4</coordinates></LineString></Placemark>
<Placemark><styleUrl>#s</styleUrl><LineString><tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode><coordinates>1,2,600 3,4,900</coordinates></LineString></Placemark>
<Placemark><styleUrl>#s</styleUrl><LineString><extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode><coordinates>1,2,150 3,4,150</coordinates></LineString></Placemark>
<Placemark><styleUrl>#s</styleUrl><Polygon><outerBoundaryIs><LinearRing>
<coordinates>0,0 1,0 1,1 0,0</coordinates></LinearRing></outerBoundaryIs>
<innerBoundaryIs><LinearRing><coordinates>0.1,0.1 0.2,0.1 0.2,0.2 0.1,0.1</coordinates></LinearRing></innerBoundaryIs>
</Polygon></Placemark>
<Placemark><Polygon><extrude>1</extrude><altitudeMode>absolute</altitudeMode>
<outerBoundaryIs><LinearRing><coordinates>0,0,200 1,0,200 1,1,200 0,0,200</coordinates></LinearRing></outerBoundaryIs>
</Polygon></Placemark>
<Placemark><Style><PolyStyle><fill>0</fill><outline>0</outline></PolyStyle></Style>
<Polygon><outerBoundaryIs><LinearRing><coordinates>0,0 1,0 1,1 0,0</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark>`),
);
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(`
<GroundOverlay><name>box</name><Icon><href>blob:packed</href></Icon>
<altitudeMode>absolute</altitudeMode><altitude>120</altitude>
<LatLonBox><north>2</north><south>1</south><east>200</east><west>-10</west>
<rotation>20</rotation></LatLonBox></GroundOverlay>
<GroundOverlay><color>80ff0000</color>
<gx:LatLonQuad><coordinates>0,0 1,0 1,1 0,1</coordinates></gx:LatLonQuad></GroundOverlay>`),
);
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(`
<Folder><name>f</name>
<Placemark><MultiGeometry>${point()}<LineString><coordinates>1,2 3,4</coordinates></LineString>
<MultiGeometry>${point()}</MultiGeometry></MultiGeometry></Placemark>
<Placemark><name>only the first geometry</name>${point()}${point('5,6')}</Placemark>
</Folder>
<Placemark><name>nowhere</name></Placemark>`),
);
// 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(
'<Placemark><Style><IconStyle><Icon/></IconStyle></Style></Placemark>',
),
).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(`
<GroundOverlay><color>ff00ff00</color></GroundOverlay>
<GroundOverlay><color>ff00ff00</color>
<LatLonBox><north>1</north><south>2</south><east>3</east><west>4</west></LatLonBox></GroundOverlay>`),
);
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(
`<Placemark><Style><IconStyle><hotSpot x="0.5" y="0"/></IconStyle></Style>${point()}</Placemark>`,
),
);
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);
});
+523 -479
View File
File diff suppressed because it is too large. Load diff
+256
View File
@@ -0,0 +1,256 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createImportedDrawing } from './geoImportPins.js';
import { bindFakeGroundRenderer } from '../testSupport/fakeGroundRenderer.mjs';
import { toLonLatH } from '../geo/ellipsoid.js';
const FEATURES = [
{
kind: 'pin',
name: 'tower',
lon: 1,
lat: 2,
height: 0,
mode: 'absolute',
image: 'blob:icon',
size: [32, 32],
scale: 1.5,
color: [1, 0, 0, 1],
course: 90,
pixelOffset: { x: 0, y: -16 },
scaleByDistance: { near: 1, nearValue: 1, far: 2, farValue: 0.1 },
},
{ kind: 'pin', lon: 3, lat: 4, height: 0, mode: 'clamp', image: null },
{
kind: 'line',
name: 'walk',
coords: [
[1, 2, 50],
[3, 4, 50],
],
mode: 'draped',
color: [1, 1, 0, 1],
width: 6,
},
{
kind: 'polygon',
rings: [
[
[0, 0, 200],
[1, 0, 200],
[1, 1, 200],
],
],
clamp: false,
extrude: true,
fill: [1, 0, 0, 0.8],
outline: [0, 0, 0, 1],
outlineWidth: 2,
},
{
kind: 'wall',
coords: [
[1, 2, 150],
[3, 4, 150],
],
fill: [0, 1, 0, 0.5],
outline: [0, 1, 0, 1],
},
{
kind: 'overlay',
rect: { west: 0, south: 0, east: 1, north: 1 },
rotation: 0.3,
image: 'blob:overlay',
},
];
/** A drawing of `features` on a renderer double, with double-clicks it is handed. */
function drawn(features = FEATURES) {
const viewer = {};
const ground = bindFakeGroundRenderer(viewer);
const clicks = [];
const input = {
onPointer(type, fn) {
if (type === 'dblclick') clicks.push(fn);
return () => clicks.splice(clicks.indexOf(fn), 1);
},
stockEnabled: () => true,
};
const drawing = createImportedDrawing(viewer, features, {
inputFor: () => input,
});
drawing.mount();
const items = (kind) => [
...ground.draw.set(drawing.ids[kind]).items.values(),
];
const doubleClick = (hit) => {
ground.setPick(() => hit);
for (const fn of clicks) fn({ position: { x: 1, y: 1 } });
};
return { ground, drawing, items, doubleClick, clicks };
}
test('pins draw as icons with the look their file gave them', (t) => {
const { ground, drawing, items } = drawn();
t.after(ground.unbind);
const [pin, ...others] = items('icons');
assert.deepEqual(drawing.items().icons, [pin], 'what it hands the set');
assert.equal(others.length, 0, 'a pin with no icon draws nothing');
assert.deepEqual(pin.pos, { lon: 1, lat: 2, height: 0 });
assert.equal(pin.height, 'absolute');
assert.deepEqual(
[pin.image, pin.size, pin.scale, pin.color],
['blob:icon', [32, 32], 1.5, [1, 0, 0, 1]],
);
assert.deepEqual([pin.rotationMode, pin.course], ['course', 90]);
assert.deepEqual(pin.pixelOffset, { x: 0, y: -16 });
assert.equal(pin.scaleByDistance.farValue, 0.1);
assert.equal(pin.disableDepthTest, undefined, 'at its own height');
assert.deepEqual(pin.meta, { name: 'tower' });
});
test('paths, polygons, walls and overlays draw as lines and shapes', (t) => {
const { ground, items } = drawn();
t.after(ground.unbind);
const [line] = items('lines');
assert.equal(line.mode, 'draped');
assert.equal(line.widthPx, 6);
assert.deepEqual(line.style, { kind: 'solid', color: [1, 1, 0, 1] });
assert.ok(
line.positions.every((p) => Math.abs(toLonLatH(p).height) < 1e-6),
'draped from the surface',
);
const [polygon, wall, overlay] = items('shapes');
assert.equal(polygon.geometry.perPositionHeight, true);
assert.equal(polygon.extrudeHeight, 0, 'down to the ellipsoid');
assert.deepEqual(polygon.outline, [0, 0, 0, 1]);
assert.equal(polygon.outlineWidth, 2);
assert.equal(polygon.lit, true);
assert.equal(polygon.height, undefined);
assert.equal(wall.geometry.kind, 'wall');
assert.deepEqual(wall.geometry.positions[0], { lon: 1, lat: 2, height: 150 });
assert.equal(overlay.geometry.kind, 'rectangle');
assert.equal(overlay.geometry.rotation, 0.3);
assert.deepEqual(overlay.fill, { image: 'blob:overlay' });
assert.equal(overlay.height, 'clampGround');
});
test('a double-click follows a pin and zooms to a path, until the file goes', (t) => {
const { ground, drawing, doubleClick, clicks } = drawn();
t.after(ground.unbind);
doubleClick({ set: drawing.ids.icons, id: 'p0' });
const followed = ground.follows.at(-1);
assert.equal(followed.key, `${drawing.ids.icons}:p0`);
const at = toLonLatH(followed.position());
assert.ok(Math.abs(at.lon - 1) < 1e-9 && Math.abs(at.lat - 2) < 1e-9);
doubleClick({ set: drawing.ids.lines, id: 'l2' });
assert.ok(ground.flights.at(-1).sphere.radius > 0, 'zoomed to the path');
assert.equal(ground.follows.at(-1), null, 'which ends the follow');
doubleClick({ set: 'someone-else', id: 'p0' });
assert.equal(ground.flights.length, 1, 'other sets are not followed');
doubleClick({ set: drawing.ids.icons, id: 'p0' });
drawing.destroy();
assert.equal(ground.follows.at(-1), null, 'a removed pin is not followed');
assert.equal(clicks.length, 0);
assert.equal(ground.liveSets().length, 0);
assert.deepEqual(drawing.items(), { icons: [], lines: [], shapes: [] });
});
test('hiding a file hides its sets and takes no double-clicks', (t) => {
const { ground, drawing, doubleClick } = drawn();
t.after(ground.unbind);
drawing.show = false;
for (const id of Object.values(drawing.ids))
assert.equal(ground.draw.set(id).show, false);
doubleClick({ set: drawing.ids.icons, id: 'p0' });
assert.equal(ground.follows.length, 0);
drawing.show = true;
assert.equal(ground.draw.set(drawing.ids.icons).show, true);
});
test('a ground pin on the bare ellipsoid stands at 0, not on its sagging tiles', async (t) => {
const pin = { kind: 'pin', lon: 1, lat: 2, height: 0, mode: 'clamp' };
for (const [source, sag, expected] of [
['ellipsoid', -46000, 0],
['ellipsoid', -500, 0],
['terrain', -30, -30],
['terrain', -4000, -4000],
]) {
const { ground, drawing } = drawn([
{ ...pin, image: 'blob:icon' },
{ ...pin, lon: 3, image: 'blob:icon' },
]);
t.after(ground.unbind);
ground.globe.terrain = () => sag;
const probes = [];
ground.renderer.query.sampleHeight = async (points) => {
probes.push(points);
return points.map(() => ({ height: 0, source }));
};
await drawing.refreshGround();
assert.deepEqual(
drawing.items().icons.map((i) => i.pos.height),
[expected, expected],
`${source} ${sag}`,
);
assert.equal(probes.length, 2, 'asked once for the file, both ways');
assert.deepEqual(probes[0], [{ lon: 1, lat: 2 }]);
drawing.destroy();
}
});
test('the ellipsoid is asked about once for each surface', async (t) => {
const pin = { kind: 'pin', lon: 1, lat: 2, height: 0, mode: 'clamp' };
const { ground, drawing } = drawn([{ ...pin, image: 'blob:icon' }]);
t.after(ground.unbind);
let version = 1;
ground.renderer.globe.surfaceVersion = () => version;
ground.globe.terrain = () => -30;
let asked = 0;
ground.renderer.query.sampleHeight = async (points) => {
asked += 1;
return points.map(() => ({ height: -30, source: 'terrain' }));
};
await drawing.refreshGround();
await drawing.refreshGround();
assert.equal(asked, 2, 'one probe, both ways, for the first surface');
version = 2;
await drawing.refreshGround();
assert.equal(asked, 4, 'and again for the next');
assert.equal(drawing.items().icons[0].pos.height, -30);
});
test('nothing draws after destroy, and an overlay without bounds draws nowhere', async (t) => {
const { ground, drawing } = drawn([
{ kind: 'pin', lon: 1, lat: 2, height: 0, mode: 'clamp', image: 'b' },
{ kind: 'overlay', rect: null, color: [1, 1, 1, 1] },
]);
t.after(ground.unbind);
assert.equal(ground.draw.set(drawing.ids.shapes), undefined, 'no shapes');
assert.equal(drawing.points().length, 1, 'framed by its pin alone');
const pending = drawing.refreshGround();
drawing.destroy();
await pending;
drawing.mount();
await drawing.refreshGround();
assert.equal(ground.liveSets().length, 0);
assert.deepEqual(drawing.items(), { icons: [], lines: [], shapes: [] });
});
test('a turned overlay across the antimeridian frames there', (t) => {
const { ground, drawing } = drawn([
{
kind: 'overlay',
rect: { west: 179, south: -1, east: -179, north: 1 },
rotation: Math.PI / 4,
color: [1, 1, 1, 1],
},
]);
t.after(ground.unbind);
const points = drawing.points();
assert.equal(points.length, 4);
for (const [lon, lat] of points) {
assert.ok(Math.abs(lat) < 2, `latitude ${lat}`);
assert.ok(Math.abs(lon) > 178 && Math.abs(lon) <= 180, `longitude ${lon}`);
}
});