Add support for scientific notation to WKT format

This commit is contained in:
Marc Jansen
2015-05-18 10:07:03 +02:00
parent 1ac41c7403
commit 2b76bc05a5
2 changed files with 69 additions and 1 deletions

View File

@@ -491,12 +491,23 @@ ol.format.WKT.Lexer.prototype.nextToken = function() {
ol.format.WKT.Lexer.prototype.readNumber_ = function() { ol.format.WKT.Lexer.prototype.readNumber_ = function() {
var c, index = this.index_; var c, index = this.index_;
var decimal = false; var decimal = false;
var scientificNotation = false;
do { do {
if (c == '.') { if (c == '.') {
decimal = true; decimal = true;
} else if (goog.isDef(c) && c.toLowerCase() == 'e') {
scientificNotation = true;
} }
c = this.nextChar_(); c = this.nextChar_();
} while (this.isNumeric_(c, decimal)); } while (
this.isNumeric_(c, decimal) ||
// once c is defined we may have a scientific number indicated by 'e'
// but only if we haven't detected a scientific number before
!scientificNotation && goog.isDef(c) && c.toLowerCase() == 'e' ||
// once we know that we have a scientific number, both '-' and '+'
// are allowed
scientificNotation && (c == '-' || c == '+')
);
return parseFloat(this.wkt.substring(index, this.index_--)); return parseFloat(this.wkt.substring(index, this.index_--));
}; };

View File

@@ -369,6 +369,63 @@ describe('ol.format.WKT', function() {
expect(format.writeFeatures(features)).to.eql(wkt); expect(format.writeFeatures(features)).to.eql(wkt);
}); });
describe('scientific notation supported', function() {
it('handles scientific notation correctly', function() {
var wkt = 'POINT(3e1 1e1)';
var geom = format.readGeometry(wkt);
expect(geom.getCoordinates()).to.eql([30, 10]);
expect(format.writeGeometry(geom)).to.eql('POINT(30 10)');
});
it('works with with negative exponent', function() {
var wkt = 'POINT(3e-1 1e-1)';
var geom = format.readGeometry(wkt);
expect(geom.getCoordinates()).to.eql([0.3, 0.1]);
expect(format.writeGeometry(geom)).to.eql('POINT(0.3 0.1)');
});
it('works with with explicitly positive exponent', function() {
var wkt = 'POINT(3e+1 1e+1)';
var geom = format.readGeometry(wkt);
expect(geom.getCoordinates()).to.eql([30, 10]);
expect(format.writeGeometry(geom)).to.eql('POINT(30 10)');
});
it('handles very small numbers in scientific notation', function() {
// very small numbers keep the scientific notation, both when reading and
// writing
var wkt = 'POINT(3e-9 1e-9)';
var geom = format.readGeometry(wkt);
expect(geom.getCoordinates()).to.eql([3e-9, 1e-9]);
expect(format.writeGeometry(geom)).to.eql('POINT(3e-9 1e-9)');
});
it('handles very big numbers in scientific notation', function() {
// very big numbers keep the scientific notation, both when reading and
// writing
var wkt = 'POINT(3e25 1e25)';
var geom = format.readGeometry(wkt);
expect(geom.getCoordinates()).to.eql([3e25, 1e25]);
expect(format.writeGeometry(geom)).to.eql('POINT(3e+25 1e+25)');
});
it('works case insensitively (e / E)', function() {
var wkt = 'POINT(3E1 1E1)';
var geom = format.readGeometry(wkt);
expect(geom.getCoordinates()).to.eql([30, 10]);
expect(format.writeGeometry(geom)).to.eql('POINT(30 10)');
});
it('detects invalid scientific notation', function() {
expect(function() {
// note the double 'e'
format.readGeometry('POINT(3ee1 10)');
}).to.throwException();
});
});
}); });
goog.require('ol.Feature'); goog.require('ol.Feature');