Refactor ol.format.GeoJSON to implement ol.format.IReader
This commit is contained in:
@@ -44,7 +44,7 @@ var styleFunction = function(feature) {
|
||||
};
|
||||
|
||||
var vectorSource = new ol.source.Vector();
|
||||
ol.format.GeoJSON.readObject({
|
||||
new ol.format.GeoJSON().readObject({
|
||||
'type': 'FeatureCollection',
|
||||
'features': [
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@ goog.provide('ol.format.GeoJSON');
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.json');
|
||||
goog.require('ol.Feature');
|
||||
goog.require('ol.format.IReader');
|
||||
goog.require('ol.geom.LineString');
|
||||
goog.require('ol.geom.MultiLineString');
|
||||
goog.require('ol.geom.MultiPolygon');
|
||||
@@ -17,6 +18,7 @@ goog.require('ol.geom.Polygon');
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @implements {ol.format.IReader}
|
||||
*/
|
||||
ol.format.GeoJSON = function() {
|
||||
};
|
||||
@@ -124,30 +126,21 @@ ol.format.GeoJSON.readFeatureCollection_ = function(object, callback, opt_obj) {
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONObject} object Object.
|
||||
* @param {function(this: S, ol.Feature): T} callback Callback.
|
||||
* @param {S=} opt_obj Scope.
|
||||
* @return {T} Callback result.
|
||||
* @template S,T
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.readObject = function(object, callback, opt_obj) {
|
||||
var objectReader = ol.format.GeoJSON.OBJECT_READERS_[object.type];
|
||||
ol.format.GeoJSON.prototype.readObject = function(object, callback, opt_obj) {
|
||||
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
|
||||
var objectReader = ol.format.GeoJSON.OBJECT_READERS_[geoJSONObject.type];
|
||||
goog.asserts.assert(goog.isDef(objectReader));
|
||||
return objectReader(object, callback, opt_obj);
|
||||
return objectReader(geoJSONObject, callback, opt_obj);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} string String.
|
||||
* @param {function(ol.Feature): T} callback Callback.
|
||||
* @param {S=} opt_obj Scope.
|
||||
* @return {T} Callback result.
|
||||
* @template S,T
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.readString = function(string, callback, opt_obj) {
|
||||
var object = goog.json.parse(string);
|
||||
return ol.format.GeoJSON.readObject(
|
||||
/** @type {GeoJSONObject} */ (object), callback);
|
||||
ol.format.GeoJSON.prototype.readString = function(string, callback, opt_obj) {
|
||||
return this.readObject(goog.json.parse(string), callback, opt_obj);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -47,10 +47,12 @@ describe('ol.format.GeoJSON', function() {
|
||||
'features': [pointGeoJSON, lineStringGeoJSON, polygonGeoJSON]
|
||||
};
|
||||
|
||||
var format = new ol.format.GeoJSON();
|
||||
|
||||
describe('readObject', 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;
|
||||
});
|
||||
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() {
|
||||
var feature = ol.format.GeoJSON.readObject(lineStringGeoJSON,
|
||||
var feature = format.readObject(lineStringGeoJSON,
|
||||
function(f) {
|
||||
return f;
|
||||
});
|
||||
@@ -75,7 +77,7 @@ describe('ol.format.GeoJSON', 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;
|
||||
});
|
||||
expect(feature).to.be.an(ol.Feature);
|
||||
@@ -90,7 +92,7 @@ describe('ol.format.GeoJSON', function() {
|
||||
|
||||
it('can read a feature collection', function() {
|
||||
var features = [];
|
||||
ol.format.GeoJSON.readObject(featureCollectionGeoJSON, function(f) {
|
||||
format.readObject(featureCollectionGeoJSON, function(f) {
|
||||
features.push(f);
|
||||
});
|
||||
expect(features).to.have.length(3);
|
||||
|
||||
Reference in New Issue
Block a user