Add ol.format.KML#readName
This commit is contained in:
@@ -14,6 +14,7 @@ goog.require('goog.Uri');
|
|||||||
goog.require('goog.array');
|
goog.require('goog.array');
|
||||||
goog.require('goog.asserts');
|
goog.require('goog.asserts');
|
||||||
goog.require('goog.dom.NodeType');
|
goog.require('goog.dom.NodeType');
|
||||||
|
goog.require('goog.dom.xml');
|
||||||
goog.require('goog.math');
|
goog.require('goog.math');
|
||||||
goog.require('goog.object');
|
goog.require('goog.object');
|
||||||
goog.require('goog.string');
|
goog.require('goog.string');
|
||||||
@@ -1343,6 +1344,55 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Document|Node|string} source Souce.
|
||||||
|
* @return {string|undefined} Name.
|
||||||
|
*/
|
||||||
|
ol.format.KML.prototype.readName = function(source) {
|
||||||
|
if (source instanceof Node) {
|
||||||
|
return this.readNameFromNode(source);
|
||||||
|
} else if (goog.isString(source)) {
|
||||||
|
var doc = goog.dom.xml.loadXml(source);
|
||||||
|
return this.readNameFromNode(doc);
|
||||||
|
} else {
|
||||||
|
goog.asserts.fail();
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Node} node Node.
|
||||||
|
* @return {string|undefined} Name.
|
||||||
|
*/
|
||||||
|
ol.format.KML.prototype.readNameFromNode = function(node) {
|
||||||
|
var n;
|
||||||
|
for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) {
|
||||||
|
if (n.nodeType == goog.dom.NodeType.ELEMENT &&
|
||||||
|
goog.array.indexOf(
|
||||||
|
ol.format.KML.NAMESPACE_URIS_, n.namespaceURI) != -1 &&
|
||||||
|
n.localName == 'name') {
|
||||||
|
return ol.format.KML.readString_(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) {
|
||||||
|
if (n.nodeType == goog.dom.NodeType.ELEMENT &&
|
||||||
|
goog.array.indexOf(
|
||||||
|
ol.format.KML.NAMESPACE_URIS_, n.namespaceURI) != -1 &&
|
||||||
|
(n.localName == 'Document' ||
|
||||||
|
n.localName == 'Folder' ||
|
||||||
|
n.localName == 'Placemark' ||
|
||||||
|
n.localName == 'kml')) {
|
||||||
|
var name = this.readNameFromNode(n);
|
||||||
|
if (goog.isDef(name)) {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1292,6 +1292,65 @@ describe('ol.format.KML', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#readName', function() {
|
||||||
|
|
||||||
|
it('returns undefined if there is no name', function() {
|
||||||
|
var kml =
|
||||||
|
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
|
||||||
|
' <Document>' +
|
||||||
|
' <Folder>' +
|
||||||
|
' <Placemark/>' +
|
||||||
|
' </Folder>' +
|
||||||
|
' <Document>' +
|
||||||
|
'</kml>';
|
||||||
|
expect(format.readName(kml)).to.be(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the name of the first Document', function() {
|
||||||
|
var kml =
|
||||||
|
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
|
||||||
|
' <Document>' +
|
||||||
|
' <name>Document name</name>' +
|
||||||
|
' </Document>' +
|
||||||
|
'</kml>';
|
||||||
|
expect(format.readName(kml)).to.be('Document name');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the name of the first Folder', function() {
|
||||||
|
var kml =
|
||||||
|
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
|
||||||
|
' <Folder>' +
|
||||||
|
' <name>Folder name</name>' +
|
||||||
|
' </Folder>' +
|
||||||
|
'</kml>';
|
||||||
|
expect(format.readName(kml)).to.be('Folder name');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the name of the first Placemark', function() {
|
||||||
|
var kml =
|
||||||
|
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
|
||||||
|
' <Placemark>' +
|
||||||
|
' <name>Placemark name</name>' +
|
||||||
|
' </Placemark>' +
|
||||||
|
'</kml>';
|
||||||
|
expect(format.readName(kml)).to.be('Placemark name');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('searches breadth-first', function() {
|
||||||
|
var kml =
|
||||||
|
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
|
||||||
|
' <Document>' +
|
||||||
|
' <Placemark>' +
|
||||||
|
' <name>Placemark name</name>' +
|
||||||
|
' </Placemark>' +
|
||||||
|
' <name>Document name</name>' +
|
||||||
|
' </Document>' +
|
||||||
|
'</kml>';
|
||||||
|
expect(format.readName(kml)).to.be('Document name');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user