Port ol.parser.polyline to ol.format.Format

This commit is contained in:
Tom Payne
2013-12-04 17:27:40 +01:00
parent 461a0d0615
commit 3cf87ced1e
2 changed files with 212 additions and 49 deletions

View File

@@ -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.<number>} 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.<number>} 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.<number>} 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.<number>} 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.<number>} 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.<number>} 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.<number>} 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);
};

View File

@@ -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');