Add ol.geom.GeometryCollection support to ol.format.GeoJSON
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
// FIXME coordinate order
|
// FIXME coordinate order
|
||||||
// FIXME reprojection
|
// FIXME reprojection
|
||||||
// FIXME GeometryCollection
|
|
||||||
|
|
||||||
goog.provide('ol.format.GeoJSON');
|
goog.provide('ol.format.GeoJSON');
|
||||||
|
|
||||||
|
goog.require('goog.array');
|
||||||
goog.require('goog.asserts');
|
goog.require('goog.asserts');
|
||||||
goog.require('goog.object');
|
goog.require('goog.object');
|
||||||
goog.require('ol.Feature');
|
goog.require('ol.Feature');
|
||||||
goog.require('ol.format.JSON');
|
goog.require('ol.format.JSON');
|
||||||
|
goog.require('ol.geom.GeometryCollection');
|
||||||
goog.require('ol.geom.LineString');
|
goog.require('ol.geom.LineString');
|
||||||
goog.require('ol.geom.MultiLineString');
|
goog.require('ol.geom.MultiLineString');
|
||||||
goog.require('ol.geom.MultiPoint');
|
goog.require('ol.geom.MultiPoint');
|
||||||
@@ -41,7 +42,7 @@ goog.inherits(ol.format.GeoJSON, ol.format.JSON);
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {GeoJSONGeometry} object Object.
|
* @param {GeoJSONObject} object Object.
|
||||||
* @private
|
* @private
|
||||||
* @return {ol.geom.Geometry} Geometry.
|
* @return {ol.geom.Geometry} Geometry.
|
||||||
*/
|
*/
|
||||||
@@ -52,6 +53,19 @@ ol.format.GeoJSON.readGeometry_ = function(object) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONGeometryCollection} object Object.
|
||||||
|
* @private
|
||||||
|
* @return {ol.geom.GeometryCollection} Geometry collection.
|
||||||
|
*/
|
||||||
|
ol.format.GeoJSON.readGeometryCollectionGeometry_ = function(object) {
|
||||||
|
goog.asserts.assert(object.type == 'GeometryCollection');
|
||||||
|
var geometries = goog.array.map(
|
||||||
|
object.geometries, ol.format.GeoJSON.readGeometry_);
|
||||||
|
return new ol.geom.GeometryCollection(geometries);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {GeoJSONGeometry} object Object.
|
* @param {GeoJSONGeometry} object Object.
|
||||||
* @private
|
* @private
|
||||||
@@ -121,7 +135,7 @@ ol.format.GeoJSON.readPolygonGeometry_ = function(object) {
|
|||||||
/**
|
/**
|
||||||
* @param {ol.geom.Geometry} geometry Geometry.
|
* @param {ol.geom.Geometry} geometry Geometry.
|
||||||
* @private
|
* @private
|
||||||
* @return {GeoJSONGeometry} GeoJSON geometry.
|
* @return {GeoJSONObject} GeoJSON geometry.
|
||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.writeGeometry_ = function(geometry) {
|
ol.format.GeoJSON.writeGeometry_ = function(geometry) {
|
||||||
var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()];
|
var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()];
|
||||||
@@ -130,6 +144,22 @@ ol.format.GeoJSON.writeGeometry_ = function(geometry) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ol.geom.Geometry} geometry Geometry.
|
||||||
|
* @private
|
||||||
|
* @return {GeoJSONGeometryCollection} GeoJSON geometry collection.
|
||||||
|
*/
|
||||||
|
ol.format.GeoJSON.writeGeometryCollectionGeometry_ = function(geometry) {
|
||||||
|
goog.asserts.assertInstanceof(geometry, ol.geom.GeometryCollection);
|
||||||
|
var geometries = goog.array.map(
|
||||||
|
geometry.getGeometriesArray(), ol.format.GeoJSON.writeGeometry_);
|
||||||
|
return /** @type {GeoJSONGeometryCollection} */ ({
|
||||||
|
'type': 'GeometryCollection',
|
||||||
|
'geometries': geometries
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ol.geom.Geometry} geometry Geometry.
|
* @param {ol.geom.Geometry} geometry Geometry.
|
||||||
* @private
|
* @private
|
||||||
@@ -219,7 +249,7 @@ ol.format.GeoJSON.writePolygonGeometry_ = function(geometry) {
|
|||||||
/**
|
/**
|
||||||
* @const
|
* @const
|
||||||
* @private
|
* @private
|
||||||
* @type {Object.<string, function(GeoJSONGeometry): ol.geom.Geometry>}
|
* @type {Object.<string, function(GeoJSONObject): ol.geom.Geometry>}
|
||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.GEOMETRY_READERS_ = {
|
ol.format.GeoJSON.GEOMETRY_READERS_ = {
|
||||||
'Point': ol.format.GeoJSON.readPointGeometry_,
|
'Point': ol.format.GeoJSON.readPointGeometry_,
|
||||||
@@ -227,14 +257,15 @@ ol.format.GeoJSON.GEOMETRY_READERS_ = {
|
|||||||
'Polygon': ol.format.GeoJSON.readPolygonGeometry_,
|
'Polygon': ol.format.GeoJSON.readPolygonGeometry_,
|
||||||
'MultiPoint': ol.format.GeoJSON.readMultiPointGeometry_,
|
'MultiPoint': ol.format.GeoJSON.readMultiPointGeometry_,
|
||||||
'MultiLineString': ol.format.GeoJSON.readMultiLineStringGeometry_,
|
'MultiLineString': ol.format.GeoJSON.readMultiLineStringGeometry_,
|
||||||
'MultiPolygon': ol.format.GeoJSON.readMultiPolygonGeometry_
|
'MultiPolygon': ol.format.GeoJSON.readMultiPolygonGeometry_,
|
||||||
|
'GeometryCollection': ol.format.GeoJSON.readGeometryCollectionGeometry_
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @const
|
* @const
|
||||||
* @private
|
* @private
|
||||||
* @type {Object.<string, function(ol.geom.Geometry): GeoJSONGeometry>}
|
* @type {Object.<string, function(ol.geom.Geometry): GeoJSONObject>}
|
||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.GEOMETRY_WRITERS_ = {
|
ol.format.GeoJSON.GEOMETRY_WRITERS_ = {
|
||||||
'Point': ol.format.GeoJSON.writePointGeometry_,
|
'Point': ol.format.GeoJSON.writePointGeometry_,
|
||||||
@@ -242,7 +273,8 @@ ol.format.GeoJSON.GEOMETRY_WRITERS_ = {
|
|||||||
'Polygon': ol.format.GeoJSON.writePolygonGeometry_,
|
'Polygon': ol.format.GeoJSON.writePolygonGeometry_,
|
||||||
'MultiPoint': ol.format.GeoJSON.writeMultiPointGeometry_,
|
'MultiPoint': ol.format.GeoJSON.writeMultiPointGeometry_,
|
||||||
'MultiLineString': ol.format.GeoJSON.writeMultiLineStringGeometry_,
|
'MultiLineString': ol.format.GeoJSON.writeMultiLineStringGeometry_,
|
||||||
'MultiPolygon': ol.format.GeoJSON.writeMultiPolygonGeometry_
|
'MultiPolygon': ol.format.GeoJSON.writeMultiPolygonGeometry_,
|
||||||
|
'GeometryCollection': ol.format.GeoJSON.writeGeometryCollectionGeometry_
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -279,7 +279,7 @@ describe('ol.format.GeoJSON', function() {
|
|||||||
expect(rings[2]).to.be.a(ol.geom.LinearRing);
|
expect(rings[2]).to.be.a(ol.geom.LinearRing);
|
||||||
});
|
});
|
||||||
|
|
||||||
it.skip('parses geometry collection', function() {
|
it('parses geometry collection', function() {
|
||||||
var str = JSON.stringify({
|
var str = JSON.stringify({
|
||||||
type: 'GeometryCollection',
|
type: 'GeometryCollection',
|
||||||
geometries: [
|
geometries: [
|
||||||
@@ -288,7 +288,9 @@ describe('ol.format.GeoJSON', function() {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
var array = format.readGeometry(str);
|
var geometryCollection = format.readGeometry(str);
|
||||||
|
expect(geometryCollection).to.be.an(ol.geom.GeometryCollection);
|
||||||
|
var array = geometryCollection.getGeometries();
|
||||||
expect(array.length).to.be(2);
|
expect(array.length).to.be(2);
|
||||||
expect(array[0]).to.be.a(ol.geom.Point);
|
expect(array[0]).to.be.a(ol.geom.Point);
|
||||||
expect(array[1]).to.be.a(ol.geom.LineString);
|
expect(array[1]).to.be.a(ol.geom.LineString);
|
||||||
@@ -441,17 +443,20 @@ describe('ol.format.GeoJSON', function() {
|
|||||||
format.readGeometry(geojson).getCoordinates());
|
format.readGeometry(geojson).getCoordinates());
|
||||||
});
|
});
|
||||||
|
|
||||||
it.skip('encodes geometry collection', function() {
|
it('encodes geometry collection', function() {
|
||||||
var collection = new ol.geom.GeometryCollection([
|
var collection = new ol.geom.GeometryCollection([
|
||||||
new ol.geom.Point([10, 20]),
|
new ol.geom.Point([10, 20]),
|
||||||
new ol.geom.LineString([[30, 40], [50, 60]])
|
new ol.geom.LineString([[30, 40], [50, 60]])
|
||||||
]);
|
]);
|
||||||
var geojson = format.writeGeometry(collection);
|
var geojson = format.writeGeometry(collection);
|
||||||
var got = format.readGeometry(geojson);
|
var got = format.readGeometry(geojson);
|
||||||
var components = collection.getComponents();
|
expect(got).to.be.an(ol.geom.GeometryCollection);
|
||||||
expect(components.length).to.equal(got.length);
|
var gotGeometries = got.getGeometries();
|
||||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
var geometries = collection.getGeometries();
|
||||||
expect(components[i].getCoordinates()).to.eql(got[i].getCoordinates());
|
expect(geometries.length).to.equal(gotGeometries.length);
|
||||||
|
for (var i = 0, ii = geometries.length; i < ii; ++i) {
|
||||||
|
expect(geometries[i].getCoordinates()).
|
||||||
|
to.eql(gotGeometries[i].getCoordinates());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -463,6 +468,7 @@ describe('ol.format.GeoJSON', function() {
|
|||||||
goog.require('ol.Feature');
|
goog.require('ol.Feature');
|
||||||
goog.require('ol.extent');
|
goog.require('ol.extent');
|
||||||
goog.require('ol.format.GeoJSON');
|
goog.require('ol.format.GeoJSON');
|
||||||
|
goog.require('ol.geom.GeometryCollection');
|
||||||
goog.require('ol.geom.LineString');
|
goog.require('ol.geom.LineString');
|
||||||
goog.require('ol.geom.LinearRing');
|
goog.require('ol.geom.LinearRing');
|
||||||
goog.require('ol.geom.Point');
|
goog.require('ol.geom.Point');
|
||||||
|
|||||||
Reference in New Issue
Block a user