/** * 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 `