Merge pull request #5944 from tschaub/geojson-as-features
Allow readFeature and readFeatures to be called with GeoJSON geometries
This commit is contained in:
@@ -357,8 +357,9 @@ ol.format.GeoJSON.prototype.getExtensions = function() {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a feature from a GeoJSON Feature source. Only works for Feature,
|
* Read a feature from a GeoJSON Feature source. Only works for Feature or
|
||||||
* use `readFeatures` to read FeatureCollection source.
|
* geometry types. Use {@link ol.format.GeoJSON#readFeatures} to read
|
||||||
|
* FeatureCollection source.
|
||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {Document|Node|Object|string} source Source.
|
* @param {Document|Node|Object|string} source Source.
|
||||||
@@ -370,8 +371,9 @@ ol.format.GeoJSON.prototype.readFeature;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read all features from a GeoJSON source. Works with both Feature and
|
* Read all features from a GeoJSON source. Works for all GeoJSON types.
|
||||||
* FeatureCollection sources.
|
* If the source includes only geometries, features will be created with those
|
||||||
|
* geometries.
|
||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {Document|Node|Object|string} source Source.
|
* @param {Document|Node|Object|string} source Source.
|
||||||
@@ -387,11 +389,23 @@ ol.format.GeoJSON.prototype.readFeatures;
|
|||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.prototype.readFeatureFromObject = function(
|
ol.format.GeoJSON.prototype.readFeatureFromObject = function(
|
||||||
object, opt_options) {
|
object, opt_options) {
|
||||||
var geoJSONFeature = /** @type {GeoJSONFeature} */ (object);
|
|
||||||
ol.DEBUG && console.assert(geoJSONFeature.type == 'Feature',
|
ol.DEBUG && console.assert(object.type !== 'FeatureCollection', 'Expected a Feature or geometry');
|
||||||
'geoJSONFeature.type should be Feature');
|
|
||||||
var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry,
|
/**
|
||||||
opt_options);
|
* @type {GeoJSONFeature}
|
||||||
|
*/
|
||||||
|
var geoJSONFeature = null;
|
||||||
|
if (object.type === 'Feature') {
|
||||||
|
geoJSONFeature = /** @type {GeoJSONFeature} */ (object);
|
||||||
|
} else {
|
||||||
|
geoJSONFeature = /** @type {GeoJSONFeature} */ ({
|
||||||
|
type: 'Feature',
|
||||||
|
geometry: /** @type {GeoJSONGeometry|GeoJSONGeometryCollection} */ (object)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry, opt_options);
|
||||||
var feature = new ol.Feature();
|
var feature = new ol.Feature();
|
||||||
if (this.geometryName_) {
|
if (this.geometryName_) {
|
||||||
feature.setGeometryName(this.geometryName_);
|
feature.setGeometryName(this.geometryName_);
|
||||||
@@ -414,10 +428,8 @@ ol.format.GeoJSON.prototype.readFeaturesFromObject = function(
|
|||||||
object, opt_options) {
|
object, opt_options) {
|
||||||
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
|
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
|
||||||
/** @type {Array.<ol.Feature>} */
|
/** @type {Array.<ol.Feature>} */
|
||||||
var features;
|
var features = null;
|
||||||
if (geoJSONObject.type == 'Feature') {
|
if (geoJSONObject.type === 'FeatureCollection') {
|
||||||
features = [this.readFeatureFromObject(object, opt_options)];
|
|
||||||
} else if (geoJSONObject.type == 'FeatureCollection') {
|
|
||||||
var geoJSONFeatureCollection = /** @type {GeoJSONFeatureCollection} */
|
var geoJSONFeatureCollection = /** @type {GeoJSONFeatureCollection} */
|
||||||
(object);
|
(object);
|
||||||
features = [];
|
features = [];
|
||||||
@@ -428,9 +440,9 @@ ol.format.GeoJSON.prototype.readFeaturesFromObject = function(
|
|||||||
opt_options));
|
opt_options));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ol.asserts.assert(false, 35); // Unknown GeoJSON object type
|
features = [this.readFeatureFromObject(object, opt_options)];
|
||||||
}
|
}
|
||||||
return /** Array.<ol.Feature> */ (features);
|
return features;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -154,6 +154,14 @@ describe('ol.format.GeoJSON', function() {
|
|||||||
expect(feature.get('prop0')).to.be('value0');
|
expect(feature.get('prop0')).to.be('value0');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can read a single point geometry as a feature', function() {
|
||||||
|
var feature = format.readFeature(pointGeoJSON.geometry);
|
||||||
|
expect(feature).to.be.an(ol.Feature);
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
expect(geometry).to.be.an(ol.geom.Point);
|
||||||
|
expect(geometry.getCoordinates()).to.eql([102.0, 0.5]);
|
||||||
|
});
|
||||||
|
|
||||||
it('can read a single line string feature', function() {
|
it('can read a single line string feature', function() {
|
||||||
var feature = format.readFeature(lineStringGeoJSON);
|
var feature = format.readFeature(lineStringGeoJSON);
|
||||||
expect(feature).to.be.an(ol.Feature);
|
expect(feature).to.be.an(ol.Feature);
|
||||||
@@ -277,6 +285,14 @@ describe('ol.format.GeoJSON', function() {
|
|||||||
expect(secondGeom).to.be.a(ol.geom.LineString);
|
expect(secondGeom).to.be.a(ol.geom.LineString);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can parse a polygon geometry as an array of one feature', function() {
|
||||||
|
var features = format.readFeatures(polygonGeoJSON);
|
||||||
|
expect(features).to.be.an(Array);
|
||||||
|
expect(features).to.have.length(1);
|
||||||
|
var geometry = features[0].getGeometry();
|
||||||
|
expect(geometry).to.be.an(ol.geom.Polygon);
|
||||||
|
});
|
||||||
|
|
||||||
it('parses countries.geojson', function(done) {
|
it('parses countries.geojson', function(done) {
|
||||||
afterLoadText('spec/ol/format/geojson/countries.geojson', function(text) {
|
afterLoadText('spec/ol/format/geojson/countries.geojson', function(text) {
|
||||||
var result = format.readFeatures(text);
|
var result = format.readFeatures(text);
|
||||||
|
|||||||
Reference in New Issue
Block a user