Serialize polygon as surface

This commit is contained in:
Bart van den Eijnden
2014-02-27 20:15:32 +01:00
parent cbb38c10e0
commit a6d6317d9e
3 changed files with 84 additions and 5 deletions

View File

@@ -309,6 +309,9 @@
* @property {string} featureNS Feature namespace.
* @property {string} featureType Feature type to parse.
* @property {string} srsName srsName to use when writing geometries.
* @property {boolean|undefined} surface Write gml:Surface instead of
* gml:Polygon elements. This also affects the elements in multi-part
* geometries. Default is `false´.
*/
/**

View File

@@ -52,6 +52,13 @@ ol.format.GML = function(opt_options) {
*/
this.srsName_ = options.srsName;
/**
* @private
* @type {boolean}
*/
this.surface_ = goog.isDef(options.surface) ?
options.surface : false;
goog.base(this);
};
goog.inherits(ol.format.GML, ol.format.XMLFeature);
@@ -1165,6 +1172,26 @@ ol.format.GML.writePolygon_ = function(node, geometry, objectStack) {
};
/**
* @param {Node} node Node.
* @param {ol.geom.Polygon} geometry Polygon geometry.
* @param {Array.<*>} objectStack Node stack.
* @private
*/
ol.format.GML.writeSurface_ = function(node, geometry, objectStack) {
var context = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isObject(context));
var srsName = goog.object.get(context, 'srsName');
if (goog.isDefAndNotNull(srsName)) {
node.setAttribute('srsName', srsName);
}
ol.xml.pushSerializeAndPop({node: node, srsName: srsName},
ol.format.GML.PATCH_SERIALIZERS_,
ol.xml.makeSimpleNodeFactory('patches'), [geometry],
objectStack);
};
/**
* @param {Node} node Node.
* @param {ol.geom.MultiPolygon} geometry MultiPolygon geometry.
@@ -1224,6 +1251,23 @@ ol.format.GML.writeSurfaceMember_ = function(node, polygon, objectStack) {
};
/**
* @param {Node} node Node.
* @param {ol.geom.Polygon} polygon Polygon geometry.
* @param {Array.<*>} objectStack Node stack.
* @private
*/
ol.format.GML.writeSurfacePatches_ = function(node, polygon, objectStack) {
var context = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isObject(context));
var srsName = goog.object.get(context, 'srsName');
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
({node: node, srsName: srsName, writeSrsName: false}),
ol.format.GML.SURFACE_SERIALIZERS_,
ol.xml.makeSimpleNodeFactory('PolygonPatch'), [polygon], []);
};
/**
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
* @private
@@ -1235,6 +1279,17 @@ ol.format.GML.SURFACEMEMBER_SERIALIZERS_ = {
};
/**
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
* @private
*/
ol.format.GML.PATCH_SERIALIZERS_ = {
'http://www.opengis.net/gml': {
'patches': ol.xml.makeChildAppender(ol.format.GML.writeSurfacePatches_)
}
};
/**
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
* @private
@@ -1247,6 +1302,17 @@ ol.format.GML.RING_SERIALIZERS_ = {
};
/**
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
* @private
*/
ol.format.GML.SURFACE_SERIALIZERS_ = {
'http://www.opengis.net/gml': {
'PolygonPatch': ol.xml.makeChildAppender(ol.format.GML.writePolygon_)
}
};
/**
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
* @private
@@ -1257,6 +1323,7 @@ ol.format.GML.GEOMETRY_SERIALIZERS_ = {
'LineString': ol.xml.makeChildAppender(ol.format.GML.writeLineString_),
'LinearRing': ol.xml.makeChildAppender(ol.format.GML.writeLinearRing_),
'Polygon': ol.xml.makeChildAppender(ol.format.GML.writePolygon_),
'Surface': ol.xml.makeChildAppender(ol.format.GML.writeSurface_),
'MultiSurface': ol.xml.makeChildAppender(ol.format.GML.writeMultiSurface_)
}
};
@@ -1272,6 +1339,9 @@ ol.format.GML.GEOMETRY_SERIALIZERS_ = {
*/
ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack,
opt_nodeName) {
var context = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isObject(context));
var surface = goog.object.get(context, 'surface');
goog.asserts.assertInstanceof(value, ol.geom.Geometry);
var parentNode = objectStack[objectStack.length - 1].node;
goog.asserts.assert(ol.xml.isNode(parentNode));
@@ -1279,6 +1349,9 @@ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack,
if (nodeName === 'MultiPolygon') {
nodeName = 'MultiSurface';
}
if (nodeName === 'Polygon' && surface === true) {
nodeName = 'Surface';
}
return ol.xml.createElementNS(parentNode.namespaceURI,
nodeName);
};
@@ -1289,7 +1362,7 @@ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack,
*/
ol.format.GML.prototype.writeGeometryNode = function(geometry) {
var geom = ol.xml.createElementNS('http://www.opengis.net/gml', 'geom');
var context = {node: geom, srsName: this.srsName_};
var context = {node: geom, srsName: this.srsName_, surface: this.surface_};
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
(context), ol.format.GML.GEOMETRY_SERIALIZERS_,
ol.format.GML.GEOMETRY_NODE_FACTORY_, [geometry], []);

View File

@@ -179,12 +179,12 @@ describe('ol.format.GML', function() {
describe('surface', function() {
it('can read a surface geometry', function() {
it('can read and write a surface geometry', function() {
var text =
'<gml:Surface xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:patches>' +
' <gml:PolygonPatch interpolation="planar">' +
' <gml:PolygonPatch>' +
' <gml:exterior>' +
' <gml:LinearRing>' +
' <gml:posList>1 2 3 2 3 4 1 2</gml:posList>' +
@@ -208,6 +208,9 @@ describe('ol.format.GML', function() {
expect(g.getCoordinates()).to.eql([[[1, 2, 0], [3, 2, 0], [3, 4, 0],
[1, 2, 0]], [[2, 3, 0], [2, 5, 0], [4, 5, 0], [2, 3, 0]],
[[3, 4, 0], [3, 6, 0], [5, 6, 0], [3, 4, 0]]]);
format = new ol.format.GML({srsName: 'CRS:84', surface: true});
var serialized = format.writeGeometry(g);
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
});
@@ -573,7 +576,7 @@ describe('ol.format.GML', function() {
' <gml:surfaceMember>' +
' <gml:Surface>' +
' <gml:patches>' +
' <gml:PolygonPatch interpolation="planar">' +
' <gml:PolygonPatch>' +
' <gml:exterior>' +
' <gml:LinearRing>' +
' <gml:posList>1 2 3 2 3 4 1 2</gml:posList>' +
@@ -596,7 +599,7 @@ describe('ol.format.GML', function() {
' <gml:surfaceMember>' +
' <gml:Surface>' +
' <gml:patches>' +
' <gml:PolygonPatch interpolation="planar">' +
' <gml:PolygonPatch>' +
' <gml:exterior>' +
' <gml:LinearRing>' +
' <gml:posList>1 2 3 2 3 4 1 2</gml:posList>' +