Skip to content

Commit 4abc887

Browse files
committed
Encode empty geometries as WKT strings
1 parent fe8a72d commit 4abc887

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

src/ol/format/wktformat.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ol.format.WKT = function(opt_options) {
2929
goog.base(this);
3030

3131
/**
32-
* Split GEOMETRYCOLLECTION into multiple features.
32+
* Split GeometryCollection into multiple features.
3333
* @type {boolean}
3434
* @private
3535
*/
@@ -40,13 +40,23 @@ ol.format.WKT = function(opt_options) {
4040
goog.inherits(ol.format.WKT, ol.format.TextFeature);
4141

4242

43+
/**
44+
* @const
45+
* @type {string}
46+
*/
47+
ol.format.WKT.EMPTY = 'EMPTY';
48+
49+
4350
/**
4451
* @param {ol.geom.Point} geom Point geometry.
4552
* @return {string} Coordinates part of Point as WKT.
4653
* @private
4754
*/
4855
ol.format.WKT.encodePointGeometry_ = function(geom) {
4956
var coordinates = geom.getCoordinates();
57+
if (goog.array.isEmpty(coordinates)) {
58+
return '';
59+
}
5060
return coordinates[0] + ' ' + coordinates[1];
5161
};
5262

@@ -154,7 +164,12 @@ ol.format.WKT.encode_ = function(geom) {
154164
var type = geom.getType();
155165
var geometryEncoder = ol.format.WKT.GeometryEncoder_[type];
156166
goog.asserts.assert(goog.isDef(geometryEncoder));
157-
return type.toUpperCase() + '(' + geometryEncoder(geom) + ')';
167+
var enc = geometryEncoder(geom);
168+
type = type.toUpperCase();
169+
if (enc.length === 0) {
170+
return type + ' ' + ol.format.WKT.EMPTY;
171+
}
172+
return type + '(' + enc + ')';
158173
};
159174

160175

@@ -479,7 +494,7 @@ ol.format.WKT.Lexer.prototype.readText_ = function() {
479494
do {
480495
c = this.nextChar_();
481496
} while (this.isAlpha_(c));
482-
return this.wkt.substring(index, this.index_--).toLowerCase();
497+
return this.wkt.substring(index, this.index_--).toUpperCase();
483498
};
484499

485500

@@ -555,7 +570,7 @@ ol.format.WKT.Parser.prototype.parseGeometry_ = function() {
555570
var token = this.token_;
556571
if (this.match(ol.format.WKT.TokenType.TEXT)) {
557572
var geomType = token.value;
558-
if (geomType == 'geometrycollection') {
573+
if (geomType == ol.geom.GeometryType.GEOMETRY_COLLECTION.toUpperCase()) {
559574
var geometries = this.parseGeometryCollectionText_();
560575
return new ol.geom.GeometryCollection(geometries);
561576
} else {
@@ -779,7 +794,7 @@ ol.format.WKT.Parser.prototype.parsePolygonTextList_ = function() {
779794
*/
780795
ol.format.WKT.Parser.prototype.isEmptyGeometry_ = function() {
781796
var isEmpty = this.token_.type == ol.format.WKT.TokenType.TEXT &&
782-
this.token_.value == 'empty';
797+
this.token_.value == ol.format.WKT.EMPTY;
783798
if (isEmpty) {
784799
this.consume_();
785800
}
@@ -802,12 +817,12 @@ ol.format.WKT.Parser.prototype.raiseError_ = function() {
802817
* @private
803818
*/
804819
ol.format.WKT.Parser.GeometryConstructor_ = {
805-
'point': ol.geom.Point,
806-
'linestring': ol.geom.LineString,
807-
'polygon': ol.geom.Polygon,
808-
'multipoint': ol.geom.MultiPoint,
809-
'multilinestring': ol.geom.MultiLineString,
810-
'multipolygon': ol.geom.MultiPolygon
820+
'POINT': ol.geom.Point,
821+
'LINESTRING': ol.geom.LineString,
822+
'POLYGON': ol.geom.Polygon,
823+
'MULTIPOINT': ol.geom.MultiPoint,
824+
'MULTILINESTRING': ol.geom.MultiLineString,
825+
'MULTIPOLYGON': ol.geom.MultiPolygon
811826
};
812827

813828

@@ -816,10 +831,10 @@ ol.format.WKT.Parser.GeometryConstructor_ = {
816831
* @private
817832
*/
818833
ol.format.WKT.Parser.GeometryParser_ = {
819-
'point': ol.format.WKT.Parser.prototype.parsePointText_,
820-
'linestring': ol.format.WKT.Parser.prototype.parseLineStringText_,
821-
'polygon': ol.format.WKT.Parser.prototype.parsePolygonText_,
822-
'multipoint': ol.format.WKT.Parser.prototype.parseMultiPointText_,
823-
'multilinestring': ol.format.WKT.Parser.prototype.parseMultiLineStringText_,
824-
'multipolygon': ol.format.WKT.Parser.prototype.parseMultiPolygonText_
834+
'POINT': ol.format.WKT.Parser.prototype.parsePointText_,
835+
'LINESTRING': ol.format.WKT.Parser.prototype.parseLineStringText_,
836+
'POLYGON': ol.format.WKT.Parser.prototype.parsePolygonText_,
837+
'MULTIPOINT': ol.format.WKT.Parser.prototype.parseMultiPointText_,
838+
'MULTILINESTRING': ol.format.WKT.Parser.prototype.parseMultiLineStringText_,
839+
'MULTIPOLYGON': ol.format.WKT.Parser.prototype.parseMultiPolygonText_
825840
};

0 commit comments

Comments
 (0)