Skip to content

Commit 9b912da

Browse files
author
Tom Fili
authored
Merge pull request #5860 from nrivera-Novetta/master
Adds function that inserts missing namespace into KML
2 parents 404a1e9 + 83cd5f7 commit 9b912da

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Change Log
22
==========
33
### 1.39 - 2017-11-01
4+
5+
* Added function that inserts missing namespace declarations into KML files. [#5860](https://github.com/AnalyticalGraphicsInc/cesium/pull/5860)
46
* Added support for the layer.json `parentUrl` property in `CesiumTerrainProvider` to allow for compositing of tilesets.
57
* Fixed a bug that caused KML ground overlays to appear distorted when rotation was applied. [#5914](https://github.com/AnalyticalGraphicsInc/cesium/issues/5914)
68

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
9393
* [Logilab](https://www.logilab.fr/)
9494
* [Florent Cayré](https://github.com/fcayre/)
9595
* [Novetta](http://www.novetta.com/)
96+
* [Natanael Rivera](https://github.com/nrivera-Novetta/)
9697
* [Justin Burr](https://github.com/jburr-nc/)
9798

9899
## [Individual CLA](Documentation/Contributors/CLAs/individual-cla-agi-v1.0.txt)

Source/DataSources/KmlDataSource.js

+30
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,33 @@ define([
255255
return deferred.promise;
256256
}
257257

258+
function insertNamespaces(text) {
259+
var namespaceMap = {
260+
xsi : 'http://www.w3.org/2001/XMLSchema-instance'
261+
};
262+
var firstPart, lastPart, reg, declaration;
263+
264+
for (var key in namespaceMap) {
265+
if (namespaceMap.hasOwnProperty(key)) {
266+
reg = RegExp('[< ]' + key + ':');
267+
declaration = 'xmlns:' + key + '=';
268+
if (reg.test(text) && text.indexOf(declaration) === -1) {
269+
if (!defined(firstPart)) {
270+
firstPart = text.substr(0, text.indexOf('<kml') + 4);
271+
lastPart = text.substr(firstPart.length);
272+
}
273+
firstPart += ' ' + declaration + '"' + namespaceMap[key] + '"';
274+
}
275+
}
276+
}
277+
278+
if (defined(firstPart)) {
279+
text = firstPart + lastPart;
280+
}
281+
282+
return text;
283+
}
284+
258285
function loadXmlFromZip(reader, entry, uriResolver, deferred) {
259286
entry.getData(new zip.TextWriter(), function(text) {
260287
uriResolver.kml = parser.parseFromString(text, 'application/xml');
@@ -2324,6 +2351,9 @@ define([
23242351
//There's no official way to validate if a parse was successful.
23252352
//The following check detects the error on various browsers.
23262353

2354+
//Insert missing namespaces
2355+
text = insertNamespaces(text);
2356+
23272357
//IE raises an exception
23282358
var kml;
23292359
var error;
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<kml xmlns="http://www.opengis.net/kml/2.2">
3+
<Document xsi:schemaLocation="http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd">
4+
<Placemark>
5+
<Style>
6+
<IconStyle>
7+
<Icon>
8+
<href>image.png</href>
9+
</Icon>
10+
</IconStyle>
11+
</Style>
12+
<description><![CDATA[image.png <a href="./image.png">image.png</a><img src="image.png"/>]]></description>
13+
<Point>
14+
<coordinates>1,2,3</coordinates>
15+
</Point>
16+
</Placemark>
17+
</Document>
18+
</kml>

Specs/DataSources/KmlDataSourceSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ defineSuite([
244244
});
245245
});
246246

247+
it('load inserts missing namespace declaration', function() {
248+
var dataSource = new KmlDataSource(options);
249+
return dataSource.load('Data/KML/undeclaredNamespaces.kml').then(function(source) {
250+
expect(source).toBe(dataSource);
251+
expect(source.entities.values.length).toEqual(1);
252+
});
253+
});
254+
247255
it('load rejects nonexistent URL', function() {
248256
return KmlDataSource.load('test.invalid', options).otherwise(function(e) {
249257
expect(e).toBeInstanceOf(RequestErrorEvent);

0 commit comments

Comments
 (0)