Merge pull request #5212 from probins/kmlwrite

KML: fix GeometryCollection write
This commit is contained in:
Tim Schaub
2016-04-14 04:27:18 -06:00
2 changed files with 47 additions and 5 deletions

View File

@@ -2313,11 +2313,12 @@ ol.format.KML.writeLineStyle_ = function(node, style, objectStack) {
*/
ol.format.KML.writeMultiGeometry_ = function(node, geometry, objectStack) {
goog.asserts.assert(
(geometry instanceof ol.geom.GeometryCollection) ||
(geometry instanceof ol.geom.MultiPoint) ||
(geometry instanceof ol.geom.MultiLineString) ||
(geometry instanceof ol.geom.MultiPolygon),
'geometry should be one of: ol.geom.MultiPoint, ' +
'ol.geom.MultiLineString or ol.geom.MultiPolygon');
'geometry should be one of: ol.geom.GeometryCollection, ' +
'ol.geom.MultiPoint, ol.geom.MultiLineString or ol.geom.MultiPolygon');
/** @type {ol.xml.NodeStackItem} */
var context = {node: node};
var type = geometry.getType();
@@ -2325,7 +2326,10 @@ ol.format.KML.writeMultiGeometry_ = function(node, geometry, objectStack) {
var geometries;
/** @type {function(*, Array.<*>, string=): (Node|undefined)} */
var factory;
if (type == ol.geom.GeometryType.MULTI_POINT) {
if (type == ol.geom.GeometryType.GEOMETRY_COLLECTION) {
geometries = geometry.getGeometries();
factory = ol.format.KML.GEOMETRY_NODE_FACTORY_;
} else if (type == ol.geom.GeometryType.MULTI_POINT) {
geometries =
(/** @type {ol.geom.MultiPoint} */ (geometry)).getPoints();
factory = ol.format.KML.POINT_NODE_FACTORY_;
@@ -2582,7 +2586,8 @@ ol.format.KML.GEOMETRY_TYPE_TO_NODENAME_ = {
'Polygon': 'Polygon',
'MultiPoint': 'MultiGeometry',
'MultiLineString': 'MultiGeometry',
'MultiPolygon': 'MultiGeometry'
'MultiPolygon': 'MultiGeometry',
'GeometryCollection': 'MultiGeometry'
};
@@ -2711,7 +2716,9 @@ ol.format.KML.MULTI_GEOMETRY_SERIALIZERS_ = ol.xml.makeStructureNS(
ol.format.KML.writePrimitiveGeometry_),
'Point': ol.xml.makeChildAppender(
ol.format.KML.writePrimitiveGeometry_),
'Polygon': ol.xml.makeChildAppender(ol.format.KML.writePolygon_)
'Polygon': ol.xml.makeChildAppender(ol.format.KML.writePolygon_),
'GeometryCollection': ol.xml.makeChildAppender(
ol.format.KML.writeMultiGeometry_)
});

View File

@@ -1022,6 +1022,41 @@ describe('ol.format.KML', function() {
expect(gs[0]).to.be.an(ol.geom.GeometryCollection);
});
it('can write GeometryCollection geometries', function() {
var collection = new ol.geom.GeometryCollection([
new ol.geom.Point([1,2]),
new ol.geom.LineString([[1,2],[3,4]]),
new ol.geom.Polygon([[[1,2],[3,4],[3,2],[1,2]]])
]);
var features = [new ol.Feature(collection)];
var node = format.writeFeaturesNode(features);
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <MultiGeometry>' +
' <Point>' +
' <coordinates>1,2</coordinates>' +
' </Point>' +
' <LineString>' +
' <coordinates>1,2 3,4</coordinates>' +
' </LineString>' +
' <Polygon>' +
' <outerBoundaryIs>' +
' <LinearRing>' +
' <coordinates>1,2 3,4 3,2 1,2</coordinates>' +
' </LinearRing>' +
' </outerBoundaryIs>' +
' </Polygon>' +
' </MultiGeometry>' +
' </Placemark>' +
'</kml>';
expect(node).to.xmleql(ol.xml.parse(text));
});
it('can read gx:Track', function() {
var text =
'<kml xmlns="http://earth.google.com/kml/2.2"' +