Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature attributes are not serialized to KML format #100

Open
towa48 opened this issue Dec 10, 2011 · 1 comment
Open

Feature attributes are not serialized to KML format #100

towa48 opened this issue Dec 10, 2011 · 1 comment

Comments

@towa48
Copy link
Contributor

towa48 commented Dec 10, 2011

Feature attributes are not serialized to KML format. Method OpenLayers.Format.KML.createPlacemarkXML() serialize only name, description and geometry. But i have portion of ExtendedData in my features. Method OpenLayers.Format.KML.parseExtendedData() deserialize Data and SimpleData elemnts to objects and do not make a difference between them.

http://code.google.com/apis/kml/documentation/kmlreference.html#extendeddata
How can I choose between Data and SimpleData?
This code serialize to Data by default.

OpenLayers.Format.KML.prototype.createPlacemarkXML = function (feature) {
    var placemarkNode = this.createElementNS(this.kmlns, "Placemark"),
        placemarkName = this.createElementNS(this.kmlns, "name"),
        placemarkDesc = this.createElementNS(this.kmlns, "description"),
        name, desc, extendedData, attributeName, geometryNode;

    // Placemark id
    if (feature.fid != null) {
        placemarkNode.setAttribute("id", feature.fid);
    }

    // Placemark name
    name = feature.style && feature.style.label ? feature.style.label : feature.attributes.name || feature.id;
    placemarkName.appendChild(this.createTextNode(name));
    placemarkNode.appendChild(placemarkName);

    // Placemark description
    desc = feature.attributes.description || this.placemarksDesc;
    placemarkDesc.appendChild(this.createTextNode(desc));
    placemarkNode.appendChild(placemarkDesc);

    // Geometry node (Point, LineString, etc. nodes)
    geometryNode = this.buildGeometryNode(feature.geometry);
    placemarkNode.appendChild(geometryNode);

    // Search for extended data (see KML spec).
    for (attributeName in feature.attributes) {
        if (feature.attributes[attributeName].value) {
            if (!extendedData) {
                extendedData = this.createElementNS(this.kmlns, "ExtendedData");
            }

            // value element is necessary
            var data = this.createElementNS(this.kmlns, "Data"),
                value = this.createElementNS(this.kmlns, "value");
            data.setAttribute("name", attributeName);
            value.appendChild(this.createTextNode(feature.attributes[attributeName].value));
            data.appendChild(value);
            extendedData.appendChild(data);

            // displayName element
            if (feature.attributes[attributeName].displayName) {
                var displayName = this.createElementNS(this.kmlns, "displayName");
                displayName.appendChild(this.createTextNode(feature.attributes[attributeName].displayName));
                data.appendChild(displayName);
                extendedData.appendChild(displayName);
            }
        }
    }
    if (extendedData) {
        placemarkNode.appendChild(extendedData);
    }

    // TODO: implement serialization for other placemark elements.
    // See KML spec http://code.google.com/apis/kml/documentation/kmlreference.html#placemark
    return placemarkNode;
}
@probins
Copy link
Contributor

probins commented Jan 30, 2012

this raises another issue: what form should attributes take? At the moment, kml is the only format (I think) which creates attributes as objects; all the others are simple key=value pairs. This means that attributes read in from kml aren't compatible with those created from other formats. I assume that this was done to cater for displayName on Data extended data nodes (SimpleData doesn't have this). Your function above won't output anything for attributes in the normal key=value form. (You can test in examples/vector-formats.html)

As displayName is optional anyway and won't be present on many if not most extendedData, I would question whether this incompatibility is worth it, and whether it wouldn't be better to store both Data and SimpleData nodes as simple key=value pairs, the same as other formats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants