diff --git a/src/ol/format/textfeatureformat.js b/src/ol/format/textfeatureformat.js index bdfa98cbbf..0b30b5dc4a 100644 --- a/src/ol/format/textfeatureformat.js +++ b/src/ol/format/textfeatureformat.js @@ -47,13 +47,14 @@ ol.format.TextFeature.prototype.getType = function() { /** * @inheritDoc */ -ol.format.TextFeature.prototype.readFeature = function(source) { - return this.readFeatureFromText(this.getText_(source)); +ol.format.TextFeature.prototype.readFeature = function(source, opt_options) { + return this.readFeatureFromText(this.getText_(source), opt_options); }; /** * @param {string} text Text. + * @param {olx.format.ReadOptions=} opt_options Read options. * @protected * @return {ol.Feature} Feature. */ @@ -63,13 +64,14 @@ ol.format.TextFeature.prototype.readFeatureFromText = goog.abstractMethod; /** * @inheritDoc */ -ol.format.TextFeature.prototype.readFeatures = function(source) { - return this.readFeaturesFromText(this.getText_(source)); +ol.format.TextFeature.prototype.readFeatures = function(source, opt_options) { + return this.readFeaturesFromText(this.getText_(source), opt_options); }; /** * @param {string} text Text. + * @param {olx.format.ReadOptions=} opt_options Read options. * @protected * @return {Array.} Features. */ @@ -79,13 +81,14 @@ ol.format.TextFeature.prototype.readFeaturesFromText = goog.abstractMethod; /** * @inheritDoc */ -ol.format.TextFeature.prototype.readGeometry = function(source) { - return this.readGeometryFromText(this.getText_(source)); +ol.format.TextFeature.prototype.readGeometry = function(source, opt_options) { + return this.readGeometryFromText(this.getText_(source), opt_options); }; /** * @param {string} text Text. + * @param {olx.format.ReadOptions=} opt_options Read options. * @protected * @return {ol.geom.Geometry} Geometry. */ @@ -111,13 +114,14 @@ ol.format.TextFeature.prototype.readProjectionFromText = goog.abstractMethod; /** * @inheritDoc */ -ol.format.TextFeature.prototype.writeFeature = function(feature) { - return this.writeFeatureText(feature); +ol.format.TextFeature.prototype.writeFeature = function(feature, opt_options) { + return this.writeFeatureText(feature, opt_options); }; /** * @param {ol.Feature} feature Features. + * @param {olx.format.WriteOptions=} opt_options Write options. * @protected * @return {string} Text. */ @@ -127,13 +131,15 @@ ol.format.TextFeature.prototype.writeFeatureText = goog.abstractMethod; /** * @inheritDoc */ -ol.format.TextFeature.prototype.writeFeatures = function(features) { - return this.writeFeaturesText(features); +ol.format.TextFeature.prototype.writeFeatures = function( + features, opt_options) { + return this.writeFeaturesText(features, opt_options); }; /** * @param {Array.} features Features. + * @param {olx.format.WriteOptions=} opt_options Write options. * @protected * @return {string} Text. */ @@ -143,13 +149,15 @@ ol.format.TextFeature.prototype.writeFeaturesText = goog.abstractMethod; /** * @inheritDoc */ -ol.format.TextFeature.prototype.writeGeometry = function(geometry) { - return this.writeGeometryText(geometry); +ol.format.TextFeature.prototype.writeGeometry = function( + geometry, opt_options) { + return this.writeGeometryText(geometry, opt_options); }; /** * @param {ol.geom.Geometry} geometry Geometry. + * @param {olx.format.WriteOptions=} opt_options Write options. * @protected * @return {string} Text. */ diff --git a/src/ol/format/wktformat.js b/src/ol/format/wktformat.js index e0ba9b918a..d0c1f0a930 100644 --- a/src/ol/format/wktformat.js +++ b/src/ol/format/wktformat.js @@ -3,6 +3,7 @@ goog.provide('ol.format.WKT'); goog.require('goog.array'); goog.require('goog.asserts'); goog.require('ol.Feature'); +goog.require('ol.format.Feature'); goog.require('ol.format.TextFeature'); goog.require('ol.geom.Geometry'); goog.require('ol.geom.GeometryCollection'); @@ -208,6 +209,7 @@ ol.format.WKT.prototype.parse_ = function(wkt) { * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.Feature} Feature. * @api */ @@ -217,8 +219,8 @@ ol.format.WKT.prototype.readFeature; /** * @inheritDoc */ -ol.format.WKT.prototype.readFeatureFromText = function(text) { - var geom = this.readGeometryFromText(text); +ol.format.WKT.prototype.readFeatureFromText = function(text, opt_options) { + var geom = this.readGeometryFromText(text, opt_options); if (goog.isDef(geom)) { var feature = new ol.Feature(); feature.setGeometry(geom); @@ -233,6 +235,7 @@ ol.format.WKT.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 */ @@ -242,9 +245,9 @@ ol.format.WKT.prototype.readFeatures; /** * @inheritDoc */ -ol.format.WKT.prototype.readFeaturesFromText = function(text) { +ol.format.WKT.prototype.readFeaturesFromText = function(text, opt_options) { var geometries = []; - var geometry = this.readGeometryFromText(text); + var geometry = this.readGeometryFromText(text, opt_options); if (this.splitCollection_ && geometry.getType() == ol.geom.GeometryType.GEOMETRY_COLLECTION) { geometries = (/** @type {ol.geom.GeometryCollection} */ (geometry)) @@ -267,6 +270,7 @@ ol.format.WKT.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 */ @@ -276,8 +280,14 @@ ol.format.WKT.prototype.readGeometry; /** * @inheritDoc */ -ol.format.WKT.prototype.readGeometryFromText = function(text) { - return this.parse_(text) || null; +ol.format.WKT.prototype.readGeometryFromText = function(text, opt_options) { + var geometry = this.parse_(text); + if (goog.isDef(geometry)) { + return ol.format.Feature.transformWithOptions( + geometry, false, false, opt_options); + } else { + return null; + } }; @@ -294,6 +304,7 @@ ol.format.WKT.prototype.readProjectionFromText = function(text) { * * @function * @param {ol.Feature} feature Feature. + * @param {olx.format.WriteOptions=} opt_options Write options. * @return {string} WKT string. * @api */ @@ -303,10 +314,10 @@ ol.format.WKT.prototype.writeFeature; /** * @inheritDoc */ -ol.format.WKT.prototype.writeFeatureText = function(feature) { +ol.format.WKT.prototype.writeFeatureText = function(feature, opt_options) { var geometry = feature.getGeometry(); if (goog.isDef(geometry)) { - return this.writeGeometryText(geometry); + return this.writeGeometryText(geometry, opt_options); } return ''; }; @@ -317,6 +328,7 @@ ol.format.WKT.prototype.writeFeatureText = function(feature) { * * @function * @param {Array.} features Features. + * @param {olx.format.WriteOptions=} opt_options Write options. * @return {string} WKT string. * @api */ @@ -326,16 +338,16 @@ ol.format.WKT.prototype.writeFeatures; /** * @inheritDoc */ -ol.format.WKT.prototype.writeFeaturesText = function(features) { +ol.format.WKT.prototype.writeFeaturesText = function(features, opt_options) { if (features.length == 1) { - return this.writeFeatureText(features[0]); + return this.writeFeatureText(features[0], opt_options); } var geometries = []; for (var i = 0, ii = features.length; i < ii; ++i) { geometries.push(features[i].getGeometry()); } var collection = new ol.geom.GeometryCollection(geometries); - return this.writeGeometryText(collection); + return this.writeGeometryText(collection, opt_options); }; @@ -353,8 +365,9 @@ ol.format.WKT.prototype.writeGeometry; /** * @inheritDoc */ -ol.format.WKT.prototype.writeGeometryText = function(geometry) { - return ol.format.WKT.encode_(geometry); +ol.format.WKT.prototype.writeGeometryText = function(geometry, opt_options) { + return ol.format.WKT.encode_(ol.format.Feature.transformWithOptions( + geometry, true, true, opt_options)); }; diff --git a/test/spec/ol/format/wktformat.test.js b/test/spec/ol/format/wktformat.test.js index f14e6ccfd6..66f9e84088 100644 --- a/test/spec/ol/format/wktformat.test.js +++ b/test/spec/ol/format/wktformat.test.js @@ -15,6 +15,21 @@ describe('ol.format.WKT', function() { expect(geom.getCoordinates()).to.eql([30, 10]); }); + it('Point transformed / read / written correctly', function() { + var wkt = 'POINT(1 2)'; + var geom = format.readGeometry(wkt, { + dataProjection: 'EPSG:4326', + featureProjection: 'EPSG:3857' + }); + expect(geom.getCoordinates()).to.eql( + ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857')); + var newWkt = format.writeGeometry(geom, { + dataProjection: 'EPSG:4326', + featureProjection: 'EPSG:3857' + }); + expect(newWkt).to.eql(wkt); + }); + it('MultiPoint read / written correctly', function() { // there are two forms to test var wkt = 'MULTIPOINT((10 40),(40 30),(20 20),(30 10))'; @@ -231,7 +246,67 @@ describe('ol.format.WKT', function() { expect(format.writeFeatures(features)).to.eql(wkt); }); + it('Point feature read / written correctly', function() { + var wkt = 'POINT(30 10)'; + var feature = format.readFeature(wkt); + var geom = feature.getGeometry(); + expect(geom.getCoordinates()).to.eql([30, 10]); + expect(format.writeFeature(feature)).to.eql(wkt); + }); + + it('Point feature transformed / read / written correctly', function() { + var wkt = 'POINT(1 2)'; + var feature = format.readFeature(wkt, { + dataProjection: 'EPSG:4326', + featureProjection: 'EPSG:3857' + }); + var geom = feature.getGeometry(); + expect(geom.getCoordinates()).to.eql( + ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857')); + var newWkt = format.writeFeature(feature, { + dataProjection: 'EPSG:4326', + featureProjection: 'EPSG:3857' + }); + expect(newWkt).to.eql(wkt); + }); + + it('Features read / written correctly', function() { + var wkt = 'GEOMETRYCOLLECTION(POINT(1 2),POINT(3 4))'; + var features = format.readFeatures(wkt); + expect(features.length).to.eql(2); + var point1 = features[0].getGeometry(); + var point2 = features[1].getGeometry(); + expect(point1.getType()).to.eql(ol.geom.GeometryType.POINT); + expect(point2.getType()).to.eql(ol.geom.GeometryType.POINT); + expect(point1.getCoordinates()).to.eql([1, 2]); + expect(point2.getCoordinates()).to.eql([3, 4]); + expect(format.writeFeatures(features)).to.eql(wkt); + }); + + it('Features transformed / read / written correctly', function() { + var wkt = 'GEOMETRYCOLLECTION(POINT(1 2),POINT(4 5))'; + var features = format.readFeatures(wkt, { + dataProjection: 'EPSG:4326', + featureProjection: 'EPSG:3857' + }); + expect(features.length).to.eql(2); + var point1 = features[0].getGeometry(); + var point2 = features[1].getGeometry(); + expect(point1.getType()).to.eql(ol.geom.GeometryType.POINT); + expect(point2.getType()).to.eql(ol.geom.GeometryType.POINT); + expect(point1.getCoordinates()).to.eql( + ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857')); + expect(point2.getCoordinates()).to.eql( + ol.proj.transform([4, 5], 'EPSG:4326', 'EPSG:3857')); + var newWkt = format.writeFeatures(features, { + dataProjection: 'EPSG:4326', + featureProjection: 'EPSG:3857' + }); + expect(newWkt).to.eql(wkt); + }); + }); goog.require('ol.geom.GeometryType'); goog.require('ol.format.WKT'); +goog.require('ol.proj');