From 8eef633abc95f94c2ce561cd965cc822bc275b65 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Wed, 21 May 2014 16:41:35 +0200 Subject: [PATCH] Ability to specify geometryName on ol.format.GeoJSON --- externs/olx.js | 10 +++++++++- src/ol/format/geojsonformat.js | 14 +++++++++++++- test/spec/ol/format/geojsonformat.test.js | 7 +++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index 820674337e..c642690383 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -1104,7 +1104,8 @@ olx.control.ZoomToExtentOptions.prototype.extent; /** - * @typedef {{defaultProjection: ol.proj.ProjectionLike}} + * @typedef {{defaultProjection: ol.proj.ProjectionLike, + * geometryName: (string|undefined)}} * @todo api */ olx.format.GeoJSONOptions; @@ -1117,6 +1118,13 @@ olx.format.GeoJSONOptions; olx.format.GeoJSONOptions.prototype.defaultProjection; +/** + * Geometry name to use when creating features. + * @type {string|undefined} + */ +olx.format.GeoJSONOptions.prototype.geometryName; + + /** * @typedef {{defaultProjection: ol.proj.ProjectionLike}} * @todo api diff --git a/src/ol/format/geojsonformat.js b/src/ol/format/geojsonformat.js index 66ca9075cb..c0785dbfaf 100644 --- a/src/ol/format/geojsonformat.js +++ b/src/ol/format/geojsonformat.js @@ -41,6 +41,14 @@ ol.format.GeoJSON = function(opt_options) { this.defaultProjection_ = ol.proj.get(options.defaultProjection ? options.defaultProjection : 'EPSG:4326'); + + /** + * Name of the geometry attribute for features. + * @type {string|undefined} + * @private + */ + this.geometryName_ = options.geometryName; + }; goog.inherits(ol.format.GeoJSON, ol.format.JSONFeature); @@ -346,7 +354,11 @@ ol.format.GeoJSON.prototype.readFeatureFromObject = function(object) { var geoJSONFeature = /** @type {GeoJSONFeature} */ (object); goog.asserts.assert(geoJSONFeature.type == 'Feature'); var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry); - var feature = new ol.Feature(geometry); + var feature = new ol.Feature(); + if (goog.isDef(this.geometryName_)) { + feature.setGeometryName(this.geometryName_); + } + feature.setGeometry(geometry); if (goog.isDef(geoJSONFeature.id)) { feature.setId(geoJSONFeature.id); } diff --git a/test/spec/ol/format/geojsonformat.test.js b/test/spec/ol/format/geojsonformat.test.js index 52cfb3fc69..60e11ea4de 100644 --- a/test/spec/ol/format/geojsonformat.test.js +++ b/test/spec/ol/format/geojsonformat.test.js @@ -174,6 +174,13 @@ describe('ol.format.GeoJSON', function() { expect(features[2].getGeometry()).to.be.an(ol.geom.Polygon); }); + it('can create a feature with a specific geometryName', function() { + var feature = new ol.format.GeoJSON({geometryName: 'the_geom'}). + readFeature(pointGeoJSON); + expect(feature.getGeometryName()).to.be('the_geom'); + expect(feature.getGeometry()).to.be.an(ol.geom.Point); + }); + }); describe('#readFeatures', function() {