From 461a0d06151cec40ea3acd4289b59be1b90e20a7 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 4 Dec 2013 17:25:13 +0100 Subject: [PATCH 1/8] Move back ol.parser.polyline --- .../parser/polylineparser.js => src/ol/format/polylineformat.js | 0 .../spec/ol/format/polylineformat.test.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename old/src/ol/parser/polylineparser.js => src/ol/format/polylineformat.js (100%) rename old/test/spec/ol/parser/polyline.test.js => test/spec/ol/format/polylineformat.test.js (100%) diff --git a/old/src/ol/parser/polylineparser.js b/src/ol/format/polylineformat.js similarity index 100% rename from old/src/ol/parser/polylineparser.js rename to src/ol/format/polylineformat.js diff --git a/old/test/spec/ol/parser/polyline.test.js b/test/spec/ol/format/polylineformat.test.js similarity index 100% rename from old/test/spec/ol/parser/polyline.test.js rename to test/spec/ol/format/polylineformat.test.js From 3cf87ced1e9ad0633cf4915544ecc2b896760934 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 4 Dec 2013 17:27:40 +0100 Subject: [PATCH 2/8] Port ol.parser.polyline to ol.format.Format --- src/ol/format/polylineformat.js | 140 ++++++++++++++++----- test/spec/ol/format/polylineformat.test.js | 121 +++++++++++++++--- 2 files changed, 212 insertions(+), 49 deletions(-) diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index c0793d0ccc..157aeedb1e 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -1,4 +1,21 @@ -goog.provide('ol.parser.polyline'); +goog.provide('ol.format.Polyline'); + +goog.require('goog.asserts'); +goog.require('ol.Feature'); +goog.require('ol.format.Text'); +goog.require('ol.geom.LineString'); +goog.require('ol.geom.flat'); + + + +/** + * @constructor + * @extends {ol.format.Text} + */ +ol.format.Polyline = function() { + goog.base(this); +}; +goog.inherits(ol.format.Polyline, ol.format.Text); /** @@ -10,10 +27,10 @@ goog.provide('ol.parser.polyline'); * @param {number=} opt_dimension The dimension of the coordinates in the array. * @return {string} The encoded string. */ -ol.parser.polyline.encodeFlatCoordinates = +ol.format.Polyline.encodeFlatCoordinates = function(flatPoints, opt_dimension) { var dimension = opt_dimension || 2; - return ol.parser.polyline.encodeDeltas(flatPoints, dimension); + return ol.format.Polyline.encodeDeltas(flatPoints, dimension); }; @@ -25,9 +42,9 @@ ol.parser.polyline.encodeFlatCoordinates = * encoded string. * @return {Array.} A flat array of coordinates. */ -ol.parser.polyline.decodeFlatCoordinates = function(encoded, opt_dimension) { +ol.format.Polyline.decodeFlatCoordinates = function(encoded, opt_dimension) { var dimension = opt_dimension || 2; - return ol.parser.polyline.decodeDeltas(encoded, dimension); + return ol.format.Polyline.decodeDeltas(encoded, dimension); }; @@ -42,7 +59,7 @@ ol.parser.polyline.decodeFlatCoordinates = function(encoded, opt_dimension) { * multiplied. The remaining decimal places will get rounded away. * @return {string} The encoded string. */ -ol.parser.polyline.encodeDeltas = function(numbers, dimension, opt_factor) { +ol.format.Polyline.encodeDeltas = function(numbers, dimension, opt_factor) { var factor = opt_factor || 1e5; var d; @@ -62,7 +79,7 @@ ol.parser.polyline.encodeDeltas = function(numbers, dimension, opt_factor) { } } - return ol.parser.polyline.encodeFloats(numbers, factor); + return ol.format.Polyline.encodeFloats(numbers, factor); }; @@ -75,7 +92,7 @@ ol.parser.polyline.encodeDeltas = function(numbers, dimension, opt_factor) { * be divided. * @return {Array.} A list of n-dimensional points. */ -ol.parser.polyline.decodeDeltas = function(encoded, dimension, opt_factor) { +ol.format.Polyline.decodeDeltas = function(encoded, dimension, opt_factor) { var factor = opt_factor || 1e5; var d; @@ -84,7 +101,7 @@ ol.parser.polyline.decodeDeltas = function(encoded, dimension, opt_factor) { lastNumbers[d] = 0; } - var numbers = ol.parser.polyline.decodeFloats(encoded, factor); + var numbers = ol.format.Polyline.decodeFloats(encoded, factor); var numbersLength = numbers.length; for (var i = 0; i < numbersLength;) { @@ -109,7 +126,7 @@ ol.parser.polyline.decodeDeltas = function(encoded, dimension, opt_factor) { * multiplied. The remaining decimal places will get rounded away. * @return {string} The encoded string. */ -ol.parser.polyline.encodeFloats = function(numbers, opt_factor) { +ol.format.Polyline.encodeFloats = function(numbers, opt_factor) { var factor = opt_factor || 1e5; var numbersLength = numbers.length; @@ -117,7 +134,7 @@ ol.parser.polyline.encodeFloats = function(numbers, opt_factor) { numbers[i] = Math.round(numbers[i] * factor); } - return ol.parser.polyline.encodeSignedIntegers(numbers); + return ol.format.Polyline.encodeSignedIntegers(numbers); }; @@ -128,10 +145,10 @@ ol.parser.polyline.encodeFloats = function(numbers, opt_factor) { * @param {number=} opt_factor The factor by which the result will be divided. * @return {Array.} A list of floating point numbers. */ -ol.parser.polyline.decodeFloats = function(encoded, opt_factor) { +ol.format.Polyline.decodeFloats = function(encoded, opt_factor) { var factor = opt_factor || 1e5; - var numbers = ol.parser.polyline.decodeSignedIntegers(encoded); + var numbers = ol.format.Polyline.decodeSignedIntegers(encoded); var numbersLength = numbers.length; for (var i = 0; i < numbersLength; ++i) { @@ -150,7 +167,7 @@ ol.parser.polyline.decodeFloats = function(encoded, opt_factor) { * @param {Array.} numbers A list of signed integers. * @return {string} The encoded string. */ -ol.parser.polyline.encodeSignedIntegers = function(numbers) { +ol.format.Polyline.encodeSignedIntegers = function(numbers) { var numbersLength = numbers.length; for (var i = 0; i < numbersLength; ++i) { var num = numbers[i]; @@ -163,7 +180,7 @@ ol.parser.polyline.encodeSignedIntegers = function(numbers) { numbers[i] = signedNum; } - return ol.parser.polyline.encodeUnsignedIntegers(numbers); + return ol.format.Polyline.encodeUnsignedIntegers(numbers); }; @@ -173,8 +190,8 @@ ol.parser.polyline.encodeSignedIntegers = function(numbers) { * @param {string} encoded An encoded string. * @return {Array.} A list of signed integers. */ -ol.parser.polyline.decodeSignedIntegers = function(encoded) { - var numbers = ol.parser.polyline.decodeUnsignedIntegers(encoded); +ol.format.Polyline.decodeSignedIntegers = function(encoded) { + var numbers = ol.format.Polyline.decodeUnsignedIntegers(encoded); var numbersLength = numbers.length; for (var i = 0; i < numbersLength; ++i) { @@ -192,12 +209,12 @@ ol.parser.polyline.decodeSignedIntegers = function(encoded) { * @param {Array.} numbers A list of unsigned integers. * @return {string} The encoded string. */ -ol.parser.polyline.encodeUnsignedIntegers = function(numbers) { +ol.format.Polyline.encodeUnsignedIntegers = function(numbers) { var encoded = ''; var numbersLength = numbers.length; for (var i = 0; i < numbersLength; ++i) { - encoded += ol.parser.polyline.encodeUnsignedInteger(numbers[i]); + encoded += ol.format.Polyline.encodeUnsignedInteger(numbers[i]); } return encoded; @@ -210,7 +227,7 @@ ol.parser.polyline.encodeUnsignedIntegers = function(numbers) { * @param {string} encoded An encoded string. * @return {Array.} A list of unsigned integers. */ -ol.parser.polyline.decodeUnsignedIntegers = function(encoded) { +ol.format.Polyline.decodeUnsignedIntegers = function(encoded) { var numbers = []; var current = 0; @@ -243,9 +260,9 @@ ol.parser.polyline.decodeUnsignedIntegers = function(encoded) { * The remaining decimal places will get rounded away. * @return {string} The encoded string. */ -ol.parser.polyline.encodeFloat = function(num, opt_factor) { +ol.format.Polyline.encodeFloat = function(num, opt_factor) { num = Math.round(num * (opt_factor || 1e5)); - return ol.parser.polyline.encodeSignedInteger(num); + return ol.format.Polyline.encodeSignedInteger(num); }; @@ -256,8 +273,8 @@ ol.parser.polyline.encodeFloat = function(num, opt_factor) { * @param {number=} opt_factor The factor by which the result will be divided. * @return {number} The decoded floating point number. */ -ol.parser.polyline.decodeFloat = function(encoded, opt_factor) { - var result = ol.parser.polyline.decodeSignedInteger(encoded); +ol.format.Polyline.decodeFloat = function(encoded, opt_factor) { + var result = ol.format.Polyline.decodeSignedInteger(encoded); return result / (opt_factor || 1e5); }; @@ -268,13 +285,13 @@ ol.parser.polyline.decodeFloat = function(encoded, opt_factor) { * @param {number} num Signed integer that should be encoded. * @return {string} The encoded string. */ -ol.parser.polyline.encodeSignedInteger = function(num) { +ol.format.Polyline.encodeSignedInteger = function(num) { var signedNum = num << 1; if (num < 0) { signedNum = ~(signedNum); } - return ol.parser.polyline.encodeUnsignedInteger(signedNum); + return ol.format.Polyline.encodeUnsignedInteger(signedNum); }; @@ -284,8 +301,8 @@ ol.parser.polyline.encodeSignedInteger = function(num) { * @param {string} encoded An encoded string. * @return {number} The decoded signed integer. */ -ol.parser.polyline.decodeSignedInteger = function(encoded) { - var result = ol.parser.polyline.decodeUnsignedInteger(encoded); +ol.format.Polyline.decodeSignedInteger = function(encoded) { + var result = ol.format.Polyline.decodeUnsignedInteger(encoded); return ((result & 1) ? ~(result >> 1) : (result >> 1)); }; @@ -296,7 +313,7 @@ ol.parser.polyline.decodeSignedInteger = function(encoded) { * @param {number} num Unsigned integer that should be encoded. * @return {string} The encoded string. */ -ol.parser.polyline.encodeUnsignedInteger = function(num) { +ol.format.Polyline.encodeUnsignedInteger = function(num) { var value, encoded = ''; while (num >= 0x20) { value = (0x20 | (num & 0x1f)) + 63; @@ -315,7 +332,7 @@ ol.parser.polyline.encodeUnsignedInteger = function(num) { * @param {string} encoded An encoded string. * @return {number} The decoded unsigned integer. */ -ol.parser.polyline.decodeUnsignedInteger = function(encoded) { +ol.format.Polyline.decodeUnsignedInteger = function(encoded) { var result = 0; var shift = 0; @@ -333,3 +350,66 @@ ol.parser.polyline.decodeUnsignedInteger = function(encoded) { return result; }; + + +/** + * @inheritDoc + */ +ol.format.Polyline.prototype.readFeatureFromText = function(text) { + var geometry = this.readGeometryFromText(text); + return new ol.Feature(geometry); +}; + + +/** + * @inheritDoc + */ +ol.format.Polyline.prototype.readFeaturesFromText = function(text) { + var feature = this.readFeatureFromText(text); + return [feature]; +}; + + +/** + * @inheritDoc + */ +ol.format.Polyline.prototype.readGeometryFromText = function(text) { + var flatCoordinates = ol.format.Polyline.decodeFlatCoordinates(text, 2); + var coordinates = ol.geom.flat.inflateCoordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + return new ol.geom.LineString(coordinates); +}; + + +/** + * @inheritDoc + */ +ol.format.Polyline.prototype.writeFeatureText = function(feature) { + var geometry = feature.getGeometry(); + if (goog.isDefAndNotNull(geometry)) { + return this.writeGeometryText(geometry); + } else { + goog.asserts.fail(); + return ''; + } +}; + + +/** + * @inheritDoc + */ +ol.format.Polyline.prototype.writeFeaturesText = function(features) { + goog.asserts.assert(features.length == 1); + return this.writeFeatureText(features[0]); +}; + + +/** + * @inheritDoc + */ +ol.format.Polyline.prototype.writeGeometryText = function(geometry) { + goog.asserts.assert(geometry.getType() == ol.geom.GeometryType.LINE_STRING); + var flatCoordinates = geometry.getFlatCoordinates(); + var stride = geometry.getStride(); + return ol.format.Polyline.encodeFlatCoordinates(flatCoordinates, stride); +}; diff --git a/test/spec/ol/format/polylineformat.test.js b/test/spec/ol/format/polylineformat.test.js index 537ed78619..ac137aa19f 100644 --- a/test/spec/ol/format/polylineformat.test.js +++ b/test/spec/ol/format/polylineformat.test.js @@ -1,13 +1,19 @@ -goog.provide('ol.test.parser.polyline'); +goog.provide('ol.test.format.Polyline'); -describe('ol.parser.polyline', function() { +describe('ol.format.Polyline', function() { + var format; + var points; var flatPoints, encodedFlatPoints; var floats, smallFloats, encodedFloats; var signedIntegers, encodedSignedIntegers; var unsignedIntegers, encodedUnsignedIntegers; 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]; @@ -31,7 +37,7 @@ describe('ol.parser.polyline', function() { describe('encodeFlatCoordinates', function() { it('returns expected value', function() { - var encodeFlatCoordinates = ol.parser.polyline.encodeFlatCoordinates; + var encodeFlatCoordinates = ol.format.Polyline.encodeFlatCoordinates; // from the "Encoded Polyline Algorithm Format" page at Google expect(encodeFlatCoordinates(flatPoints)).to.eql(encodedFlatPoints); @@ -40,7 +46,7 @@ describe('ol.parser.polyline', function() { describe('decodeFlatCoordinates', function() { it('returns expected value', function() { - var decodeFlatCoordinates = ol.parser.polyline.decodeFlatCoordinates; + var decodeFlatCoordinates = ol.format.Polyline.decodeFlatCoordinates; // from the "Encoded Polyline Algorithm Format" page at Google expect(decodeFlatCoordinates(encodedFlatPoints)).to.eql(flatPoints); @@ -51,7 +57,7 @@ describe('ol.parser.polyline', function() { describe('encodeDeltas', function() { it('returns expected value', function() { - var encodeDeltas = ol.parser.polyline.encodeDeltas; + var encodeDeltas = ol.format.Polyline.encodeDeltas; expect(encodeDeltas(flatPoints, 2)).to.eql(encodedFlatPoints); }); @@ -59,7 +65,7 @@ describe('ol.parser.polyline', function() { describe('decodeDeltas', function() { it('returns expected value', function() { - var decodeDeltas = ol.parser.polyline.decodeDeltas; + var decodeDeltas = ol.format.Polyline.decodeDeltas; expect(decodeDeltas(encodedFlatPoints, 2)).to.eql(flatPoints); }); @@ -69,7 +75,7 @@ describe('ol.parser.polyline', function() { describe('encodeFloats', function() { it('returns expected value', function() { - var encodeFloats = ol.parser.polyline.encodeFloats; + var encodeFloats = ol.format.Polyline.encodeFloats; expect(encodeFloats(smallFloats)).to.eql(encodedFloats); @@ -82,7 +88,7 @@ describe('ol.parser.polyline', function() { describe('decodeFloats', function() { it('returns expected value', function() { - var decodeFloats = ol.parser.polyline.decodeFloats; + var decodeFloats = ol.format.Polyline.decodeFloats; expect(decodeFloats(encodedFloats)).to.eql(smallFloats); expect(decodeFloats(encodedFloats, 1e5)).to.eql(smallFloats); @@ -94,7 +100,7 @@ describe('ol.parser.polyline', function() { describe('encodeSignedIntegers', function() { it('returns expected value', function() { - var encodeSignedIntegers = ol.parser.polyline.encodeSignedIntegers; + var encodeSignedIntegers = ol.format.Polyline.encodeSignedIntegers; expect(encodeSignedIntegers( signedIntegers)).to.eql(encodedSignedIntegers); @@ -103,7 +109,7 @@ describe('ol.parser.polyline', function() { describe('decodeSignedIntegers', function() { it('returns expected value', function() { - var decodeSignedIntegers = ol.parser.polyline.decodeSignedIntegers; + var decodeSignedIntegers = ol.format.Polyline.decodeSignedIntegers; expect(decodeSignedIntegers( encodedSignedIntegers)).to.eql(signedIntegers); @@ -114,7 +120,7 @@ describe('ol.parser.polyline', function() { describe('encodeUnsignedIntegers', function() { it('returns expected value', function() { - var encodeUnsignedIntegers = ol.parser.polyline.encodeUnsignedIntegers; + var encodeUnsignedIntegers = ol.format.Polyline.encodeUnsignedIntegers; expect(encodeUnsignedIntegers( unsignedIntegers)).to.eql(encodedUnsignedIntegers); @@ -123,7 +129,7 @@ describe('ol.parser.polyline', function() { describe('decodeUnsignedIntegers', function() { it('returns expected value', function() { - var decodeUnsignedIntegers = ol.parser.polyline.decodeUnsignedIntegers; + var decodeUnsignedIntegers = ol.format.Polyline.decodeUnsignedIntegers; expect(decodeUnsignedIntegers( encodedUnsignedIntegers)).to.eql(unsignedIntegers); @@ -134,7 +140,7 @@ describe('ol.parser.polyline', function() { describe('encodeFloat', function() { it('returns expected value', function() { - var encodeFloat = ol.parser.polyline.encodeFloat; + var encodeFloat = ol.format.Polyline.encodeFloat; expect(encodeFloat(0.00000)).to.eql('?'); expect(encodeFloat(-0.00001)).to.eql('@'); @@ -157,7 +163,7 @@ describe('ol.parser.polyline', function() { describe('decodeFloat', function() { it('returns expected value', function() { - var decodeFloat = ol.parser.polyline.decodeFloat; + var decodeFloat = ol.format.Polyline.decodeFloat; expect(decodeFloat('?')).to.eql(0.00000); expect(decodeFloat('@')).to.eql(-0.00001); @@ -182,7 +188,7 @@ describe('ol.parser.polyline', function() { describe('encodeSignedInteger', function() { it('returns expected value', function() { - var encodeSignedInteger = ol.parser.polyline.encodeSignedInteger; + var encodeSignedInteger = ol.format.Polyline.encodeSignedInteger; expect(encodeSignedInteger(0)).to.eql('?'); expect(encodeSignedInteger(-1)).to.eql('@'); @@ -200,7 +206,7 @@ describe('ol.parser.polyline', function() { describe('decodeSignedInteger', function() { it('returns expected value', function() { - var decodeSignedInteger = ol.parser.polyline.decodeSignedInteger; + var decodeSignedInteger = ol.format.Polyline.decodeSignedInteger; expect(decodeSignedInteger('?')).to.eql(0); expect(decodeSignedInteger('@')).to.eql(-1); @@ -220,7 +226,7 @@ describe('ol.parser.polyline', function() { describe('encodeUnsignedInteger', function() { it('returns expected value', function() { - var encodeUnsignedInteger = ol.parser.polyline.encodeUnsignedInteger; + var encodeUnsignedInteger = ol.format.Polyline.encodeUnsignedInteger; expect(encodeUnsignedInteger(0)).to.eql('?'); expect(encodeUnsignedInteger(1)).to.eql('@'); @@ -240,7 +246,7 @@ describe('ol.parser.polyline', function() { describe('decodeUnsignedInteger', function() { it('returns expected value', function() { - var decodeUnsignedInteger = ol.parser.polyline.decodeUnsignedInteger; + var decodeUnsignedInteger = ol.format.Polyline.decodeUnsignedInteger; expect(decodeUnsignedInteger('?')).to.eql(0); expect(decodeUnsignedInteger('@')).to.eql(1); @@ -257,6 +263,83 @@ describe('ol.parser.polyline', function() { expect(decodeUnsignedInteger('mD')).to.eql(174); }); }); + + describe('#readFeature', function() { + + it('returns the expected feature', function() { + var feature = format.readFeature(encodedFlatPoints); + expect(feature).to.be.an(ol.Feature); + var geometry = feature.getGeometry(); + expect(geometry).to.be.an(ol.geom.LineString); + expect(geometry.getFlatCoordinates()).to.eql(flatPoints); + }); + + }); + + describe('#readFeatures', function() { + + it('returns the expected feature', function() { + var features = format.readFeatures(encodedFlatPoints); + 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.getFlatCoordinates()).to.eql(flatPoints); + }); + + }); + + describe('#readGeometry', function() { + + it('returns the expected geometry', function() { + var geometry = format.readGeometry(encodedFlatPoints); + expect(geometry).to.be.an(ol.geom.LineString); + expect(geometry.getFlatCoordinates()).to.eql(flatPoints); + }); + + }); + + describe('#readProjection', function() { + + it('returns the expected projection', function() { + var projection = format.readProjection(encodedFlatPoints); + expect(projection).to.be(ol.proj.get('EPSG:4326')); + }); + + }); + + describe('#writeFeature', function() { + + it('returns the expected text', function() { + var feature = new ol.Feature(new ol.geom.LineString(points)); + expect(format.writeFeature(feature)).to.be(encodedFlatPoints); + }); + + }); + + describe('#writeFeature', function() { + + it('returns the expected text', function() { + var features = [new ol.Feature(new ol.geom.LineString(points))]; + expect(format.writeFeatures(features)).to.be(encodedFlatPoints); + }); + + }); + + describe('#writeGeometry', function() { + + it('returns the expected text', function() { + var geometry = new ol.geom.LineString(points); + expect(format.writeGeometry(geometry)).to.be(encodedFlatPoints); + }); + + }); + }); -goog.require('ol.parser.polyline'); +goog.require('ol.Feature'); +goog.require('ol.format.Polyline'); +goog.require('ol.geom.LineString'); +goog.require('ol.proj'); From d1878498388350cd1e14ac6d423e7be28e427706 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 11 Dec 2013 19:14:54 +0100 Subject: [PATCH 3/8] Making coding style closer to ol3's in ol.format.Polyline --- src/ol/format/polylineformat.js | 61 +++++++++++++++++---------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index 157aeedb1e..98377cd684 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -29,7 +29,7 @@ goog.inherits(ol.format.Polyline, ol.format.Text); */ ol.format.Polyline.encodeFlatCoordinates = function(flatPoints, opt_dimension) { - var dimension = opt_dimension || 2; + var dimension = goog.isDef(opt_dimension) ? opt_dimension : 2; return ol.format.Polyline.encodeDeltas(flatPoints, dimension); }; @@ -43,7 +43,7 @@ ol.format.Polyline.encodeFlatCoordinates = * @return {Array.} A flat array of coordinates. */ ol.format.Polyline.decodeFlatCoordinates = function(encoded, opt_dimension) { - var dimension = opt_dimension || 2; + var dimension = goog.isDef(opt_dimension) ? opt_dimension : 2; return ol.format.Polyline.decodeDeltas(encoded, dimension); }; @@ -60,7 +60,7 @@ ol.format.Polyline.decodeFlatCoordinates = function(encoded, opt_dimension) { * @return {string} The encoded string. */ ol.format.Polyline.encodeDeltas = function(numbers, dimension, opt_factor) { - var factor = opt_factor || 1e5; + var factor = goog.isDef(opt_factor) ? opt_factor : 1e5; var d; var lastNumbers = new Array(dimension); @@ -68,8 +68,8 @@ ol.format.Polyline.encodeDeltas = function(numbers, dimension, opt_factor) { lastNumbers[d] = 0; } - var numbersLength = numbers.length; - for (var i = 0; i < numbersLength;) { + var i, ii; + for (i = 0, ii = numbers.length; i < ii;) { for (d = 0; d < dimension; ++d, ++i) { var num = numbers[i]; var delta = num - lastNumbers[d]; @@ -93,7 +93,7 @@ ol.format.Polyline.encodeDeltas = function(numbers, dimension, opt_factor) { * @return {Array.} A list of n-dimensional points. */ ol.format.Polyline.decodeDeltas = function(encoded, dimension, opt_factor) { - var factor = opt_factor || 1e5; + var factor = goog.isDef(opt_factor) ? opt_factor : 1e5; var d; var lastNumbers = new Array(dimension); @@ -103,8 +103,8 @@ ol.format.Polyline.decodeDeltas = function(encoded, dimension, opt_factor) { var numbers = ol.format.Polyline.decodeFloats(encoded, factor); - var numbersLength = numbers.length; - for (var i = 0; i < numbersLength;) { + var i, ii; + for (i = 0, ii = numbers.length; i < ii;) { for (d = 0; d < dimension; ++d, ++i) { lastNumbers[d] += numbers[i]; @@ -127,10 +127,10 @@ ol.format.Polyline.decodeDeltas = function(encoded, dimension, opt_factor) { * @return {string} The encoded string. */ ol.format.Polyline.encodeFloats = function(numbers, opt_factor) { - var factor = opt_factor || 1e5; + var factor = goog.isDef(opt_factor) ? opt_factor : 1e5; - var numbersLength = numbers.length; - for (var i = 0; i < numbersLength; ++i) { + var i, ii; + for (i = 0, ii = numbers.length; i < ii; ++i) { numbers[i] = Math.round(numbers[i] * factor); } @@ -146,12 +146,12 @@ ol.format.Polyline.encodeFloats = function(numbers, opt_factor) { * @return {Array.} A list of floating point numbers. */ ol.format.Polyline.decodeFloats = function(encoded, opt_factor) { - var factor = opt_factor || 1e5; + var factor = goog.isDef(opt_factor) ? opt_factor : 1e5; var numbers = ol.format.Polyline.decodeSignedIntegers(encoded); - var numbersLength = numbers.length; - for (var i = 0; i < numbersLength; ++i) { + var i, ii; + for (i = 0, ii = numbers.length; i < ii; ++i) { numbers[i] /= factor; } @@ -168,8 +168,8 @@ ol.format.Polyline.decodeFloats = function(encoded, opt_factor) { * @return {string} The encoded string. */ ol.format.Polyline.encodeSignedIntegers = function(numbers) { - var numbersLength = numbers.length; - for (var i = 0; i < numbersLength; ++i) { + var i, ii; + for (i = 0, ii = numbers.length; i < ii; ++i) { var num = numbers[i]; var signedNum = num << 1; @@ -193,8 +193,8 @@ ol.format.Polyline.encodeSignedIntegers = function(numbers) { ol.format.Polyline.decodeSignedIntegers = function(encoded) { var numbers = ol.format.Polyline.decodeUnsignedIntegers(encoded); - var numbersLength = numbers.length; - for (var i = 0; i < numbersLength; ++i) { + var i, ii; + for (i = 0, ii = numbers.length; i < ii; ++i) { var num = numbers[i]; numbers[i] = (num & 1) ? ~(num >> 1) : (num >> 1); } @@ -212,8 +212,8 @@ ol.format.Polyline.decodeSignedIntegers = function(encoded) { ol.format.Polyline.encodeUnsignedIntegers = function(numbers) { var encoded = ''; - var numbersLength = numbers.length; - for (var i = 0; i < numbersLength; ++i) { + var i, ii; + for (i = 0, ii = numbers.length; i < ii; ++i) { encoded += ol.format.Polyline.encodeUnsignedInteger(numbers[i]); } @@ -233,8 +233,8 @@ ol.format.Polyline.decodeUnsignedIntegers = function(encoded) { var current = 0; var shift = 0; - var encodedLength = encoded.length; - for (var i = 0; i < encodedLength; ++i) { + var i, ii; + for (i = 0, ii = encoded.length; i < ii; ++i) { var b = encoded.charCodeAt(i) - 63; current |= (b & 0x1f) << shift; @@ -261,7 +261,8 @@ ol.format.Polyline.decodeUnsignedIntegers = function(encoded) { * @return {string} The encoded string. */ ol.format.Polyline.encodeFloat = function(num, opt_factor) { - num = Math.round(num * (opt_factor || 1e5)); + var factor = goog.isDef(opt_factor) ? opt_factor : 1e5; + num = Math.round(num * factor); return ol.format.Polyline.encodeSignedInteger(num); }; @@ -274,8 +275,9 @@ ol.format.Polyline.encodeFloat = function(num, opt_factor) { * @return {number} The decoded floating point number. */ ol.format.Polyline.decodeFloat = function(encoded, opt_factor) { + var factor = goog.isDef(opt_factor) ? opt_factor : 1e5; var result = ol.format.Polyline.decodeSignedInteger(encoded); - return result / (opt_factor || 1e5); + return result / factor; }; @@ -317,11 +319,11 @@ ol.format.Polyline.encodeUnsignedInteger = function(num) { var value, encoded = ''; while (num >= 0x20) { value = (0x20 | (num & 0x1f)) + 63; - encoded += (String.fromCharCode(value)); + encoded += String.fromCharCode(value); num >>= 5; } value = num + 63; - encoded += (String.fromCharCode(value)); + encoded += String.fromCharCode(value); return encoded; }; @@ -336,14 +338,15 @@ ol.format.Polyline.decodeUnsignedInteger = function(encoded) { var result = 0; var shift = 0; - var encodedLength = encoded.length; - for (var i = 0; i < encodedLength; ++i) { + var i, ii; + for (i = 0, ii = encoded.length; i < ii; ++i) { var b = encoded.charCodeAt(i) - 63; result |= (b & 0x1f) << shift; - if (b < 0x20) + if (b < 0x20) { break; + } shift += 5; } From 36a5a53ba248544869b34346e3a27312349cd45a Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Tue, 17 Dec 2013 01:14:43 +0100 Subject: [PATCH 4/8] Optimize encode --- src/ol/format/polylineformat.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index 98377cd684..a661f145c1 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -171,15 +171,8 @@ ol.format.Polyline.encodeSignedIntegers = function(numbers) { var i, ii; for (i = 0, ii = numbers.length; i < ii; ++i) { var num = numbers[i]; - - var signedNum = num << 1; - if (num < 0) { - signedNum = ~(signedNum); - } - - numbers[i] = signedNum; + numbers[i] = (num < 0) ? ~(num << 1) : (num << 1); } - return ol.format.Polyline.encodeUnsignedIntegers(numbers); }; From 1864f204f7ce45f103b44a64fd6837e584a2b602 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 17 Dec 2013 01:15:54 +0100 Subject: [PATCH 5/8] Add ol.format.Polyline#readProjectionFromText --- src/ol/format/polylineformat.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index a661f145c1..b2689ed524 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -5,6 +5,7 @@ goog.require('ol.Feature'); goog.require('ol.format.Text'); goog.require('ol.geom.LineString'); goog.require('ol.geom.flat'); +goog.require('ol.proj'); @@ -377,6 +378,14 @@ ol.format.Polyline.prototype.readGeometryFromText = function(text) { }; +/** + * @inheritDoc + */ +ol.format.Polyline.prototype.readProjectionFromText = function(text) { + return ol.proj.get('EPSG:4326'); +}; + + /** * @inheritDoc */ From ed332dba19e8731a8140824816681a3fb8bccb1c Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 17 Dec 2013 01:16:16 +0100 Subject: [PATCH 6/8] Remove whitespace --- src/ol/format/polylineformat.js | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index b2689ed524..2d5ef64eb3 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -148,14 +148,11 @@ ol.format.Polyline.encodeFloats = function(numbers, opt_factor) { */ ol.format.Polyline.decodeFloats = function(encoded, opt_factor) { var factor = goog.isDef(opt_factor) ? opt_factor : 1e5; - var numbers = ol.format.Polyline.decodeSignedIntegers(encoded); - var i, ii; for (i = 0, ii = numbers.length; i < ii; ++i) { numbers[i] /= factor; } - return numbers; }; @@ -186,13 +183,11 @@ ol.format.Polyline.encodeSignedIntegers = function(numbers) { */ ol.format.Polyline.decodeSignedIntegers = function(encoded) { var numbers = ol.format.Polyline.decodeUnsignedIntegers(encoded); - var i, ii; for (i = 0, ii = numbers.length; i < ii; ++i) { var num = numbers[i]; numbers[i] = (num & 1) ? ~(num >> 1) : (num >> 1); } - return numbers; }; @@ -205,12 +200,10 @@ ol.format.Polyline.decodeSignedIntegers = function(encoded) { */ ol.format.Polyline.encodeUnsignedIntegers = function(numbers) { var encoded = ''; - var i, ii; for (i = 0, ii = numbers.length; i < ii; ++i) { encoded += ol.format.Polyline.encodeUnsignedInteger(numbers[i]); } - return encoded; }; @@ -223,16 +216,12 @@ ol.format.Polyline.encodeUnsignedIntegers = function(numbers) { */ ol.format.Polyline.decodeUnsignedIntegers = function(encoded) { var numbers = []; - var current = 0; var shift = 0; - var i, ii; for (i = 0, ii = encoded.length; i < ii; ++i) { var b = encoded.charCodeAt(i) - 63; - current |= (b & 0x1f) << shift; - if (b < 0x20) { numbers.push(current); current = 0; @@ -241,7 +230,6 @@ ol.format.Polyline.decodeUnsignedIntegers = function(encoded) { shift += 5; } } - return numbers; }; @@ -286,7 +274,6 @@ ol.format.Polyline.encodeSignedInteger = function(num) { if (num < 0) { signedNum = ~(signedNum); } - return ol.format.Polyline.encodeUnsignedInteger(signedNum); }; @@ -331,20 +318,15 @@ ol.format.Polyline.encodeUnsignedInteger = function(num) { ol.format.Polyline.decodeUnsignedInteger = function(encoded) { var result = 0; var shift = 0; - var i, ii; for (i = 0, ii = encoded.length; i < ii; ++i) { var b = encoded.charCodeAt(i) - 63; - result |= (b & 0x1f) << shift; - if (b < 0x20) { break; } - shift += 5; } - return result; }; From 1d1dbb08c9b0fa84da63cd2d381c50bf1869fb5a Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 17 Dec 2013 01:16:41 +0100 Subject: [PATCH 7/8] Use stronger instanceof assertion --- src/ol/format/polylineformat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index 2d5ef64eb3..af36338564 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -395,7 +395,7 @@ ol.format.Polyline.prototype.writeFeaturesText = function(features) { * @inheritDoc */ ol.format.Polyline.prototype.writeGeometryText = function(geometry) { - goog.asserts.assert(geometry.getType() == ol.geom.GeometryType.LINE_STRING); + goog.asserts.assertInstanceof(geometry, ol.geom.LineString); var flatCoordinates = geometry.getFlatCoordinates(); var stride = geometry.getStride(); return ol.format.Polyline.encodeFlatCoordinates(flatCoordinates, stride); From 432c6d8af36dfe5ed81f4d9e982b9e20aff72360 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 19 Dec 2013 18:14:10 +0100 Subject: [PATCH 8/8] Optimize encode --- src/ol/format/polylineformat.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index af36338564..a50cef525f 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -270,10 +270,7 @@ ol.format.Polyline.decodeFloat = function(encoded, opt_factor) { * @return {string} The encoded string. */ ol.format.Polyline.encodeSignedInteger = function(num) { - var signedNum = num << 1; - if (num < 0) { - signedNum = ~(signedNum); - } + var signedNum = (num < 0) ? ~(num << 1) : (num << 1); return ol.format.Polyline.encodeUnsignedInteger(signedNum); };