diff --git a/src/ol/format/gpxformat.js b/src/ol/format/gpxformat.js index d3a7dc9413..2c4d6d80c1 100644 --- a/src/ol/format/gpxformat.js +++ b/src/ol/format/gpxformat.js @@ -864,17 +864,13 @@ ol.format.GPX.prototype.writeFeatures; ol.format.GPX.prototype.writeFeaturesNode = function(features, opt_options) { //FIXME Serialize metadata var gpx = ol.xml.createElementNS('http://www.topografix.com/GPX/1/1', 'gpx'); - if (goog.isDef(opt_options)) { - if (!goog.isDef(opt_options.dataProjection)) { - // for convenience set a default dataProjection - opt_options = { - featureProjection: opt_options.featureProjection, - dataProjection: this.readProjectionFromDocument(null) - }; - } - } + + // for convenience set a default dataProjection + opt_options = ol.format.XMLFeature.setDefaultDataProjection( + opt_options, this.readProjectionFromDocument(null)); features = ol.format.XMLFeature.transformFeaturesWithOptions( features, true, opt_options); + ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ ({node: gpx}), ol.format.GPX.GPX_SERIALIZERS_, ol.format.GPX.GPX_NODE_FACTORY_, features, []); diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 20f0fef1c6..83333f9e31 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -1487,6 +1487,7 @@ ol.format.KML.prototype.readSharedStyleMap_ = function(node, objectStack) { * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.Feature} Feature. * @api */ @@ -1496,7 +1497,7 @@ ol.format.KML.prototype.readFeature; /** * @inheritDoc */ -ol.format.KML.prototype.readFeatureFromNode = function(node) { +ol.format.KML.prototype.readFeatureFromNode = function(node, opt_options) { goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); if (!goog.array.contains(ol.format.KML.NAMESPACE_URIS_, node.namespaceURI)) { return null; @@ -1504,6 +1505,8 @@ ol.format.KML.prototype.readFeatureFromNode = function(node) { goog.asserts.assert(node.localName == 'Placemark'); var feature = this.readPlacemark_(node, []); if (goog.isDef(feature)) { + ol.format.XMLFeature.transformFeaturesWithOptions( + [feature], false, this.getReadOptions(node, opt_options)); return feature; } else { return null; @@ -1516,6 +1519,7 @@ ol.format.KML.prototype.readFeatureFromNode = function(node) { * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {Array.} Features. * @api */ @@ -1525,7 +1529,7 @@ ol.format.KML.prototype.readFeatures; /** * @inheritDoc */ -ol.format.KML.prototype.readFeaturesFromNode = function(node) { +ol.format.KML.prototype.readFeaturesFromNode = function(node, opt_options) { goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); if (!goog.array.contains(ol.format.KML.NAMESPACE_URIS_, node.namespaceURI)) { return []; @@ -1535,6 +1539,8 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) { if (localName == 'Document' || localName == 'Folder') { features = this.readDocumentOrFolder_(node, []); if (goog.isDef(features)) { + ol.format.XMLFeature.transformFeaturesWithOptions( + features, false, this.getReadOptions(node, opt_options)); return features; } else { return []; @@ -1542,6 +1548,8 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) { } else if (localName == 'Placemark') { var feature = this.readPlacemark_(node, []); if (goog.isDef(feature)) { + ol.format.XMLFeature.transformFeaturesWithOptions( + [feature], false, this.getReadOptions(node, opt_options)); return [feature]; } else { return []; @@ -1551,7 +1559,7 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) { var n; for (n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) { - var fs = this.readFeaturesFromNode(n); + var fs = this.readFeaturesFromNode(n, opt_options); if (goog.isDef(fs)) { goog.array.extend(features, fs); } @@ -2494,6 +2502,7 @@ ol.format.KML.OUTER_BOUNDARY_NODE_FACTORY_ = * * @function * @param {Array.} features Features. + * @param {olx.format.WriteOptions=} opt_options Options. * @return {Node} Result. * @api */ @@ -2503,7 +2512,7 @@ ol.format.KML.prototype.writeFeatures; /** * @inheritDoc */ -ol.format.KML.prototype.writeFeaturesNode = function(features) { +ol.format.KML.prototype.writeFeaturesNode = function(features, opt_options) { var kml = ol.xml.createElementNS(ol.format.KML.NAMESPACE_URIS_[4], 'kml'); var xmlnsUri = 'http://www.w3.org/2000/xmlns/'; var xmlSchemaInstanceUri = 'http://www.w3.org/2001/XMLSchema-instance'; @@ -2513,6 +2522,12 @@ ol.format.KML.prototype.writeFeaturesNode = function(features) { ol.xml.setAttributeNS(kml, xmlSchemaInstanceUri, 'xsi:schemaLocation', ol.format.KML.SCHEMA_LOCATION_); + // for convenience set a default dataProjection + opt_options = ol.format.XMLFeature.setDefaultDataProjection( + opt_options, this.readProjectionFromDocument(null)); + features = ol.format.XMLFeature.transformFeaturesWithOptions( + features, true, opt_options); + var /** @type {ol.xml.NodeStackItem} */ context = {node: kml}; var properties = {}; if (features.length > 1) { diff --git a/src/ol/format/xmlfeatureformat.js b/src/ol/format/xmlfeatureformat.js index dc185b74df..79ba70b62d 100644 --- a/src/ol/format/xmlfeatureformat.js +++ b/src/ol/format/xmlfeatureformat.js @@ -277,3 +277,24 @@ ol.format.XMLFeature.transformFeaturesWithOptions = function( } return features; }; + + +/** + * @param {(olx.format.WriteOptions|olx.format.ReadOptions)=} opt_options + * Options. + * @param {ol.proj.ProjectionLike} defaultDataProjection Default projection. + * @protected + * @return {(olx.format.WriteOptions|olx.format.ReadOptions)=} Updated options. + */ +ol.format.XMLFeature.setDefaultDataProjection = function( + opt_options, defaultDataProjection) { + if (goog.isDef(opt_options)) { + if (!goog.isDef(opt_options.dataProjection)) { + opt_options = { + featureProjection: opt_options.featureProjection, + dataProjection: defaultDataProjection + }; + } + } + return opt_options; +}; diff --git a/test/spec/ol/format/gpxformat.test.js b/test/spec/ol/format/gpxformat.test.js index 0897b38c1d..d26b101553 100644 --- a/test/spec/ol/format/gpxformat.test.js +++ b/test/spec/ol/format/gpxformat.test.js @@ -1,7 +1,5 @@ goog.provide('ol.test.format.GPX'); -goog.require('ol.proj'); - describe('ol.format.GPX', function() { var format; @@ -559,4 +557,5 @@ goog.require('ol.format.GPX'); goog.require('ol.geom.LineString'); goog.require('ol.geom.MultiLineString'); goog.require('ol.geom.Point'); +goog.require('ol.proj'); goog.require('ol.xml'); diff --git a/test/spec/ol/format/kmlformat.test.js b/test/spec/ol/format/kmlformat.test.js index b3add3f507..6f49b449a4 100644 --- a/test/spec/ol/format/kmlformat.test.js +++ b/test/spec/ol/format/kmlformat.test.js @@ -119,6 +119,76 @@ describe('ol.format.KML', function() { expect(g.getCoordinates()).to.eql([1, 2, 3]); }); + it('can transform and read Point geometries', function() { + var text = + '' + + ' ' + + ' ' + + ' 1,2,3' + + ' ' + + ' ' + + ''; + var fs = format.readFeatures(text, { + featureProjection: 'EPSG:3857' + }); + expect(fs).to.have.length(1); + var f = fs[0]; + expect(f).to.be.an(ol.Feature); + var g = f.getGeometry(); + expect(g).to.be.an(ol.geom.Point); + var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'); + expectedPoint.push(3); + expect(g.getCoordinates()).to.eql(expectedPoint); + }); + + it('can read a single Point geometry', function() { + var text = + '' + + ' ' + + ' ' + + ' 1,2,3' + + ' ' + + ' ' + + ''; + var f = format.readFeature(text); + expect(f).to.be.an(ol.Feature); + var g = f.getGeometry(); + expect(g).to.be.an(ol.geom.Point); + expect(g.getCoordinates()).to.eql([1, 2, 3]); + }); + + it('can transform and read a single Point geometry', function() { + var text = + '' + + ' ' + + ' ' + + ' 1,2,3' + + ' ' + + ' ' + + ''; + var f = format.readFeature(text, { + featureProjection: 'EPSG:3857' + }); + expect(f).to.be.an(ol.Feature); + var g = f.getGeometry(); + expect(g).to.be.an(ol.geom.Point); + var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'); + expectedPoint.push(3); + expect(g.getCoordinates()).to.eql(expectedPoint); + }); + it('can write XY Point geometries', function() { var layout = ol.geom.GeometryLayout.XY; var point = new ol.geom.Point([1, 2], layout); @@ -159,6 +229,29 @@ describe('ol.format.KML', function() { expect(node).to.xmleql(ol.xml.load(text)); }); + it('can transform and write XYZ Point geometries', function() { + var layout = ol.geom.GeometryLayout.XYZ; + var point = new ol.geom.Point([1, 2, 3], layout).transform( + 'EPSG:4326', 'EPSG:3857'); + var features = [new ol.Feature(point)]; + var node = format.writeFeatures(features, { + featureProjection: 'EPSG:3857' + }); + var text = + '' + + ' ' + + ' ' + + ' 1,2,3' + + ' ' + + ' ' + + ''; + expect(node).to.xmleql(ol.xml.load(text)); + }); + it('can write XYM Point geometries', function() { var layout = ol.geom.GeometryLayout.XYM; var point = new ol.geom.Point([1, 2, 100], layout); @@ -1890,6 +1983,28 @@ describe('ol.format.KML', function() { expect(fs[0]).to.be.an(ol.Feature); }); + it('can transform and read a single feature from a Document', function() { + var text = + '' + + ' ' + + ' ' + + ' 1,2,3' + + ' ' + + ' ' + + ''; + var fs = format.readFeatures(text, { + featureProjection: 'EPSG:3857' + }); + expect(fs).to.have.length(1); + var f = fs[0]; + expect(f).to.be.an(ol.Feature); + var g = f.getGeometry(); + expect(g).to.be.an(ol.geom.Point); + var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'); + expectedPoint.push(3); + expect(g.getCoordinates()).to.eql(expectedPoint); + }); + it('can read a multiple features from a Document', function() { var text = '' + @@ -2317,6 +2432,7 @@ goog.require('ol.geom.Polygon'); goog.require('ol.style.Fill'); goog.require('ol.style.Icon'); goog.require('ol.style.IconOrigin'); +goog.require('ol.proj'); goog.require('ol.style.Stroke'); goog.require('ol.style.Style'); goog.require('ol.style.Text'); diff --git a/test/spec/ol/format/topojson.test.js b/test/spec/ol/format/topojson.test.js index 8ef332cdb8..47f0ac7acd 100644 --- a/test/spec/ol/format/topojson.test.js +++ b/test/spec/ol/format/topojson.test.js @@ -1,7 +1,5 @@ goog.provide('ol.test.format.TopoJSON'); -goog.require('ol.proj'); - var aruba = { type: 'Topology', transform: { @@ -160,4 +158,5 @@ goog.require('ol.Feature'); goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.Polygon'); goog.require('ol.format.Feature'); +goog.require('ol.proj'); goog.require('ol.format.TopoJSON');