Read crs from top-level object when parsing as features

This commit is contained in:
Tim Schaub
2013-08-25 12:32:22 -06:00
parent 379c11e1ce
commit 7e2bd66417
3 changed files with 114 additions and 18 deletions

View File

@@ -3,6 +3,7 @@ goog.provide('ol.parser.AsyncStringFeatureParser');
goog.provide('ol.parser.DomFeatureParser');
goog.provide('ol.parser.ObjectFeatureParser');
goog.provide('ol.parser.ReadFeaturesOptions');
goog.provide('ol.parser.ReadFeaturesResult');
goog.provide('ol.parser.StringFeatureParser');
goog.require('ol.Feature');

View File

@@ -15,6 +15,7 @@ goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.Parser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.ReadFeaturesResult');
goog.require('ol.parser.StringFeatureParser');
@@ -63,8 +64,7 @@ ol.parser.GeoJSON.read = function(str) {
ol.parser.GeoJSON.prototype.readFeaturesFromString =
function(str, opt_options) {
var json = /** @type {GeoJSONFeatureCollection} */ (JSON.parse(str));
return {features: this.parseAsFeatureCollection_(json, opt_options),
metadata: {projection: 'EPSG:4326'}};
return this.parseAsFeatureCollection_(json, opt_options);
};
@@ -77,8 +77,7 @@ ol.parser.GeoJSON.prototype.readFeaturesFromString =
*/
ol.parser.GeoJSON.prototype.readFeaturesFromObject =
function(object, opt_options) {
return {features: this.parseAsFeatureCollection_(object, opt_options),
metadata: {projection: 'EPSG:4326'}};
return this.parseAsFeatureCollection_(object, opt_options);
};
@@ -119,7 +118,8 @@ ol.parser.GeoJSON.prototype.parse_ = function(json, opt_options) {
/**
* @param {GeoJSONObject} json GeoJSON object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {Array.<ol.Feature>} Parsed object coerced into array of features.
* @return {ol.parser.ReadFeaturesResult} Parsed object coerced into array of
* features.
* @private
*/
ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json,
@@ -149,7 +149,14 @@ ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json,
}
}
}
return features;
var projection = 'EPSG:4326';
if (json.hasOwnProperty('crs')) {
var crs = /** GeoJSONCRS */ (json.crs);
if (crs.type === 'name') {
projection = (/** GeoJSONCRSName */ (crs.properties)).name;
}
}
return {features: features, metadata: {projection: projection}};
};

View File

@@ -269,7 +269,7 @@ describe('ol.parser.GeoJSON', function() {
describe('#parseAsFeatureCollection_()', function() {
it('returns an array of features for FeatureCollection', function() {
it('generates an array of features for FeatureCollection', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
@@ -310,8 +310,9 @@ describe('ol.parser.GeoJSON', function() {
}
}]
};
var features = parser.parseAsFeatureCollection_(json,
var result = parser.parseAsFeatureCollection_(json,
{callback: callback});
var features = result.features;
expect(features.length).to.be(2);
@@ -328,9 +329,81 @@ describe('ol.parser.GeoJSON', function() {
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('returns an array of features for Feature', function() {
it('reads named crs from top-level object', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON();
var json = {
type: 'FeatureCollection',
crs: {
type: 'name',
properties: {
name: 'EPSG:1234'
}
},
features: [{
type: 'Feature',
properties: {
foo: 'bar'
},
geometry: {
type: 'Point',
coordinates: [1, 2]
}
}, {
type: 'Feature',
properties: {
bam: 'baz'
},
geometry: {
type: 'LineString',
coordinates: [[1, 2], [3, 4]]
}
}]
};
var result = parser.parseAsFeatureCollection_(json,
{callback: callback});
var features = result.features;
expect(features.length).to.be(2);
var first = features[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('foo')).to.be('bar');
expect(first.getGeometry()).to.be.a(ol.geom.Point);
var second = features[1];
expect(second).to.be.a(ol.Feature);
expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:1234');
});
it('generates an array of features for Feature', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
@@ -359,8 +432,9 @@ describe('ol.parser.GeoJSON', function() {
coordinates: [[1, 2], [3, 4]]
}
};
var features = parser.parseAsFeatureCollection_(json,
var result = parser.parseAsFeatureCollection_(json,
{callback: callback});
var features = result.features;
expect(features.length).to.be(1);
@@ -372,9 +446,11 @@ describe('ol.parser.GeoJSON', function() {
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('returns an array of features for GeometryCollection', function() {
it('generates an array of features for GeometryCollection', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
@@ -406,8 +482,9 @@ describe('ol.parser.GeoJSON', function() {
coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]]
}]
};
var features = parser.parseAsFeatureCollection_(json,
var result = parser.parseAsFeatureCollection_(json,
{callback: callback});
var features = result.features;
expect(features.length).to.be(3);
@@ -418,9 +495,11 @@ describe('ol.parser.GeoJSON', function() {
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(8);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('returns an array of features for Point', function() {
it('generates an array of features for Point', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
@@ -443,8 +522,9 @@ describe('ol.parser.GeoJSON', function() {
type: 'Point',
coordinates: [1, 2]
};
var features = parser.parseAsFeatureCollection_(json,
var result = parser.parseAsFeatureCollection_(json,
{callback: callback});
var features = result.features;
expect(features.length).to.be(1);
@@ -453,9 +533,11 @@ describe('ol.parser.GeoJSON', function() {
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('returns an array of features for LineString', function() {
it('generates an array of features for LineString', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
@@ -478,8 +560,9 @@ describe('ol.parser.GeoJSON', function() {
type: 'LineString',
coordinates: [[3, 4], [5, 6]]
};
var features = parser.parseAsFeatureCollection_(json,
var result = parser.parseAsFeatureCollection_(json,
{callback: callback});
var features = result.features;
expect(features.length).to.be(1);
@@ -488,9 +571,11 @@ describe('ol.parser.GeoJSON', function() {
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('returns an array of features for Polygon', function() {
it('generates an array of features for Polygon', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
@@ -513,8 +598,9 @@ describe('ol.parser.GeoJSON', function() {
type: 'Polygon',
coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]]
};
var features = parser.parseAsFeatureCollection_(json,
var result = parser.parseAsFeatureCollection_(json,
{callback: callback});
var features = result.features;
expect(features.length).to.be(1);
@@ -523,6 +609,8 @@ describe('ol.parser.GeoJSON', function() {
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(8);
expect(result.metadata.projection).to.be('EPSG:4326');
});