Add write support for gml:Polygon

This commit is contained in:
Bart van den Eijnden
2014-02-26 20:45:52 +01:00
parent 5240929a53
commit 476bd901f0
2 changed files with 112 additions and 38 deletions

View File

@@ -1091,8 +1091,7 @@ ol.format.GML.writePoint_ = function(node, geometry, objectStack) {
if (goog.isDefAndNotNull(srsName)) {
node.setAttribute('srsName', srsName);
}
var context = {node: node, srsName: srsName};
ol.xml.pushSerializeAndPop(context,
ol.xml.pushSerializeAndPop({node: node, srsName: srsName},
ol.format.GML.FLAT_COORDINATES_SERIALIZERS_,
ol.format.GML.POS_NODE_FACTORY_, [geometry], []);
};
@@ -1111,13 +1110,85 @@ ol.format.GML.writeLineString_ = function(node, geometry, objectStack) {
if (goog.isDefAndNotNull(srsName)) {
node.setAttribute('srsName', srsName);
}
var context = {node: node, srsName: srsName};
ol.xml.pushSerializeAndPop(context,
ol.xml.pushSerializeAndPop({node: node, srsName: srsName},
ol.format.GML.FLAT_COORDINATES_SERIALIZERS_,
ol.format.GML.POSLIST_NODE_FACTORY_, [geometry], []);
};
/**
* @param {Node} node Node.
* @param {ol.geom.LinearRing} geometry LinearRing geometry.
* @param {Array.<*>} objectStack Node stack.
* @private
*/
ol.format.GML.writeLinearRing_ = 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.FLAT_COORDINATES_SERIALIZERS_,
ol.format.GML.POSLIST_NODE_FACTORY_, [geometry], []);
};
/**
* @param {Node} node Node.
* @param {ol.geom.Polygon} geometry Polygon geometry.
* @param {Array.<*>} objectStack Node stack.
* @private
*/
ol.format.GML.writePolygon_ = 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);
}
var rings = geometry.getLinearRings();
for (var i = 0, ii = rings.length; i < ii; ++i) {
if (i === 0) {
ol.xml.pushSerializeAndPop({node: node, srsName: srsName},
ol.format.GML.RING_SERIALIZERS_,
ol.xml.makeSimpleNodeFactory('exterior'), [rings[i]], objectStack);
} else {
ol.xml.pushSerializeAndPop({node: node, srsName: srsName},
ol.format.GML.RING_SERIALIZERS_,
ol.xml.makeSimpleNodeFactory('interior'), [rings[i]], objectStack);
}
}
};
/**
* @param {Node} node Node.
* @param {ol.geom.LinearRing} ring LinearRing geometry.
* @param {Array.<*>} objectStack Node stack.
* @private
*/
ol.format.GML.writeRing_ = function(node, ring, objectStack) {
var context = {node: node};
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
(context), ol.format.GML.GEOMETRY_SERIALIZERS_,
ol.format.GML.GEOMETRY_NODE_FACTORY_, [ring], []);
};
/**
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
* @private
*/
ol.format.GML.RING_SERIALIZERS_ = {
'http://www.opengis.net/gml': {
'exterior': ol.xml.makeChildAppender(ol.format.GML.writeRing_),
'interior': ol.xml.makeChildAppender(ol.format.GML.writeRing_)
}
};
/**
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
* @private
@@ -1125,7 +1196,9 @@ ol.format.GML.writeLineString_ = function(node, geometry, objectStack) {
ol.format.GML.GEOMETRY_SERIALIZERS_ = {
'http://www.opengis.net/gml': {
'Point': ol.xml.makeChildAppender(ol.format.GML.writePoint_),
'LineString': ol.xml.makeChildAppender(ol.format.GML.writeLineString_)
'LineString': ol.xml.makeChildAppender(ol.format.GML.writeLineString_),
'LinearRing': ol.xml.makeChildAppender(ol.format.GML.writeLinearRing_),
'Polygon': ol.xml.makeChildAppender(ol.format.GML.writePolygon_)
}
};

View File

@@ -10,9 +10,10 @@ var readGeometry = function(format, text) {
describe('ol.format.GML', function() {
var format;
var format, formatWGS84;
beforeEach(function() {
format = new ol.format.GML({srsName: 'CRS:84'});
formatWGS84 = new ol.format.GML({srsName: 'urn:x-ogc:def:crs:EPSG:4326'});
});
describe('#readGeometry', function() {
@@ -60,13 +61,10 @@ describe('ol.format.GML', function() {
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
' <gml:posList>-90 -180 90 180</gml:posList>' +
'</gml:LineString>';
format = new ol.format.GML({
srsName: 'urn:x-ogc:def:crs:EPSG:4326'
});
var g = readGeometry(format, text);
expect(g).to.be.an(ol.geom.LineString);
expect(g.getCoordinates()).to.eql([[-180, -90, 0], [180, 90, 0]]);
var serialized = format.writeGeometry(g);
var serialized = formatWGS84.writeGeometry(g);
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
@@ -77,38 +75,37 @@ describe('ol.format.GML', function() {
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
' <gml:pos>-90 -180</gml:pos>' +
'</gml:Point>';
format = new ol.format.GML({
srsName: 'urn:x-ogc:def:crs:EPSG:4326'
});
var g = readGeometry(format, text);
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([-180, -90, 0]);
var serialized = format.writeGeometry(g);
var serialized = formatWGS84.writeGeometry(g);
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
it('can read multi surface geometry with right axis order', function() {
var text =
'<gml:MultiSurface xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
' <gml:surfaceMember>' +
' <gml:Polygon>' +
' <gml:exterior>' +
' <gml:LinearRing>' +
' <gml:posList>38.9661 -77.0081 38.9931 -77.0421 ' +
' 38.9321 -77.1221 38.9151 -77.0781 38.8861 -77.0671 ' +
' 38.8621 -77.0391 38.8381 -77.0401 38.8291 -77.0451 ' +
' 38.8131 -77.0351 38.7881 -77.0451' +
' 38.8891 -76.9111 38.9661 -77.0081</gml:posList>' +
' </gml:LinearRing>' +
' </gml:exterior>' +
' </gml:Polygon>' +
' </gml:surfaceMember>' +
'</gml:MultiSurface>';
var g = readGeometry(format, text);
expect(g.getCoordinates()[0][0][0][0]).to.equal(-77.0081);
expect(g.getCoordinates()[0][0][0][1]).to.equal(38.9661);
});
it('can read and write multi surface geometry with right axis order',
function() {
var text =
'<gml:MultiSurface xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
' <gml:surfaceMember>' +
' <gml:Polygon>' +
' <gml:exterior>' +
' <gml:LinearRing>' +
' <gml:posList>38.9661 -77.0081 38.9931 -77.0421 ' +
' 38.9321 -77.1221 38.9151 -77.0781 38.8861 ' +
' -77.0671 38.8621 -77.0391 38.8381 -77.0401 ' +
' 38.8291 -77.0451 38.8131 -77.0351 38.7881 ' +
' -77.0451 38.8891 -76.9111 38.9661 -77.0081' +
' </gml:posList>' +
' </gml:LinearRing>' +
' </gml:exterior>' +
' </gml:Polygon>' +
' </gml:surfaceMember>' +
'</gml:MultiSurface>';
var g = readGeometry(format, text);
expect(g.getCoordinates()[0][0][0][0]).to.equal(-77.0081);
expect(g.getCoordinates()[0][0][0][1]).to.equal(38.9661);
});
});
@@ -129,7 +126,7 @@ describe('ol.format.GML', function() {
describe('linearring', function() {
it('can read a linearring geometry', function() {
it('can read and write a linearring geometry', function() {
var text =
'<gml:LinearRing xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
@@ -139,13 +136,15 @@ describe('ol.format.GML', function() {
expect(g).to.be.an(ol.geom.Polygon);
expect(g.getCoordinates()).to.eql(
[[[1, 2, 0], [3, 4, 0], [5, 6, 0], [1, 2, 0]]]);
var serialized = format.writeGeometry(g.getLinearRings()[0]);
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
});
describe('polygon', function() {
it('can read a polygon geometry', function() {
it('can read and write a polygon geometry', function() {
var text =
'<gml:Polygon xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
@@ -170,6 +169,8 @@ 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]]]);
var serialized = format.writeGeometry(g);
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
});