KML: write attributes; add option for kvp attributes

This commit is contained in:
Peter Robins
2012-02-26 15:23:17 +00:00
parent 3113aba7f1
commit eb47d97774
2 changed files with 155 additions and 55 deletions

View File

@@ -64,9 +64,25 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
* APIProperty: extractAttributes
* {Boolean} Extract attributes from KML. Default is true.
* Extracting styleUrls requires this to be set to true
* Note that currently only Data and SimpleData
* elements are handled.
*/
extractAttributes: true,
/**
* APIProperty: kvpAttributes
* {Boolean} Only used if extractAttributes is true.
* If set to true, attributes will be simple
* key-value pairs, compatible with other formats,
* Any displayName elements will be ignored.
* If set to false, attributes will be objects,
* retaining any displayName elements, but not
* compatible with other formats. Any CDATA in
* displayName will be read in as a string value.
* Default is false.
*/
kvpAttributes: false,
/**
* Property: extractStyles
* {Boolean} Extract styles from KML. Default is false.
@@ -1078,12 +1094,16 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
var valueNode = data.getElementsByTagName("value");
if (valueNode.length) {
ed['value'] = this.getChildValue(valueNode[0]);
}
var nameNode = data.getElementsByTagName("displayName");
if (nameNode.length) {
ed['displayName'] = this.getChildValue(nameNode[0]);
}
attributes[key] = ed;
if (this.kvpAttributes) {
attributes[key] = ed['value'];
} else {
var nameNode = data.getElementsByTagName("displayName");
if (nameNode.length) {
ed['displayName'] = this.getChildValue(nameNode[0]);
}
attributes[key] = ed;
}
}
var simpleDataNodes = node.getElementsByTagName("SimpleData");
for (i = 0, len = simpleDataNodes.length; i < len; i++) {
@@ -1091,8 +1111,12 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
data = simpleDataNodes[i];
key = data.getAttribute("name");
ed['value'] = this.getChildValue(data);
ed['displayName'] = key;
attributes[key] = ed;
if (this.kvpAttributes) {
attributes[key] = ed['value'];
} else {
ed['displayName'] = key;
attributes[key] = ed;
}
}
return attributes;
@@ -1209,7 +1233,14 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
var geometryNode = this.buildGeometryNode(feature.geometry);
placemarkNode.appendChild(geometryNode);
// TBD - deal with remaining (non name/description) attributes.
// output attributes as extendedData
if (feature.attributes) {
var edNode = this.buildExtendedData(feature.attributes);
if (edNode) {
placemarkNode.appendChild(edNode);
}
}
return placemarkNode;
},
@@ -1440,5 +1471,48 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
return point.x + "," + point.y;
},
/**
* Method: buildExtendedData
*
* Parameters:
* attributes - {Object}
*
* Returns
* {DOMElement} A KML ExtendedData node or {null} if no attributes.
*/
buildExtendedData: function(attributes) {
var extendedData = this.createElementNS(this.kmlns, "ExtendedData");
for (attributeName in attributes) {
// empty, name, description, styleUrl attributes ignored
if (attributes[attributeName] && attributeName != "name" && attributeName != "description" && attributeName != "styleUrl") {
var data = this.createElementNS(this.kmlns, "Data");
data.setAttribute("name", attributeName);
var value = this.createElementNS(this.kmlns, "value");
if (typeof attributes[attributeName] == "object") {
// cater for object attributes with 'value' properties
// other object properties will output an empty node
if (attributes[attributeName].value) {
value.appendChild(this.createTextNode(attributes[attributeName].value));
}
if (attributes[attributeName].displayName) {
var displayName = this.createElementNS(this.kmlns, "displayName");
// displayName always written as CDATA
displayName.appendChild(this.getXMLDoc().createCDATASection(attributes[attributeName].displayName));
data.appendChild(displayName);
}
} else {
value.appendChild(this.createTextNode(attributes[attributeName]));
}
data.appendChild(value);
extendedData.appendChild(data);
}
}
if (this.isSimpleContent(extendedData)) {
return null;
} else {
return extendedData;
}
},
CLASS_NAME: "OpenLayers.Format.KML"
});