diff --git a/src/ol/format/featureformat.js b/src/ol/format/featureformat.js index d7176bcde0..ff674c19c7 100644 --- a/src/ol/format/featureformat.js +++ b/src/ol/format/featureformat.js @@ -146,3 +146,24 @@ ol.format.Feature.transformWithOptions = function( return geometry; } }; + + +/** + * @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.Feature.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/src/ol/format/gpxformat.js b/src/ol/format/gpxformat.js index 2c4d6d80c1..bcec5da2fb 100644 --- a/src/ol/format/gpxformat.js +++ b/src/ol/format/gpxformat.js @@ -5,6 +5,7 @@ goog.require('goog.asserts'); goog.require('goog.dom.NodeType'); goog.require('goog.object'); goog.require('ol.Feature'); +goog.require('ol.format.Feature'); goog.require('ol.format.XMLFeature'); goog.require('ol.format.XSD'); goog.require('ol.geom.LineString'); @@ -866,7 +867,7 @@ ol.format.GPX.prototype.writeFeaturesNode = function(features, opt_options) { var gpx = ol.xml.createElementNS('http://www.topografix.com/GPX/1/1', 'gpx'); // for convenience set a default dataProjection - opt_options = ol.format.XMLFeature.setDefaultDataProjection( + opt_options = ol.format.Feature.setDefaultDataProjection( opt_options, this.readProjectionFromDocument(null)); features = ol.format.XMLFeature.transformFeaturesWithOptions( features, true, opt_options); diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 83333f9e31..8dc1aaf0c8 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -16,6 +16,7 @@ goog.require('ol.Feature'); goog.require('ol.array'); goog.require('ol.color'); goog.require('ol.feature'); +goog.require('ol.format.Feature'); goog.require('ol.format.XMLFeature'); goog.require('ol.format.XSD'); goog.require('ol.geom.Geometry'); @@ -2523,7 +2524,7 @@ ol.format.KML.prototype.writeFeaturesNode = function(features, opt_options) { ol.format.KML.SCHEMA_LOCATION_); // for convenience set a default dataProjection - opt_options = ol.format.XMLFeature.setDefaultDataProjection( + opt_options = ol.format.Feature.setDefaultDataProjection( opt_options, this.readProjectionFromDocument(null)); features = ol.format.XMLFeature.transformFeaturesWithOptions( features, true, opt_options); diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index 0f08dc1950..750f1aa078 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -2,6 +2,7 @@ goog.provide('ol.format.Polyline'); goog.require('goog.asserts'); goog.require('ol.Feature'); +goog.require('ol.format.Feature'); goog.require('ol.format.TextFeature'); goog.require('ol.geom.LineString'); goog.require('ol.geom.flat.inflate'); @@ -248,6 +249,7 @@ ol.format.Polyline.encodeUnsignedInteger = function(num) { * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.Feature} Feature. * @api */ @@ -257,8 +259,8 @@ ol.format.Polyline.prototype.readFeature; /** * @inheritDoc */ -ol.format.Polyline.prototype.readFeatureFromText = function(text) { - var geometry = this.readGeometryFromText(text); +ol.format.Polyline.prototype.readFeatureFromText = function(text, opt_options) { + var geometry = this.readGeometryFromText(text, opt_options); return new ol.Feature(geometry); }; @@ -269,6 +271,7 @@ ol.format.Polyline.prototype.readFeatureFromText = function(text) { * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {Array.} Features. * @api */ @@ -278,8 +281,9 @@ ol.format.Polyline.prototype.readFeatures; /** * @inheritDoc */ -ol.format.Polyline.prototype.readFeaturesFromText = function(text) { - var feature = this.readFeatureFromText(text); +ol.format.Polyline.prototype.readFeaturesFromText = + function(text, opt_options) { + var feature = this.readFeatureFromText(text, opt_options); return [feature]; }; @@ -289,6 +293,7 @@ ol.format.Polyline.prototype.readFeaturesFromText = function(text) { * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.geom.Geometry} Geometry. * @api */ @@ -298,11 +303,18 @@ ol.format.Polyline.prototype.readGeometry; /** * @inheritDoc */ -ol.format.Polyline.prototype.readGeometryFromText = function(text) { +ol.format.Polyline.prototype.readGeometryFromText = + function(text, opt_options) { var flatCoordinates = ol.format.Polyline.decodeDeltas(text, 2, this.factor_); var coordinates = ol.geom.flat.inflate.coordinates( flatCoordinates, 0, flatCoordinates.length, 2); - return new ol.geom.LineString(coordinates); + + // for convenience set a default dataProjection + opt_options = ol.format.Feature.setDefaultDataProjection( + opt_options, this.readProjectionFromText(null)); + + return ol.format.Feature.transformWithOptions( + new ol.geom.LineString(coordinates), false, false, opt_options); }; @@ -328,10 +340,10 @@ ol.format.Polyline.prototype.readProjectionFromText = function(text) { /** * @inheritDoc */ -ol.format.Polyline.prototype.writeFeatureText = function(feature) { +ol.format.Polyline.prototype.writeFeatureText = function(feature, opt_options) { var geometry = feature.getGeometry(); if (goog.isDefAndNotNull(geometry)) { - return this.writeGeometryText(geometry); + return this.writeGeometryText(geometry, opt_options); } else { goog.asserts.fail(); return ''; @@ -342,9 +354,10 @@ ol.format.Polyline.prototype.writeFeatureText = function(feature) { /** * @inheritDoc */ -ol.format.Polyline.prototype.writeFeaturesText = function(features) { +ol.format.Polyline.prototype.writeFeaturesText = + function(features, opt_options) { goog.asserts.assert(features.length == 1); - return this.writeFeatureText(features[0]); + return this.writeFeatureText(features[0], opt_options); }; @@ -353,6 +366,7 @@ ol.format.Polyline.prototype.writeFeaturesText = function(features) { * * @function * @param {ol.geom.Geometry} geometry Geometry. + * @param {olx.format.WriteOptions=} opt_options Write options. * @return {string} Geometry. * @api */ @@ -362,8 +376,14 @@ ol.format.Polyline.prototype.writeGeometry; /** * @inheritDoc */ -ol.format.Polyline.prototype.writeGeometryText = function(geometry) { +ol.format.Polyline.prototype.writeGeometryText = + function(geometry, opt_options) { goog.asserts.assertInstanceof(geometry, ol.geom.LineString); + // for convenience set a default dataProjection + opt_options = ol.format.Feature.setDefaultDataProjection( + opt_options, this.readProjectionFromText(null)); + geometry = ol.format.Feature.transformWithOptions( + geometry, true, true, opt_options); var flatCoordinates = geometry.getFlatCoordinates(); var stride = geometry.getStride(); return ol.format.Polyline.encodeDeltas(flatCoordinates, stride, this.factor_); diff --git a/src/ol/format/xmlfeatureformat.js b/src/ol/format/xmlfeatureformat.js index 79ba70b62d..dc185b74df 100644 --- a/src/ol/format/xmlfeatureformat.js +++ b/src/ol/format/xmlfeatureformat.js @@ -277,24 +277,3 @@ 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/polylineformat.test.js b/test/spec/ol/format/polylineformat.test.js index ff38fb6a79..2c09ff8dfc 100644 --- a/test/spec/ol/format/polylineformat.test.js +++ b/test/spec/ol/format/polylineformat.test.js @@ -11,13 +11,17 @@ describe('ol.format.Polyline', function() { function resetTestingData() { format = new ol.format.Polyline(); - points = [[38.50000, -120.20000], - [40.70000, -120.95000], - [43.25200, -126.45300]]; - flatPoints = [38.50000, -120.20000, - 40.70000, -120.95000, - 43.25200, -126.45300]; - encodedFlatPoints = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'; + points = [[-120.20000, 38.50000], + [-120.95000, 40.70000], + [-126.45300, 43.25200]]; + flatPoints = [-120.20000, 38.50000, + -120.95000, 40.70000, + -126.45300, 43.25200]; + encodedFlatPoints = '~ps|U_p~iFnnqC_ulLvxq`@_mqN'; + points3857 = [ + ol.proj.transform([-120.20000, 38.50000], 'EPSG:4326', 'EPSG:3857'), + ol.proj.transform([-120.95000, 40.70000], 'EPSG:4326', 'EPSG:3857'), + ol.proj.transform([-126.45300, 43.25200], 'EPSG:4326', 'EPSG:3857')]; floats = [0.00, 0.15, -0.01, -0.16, 0.16, 0.01]; smallFloats = [0.00000, 0.00015, -0.00001, -0.00016, 0.00016, 0.00001]; @@ -253,6 +257,16 @@ describe('ol.format.Polyline', function() { expect(geometry.getFlatCoordinates()).to.eql(flatPoints); }); + it('transforms and returns the expected feature', function() { + var feature = format.readFeature(encodedFlatPoints, { + featureProjection: 'EPSG:3857' + }); + expect(feature).to.be.an(ol.Feature); + var geometry = feature.getGeometry(); + expect(geometry).to.be.an(ol.geom.LineString); + expect(geometry.getCoordinates()).to.eql(points3857); + }); + }); describe('#readFeatures', function() { @@ -268,6 +282,19 @@ describe('ol.format.Polyline', function() { expect(geometry.getFlatCoordinates()).to.eql(flatPoints); }); + it('transforms and returns the expected features', function() { + var features = format.readFeatures(encodedFlatPoints, { + featureProjection: 'EPSG:3857' + }); + expect(features).to.be.an(Array); + expect(features).to.have.length(1); + var feature = features[0]; + expect(feature).to.be.an(ol.Feature); + var geometry = feature.getGeometry(); + expect(geometry).to.be.an(ol.geom.LineString); + expect(geometry.getCoordinates()).to.eql(points3857); + }); + }); describe('#readGeometry', function() { @@ -278,6 +305,14 @@ describe('ol.format.Polyline', function() { expect(geometry.getFlatCoordinates()).to.eql(flatPoints); }); + it('transforms and returns the expected geometry', function() { + var geometry = format.readGeometry(encodedFlatPoints, { + featureProjection: 'EPSG:3857' + }); + expect(geometry).to.be.an(ol.geom.LineString); + expect(geometry.getCoordinates()).to.eql(points3857); + }); + }); describe('#readProjection', function() { @@ -296,6 +331,13 @@ describe('ol.format.Polyline', function() { expect(format.writeFeature(feature)).to.be(encodedFlatPoints); }); + it('transforms and returns the expected text', function() { + var feature = new ol.Feature(new ol.geom.LineString(points3857)); + expect(format.writeFeature(feature, { + featureProjection: 'EPSG:3857' + })).to.be(encodedFlatPoints); + }); + }); describe('#writeFeature', function() { @@ -305,6 +347,13 @@ describe('ol.format.Polyline', function() { expect(format.writeFeatures(features)).to.be(encodedFlatPoints); }); + it('transforms and returns the expected text', function() { + var features = [new ol.Feature(new ol.geom.LineString(points3857))]; + expect(format.writeFeatures(features, { + featureProjection: 'EPSG:3857' + })).to.be(encodedFlatPoints); + }); + }); describe('#writeGeometry', function() { @@ -314,6 +363,13 @@ describe('ol.format.Polyline', function() { expect(format.writeGeometry(geometry)).to.be(encodedFlatPoints); }); + it('transforms and returns the expected text', function() { + var geometry = new ol.geom.LineString(points3857); + expect(format.writeGeometry(geometry, { + featureProjection: 'EPSG:3857' + })).to.be(encodedFlatPoints); + }); + }); });