UNPKG

ol

Version:

OpenLayers mapping library

1,818 lines (1,682 loc) • 96.9 kB
/** * @module ol/format/KML */ import Feature from '../Feature.js'; import ImageState from '../ImageState.js'; import {extend} from '../array.js'; import {asArray} from '../color.js'; import GeometryCollection from '../geom/GeometryCollection.js'; import LineString from '../geom/LineString.js'; import MultiLineString from '../geom/MultiLineString.js'; import MultiPoint from '../geom/MultiPoint.js'; import MultiPolygon from '../geom/MultiPolygon.js'; import Point from '../geom/Point.js'; import Polygon from '../geom/Polygon.js'; import {toRadians} from '../math.js'; import {get as getProjection} from '../proj.js'; import Fill from '../style/Fill.js'; import Icon from '../style/Icon.js'; import Stroke from '../style/Stroke.js'; import Style from '../style/Style.js'; import Text from '../style/Text.js'; import { OBJECT_PROPERTY_NODE_FACTORY, XML_SCHEMA_INSTANCE_URI, createElementNS, getAllTextContent, isDocument, makeArrayExtender, makeArrayPusher, makeChildAppender, makeObjectPropertySetter, makeReplacer, makeSequence, makeSimpleNodeFactory, makeStructureNS, parse, parseNode, pushParseAndPop, pushSerializeAndPop, } from '../xml.js'; import {transformGeometryWithOptions} from './Feature.js'; import XMLFeature from './XMLFeature.js'; import { readBoolean, readDecimal, readString, writeBooleanTextNode, writeDecimalTextNode, writeStringTextNode, } from './xsd.js'; /** * @typedef {Object} Vec2 * @property {number} x X coordinate. * @property {import("../style/Icon.js").IconAnchorUnits} xunits Units of x. * @property {number} y Y coordinate. * @property {import("../style/Icon.js").IconAnchorUnits} yunits Units of Y. * @property {import("../style/Icon.js").IconOrigin} [origin] Origin. */ /** * @typedef {Object} GxTrackObject * @property {Array<Array<number>>} coordinates Coordinates. * @property {Array<number>} whens Whens. */ /** * @const * @type {Array<string>} */ const GX_NAMESPACE_URIS = ['http://www.google.com/kml/ext/2.2']; /** * @const * @type {Array<null|string>} */ const NAMESPACE_URIS = [ null, 'http://earth.google.com/kml/2.0', 'http://earth.google.com/kml/2.1', 'http://earth.google.com/kml/2.2', 'http://www.opengis.net/kml/2.2', ]; /** * @const * @type {string} */ const SCHEMA_LOCATION = 'http://www.opengis.net/kml/2.2 ' + 'https://developers.google.com/kml/schema/kml22gx.xsd'; /** * @type {Object<string, import("../style/Icon.js").IconAnchorUnits>} */ const ICON_ANCHOR_UNITS_MAP = { 'fraction': 'fraction', 'pixels': 'pixels', 'insetPixels': 'pixels', }; /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const PLACEMARK_PARSERS = makeStructureNS( NAMESPACE_URIS, { 'ExtendedData': extendedDataParser, 'Region': regionParser, 'MultiGeometry': makeObjectPropertySetter(readMultiGeometry, 'geometry'), 'LineString': makeObjectPropertySetter(readLineString, 'geometry'), 'LinearRing': makeObjectPropertySetter(readLinearRing, 'geometry'), 'Point': makeObjectPropertySetter(readPoint, 'geometry'), 'Polygon': makeObjectPropertySetter(readPolygon, 'geometry'), 'Style': makeObjectPropertySetter(readStyle), 'StyleMap': placemarkStyleMapParser, 'address': makeObjectPropertySetter(readString), 'description': makeObjectPropertySetter(readString), 'name': makeObjectPropertySetter(readString), 'open': makeObjectPropertySetter(readBoolean), 'phoneNumber': makeObjectPropertySetter(readString), 'styleUrl': makeObjectPropertySetter(readStyleURL), 'visibility': makeObjectPropertySetter(readBoolean), }, makeStructureNS(GX_NAMESPACE_URIS, { 'MultiTrack': makeObjectPropertySetter(readGxMultiTrack, 'geometry'), 'Track': makeObjectPropertySetter(readGxTrack, 'geometry'), }), ); /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const NETWORK_LINK_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'ExtendedData': extendedDataParser, 'Region': regionParser, 'Link': linkParser, 'address': makeObjectPropertySetter(readString), 'description': makeObjectPropertySetter(readString), 'name': makeObjectPropertySetter(readString), 'open': makeObjectPropertySetter(readBoolean), 'phoneNumber': makeObjectPropertySetter(readString), 'visibility': makeObjectPropertySetter(readBoolean), }); /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const LINK_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'href': makeObjectPropertySetter(readURI), }); /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const CAMERA_PARSERS = makeStructureNS(NAMESPACE_URIS, { Altitude: makeObjectPropertySetter(readDecimal), Longitude: makeObjectPropertySetter(readDecimal), Latitude: makeObjectPropertySetter(readDecimal), Tilt: makeObjectPropertySetter(readDecimal), AltitudeMode: makeObjectPropertySetter(readString), Heading: makeObjectPropertySetter(readDecimal), Roll: makeObjectPropertySetter(readDecimal), }); /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const REGION_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'LatLonAltBox': latLonAltBoxParser, 'Lod': lodParser, }); /** * @const * @type {Object<string, Array<string>>} */ // @ts-ignore const KML_SEQUENCE = makeStructureNS(NAMESPACE_URIS, ['Document', 'Placemark']); /** * @const * @type {Object<string, Object<string, import("../xml.js").Serializer>>} */ // @ts-ignore const KML_SERIALIZERS = makeStructureNS(NAMESPACE_URIS, { 'Document': makeChildAppender(writeDocument), 'Placemark': makeChildAppender(writePlacemark), }); /** * @type {import("../color.js").Color} */ let DEFAULT_COLOR; /** * @type {Fill|null} */ let DEFAULT_FILL_STYLE = null; /** * Get the default fill style (or null if not yet set). * @return {Fill|null} The default fill style. */ export function getDefaultFillStyle() { return DEFAULT_FILL_STYLE; } /** * @type {import("../size.js").Size} */ let DEFAULT_IMAGE_STYLE_ANCHOR; /** * @type {import("../style/Icon.js").IconAnchorUnits} */ let DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS; /** * @type {import("../style/Icon.js").IconAnchorUnits} */ let DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS; /** * @type {import("../size.js").Size} */ let DEFAULT_IMAGE_STYLE_SIZE; /** * @type {string} */ let DEFAULT_IMAGE_STYLE_SRC; /** * @type {import("../style/Image.js").default|null} */ let DEFAULT_IMAGE_STYLE = null; /** * Get the default image style (or null if not yet set). * @return {import("../style/Image.js").default|null} The default image style. */ export function getDefaultImageStyle() { return DEFAULT_IMAGE_STYLE; } /** * @type {string} */ let DEFAULT_NO_IMAGE_STYLE; /** * @type {Stroke|null} */ let DEFAULT_STROKE_STYLE = null; /** * Get the default stroke style (or null if not yet set). * @return {Stroke|null} The default stroke style. */ export function getDefaultStrokeStyle() { return DEFAULT_STROKE_STYLE; } /** * @type {Stroke} */ let DEFAULT_TEXT_STROKE_STYLE; /** * @type {Text|null} */ let DEFAULT_TEXT_STYLE = null; /** * Get the default text style (or null if not yet set). * @return {Text|null} The default text style. */ export function getDefaultTextStyle() { return DEFAULT_TEXT_STYLE; } /** * @type {Style|null} */ let DEFAULT_STYLE = null; /** * Get the default style (or null if not yet set). * @return {Style|null} The default style. */ export function getDefaultStyle() { return DEFAULT_STYLE; } /** * @type {Array<Style>|null} */ let DEFAULT_STYLE_ARRAY = null; /** * Get the default style array (or null if not yet set). * @return {Array<Style>|null} The default style. */ export function getDefaultStyleArray() { return DEFAULT_STYLE_ARRAY; } /** * Function that returns the scale needed to normalize an icon image to 32 pixels. * @param {import("../size.js").Size} size Image size. * @return {number} Scale. */ function scaleForSize(size) { return 32 / Math.min(size[0], size[1]); } function createStyleDefaults() { DEFAULT_COLOR = [255, 255, 255, 1]; DEFAULT_FILL_STYLE = new Fill({ color: DEFAULT_COLOR, }); DEFAULT_IMAGE_STYLE_ANCHOR = [20, 2]; DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS = 'pixels'; DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS = 'pixels'; DEFAULT_IMAGE_STYLE_SIZE = [64, 64]; DEFAULT_IMAGE_STYLE_SRC = 'https://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png'; DEFAULT_IMAGE_STYLE = new Icon({ anchor: DEFAULT_IMAGE_STYLE_ANCHOR, anchorOrigin: 'bottom-left', anchorXUnits: DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS, anchorYUnits: DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS, crossOrigin: 'anonymous', rotation: 0, scale: scaleForSize(DEFAULT_IMAGE_STYLE_SIZE), size: DEFAULT_IMAGE_STYLE_SIZE, src: DEFAULT_IMAGE_STYLE_SRC, }); DEFAULT_NO_IMAGE_STYLE = 'NO_IMAGE'; DEFAULT_STROKE_STYLE = new Stroke({ color: DEFAULT_COLOR, width: 1, }); DEFAULT_TEXT_STROKE_STYLE = new Stroke({ color: [51, 51, 51, 1], width: 2, }); DEFAULT_TEXT_STYLE = new Text({ font: 'bold 16px Helvetica', fill: DEFAULT_FILL_STYLE, stroke: DEFAULT_TEXT_STROKE_STYLE, scale: 0.8, }); DEFAULT_STYLE = new Style({ fill: DEFAULT_FILL_STYLE, image: DEFAULT_IMAGE_STYLE, text: DEFAULT_TEXT_STYLE, stroke: DEFAULT_STROKE_STYLE, zIndex: 0, }); DEFAULT_STYLE_ARRAY = [DEFAULT_STYLE]; } /** * @type {HTMLTextAreaElement} */ let TEXTAREA; /** * A function that takes a url `{string}` and returns a url `{string}`. * Might be used to change an icon path or to substitute a * data url obtained from a KMZ array buffer. * * @typedef {function(string):string} IconUrlFunction * @api */ /** * Function that returns a url unchanged. * @param {string} href Input url. * @return {string} Output url. */ function defaultIconUrlFunction(href) { return href; } /** * @typedef {Object} Options * @property {boolean} [extractStyles=true] Extract styles from the KML. * @property {boolean} [showPointNames=true] Show names as labels for placemarks which contain points. * @property {Array<Style>} [defaultStyle] Default style. The * default default style is the same as Google Earth. * @property {boolean} [writeStyles=true] Write styles into KML. * @property {null|string} [crossOrigin='anonymous'] The `crossOrigin` attribute for loaded images. Note that you must provide a * `crossOrigin` value if you want to access pixel data with the Canvas renderer. * @property {ReferrerPolicy} [referrerPolicy] The `referrerPolicy` property for loaded images. * @property {IconUrlFunction} [iconUrlFunction] Function that takes a url string and returns a url string. * Might be used to change an icon path or to substitute a data url obtained from a KMZ array buffer. */ /** * @classdesc * Feature format for reading and writing data in the KML format. * * {@link module:ol/format/KML~KML#readFeature} will read the first feature from * a KML source. * * MultiGeometries are converted into GeometryCollections if they are a mix of * geometry types, and into MultiPoint/MultiLineString/MultiPolygon if they are * all of the same type. * * @api */ class KML extends XMLFeature { /** * @param {Options} [options] Options. */ constructor(options) { super(); options = options ? options : {}; if (!DEFAULT_STYLE_ARRAY) { createStyleDefaults(); } /** * @type {import("../proj/Projection.js").default} */ this.dataProjection = getProjection('EPSG:4326'); /** * @private * @type {Array<Style>} */ this.defaultStyle_ = options.defaultStyle ? options.defaultStyle : DEFAULT_STYLE_ARRAY; /** * @private * @type {boolean} */ this.extractStyles_ = options.extractStyles !== undefined ? options.extractStyles : true; /** * @type {boolean} */ this.writeStyles_ = options.writeStyles !== undefined ? options.writeStyles : true; /** * @private * @type {!Object<string, (Array<Style>|string)>} */ this.sharedStyles_ = {}; /** * @private * @type {boolean} */ this.showPointNames_ = options.showPointNames !== undefined ? options.showPointNames : true; /** * @type {null|string} */ this.crossOrigin_ = options.crossOrigin !== undefined ? options.crossOrigin : 'anonymous'; /** * @type {ReferrerPolicy} */ this.referrerPolicy_ = options.referrerPolicy; /** * @type {IconUrlFunction} */ this.iconUrlFunction_ = options.iconUrlFunction ? options.iconUrlFunction : defaultIconUrlFunction; this.supportedMediaTypes = ['application/vnd.google-earth.kml+xml']; } /** * @param {Node} node Node. * @param {Array<*>} objectStack Object stack. * @private * @return {Array<Feature>|undefined} Features. */ readDocumentOrFolder_(node, objectStack) { // FIXME use scope somehow const parsersNS = makeStructureNS(NAMESPACE_URIS, { 'Document': makeArrayExtender(this.readDocumentOrFolder_, this), 'Folder': makeArrayExtender(this.readDocumentOrFolder_, this), 'Placemark': makeArrayPusher(this.readPlacemark_, this), 'Style': this.readSharedStyle_.bind(this), 'StyleMap': this.readSharedStyleMap_.bind(this), }); /** @type {Array<Feature>} */ // @ts-ignore const features = pushParseAndPop([], parsersNS, node, objectStack, this); if (features) { return features; } return undefined; } /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @private * @return {Feature|undefined} Feature. */ readPlacemark_(node, objectStack) { const object = pushParseAndPop( {'geometry': null}, PLACEMARK_PARSERS, node, objectStack, this, ); if (!object) { return undefined; } const feature = new Feature(); const id = node.getAttribute('id'); if (id !== null) { feature.setId(id); } const options = /** @type {import("./Feature.js").ReadOptions} */ ( objectStack[0] ); const geometry = object['geometry']; if (geometry) { transformGeometryWithOptions(geometry, false, options); } feature.setGeometry(geometry); delete object['geometry']; if (this.extractStyles_) { const style = object['Style']; const styleUrl = object['styleUrl']; const styleFunction = createFeatureStyleFunction( style, styleUrl, this.defaultStyle_, this.sharedStyles_, this.showPointNames_, ); feature.setStyle(styleFunction); } delete object['Style']; // we do not remove the styleUrl property from the object, so it // gets stored on feature when setProperties is called feature.setProperties(object, true); return feature; } /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @private */ readSharedStyle_(node, objectStack) { const id = node.getAttribute('id'); if (id !== null) { const style = readStyle.call(this, node, objectStack); if (style) { let styleUri; let baseURI = node.baseURI; if (!baseURI || baseURI == 'about:blank') { baseURI = window.location.href; } if (baseURI) { const url = new URL('#' + id, baseURI); styleUri = url.href; } else { styleUri = '#' + id; } this.sharedStyles_[styleUri] = style; } } } /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @private */ readSharedStyleMap_(node, objectStack) { const id = node.getAttribute('id'); if (id === null) { return; } const styleMapValue = readStyleMapValue.call(this, node, objectStack); if (!styleMapValue) { return; } let styleUri; let baseURI = node.baseURI; if (!baseURI || baseURI == 'about:blank') { baseURI = window.location.href; } if (baseURI) { const url = new URL('#' + id, baseURI); styleUri = url.href; } else { styleUri = '#' + id; } this.sharedStyles_[styleUri] = styleMapValue; } /** * @param {Element} node Node. * @param {import("./Feature.js").ReadOptions} [options] Options. * @return {import("../Feature.js").default} Feature. * @override */ readFeatureFromNode(node, options) { if (!NAMESPACE_URIS.includes(node.namespaceURI)) { return null; } const feature = this.readPlacemark_(node, [ this.getReadOptions(node, options), ]); if (feature) { return feature; } return null; } /** * @protected * @param {Element} node Node. * @param {import("./Feature.js").ReadOptions} [options] Options. * @return {Array<import("../Feature.js").default>} Features. * @override */ readFeaturesFromNode(node, options) { if (!NAMESPACE_URIS.includes(node.namespaceURI)) { return []; } let features; const localName = node.localName; if (localName == 'Document' || localName == 'Folder') { features = this.readDocumentOrFolder_(node, [ this.getReadOptions(node, options), ]); if (features) { return features; } return []; } if (localName == 'Placemark') { const feature = this.readPlacemark_(node, [ this.getReadOptions(node, options), ]); if (feature) { return [feature]; } return []; } if (localName == 'kml') { features = []; for (let n = node.firstElementChild; n; n = n.nextElementSibling) { const fs = this.readFeaturesFromNode(n, options); if (fs) { extend(features, fs); } } return features; } return []; } /** * Read the name of the KML. * * @param {Document|Element|string} source Source. * @return {string|undefined} Name. * @api */ readName(source) { if (!source) { return undefined; } if (typeof source === 'string') { const doc = parse(source); return this.readNameFromDocument(doc); } if (isDocument(source)) { return this.readNameFromDocument(/** @type {Document} */ (source)); } return this.readNameFromNode(/** @type {Element} */ (source)); } /** * @param {Document} doc Document. * @return {string|undefined} Name. */ readNameFromDocument(doc) { for (let n = /** @type {Node} */ (doc.firstChild); n; n = n.nextSibling) { if (n.nodeType == Node.ELEMENT_NODE) { const name = this.readNameFromNode(/** @type {Element} */ (n)); if (name) { return name; } } } return undefined; } /** * @param {Element} node Node. * @return {string|undefined} Name. */ readNameFromNode(node) { for (let n = node.firstElementChild; n; n = n.nextElementSibling) { if (NAMESPACE_URIS.includes(n.namespaceURI) && n.localName == 'name') { return readString(n); } } for (let n = node.firstElementChild; n; n = n.nextElementSibling) { const localName = n.localName; if ( NAMESPACE_URIS.includes(n.namespaceURI) && (localName == 'Document' || localName == 'Folder' || localName == 'Placemark' || localName == 'kml') ) { const name = this.readNameFromNode(n); if (name) { return name; } } } return undefined; } /** * Read the network links of the KML. * * @param {Document|Element|string} source Source. * @return {Array<Object>} Network links. * @api */ readNetworkLinks(source) { const networkLinks = []; if (typeof source === 'string') { const doc = parse(source); extend(networkLinks, this.readNetworkLinksFromDocument(doc)); } else if (isDocument(source)) { extend( networkLinks, this.readNetworkLinksFromDocument(/** @type {Document} */ (source)), ); } else { extend( networkLinks, this.readNetworkLinksFromNode(/** @type {Element} */ (source)), ); } return networkLinks; } /** * @param {Document} doc Document. * @return {Array<Object>} Network links. */ readNetworkLinksFromDocument(doc) { const networkLinks = []; for (let n = /** @type {Node} */ (doc.firstChild); n; n = n.nextSibling) { if (n.nodeType == Node.ELEMENT_NODE) { extend( networkLinks, this.readNetworkLinksFromNode(/** @type {Element} */ (n)), ); } } return networkLinks; } /** * @param {Element} node Node. * @return {Array<Object>} Network links. */ readNetworkLinksFromNode(node) { const networkLinks = []; for (let n = node.firstElementChild; n; n = n.nextElementSibling) { if ( NAMESPACE_URIS.includes(n.namespaceURI) && n.localName == 'NetworkLink' ) { const obj = pushParseAndPop({}, NETWORK_LINK_PARSERS, n, []); networkLinks.push(obj); } } for (let n = node.firstElementChild; n; n = n.nextElementSibling) { const localName = n.localName; if ( NAMESPACE_URIS.includes(n.namespaceURI) && (localName == 'Document' || localName == 'Folder' || localName == 'kml') ) { extend(networkLinks, this.readNetworkLinksFromNode(n)); } } return networkLinks; } /** * Read the regions of the KML. * * @param {Document|Element|string} source Source. * @return {Array<Object>} Regions. * @api */ readRegion(source) { const regions = []; if (typeof source === 'string') { const doc = parse(source); extend(regions, this.readRegionFromDocument(doc)); } else if (isDocument(source)) { extend( regions, this.readRegionFromDocument(/** @type {Document} */ (source)), ); } else { extend(regions, this.readRegionFromNode(/** @type {Element} */ (source))); } return regions; } /** * @param {Document} doc Document. * @return {Array<Object>} Region. */ readRegionFromDocument(doc) { const regions = []; for (let n = /** @type {Node} */ (doc.firstChild); n; n = n.nextSibling) { if (n.nodeType == Node.ELEMENT_NODE) { extend(regions, this.readRegionFromNode(/** @type {Element} */ (n))); } } return regions; } /** * @param {Element} node Node. * @return {Array<Object>} Region. * @api */ readRegionFromNode(node) { const regions = []; for (let n = node.firstElementChild; n; n = n.nextElementSibling) { if (NAMESPACE_URIS.includes(n.namespaceURI) && n.localName == 'Region') { const obj = pushParseAndPop({}, REGION_PARSERS, n, []); regions.push(obj); } } for (let n = node.firstElementChild; n; n = n.nextElementSibling) { const localName = n.localName; if ( NAMESPACE_URIS.includes(n.namespaceURI) && (localName == 'Document' || localName == 'Folder' || localName == 'kml') ) { extend(regions, this.readRegionFromNode(n)); } } return regions; } /** * @typedef {Object} KMLCamera Specifies the observer's viewpoint and associated view parameters. * @property {number} [Latitude] Latitude of the camera. * @property {number} [Longitude] Longitude of the camera. * @property {number} [Altitude] Altitude of the camera. * @property {string} [AltitudeMode] Floor-related altitude mode. * @property {number} [Heading] Horizontal camera rotation. * @property {number} [Tilt] Lateral camera rotation. * @property {number} [Roll] Vertical camera rotation. */ /** * Read the cameras of the KML. * * @param {Document|Element|string} source Source. * @return {Array<KMLCamera>} Cameras. * @api */ readCamera(source) { const cameras = []; if (typeof source === 'string') { const doc = parse(source); extend(cameras, this.readCameraFromDocument(doc)); } else if (isDocument(source)) { extend( cameras, this.readCameraFromDocument(/** @type {Document} */ (source)), ); } else { extend(cameras, this.readCameraFromNode(/** @type {Element} */ (source))); } return cameras; } /** * @param {Document} doc Document. * @return {Array<KMLCamera>} Cameras. */ readCameraFromDocument(doc) { const cameras = []; for (let n = /** @type {Node} */ (doc.firstChild); n; n = n.nextSibling) { if (n.nodeType === Node.ELEMENT_NODE) { extend(cameras, this.readCameraFromNode(/** @type {Element} */ (n))); } } return cameras; } /** * @param {Element} node Node. * @return {Array<KMLCamera>} Cameras. * @api */ readCameraFromNode(node) { const cameras = []; for (let n = node.firstElementChild; n; n = n.nextElementSibling) { if (NAMESPACE_URIS.includes(n.namespaceURI) && n.localName === 'Camera') { const obj = pushParseAndPop({}, CAMERA_PARSERS, n, []); cameras.push(obj); } } for (let n = node.firstElementChild; n; n = n.nextElementSibling) { const localName = n.localName; if ( NAMESPACE_URIS.includes(n.namespaceURI) && (localName === 'Document' || localName === 'Folder' || localName === 'Placemark' || localName === 'kml') ) { extend(cameras, this.readCameraFromNode(n)); } } return cameras; } /** * Encode an array of features in the KML format as an XML node. GeometryCollections, * MultiPoints, MultiLineStrings, and MultiPolygons are output as MultiGeometries. * * @param {Array<Feature>} features Features. * @param {import("./Feature.js").WriteOptions} [options] Options. * @return {Node} Node. * @api * @override */ writeFeaturesNode(features, options) { options = this.adaptOptions(options); const kml = createElementNS(NAMESPACE_URIS[4], 'kml'); const xmlnsUri = 'http://www.w3.org/2000/xmlns/'; kml.setAttributeNS(xmlnsUri, 'xmlns:gx', GX_NAMESPACE_URIS[0]); kml.setAttributeNS(xmlnsUri, 'xmlns:xsi', XML_SCHEMA_INSTANCE_URI); kml.setAttributeNS( XML_SCHEMA_INSTANCE_URI, 'xsi:schemaLocation', SCHEMA_LOCATION, ); const /** @type {import("../xml.js").NodeStackItem} */ context = { node: kml, }; /** @type {!Object<string, (Array<Feature>|Feature|undefined)>} */ const properties = {}; if (features.length > 1) { properties['Document'] = features; } else if (features.length == 1) { properties['Placemark'] = features[0]; } const orderedKeys = KML_SEQUENCE[kml.namespaceURI]; const values = makeSequence(properties, orderedKeys); pushSerializeAndPop( context, KML_SERIALIZERS, OBJECT_PROPERTY_NODE_FACTORY, values, [options], orderedKeys, this, ); return kml; } } /** * @param {Style|undefined} foundStyle Style. * @param {string} name Name. * @return {Style} style Style. */ function createNameStyleFunction(foundStyle, name) { const textOffset = [0, 0]; /** @type {CanvasTextAlign} */ let textAlign = 'start'; const imageStyle = foundStyle.getImage(); if (imageStyle) { const imageSize = imageStyle.getSize(); if (imageSize && imageSize.length == 2) { const imageScale = imageStyle.getScaleArray(); const anchor = imageStyle.getAnchor(); // Offset the label to be centered to the right of the icon, // if there is one. textOffset[0] = imageScale[0] * (imageSize[0] - anchor[0]); textOffset[1] = imageScale[1] * (imageSize[1] / 2 - anchor[1]); textAlign = 'left'; } } let textStyle = foundStyle.getText(); if (textStyle) { // clone the text style, customizing it with name, alignments and offset. // Note that kml does not support many text options that OpenLayers does (rotation, textBaseline). textStyle = textStyle.clone(); textStyle.setFont(textStyle.getFont() || DEFAULT_TEXT_STYLE.getFont()); textStyle.setScale(textStyle.getScale() || DEFAULT_TEXT_STYLE.getScale()); textStyle.setFill(textStyle.getFill() || DEFAULT_TEXT_STYLE.getFill()); textStyle.setStroke(textStyle.getStroke() || DEFAULT_TEXT_STROKE_STYLE); } else { textStyle = DEFAULT_TEXT_STYLE.clone(); } textStyle.setText(name); textStyle.setOffsetX(textOffset[0]); textStyle.setOffsetY(textOffset[1]); textStyle.setTextAlign(textAlign); const nameStyle = new Style({ image: imageStyle, text: textStyle, }); return nameStyle; } /** * @param {Array<Style>|undefined} style Style. * @param {string} styleUrl Style URL. * @param {Array<Style>} defaultStyle Default style. * @param {!Object<string, (Array<Style>|string)>} sharedStyles Shared styles. * @param {boolean|undefined} showPointNames true to show names for point placemarks. * @return {import("../style/Style.js").StyleFunction} Feature style function. */ function createFeatureStyleFunction( style, styleUrl, defaultStyle, sharedStyles, showPointNames, ) { return ( /** * @param {Feature} feature feature. * @param {number} resolution Resolution. * @return {Array<Style>|Style} Style. */ function (feature, resolution) { let drawName = showPointNames; let name = ''; let multiGeometryPoints = []; if (drawName) { const geometry = feature.getGeometry(); if (geometry) { if (geometry instanceof GeometryCollection) { multiGeometryPoints = geometry .getGeometriesArrayRecursive() .filter(function (geometry) { const type = geometry.getType(); return type === 'Point' || type === 'MultiPoint'; }); drawName = multiGeometryPoints.length > 0; } else { const type = geometry.getType(); drawName = type === 'Point' || type === 'MultiPoint'; } } } if (drawName) { name = /** @type {string} */ (feature.get('name')); drawName = drawName && !!name; // convert any html character codes if (drawName && /&[^&]+;/.test(name)) { if (!TEXTAREA) { TEXTAREA = document.createElement('textarea'); } TEXTAREA.innerHTML = name; name = TEXTAREA.value; } } let featureStyle = defaultStyle; if (style) { featureStyle = style; } else if (styleUrl) { featureStyle = findStyle(styleUrl, defaultStyle, sharedStyles); } if (drawName) { const nameStyle = createNameStyleFunction(featureStyle[0], name); if (multiGeometryPoints.length > 0) { // in multigeometries restrict the name style to points and create a // style without image or text for geometries requiring fill or stroke // including any polygon specific style if there is one nameStyle.setGeometry(new GeometryCollection(multiGeometryPoints)); const baseStyle = new Style({ geometry: featureStyle[0].getGeometry(), image: null, fill: featureStyle[0].getFill(), stroke: featureStyle[0].getStroke(), text: null, }); return [nameStyle, baseStyle].concat(featureStyle.slice(1)); } return nameStyle; } return featureStyle; } ); } /** * @param {Array<Style>|string|undefined} styleValue Style value. * @param {Array<Style>} defaultStyle Default style. * @param {!Object<string, (Array<Style>|string)>} sharedStyles * Shared styles. * @return {Array<Style>} Style. */ function findStyle(styleValue, defaultStyle, sharedStyles) { if (Array.isArray(styleValue)) { return styleValue; } if (typeof styleValue === 'string') { return findStyle(sharedStyles[styleValue], defaultStyle, sharedStyles); } return defaultStyle; } /** * @param {Node} node Node. * @return {import("../color.js").Color|undefined} Color. */ function readColor(node) { const s = getAllTextContent(node, false); // The KML specification states that colors should not include a leading `#` // but we tolerate them. const m = /^\s*#?\s*([0-9A-Fa-f]{8})\s*$/.exec(s); if (m) { const hexColor = m[1]; return [ parseInt(hexColor.substr(6, 2), 16), parseInt(hexColor.substr(4, 2), 16), parseInt(hexColor.substr(2, 2), 16), parseInt(hexColor.substr(0, 2), 16) / 255, ]; } return undefined; } /** * @param {Node} node Node. * @return {Array<number>|undefined} Flat coordinates. */ export function readFlatCoordinates(node) { let s = getAllTextContent(node, false); const flatCoordinates = []; // The KML specification states that coordinate tuples should not include // spaces, but we tolerate them. s = s.replace(/\s*,\s*/g, ','); const re = /^\s*([+\-]?\d*\.?\d+(?:e[+\-]?\d+)?),([+\-]?\d*\.?\d+(?:e[+\-]?\d+)?)(?:\s+|,|$)(?:([+\-]?\d*\.?\d+(?:e[+\-]?\d+)?)(?:\s+|$))?\s*/i; let m; while ((m = re.exec(s))) { const x = parseFloat(m[1]); const y = parseFloat(m[2]); const z = m[3] ? parseFloat(m[3]) : 0; flatCoordinates.push(x, y, z); s = s.substr(m[0].length); } if (s !== '') { return undefined; } return flatCoordinates; } /** * @param {Node} node Node. * @return {string} URI. */ function readURI(node) { const s = getAllTextContent(node, false).trim(); let baseURI = node.baseURI; if (!baseURI || baseURI == 'about:blank') { baseURI = window.location.href; } if (baseURI) { const url = new URL(s, baseURI); return url.href; } return s; } /** * @param {Node} node Node. * @return {string} URI. */ function readStyleURL(node) { // KML files in the wild occasionally forget the leading // `#` on styleUrlsdefined in the same document. const s = getAllTextContent(node, false) .trim() .replace(/^(?!.*#)/, '#'); let baseURI = node.baseURI; if (!baseURI || baseURI == 'about:blank') { baseURI = window.location.href; } if (baseURI) { const url = new URL(s, baseURI); return url.href; } return s; } /** * @param {Element} node Node. * @return {Vec2} Vec2. */ function readVec2(node) { const xunits = node.getAttribute('xunits'); const yunits = node.getAttribute('yunits'); /** @type {import('../style/Icon.js').IconOrigin} */ let origin; if (xunits !== 'insetPixels') { if (yunits !== 'insetPixels') { origin = 'bottom-left'; } else { origin = 'top-left'; } } else { if (yunits !== 'insetPixels') { origin = 'bottom-right'; } else { origin = 'top-right'; } } return { x: parseFloat(node.getAttribute('x')), xunits: ICON_ANCHOR_UNITS_MAP[xunits], y: parseFloat(node.getAttribute('y')), yunits: ICON_ANCHOR_UNITS_MAP[yunits], origin: origin, }; } /** * @param {Node} node Node. * @return {number|undefined} Scale. */ function readScale(node) { return readDecimal(node); } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const STYLE_MAP_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'Pair': pairDataParser, }); /** * @this {KML} * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {Array<Style>|string|undefined} StyleMap. */ function readStyleMapValue(node, objectStack) { return pushParseAndPop(undefined, STYLE_MAP_PARSERS, node, objectStack, this); } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const ICON_STYLE_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'Icon': makeObjectPropertySetter(readIcon), 'color': makeObjectPropertySetter(readColor), 'heading': makeObjectPropertySetter(readDecimal), 'hotSpot': makeObjectPropertySetter(readVec2), 'scale': makeObjectPropertySetter(readScale), }); /** * @this {KML} * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. */ function iconStyleParser(node, objectStack) { // FIXME refreshMode // FIXME refreshInterval // FIXME viewRefreshTime // FIXME viewBoundScale // FIXME viewFormat // FIXME httpQuery const object = pushParseAndPop({}, ICON_STYLE_PARSERS, node, objectStack); if (!object) { return; } const styleObject = /** @type {Object} */ ( objectStack[objectStack.length - 1] ); const IconObject = 'Icon' in object ? object['Icon'] : {}; const drawIcon = !('Icon' in object) || Object.keys(IconObject).length > 0; let src; const href = /** @type {string|undefined} */ (IconObject['href']); if (href) { src = href; } else if (drawIcon) { src = DEFAULT_IMAGE_STYLE_SRC; } let anchor, anchorXUnits, anchorYUnits; /** @type {import('../style/Icon.js').IconOrigin|undefined} */ let anchorOrigin = 'bottom-left'; const hotSpot = /** @type {Vec2|undefined} */ (object['hotSpot']); if (hotSpot) { anchor = [hotSpot.x, hotSpot.y]; anchorXUnits = hotSpot.xunits; anchorYUnits = hotSpot.yunits; anchorOrigin = hotSpot.origin; } else if (/^https?:\/\/maps\.(?:google|gstatic)\.com\//.test(src)) { // Google hotspots from https://kml4earth.appspot.com/icons.html#notes if (src.includes('pushpin')) { anchor = DEFAULT_IMAGE_STYLE_ANCHOR; anchorXUnits = DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS; anchorYUnits = DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS; } else if (src.includes('arrow-reverse')) { anchor = [54, 42]; anchorXUnits = DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS; anchorYUnits = DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS; } else if (src.includes('paddle')) { anchor = [32, 1]; anchorXUnits = DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS; anchorYUnits = DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS; } } let offset; const x = /** @type {number|undefined} */ (IconObject['x']); const y = /** @type {number|undefined} */ (IconObject['y']); if (x !== undefined && y !== undefined) { offset = [x, y]; } let size; const w = /** @type {number|undefined} */ (IconObject['w']); const h = /** @type {number|undefined} */ (IconObject['h']); if (w !== undefined && h !== undefined) { size = [w, h]; } let rotation; const heading = /** @type {number} */ (object['heading']); if (heading !== undefined) { rotation = toRadians(heading); } const scale = /** @type {number|undefined} */ (object['scale']); const color = /** @type {Array<number>|undefined} */ (object['color']); if (drawIcon) { if (src == DEFAULT_IMAGE_STYLE_SRC) { size = DEFAULT_IMAGE_STYLE_SIZE; } const imageStyle = new Icon({ anchor: anchor, anchorOrigin: anchorOrigin, anchorXUnits: anchorXUnits, anchorYUnits: anchorYUnits, crossOrigin: this.crossOrigin_, referrerPolicy: this.referrerPolicy_, offset: offset, offsetOrigin: 'bottom-left', rotation: rotation, scale: scale, size: size, src: this.iconUrlFunction_(src), color: color, }); const imageScale = imageStyle.getScaleArray()[0]; const imageSize = imageStyle.getSize(); if (imageSize === null) { const imageState = imageStyle.getImageState(); if (imageState === ImageState.IDLE || imageState === ImageState.LOADING) { const listener = function () { const imageState = imageStyle.getImageState(); if ( !( imageState === ImageState.IDLE || imageState === ImageState.LOADING ) ) { const imageSize = imageStyle.getSize(); if (imageSize && imageSize.length == 2) { const resizeScale = scaleForSize(imageSize); imageStyle.setScale(imageScale * resizeScale); } imageStyle.unlistenImageChange(listener); } }; imageStyle.listenImageChange(listener); if (imageState === ImageState.IDLE) { imageStyle.load(); } } } else if (imageSize.length == 2) { const resizeScale = scaleForSize(imageSize); imageStyle.setScale(imageScale * resizeScale); } styleObject['imageStyle'] = imageStyle; } else { // handle the case when we explicitly want to draw no icon. styleObject['imageStyle'] = DEFAULT_NO_IMAGE_STYLE; } } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const LABEL_STYLE_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'color': makeObjectPropertySetter(readColor), 'scale': makeObjectPropertySetter(readScale), }); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. */ function labelStyleParser(node, objectStack) { // FIXME colorMode const object = pushParseAndPop({}, LABEL_STYLE_PARSERS, node, objectStack); if (!object) { return; } const styleObject = objectStack[objectStack.length - 1]; const textStyle = new Text({ fill: new Fill({ color: /** @type {import("../color.js").Color} */ ('color' in object ? object['color'] : DEFAULT_COLOR), }), scale: /** @type {number|undefined} */ (object['scale']), }); styleObject['textStyle'] = textStyle; } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const LINE_STYLE_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'color': makeObjectPropertySetter(readColor), 'width': makeObjectPropertySetter(readDecimal), }); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. */ function lineStyleParser(node, objectStack) { // FIXME colorMode // FIXME gx:outerColor // FIXME gx:outerWidth // FIXME gx:physicalWidth // FIXME gx:labelVisibility const object = pushParseAndPop({}, LINE_STYLE_PARSERS, node, objectStack); if (!object) { return; } const styleObject = objectStack[objectStack.length - 1]; const strokeStyle = new Stroke({ color: /** @type {import("../color.js").Color} */ ('color' in object ? object['color'] : DEFAULT_COLOR), width: /** @type {number} */ ('width' in object ? object['width'] : 1), }); styleObject['strokeStyle'] = strokeStyle; } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const POLY_STYLE_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'color': makeObjectPropertySetter(readColor), 'fill': makeObjectPropertySetter(readBoolean), 'outline': makeObjectPropertySetter(readBoolean), }); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. */ function polyStyleParser(node, objectStack) { // FIXME colorMode const object = pushParseAndPop({}, POLY_STYLE_PARSERS, node, objectStack); if (!object) { return; } const styleObject = objectStack[objectStack.length - 1]; const fillStyle = new Fill({ color: /** @type {import("../color.js").Color} */ ('color' in object ? object['color'] : DEFAULT_COLOR), }); styleObject['fillStyle'] = fillStyle; const fill = /** @type {boolean|undefined} */ (object['fill']); if (fill !== undefined) { styleObject['fill'] = fill; } const outline = /** @type {boolean|undefined} */ (object['outline']); if (outline !== undefined) { styleObject['outline'] = outline; } } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const FLAT_LINEAR_RING_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'coordinates': makeReplacer(readFlatCoordinates), }); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {Array<number>} LinearRing flat coordinates. */ function readFlatLinearRing(node, objectStack) { return pushParseAndPop(null, FLAT_LINEAR_RING_PARSERS, node, objectStack); } /** * @param {Node} node Node. * @param {Array<*>} objectStack Object stack. */ function gxCoordParser(node, objectStack) { const gxTrackObject = /** @type {GxTrackObject} */ (objectStack[objectStack.length - 1]); const coordinates = gxTrackObject.coordinates; const s = getAllTextContent(node, false); const re = /^\s*([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s+([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s+([+\-]?\d+(?:\.\d*)?(?:e[+\-]?\d*)?)\s*$/i; const m = re.exec(s); if (m) { const x = parseFloat(m[1]); const y = parseFloat(m[2]); const z = parseFloat(m[3]); coordinates.push([x, y, z]); } else { coordinates.push([]); } } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const GX_MULTITRACK_GEOMETRY_PARSERS = makeStructureNS(GX_NAMESPACE_URIS, { 'Track': makeArrayPusher(readGxTrack), }); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {MultiLineString|undefined} MultiLineString. */ function readGxMultiTrack(node, objectStack) { const lineStrings = pushParseAndPop( [], GX_MULTITRACK_GEOMETRY_PARSERS, node, objectStack, ); if (!lineStrings) { return undefined; } return new MultiLineString(lineStrings); } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const GX_TRACK_PARSERS = makeStructureNS( NAMESPACE_URIS, { 'when': whenParser, }, makeStructureNS(GX_NAMESPACE_URIS, { 'coord': gxCoordParser, }), ); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {LineString|undefined} LineString. */ function readGxTrack(node, objectStack) { const gxTrackObject = pushParseAndPop( /** @type {GxTrackObject} */ ({ coordinates: [], whens: [], }), GX_TRACK_PARSERS, node, objectStack, ); if (!gxTrackObject) { return undefined; } const flatCoordinates = []; const coordinates = gxTrackObject.coordinates; const whens = gxTrackObject.whens; for ( let i = 0, ii = Math.min(coordinates.length, whens.length); i < ii; ++i ) { if (coordinates[i].length == 3) { flatCoordinates.push( coordinates[i][0], coordinates[i][1], coordinates[i][2], whens[i], ); } } return new LineString(flatCoordinates, 'XYZM'); } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const ICON_PARSERS = makeStructureNS( NAMESPACE_URIS, { 'href': makeObjectPropertySetter(readURI), }, makeStructureNS(GX_NAMESPACE_URIS, { 'x': makeObjectPropertySetter(readDecimal), 'y': makeObjectPropertySetter(readDecimal), 'w': makeObjectPropertySetter(readDecimal), 'h': makeObjectPropertySetter(readDecimal), }), ); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {Object} Icon object. */ function readIcon(node, objectStack) { const iconObject = pushParseAndPop({}, ICON_PARSERS, node, objectStack); if (iconObject) { return iconObject; } return null; } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const GEOMETRY_FLAT_COORDINATES_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'coordinates': makeReplacer(readFlatCoordinates), }); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {Array<number>} Flat coordinates. */ function readFlatCoordinatesFromNode(node, objectStack) { return pushParseAndPop( null, GEOMETRY_FLAT_COORDINATES_PARSERS, node, objectStack, ); } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const EXTRUDE_AND_ALTITUDE_MODE_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'extrude': makeObjectPropertySetter(readBoolean), 'tessellate': makeObjectPropertySetter(readBoolean), 'altitudeMode': makeObjectPropertySetter(readString), }); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {LineString|undefined} LineString. */ function readLineString(node, objectStack) { const properties = pushParseAndPop( {}, EXTRUDE_AND_ALTITUDE_MODE_PARSERS, node, objectStack, ); const flatCoordinates = readFlatCoordinatesFromNode(node, objectStack); if (flatCoordinates) { const lineString = new LineString(flatCoordinates, 'XYZ'); lineString.setProperties(properties, true); return lineString; } return undefined; } /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {Polygon|undefined} Polygon. */ function readLinearRing(node, objectStack) { const properties = pushParseAndPop( {}, EXTRUDE_AND_ALTITUDE_MODE_PARSERS, node, objectStack, ); const flatCoordinates = readFlatCoordinatesFromNode(node, objectStack); if (flatCoordinates) { const polygon = new Polygon(flatCoordinates, 'XYZ', [ flatCoordinates.length, ]); polygon.setProperties(properties, true); return polygon; } return undefined; } /** * @const * @type {Object<string, Object<string, import("../xml.js").Parser>>} */ // @ts-ignore const MULTI_GEOMETRY_PARSERS = makeStructureNS(NAMESPACE_URIS, { 'LineString': makeArrayPusher(readLineString), 'LinearRing': makeArrayPusher(readLinearRing), 'MultiGeometry': makeArrayPusher(readMultiGeometry), 'Point': makeArrayPusher(readPoint), 'Polygon': makeArrayPusher(readPolygon), }); /** * @param {Element} node Node. * @param {Array<*>} objectStack Object stack. * @return {import("../geom/Geometry.js").default} Geometry. */ function readMultiGeometry(node,