Parse KML extrude and altitudeMode of simple geometries

Point, LineString and Polygon are handled.
LinearRing properties are not handled.

An 'extrude' boolean property is set in the geometry properties.
An 'altitudeMode' string property is set in the geometry properties.
This commit is contained in:
Guillaume Beraudo
2015-03-30 20:21:01 +02:00
parent ea8ee16f49
commit 5bd63f7e2c
2 changed files with 40 additions and 0 deletions

View File

@@ -802,11 +802,15 @@ ol.format.KML.readLineString_ = function(node, objectStack) {
'node.nodeType should be ELEMENT'); 'node.nodeType should be ELEMENT');
goog.asserts.assert(node.localName == 'LineString', goog.asserts.assert(node.localName == 'LineString',
'localName should be LineString'); 'localName should be LineString');
var properties = ol.xml.pushParseAndPop(/** @type {Object<string,*>} */ ({}),
ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_, node,
objectStack);
var flatCoordinates = var flatCoordinates =
ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack); ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack);
if (goog.isDef(flatCoordinates)) { if (goog.isDef(flatCoordinates)) {
var lineString = new ol.geom.LineString(null); var lineString = new ol.geom.LineString(null);
lineString.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates); lineString.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates);
lineString.setProperties(properties);
return lineString; return lineString;
} else { } else {
return undefined; return undefined;
@@ -825,12 +829,16 @@ ol.format.KML.readLinearRing_ = function(node, objectStack) {
'node.nodeType should be ELEMENT'); 'node.nodeType should be ELEMENT');
goog.asserts.assert(node.localName == 'LinearRing', goog.asserts.assert(node.localName == 'LinearRing',
'localName should be LinearRing'); 'localName should be LinearRing');
var properties = ol.xml.pushParseAndPop(/** @type {Object<string,*>} */ ({}),
ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_, node,
objectStack);
var flatCoordinates = var flatCoordinates =
ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack); ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack);
if (goog.isDef(flatCoordinates)) { if (goog.isDef(flatCoordinates)) {
var polygon = new ol.geom.Polygon(null); var polygon = new ol.geom.Polygon(null);
polygon.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates, polygon.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates,
[flatCoordinates.length]); [flatCoordinates.length]);
polygon.setProperties(properties);
return polygon; return polygon;
} else { } else {
return undefined; return undefined;
@@ -920,6 +928,9 @@ ol.format.KML.readPoint_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT, goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT,
'node.nodeType should be ELEMENT'); 'node.nodeType should be ELEMENT');
goog.asserts.assert(node.localName == 'Point', 'localName should be Point'); goog.asserts.assert(node.localName == 'Point', 'localName should be Point');
var properties = ol.xml.pushParseAndPop(/** @type {Object<string,*>} */ ({}),
ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_, node,
objectStack);
var flatCoordinates = var flatCoordinates =
ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack); ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack);
if (goog.isDefAndNotNull(flatCoordinates)) { if (goog.isDefAndNotNull(flatCoordinates)) {
@@ -927,6 +938,7 @@ ol.format.KML.readPoint_ = function(node, objectStack) {
goog.asserts.assert(flatCoordinates.length == 3, goog.asserts.assert(flatCoordinates.length == 3,
'flatCoordinates should have a length of 3'); 'flatCoordinates should have a length of 3');
point.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates); point.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates);
point.setProperties(properties);
return point; return point;
} else { } else {
return undefined; return undefined;
@@ -945,6 +957,9 @@ ol.format.KML.readPolygon_ = function(node, objectStack) {
'node.nodeType should be ELEMENT'); 'node.nodeType should be ELEMENT');
goog.asserts.assert(node.localName == 'Polygon', goog.asserts.assert(node.localName == 'Polygon',
'localName should be Polygon'); 'localName should be Polygon');
var properties = ol.xml.pushParseAndPop(/** @type {Object<string,*>} */ ({}),
ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_, node,
objectStack);
var flatLinearRings = ol.xml.pushParseAndPop( var flatLinearRings = ol.xml.pushParseAndPop(
/** @type {Array.<Array.<number>>} */ ([null]), /** @type {Array.<Array.<number>>} */ ([null]),
ol.format.KML.FLAT_LINEAR_RINGS_PARSERS_, node, objectStack); ol.format.KML.FLAT_LINEAR_RINGS_PARSERS_, node, objectStack);
@@ -960,6 +975,7 @@ ol.format.KML.readPolygon_ = function(node, objectStack) {
} }
polygon.setFlatCoordinates( polygon.setFlatCoordinates(
ol.geom.GeometryLayout.XYZ, flatCoordinates, ends); ol.geom.GeometryLayout.XYZ, flatCoordinates, ends);
polygon.setProperties(properties);
return polygon; return polygon;
} else { } else {
return undefined; return undefined;
@@ -1266,6 +1282,18 @@ ol.format.KML.EXTENDED_DATA_PARSERS_ = ol.xml.makeParsersNS(
}); });
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_ = ol.xml.makeParsersNS(
ol.format.KML.NAMESPACE_URIS_, {
'extrude': ol.xml.makeObjectPropertySetter(ol.format.XSD.readBoolean),
'altitudeMode': ol.xml.makeObjectPropertySetter(ol.format.XSD.readString)
});
/** /**
* @const * @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>} * @type {Object.<string, Object.<string, ol.xml.Parser>>}

View File

@@ -133,6 +133,8 @@ describe('ol.format.KML', function() {
' <Placemark>' + ' <Placemark>' +
' <Point>' + ' <Point>' +
' <coordinates>1,2,3</coordinates>' + ' <coordinates>1,2,3</coordinates>' +
' <extrude>0</extrude>' +
' <altitudeMode>absolute</altitudeMode>' +
' </Point>' + ' </Point>' +
' </Placemark>' + ' </Placemark>' +
'</kml>'; '</kml>';
@@ -143,6 +145,8 @@ describe('ol.format.KML', function() {
var g = f.getGeometry(); var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point); expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([1, 2, 3]); expect(g.getCoordinates()).to.eql([1, 2, 3]);
expect(g.get('extrude')).to.be(false);
expect(g.get('altitudeMode')).to.be('absolute');
}); });
it('can transform and read Point geometries', function() { it('can transform and read Point geometries', function() {
@@ -338,6 +342,8 @@ describe('ol.format.KML', function() {
' <Placemark>' + ' <Placemark>' +
' <LineString>' + ' <LineString>' +
' <coordinates>1,2,3 4,5,6</coordinates>' + ' <coordinates>1,2,3 4,5,6</coordinates>' +
' <extrude>0</extrude>' +
' <altitudeMode>absolute</altitudeMode>' +
' </LineString>' + ' </LineString>' +
' </Placemark>' + ' </Placemark>' +
'</kml>'; '</kml>';
@@ -348,6 +354,8 @@ describe('ol.format.KML', function() {
var g = f.getGeometry(); var g = f.getGeometry();
expect(g).to.be.an(ol.geom.LineString); expect(g).to.be.an(ol.geom.LineString);
expect(g.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]); expect(g.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]);
expect(g.get('extrude')).to.be(false);
expect(g.get('altitudeMode')).to.be('absolute');
}); });
it('can write XY LineString geometries', function() { it('can write XY LineString geometries', function() {
@@ -540,6 +548,8 @@ describe('ol.format.KML', function() {
'<kml xmlns="http://earth.google.com/kml/2.2">' + '<kml xmlns="http://earth.google.com/kml/2.2">' +
' <Placemark>' + ' <Placemark>' +
' <Polygon>' + ' <Polygon>' +
' <extrude>0</extrude>' +
' <altitudeMode>absolute</altitudeMode>' +
' <outerBoundaryIs>' + ' <outerBoundaryIs>' +
' <LinearRing>' + ' <LinearRing>' +
' <coordinates>0,0,1 0,5,1 5,5,2 5,0,3</coordinates>' + ' <coordinates>0,0,1 0,5,1 5,5,2 5,0,3</coordinates>' +
@@ -556,6 +566,8 @@ describe('ol.format.KML', function() {
expect(g).to.be.an(ol.geom.Polygon); expect(g).to.be.an(ol.geom.Polygon);
expect(g.getCoordinates()).to.eql( expect(g.getCoordinates()).to.eql(
[[[0, 0, 1], [0, 5, 1], [5, 5, 2], [5, 0, 3]]]); [[[0, 0, 1], [0, 5, 1], [5, 5, 2], [5, 0, 3]]]);
expect(g.get('extrude')).to.be(false);
expect(g.get('altitudeMode')).to.be('absolute');
}); });
it('can write XY Polygon geometries', function() { it('can write XY Polygon geometries', function() {