Restore the WKT parser, example & tests

This commit is contained in:
Erik Timmers
2014-06-06 22:23:29 +02:00
parent fa5f99c716
commit 61378098e6
6 changed files with 82 additions and 79 deletions

View File

@@ -43,8 +43,8 @@
</div>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="loader.js?id=wkt" type="text/javascript"></script>
<script src="../resources/example-behaviour.js" type="text/javascript"></script>
<script src="loader.js?id=wkt" type="text/javascript"></script>
</body>
</html>

View File

@@ -1,11 +1,9 @@
goog.require('ol.Feature');
goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.parser.WKT');
goog.require('ol.proj');
goog.require('ol.source.OSM');
goog.require('ol.source.Vector');
@@ -14,12 +12,12 @@ var raster = new ol.layer.Tile({
});
var parser = new ol.parser.WKT();
var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
var geom = parser.read(
'POLYGON((10.689697265625 -25.0927734375, 34.595947265625 ' +
'-20.1708984375, 38.814697265625 -35.6396484375, 13.502197265625 ' +
'-39.1552734375, 10.689697265625 -25.0927734375))');
geom.transform(transform);
geom.transform('EPSG:4326', 'EPSG:3857');
var feature = new ol.Feature();
feature.setGeometry(geom);
@@ -31,7 +29,7 @@ var vector = new ol.layer.Vector({
var map = new ol.Map({
layers: [raster, vector],
renderer: ol.RendererHint.CANVAS,
renderer: exampleNS.getRendererFromQueryString(),
target: 'map',
view: new ol.View2D({
center: [2952104.019976033, -3277504.823700756],

View File

@@ -21,6 +21,7 @@ goog.require('ol.parser.StringFeatureParser');
* @extends {ol.parser.Parser}
* @implements {ol.parser.StringFeatureParser}
* @todo stability experimental
* @todo api
*/
ol.parser.WKT = function() {
};
@@ -78,12 +79,12 @@ ol.parser.WKT.prototype.parseLineString_ = function(str) {
ol.parser.WKT.prototype.parseMultiPoint_ = function(str) {
var point;
var points = goog.string.trim(str).split(',');
var parts = [];
var geom = new ol.geom.MultiPoint(null);
for (var i = 0, ii = points.length; i < ii; ++i) {
point = points[i].replace(ol.parser.WKT.regExes.trimParens, '$1');
parts.push(this.parsePoint_.apply(this, [point]));
geom.appendPoint(this.parsePoint_.apply(this, [point]));
}
return ol.geom.MultiPoint.fromParts(parts);
return geom;
};
@@ -95,12 +96,12 @@ ol.parser.WKT.prototype.parseMultiPoint_ = function(str) {
ol.parser.WKT.prototype.parseMultiLineString_ = function(str) {
var line;
var lines = goog.string.trim(str).split(ol.parser.WKT.regExes.parenComma);
var parts = [];
var geom = new ol.geom.MultiLineString(null);
for (var i = 0, ii = lines.length; i < ii; ++i) {
line = lines[i].replace(ol.parser.WKT.regExes.trimParens, '$1');
parts.push(this.parseLineString_.apply(this, [line]));
geom.appendLineString(this.parseLineString_.apply(this, [line]));
}
return ol.geom.MultiLineString.fromParts(parts);
return geom;
};
@@ -131,12 +132,12 @@ ol.parser.WKT.prototype.parseMultiPolygon_ = function(str) {
var polygon;
var polygons = goog.string.trim(str).split(
ol.parser.WKT.regExes.doubleParenComma);
var parts = [];
var geom = new ol.geom.MultiPolygon(null);
for (var i = 0, ii = polygons.length; i < ii; ++i) {
polygon = polygons[i].replace(ol.parser.WKT.regExes.trimParens, '$1');
parts.push(this.parsePolygon_.apply(this, [polygon]));
geom.appendPolygon(this.parsePolygon_.apply(this, [polygon]));
}
return ol.geom.MultiPolygon.fromParts(parts);
return geom;
};
@@ -149,11 +150,11 @@ ol.parser.WKT.prototype.parseGeometryCollection_ = function(str) {
// separate components of the collection with |
str = str.replace(ol.parser.WKT.regExes.geomCollection, '|$1');
var wktArray = goog.string.trim(str).split('|');
var components = [];
var geoms = [];
for (var i = 0, ii = wktArray.length; i < ii; ++i) {
components.push(this.parse_.apply(this, [wktArray[i]]));
geoms.push(this.parse_.apply(this, [wktArray[i]]));
}
return new ol.geom.GeometryCollection(components);
return new ol.geom.GeometryCollection(geoms);
};
@@ -175,7 +176,7 @@ ol.parser.WKT.prototype.encodePoint_ = function(geom) {
*/
ol.parser.WKT.prototype.encodeMultiPoint_ = function(geom) {
var array = [];
var components = geom.getComponents();
var components = geom.getPoints();
for (var i = 0, ii = components.length; i < ii; ++i) {
array.push('(' + this.encodePoint_.apply(this, [components[i]]) + ')');
}
@@ -190,9 +191,9 @@ ol.parser.WKT.prototype.encodeMultiPoint_ = function(geom) {
*/
ol.parser.WKT.prototype.encodeGeometryCollection_ = function(geom) {
var array = [];
var components = geom.getComponents();
for (var i = 0, ii = components.length; i < ii; ++i) {
array.push(this.encode_.apply(this, [components[i]]));
var geoms = geom.getGeometries();
for (var i = 0, ii = geoms.length; i < ii; ++i) {
array.push(this.encode_.apply(this, [geoms[i]]));
}
return array.join(',');
};
@@ -220,7 +221,7 @@ ol.parser.WKT.prototype.encodeLineString_ = function(geom) {
*/
ol.parser.WKT.prototype.encodeMultiLineString_ = function(geom) {
var array = [];
var components = geom.getComponents();
var components = geom.getLineStrings();
for (var i = 0, ii = components.length; i < ii; ++i) {
array.push('(' + this.encodeLineString_.apply(this,
[components[i]]) + ')');
@@ -236,7 +237,7 @@ ol.parser.WKT.prototype.encodeMultiLineString_ = function(geom) {
*/
ol.parser.WKT.prototype.encodePolygon_ = function(geom) {
var array = [];
var rings = geom.getRings();
var rings = geom.getLinearRings();
for (var i = 0, ii = rings.length; i < ii; ++i) {
array.push('(' + this.encodeLineString_.apply(this,
[rings[i]]) + ')');
@@ -252,7 +253,7 @@ ol.parser.WKT.prototype.encodePolygon_ = function(geom) {
*/
ol.parser.WKT.prototype.encodeMultiPolygon_ = function(geom) {
var array = [];
var components = geom.getComponents();
var components = geom.getPolygons();
for (var i = 0, ii = components.length; i < ii; ++i) {
array.push('(' + this.encodePolygon_.apply(this, [components[i]]) + ')');
}
@@ -338,6 +339,7 @@ ol.parser.WKT.prototype.encode_ = function(geom) {
* Parse a WKT string.
* @param {string} str WKT string.
* @return {ol.geom.Geometry|undefined} Parsed geometry.
* @todo api
*/
ol.parser.WKT.prototype.read = function(str) {
return this.parse_(str);
@@ -348,6 +350,7 @@ ol.parser.WKT.prototype.read = function(str) {
* Parse a WKT document provided as a string.
* @param {string} str WKT document.
* @return {ol.parser.ReadFeaturesResult} Features and metadata.
* @todo api
*/
ol.parser.WKT.prototype.readFeaturesFromString = function(str) {
var geom = this.read(str);
@@ -366,6 +369,7 @@ ol.parser.WKT.prototype.readFeaturesFromString = function(str) {
* Write out a geometry as a WKT string.
* @param {ol.geom.Geometry} geom The geometry to encode.
* @return {string} WKT for the geometry.
* @todo api
*/
ol.parser.WKT.prototype.write = function(geom) {
return this.encode_(geom);
@@ -376,6 +380,7 @@ ol.parser.WKT.prototype.write = function(geom) {
* Parse a WKT string.
* @param {string} str WKT string.
* @return {ol.geom.Geometry|undefined} Parsed geometry.
* @todo api
*/
ol.parser.WKT.read = function(str) {
return ol.parser.WKT.getInstance().read(str);
@@ -386,6 +391,7 @@ ol.parser.WKT.read = function(str) {
* Write out a geometry as a WKT string.
* @param {ol.geom.Geometry} geom The geometry to encode.
* @return {string} WKT for the geometry.
* @todo api
*/
ol.parser.WKT.write = function(geom) {
return ol.parser.WKT.getInstance().write(geom);

View File

@@ -19,22 +19,22 @@ describe('ol.parser.WKT', function() {
// there are two forms to test
var wkt = 'MULTIPOINT((10 40),(40 30),(20 20),(30 10))';
var geom = parser.read(wkt);
var components = geom.getComponents();
expect(components.length).to.eql(4);
expect(components[0].getCoordinates()).to.eql([10, 40]);
expect(components[1].getCoordinates()).to.eql([40, 30]);
expect(components[2].getCoordinates()).to.eql([20, 20]);
expect(components[3].getCoordinates()).to.eql([30, 10]);
var points = geom.getPoints();
expect(points.length).to.eql(4);
expect(points[0].getCoordinates()).to.eql([10, 40]);
expect(points[1].getCoordinates()).to.eql([40, 30]);
expect(points[2].getCoordinates()).to.eql([20, 20]);
expect(points[3].getCoordinates()).to.eql([30, 10]);
expect(parser.write(geom)).to.eql(wkt);
// this has whitespace
wkt = 'MULTIPOINT (10 40, 40 30, 20 20, 30 10)';
geom = parser.read(wkt);
components = geom.getComponents();
expect(components.length).to.eql(4);
expect(components[0].getCoordinates()).to.eql([10, 40]);
expect(components[1].getCoordinates()).to.eql([40, 30]);
expect(components[2].getCoordinates()).to.eql([20, 20]);
expect(components[3].getCoordinates()).to.eql([30, 10]);
points = geom.getPoints();
expect(points.length).to.eql(4);
expect(points[0].getCoordinates()).to.eql([10, 40]);
expect(points[1].getCoordinates()).to.eql([40, 30]);
expect(points[2].getCoordinates()).to.eql([20, 20]);
expect(points[3].getCoordinates()).to.eql([30, 10]);
});
it('LineString read / written correctly', function() {
@@ -55,10 +55,10 @@ describe('ol.parser.WKT', function() {
'(40 40,30 30,40 20,30 10))';
var geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.MULTI_LINE_STRING);
var components = geom.getComponents();
expect(components.length).to.eql(2);
expect(components[0].getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(components[0].getCoordinates()).to.eql(
var linestrings = geom.getLineStrings();
expect(linestrings.length).to.eql(2);
expect(linestrings[0].getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(linestrings[0].getCoordinates()).to.eql(
[[10, 10], [20, 20], [10, 40]]);
expect(parser.write(geom)).to.eql(wkt);
// test whitespace when reading
@@ -66,11 +66,11 @@ describe('ol.parser.WKT', function() {
'(40 40, 30 30, 40 20, 30 10) )';
geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.MULTI_LINE_STRING);
components = geom.getComponents();
expect(components.length).to.eql(2);
expect(components[0].getType()).to.eql(
linestrings = geom.getLineStrings();
expect(linestrings.length).to.eql(2);
expect(linestrings[0].getType()).to.eql(
ol.geom.GeometryType.LINE_STRING);
expect(components[0].getCoordinates()).to.eql(
expect(linestrings[0].getCoordinates()).to.eql(
[[10, 10], [20, 20], [10, 40]]);
});
@@ -78,7 +78,7 @@ describe('ol.parser.WKT', function() {
var wkt = 'POLYGON((30 10,10 20,20 40,40 40,30 10))';
var geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
var rings = geom.getRings();
var rings = geom.getLinearRings();
expect(rings.length).to.eql(1);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEAR_RING);
expect(rings[0].getCoordinates()).to.eql(
@@ -89,7 +89,7 @@ describe('ol.parser.WKT', function() {
wkt = 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,30 20,35 35,20 30))';
geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
var rings = geom.getRings();
rings = geom.getLinearRings();
expect(rings.length).to.eql(2);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEAR_RING);
expect(rings[1].getType()).to.eql(ol.geom.GeometryType.LINEAR_RING);
@@ -103,7 +103,7 @@ describe('ol.parser.WKT', function() {
wkt = 'POLYGON ( (30 10, 10 20, 20 40, 40 40, 30 10) )';
geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
var rings = geom.getRings();
rings = geom.getLinearRings();
expect(rings.length).to.eql(1);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEAR_RING);
expect(rings[0].getCoordinates()).to.eql(
@@ -116,17 +116,17 @@ describe('ol.parser.WKT', function() {
'((20 35,45 20,30 5,10 10,10 30,20 35),(30 20,20 25,20 15,30 20)))';
var geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.MULTI_POLYGON);
var components = geom.getComponents();
expect(components.length).to.eql(2);
expect(components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(components[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(components[0].getRings().length).to.eql(1);
expect(components[1].getRings().length).to.eql(2);
expect(components[0].getRings()[0].getCoordinates()).to.eql(
var polygons = geom.getPolygons();
expect(polygons.length).to.eql(2);
expect(polygons[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(polygons[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(polygons[0].getLinearRings().length).to.eql(1);
expect(polygons[1].getLinearRings().length).to.eql(2);
expect(polygons[0].getLinearRings()[0].getCoordinates()).to.eql(
[[40, 40], [45, 30], [20, 45], [40, 40]]);
expect(components[1].getRings()[0].getCoordinates()).to.eql(
expect(polygons[1].getLinearRings()[0].getCoordinates()).to.eql(
[[20, 35], [45, 20], [30, 5], [10, 10], [10, 30], [20, 35]]);
expect(components[1].getRings()[1].getCoordinates()).to.eql(
expect(polygons[1].getLinearRings()[1].getCoordinates()).to.eql(
[[30, 20], [20, 25], [20, 15], [30, 20]]);
expect(parser.write(geom)).to.eql(wkt);
@@ -136,42 +136,41 @@ describe('ol.parser.WKT', function() {
'( 30 20, 20 25,20 15 ,30 20 ) ))';
geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.MULTI_POLYGON);
var components = geom.getComponents();
expect(components.length).to.eql(2);
expect(components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(components[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(components[0].getRings().length).to.eql(1);
expect(components[1].getRings().length).to.eql(2);
expect(components[0].getRings()[0].getCoordinates()).to.eql(
polygons = geom.getPolygons();
expect(polygons.length).to.eql(2);
expect(polygons[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(polygons[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(polygons[0].getLinearRings().length).to.eql(1);
expect(polygons[1].getLinearRings().length).to.eql(2);
expect(polygons[0].getLinearRings()[0].getCoordinates()).to.eql(
[[40, 40], [45, 30], [20, 45], [40, 40]]);
expect(components[1].getRings()[0].getCoordinates()).to.eql(
expect(polygons[1].getLinearRings()[0].getCoordinates()).to.eql(
[[20, 35], [45, 20], [30, 5], [10, 10], [10, 30], [20, 35]]);
expect(components[1].getRings()[1].getCoordinates()).to.eql(
expect(polygons[1].getLinearRings()[1].getCoordinates()).to.eql(
[[30, 20], [20, 25], [20, 15], [30, 20]]);
});
it('GeometryCollection read / written correctly', function() {
var wkt = 'GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))';
var geom = parser.read(wkt);
var components = geom.getComponents();
expect(components.length).to.eql(2);
var geoms = geom.getGeometries();
expect(geoms.length).to.eql(2);
expect(geom.getType()).to.eql(ol.geom.GeometryType.GEOMETRY_COLLECTION);
expect(components[0].getType()).to.eql(ol.geom.GeometryType.POINT);
expect(components[1].getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(components[0].getCoordinates()).to.eql([4, 6]);
expect(components[1].getCoordinates()).to.eql([[4, 6], [7, 10]]);
expect(geoms[0].getType()).to.eql(ol.geom.GeometryType.POINT);
expect(geoms[1].getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(geoms[0].getCoordinates()).to.eql([4, 6]);
expect(geoms[1].getCoordinates()).to.eql([[4, 6], [7, 10]]);
expect(parser.write(geom)).to.eql(wkt);
// test whitespace when reading
wkt = 'GEOMETRYCOLLECTION ( POINT (4 6), LINESTRING (4 6, 7 10) )';
geom = parser.read(wkt);
components = geom.getComponents();
expect(components.length).to.eql(2);
geoms = geom.getGeometries();
expect(geoms.length).to.eql(2);
expect(geom.getType()).to.eql(ol.geom.GeometryType.GEOMETRY_COLLECTION);
expect(components[0].getType()).to.eql(ol.geom.GeometryType.POINT);
expect(components[1].getType()).to.eql(
ol.geom.GeometryType.LINE_STRING);
expect(components[0].getCoordinates()).to.eql([4, 6]);
expect(components[1].getCoordinates()).to.eql([[4, 6], [7, 10]]);
expect(geoms[0].getType()).to.eql(ol.geom.GeometryType.POINT);
expect(geoms[1].getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(geoms[0].getCoordinates()).to.eql([4, 6]);
expect(geoms[1].getCoordinates()).to.eql([[4, 6], [7, 10]]);
});
});