Refactor ol.format.GeoJSON to implement ol.format.IReader

This commit is contained in:
Tom Payne
2013-11-09 00:02:26 +01:00
parent 2d7c26e3e5
commit 5bca792288
3 changed files with 17 additions and 22 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ var styleFunction = function(feature) {
}; };
var vectorSource = new ol.source.Vector(); var vectorSource = new ol.source.Vector();
ol.format.GeoJSON.readObject({ new ol.format.GeoJSON().readObject({
'type': 'FeatureCollection', 'type': 'FeatureCollection',
'features': [ 'features': [
{ {
+10 -17
View File
@@ -7,6 +7,7 @@ goog.provide('ol.format.GeoJSON');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.json'); goog.require('goog.json');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.format.IReader');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString'); goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.MultiPolygon');
@@ -17,6 +18,7 @@ goog.require('ol.geom.Polygon');
/** /**
* @constructor * @constructor
* @implements {ol.format.IReader}
*/ */
ol.format.GeoJSON = function() { ol.format.GeoJSON = function() {
}; };
@@ -124,30 +126,21 @@ ol.format.GeoJSON.readFeatureCollection_ = function(object, callback, opt_obj) {
/** /**
* @param {GeoJSONObject} object Object. * @inheritDoc
* @param {function(this: S, ol.Feature): T} callback Callback.
* @param {S=} opt_obj Scope.
* @return {T} Callback result.
* @template S,T
*/ */
ol.format.GeoJSON.readObject = function(object, callback, opt_obj) { ol.format.GeoJSON.prototype.readObject = function(object, callback, opt_obj) {
var objectReader = ol.format.GeoJSON.OBJECT_READERS_[object.type]; var geoJSONObject = /** @type {GeoJSONObject} */ (object);
var objectReader = ol.format.GeoJSON.OBJECT_READERS_[geoJSONObject.type];
goog.asserts.assert(goog.isDef(objectReader)); goog.asserts.assert(goog.isDef(objectReader));
return objectReader(object, callback, opt_obj); return objectReader(geoJSONObject, callback, opt_obj);
}; };
/** /**
* @param {string} string String. * @inheritDoc
* @param {function(ol.Feature): T} callback Callback.
* @param {S=} opt_obj Scope.
* @return {T} Callback result.
* @template S,T
*/ */
ol.format.GeoJSON.readString = function(string, callback, opt_obj) { ol.format.GeoJSON.prototype.readString = function(string, callback, opt_obj) {
var object = goog.json.parse(string); return this.readObject(goog.json.parse(string), callback, opt_obj);
return ol.format.GeoJSON.readObject(
/** @type {GeoJSONObject} */ (object), callback);
}; };
+6 -4
View File
@@ -47,10 +47,12 @@ describe('ol.format.GeoJSON', function() {
'features': [pointGeoJSON, lineStringGeoJSON, polygonGeoJSON] 'features': [pointGeoJSON, lineStringGeoJSON, polygonGeoJSON]
}; };
var format = new ol.format.GeoJSON();
describe('readObject', function() { describe('readObject', function() {
it('can read a single point feature', function() { it('can read a single point feature', function() {
var feature = ol.format.GeoJSON.readObject(pointGeoJSON, function(f) { var feature = format.readObject(pointGeoJSON, function(f) {
return f; return f;
}); });
expect(feature).to.be.an(ol.Feature); expect(feature).to.be.an(ol.Feature);
@@ -61,7 +63,7 @@ describe('ol.format.GeoJSON', function() {
}); });
it('can read a single line string feature', function() { it('can read a single line string feature', function() {
var feature = ol.format.GeoJSON.readObject(lineStringGeoJSON, var feature = format.readObject(lineStringGeoJSON,
function(f) { function(f) {
return f; return f;
}); });
@@ -75,7 +77,7 @@ describe('ol.format.GeoJSON', function() {
}); });
it('can read a single polygon feature', function() { it('can read a single polygon feature', function() {
var feature = ol.format.GeoJSON.readObject(polygonGeoJSON, function(f) { var feature = format.readObject(polygonGeoJSON, function(f) {
return f; return f;
}); });
expect(feature).to.be.an(ol.Feature); expect(feature).to.be.an(ol.Feature);
@@ -90,7 +92,7 @@ describe('ol.format.GeoJSON', function() {
it('can read a feature collection', function() { it('can read a feature collection', function() {
var features = []; var features = [];
ol.format.GeoJSON.readObject(featureCollectionGeoJSON, function(f) { format.readObject(featureCollectionGeoJSON, function(f) {
features.push(f); features.push(f);
}); });
expect(features).to.have.length(3); expect(features).to.have.length(3);