Add write support for gml:Polygon
This commit is contained in:
@@ -1091,8 +1091,7 @@ ol.format.GML.writePoint_ = function(node, geometry, objectStack) {
|
|||||||
if (goog.isDefAndNotNull(srsName)) {
|
if (goog.isDefAndNotNull(srsName)) {
|
||||||
node.setAttribute('srsName', srsName);
|
node.setAttribute('srsName', srsName);
|
||||||
}
|
}
|
||||||
var context = {node: node, srsName: srsName};
|
ol.xml.pushSerializeAndPop({node: node, srsName: srsName},
|
||||||
ol.xml.pushSerializeAndPop(context,
|
|
||||||
ol.format.GML.FLAT_COORDINATES_SERIALIZERS_,
|
ol.format.GML.FLAT_COORDINATES_SERIALIZERS_,
|
||||||
ol.format.GML.POS_NODE_FACTORY_, [geometry], []);
|
ol.format.GML.POS_NODE_FACTORY_, [geometry], []);
|
||||||
};
|
};
|
||||||
@@ -1111,13 +1110,85 @@ ol.format.GML.writeLineString_ = function(node, geometry, objectStack) {
|
|||||||
if (goog.isDefAndNotNull(srsName)) {
|
if (goog.isDefAndNotNull(srsName)) {
|
||||||
node.setAttribute('srsName', srsName);
|
node.setAttribute('srsName', srsName);
|
||||||
}
|
}
|
||||||
var context = {node: node, srsName: srsName};
|
ol.xml.pushSerializeAndPop({node: node, srsName: srsName},
|
||||||
ol.xml.pushSerializeAndPop(context,
|
|
||||||
ol.format.GML.FLAT_COORDINATES_SERIALIZERS_,
|
ol.format.GML.FLAT_COORDINATES_SERIALIZERS_,
|
||||||
ol.format.GML.POSLIST_NODE_FACTORY_, [geometry], []);
|
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>>}
|
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
|
||||||
* @private
|
* @private
|
||||||
@@ -1125,7 +1196,9 @@ ol.format.GML.writeLineString_ = function(node, geometry, objectStack) {
|
|||||||
ol.format.GML.GEOMETRY_SERIALIZERS_ = {
|
ol.format.GML.GEOMETRY_SERIALIZERS_ = {
|
||||||
'http://www.opengis.net/gml': {
|
'http://www.opengis.net/gml': {
|
||||||
'Point': ol.xml.makeChildAppender(ol.format.GML.writePoint_),
|
'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_)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,10 @@ var readGeometry = function(format, text) {
|
|||||||
|
|
||||||
describe('ol.format.GML', function() {
|
describe('ol.format.GML', function() {
|
||||||
|
|
||||||
var format;
|
var format, formatWGS84;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
format = new ol.format.GML({srsName: 'CRS:84'});
|
format = new ol.format.GML({srsName: 'CRS:84'});
|
||||||
|
formatWGS84 = new ol.format.GML({srsName: 'urn:x-ogc:def:crs:EPSG:4326'});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#readGeometry', function() {
|
describe('#readGeometry', function() {
|
||||||
@@ -60,13 +61,10 @@ describe('ol.format.GML', function() {
|
|||||||
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
|
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
|
||||||
' <gml:posList>-90 -180 90 180</gml:posList>' +
|
' <gml:posList>-90 -180 90 180</gml:posList>' +
|
||||||
'</gml:LineString>';
|
'</gml:LineString>';
|
||||||
format = new ol.format.GML({
|
|
||||||
srsName: 'urn:x-ogc:def:crs:EPSG:4326'
|
|
||||||
});
|
|
||||||
var g = readGeometry(format, text);
|
var g = readGeometry(format, text);
|
||||||
expect(g).to.be.an(ol.geom.LineString);
|
expect(g).to.be.an(ol.geom.LineString);
|
||||||
expect(g.getCoordinates()).to.eql([[-180, -90, 0], [180, 90, 0]]);
|
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));
|
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">' +
|
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
|
||||||
' <gml:pos>-90 -180</gml:pos>' +
|
' <gml:pos>-90 -180</gml:pos>' +
|
||||||
'</gml:Point>';
|
'</gml:Point>';
|
||||||
format = new ol.format.GML({
|
|
||||||
srsName: 'urn:x-ogc:def:crs:EPSG:4326'
|
|
||||||
});
|
|
||||||
var g = readGeometry(format, text);
|
var g = readGeometry(format, text);
|
||||||
expect(g).to.be.an(ol.geom.Point);
|
expect(g).to.be.an(ol.geom.Point);
|
||||||
expect(g.getCoordinates()).to.eql([-180, -90, 0]);
|
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));
|
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can read multi surface geometry with right axis order', function() {
|
it('can read and write multi surface geometry with right axis order',
|
||||||
var text =
|
function() {
|
||||||
'<gml:MultiSurface xmlns:gml="http://www.opengis.net/gml" ' +
|
var text =
|
||||||
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
|
'<gml:MultiSurface xmlns:gml="http://www.opengis.net/gml" ' +
|
||||||
' <gml:surfaceMember>' +
|
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
|
||||||
' <gml:Polygon>' +
|
' <gml:surfaceMember>' +
|
||||||
' <gml:exterior>' +
|
' <gml:Polygon>' +
|
||||||
' <gml:LinearRing>' +
|
' <gml:exterior>' +
|
||||||
' <gml:posList>38.9661 -77.0081 38.9931 -77.0421 ' +
|
' <gml:LinearRing>' +
|
||||||
' 38.9321 -77.1221 38.9151 -77.0781 38.8861 -77.0671 ' +
|
' <gml:posList>38.9661 -77.0081 38.9931 -77.0421 ' +
|
||||||
' 38.8621 -77.0391 38.8381 -77.0401 38.8291 -77.0451 ' +
|
' 38.9321 -77.1221 38.9151 -77.0781 38.8861 ' +
|
||||||
' 38.8131 -77.0351 38.7881 -77.0451' +
|
' -77.0671 38.8621 -77.0391 38.8381 -77.0401 ' +
|
||||||
' 38.8891 -76.9111 38.9661 -77.0081</gml:posList>' +
|
' 38.8291 -77.0451 38.8131 -77.0351 38.7881 ' +
|
||||||
' </gml:LinearRing>' +
|
' -77.0451 38.8891 -76.9111 38.9661 -77.0081' +
|
||||||
' </gml:exterior>' +
|
' </gml:posList>' +
|
||||||
' </gml:Polygon>' +
|
' </gml:LinearRing>' +
|
||||||
' </gml:surfaceMember>' +
|
' </gml:exterior>' +
|
||||||
'</gml:MultiSurface>';
|
' </gml:Polygon>' +
|
||||||
var g = readGeometry(format, text);
|
' </gml:surfaceMember>' +
|
||||||
expect(g.getCoordinates()[0][0][0][0]).to.equal(-77.0081);
|
'</gml:MultiSurface>';
|
||||||
expect(g.getCoordinates()[0][0][0][1]).to.equal(38.9661);
|
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() {
|
describe('linearring', function() {
|
||||||
|
|
||||||
it('can read a linearring geometry', function() {
|
it('can read and write a linearring geometry', function() {
|
||||||
var text =
|
var text =
|
||||||
'<gml:LinearRing xmlns:gml="http://www.opengis.net/gml" ' +
|
'<gml:LinearRing xmlns:gml="http://www.opengis.net/gml" ' +
|
||||||
' srsName="CRS:84">' +
|
' srsName="CRS:84">' +
|
||||||
@@ -139,13 +136,15 @@ describe('ol.format.GML', 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(
|
||||||
[[[1, 2, 0], [3, 4, 0], [5, 6, 0], [1, 2, 0]]]);
|
[[[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() {
|
describe('polygon', function() {
|
||||||
|
|
||||||
it('can read a polygon geometry', function() {
|
it('can read and write a polygon geometry', function() {
|
||||||
var text =
|
var text =
|
||||||
'<gml:Polygon xmlns:gml="http://www.opengis.net/gml" ' +
|
'<gml:Polygon xmlns:gml="http://www.opengis.net/gml" ' +
|
||||||
' srsName="CRS:84">' +
|
' 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],
|
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]],
|
[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]]]);
|
[[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));
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user