Implement write transforms for ol.format.GeoJSON

This commit is contained in:
Andreas Hocevar
2014-07-18 20:50:26 +02:00
parent 8aa799850d
commit 4c4a335448
+30 -8
View File
@@ -178,13 +178,25 @@ ol.format.GeoJSON.readPolygonGeometry_ = function(object) {
/** /**
* @param {ol.geom.Geometry} geometry Geometry. * @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @private * @private
* @return {GeoJSONGeometry|GeoJSONGeometryCollection} GeoJSON geometry. * @return {GeoJSONGeometry|GeoJSONGeometryCollection} GeoJSON geometry.
*/ */
ol.format.GeoJSON.writeGeometry_ = function(geometry) { ol.format.GeoJSON.writeGeometry_ = function(geometry, opt_options) {
var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()]; var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()];
goog.asserts.assert(goog.isDef(geometryWriter)); goog.asserts.assert(goog.isDef(geometryWriter));
return geometryWriter(geometry); var featureProjection = goog.isDef(opt_options) ?
ol.proj.get(opt_options.featureProjection) : null;
var dataProjection = goog.isDef(opt_options) &&
goog.isDef(opt_options.dataProjection) ?
ol.proj.get(opt_options.dataProjection) : featureProjection;
if (!goog.isNull(featureProjection) && !goog.isNull(dataProjection) &&
!ol.proj.equivalent(featureProjection, dataProjection)) {
return geometryWriter(
geometry.clone().transform(featureProjection, dataProjection));
} else {
return geometryWriter(geometry);
}
}; };
@@ -203,13 +215,17 @@ ol.format.GeoJSON.writeEmptyGeometryCollectionGeometry_ = function(geometry) {
/** /**
* @param {ol.geom.Geometry} geometry Geometry. * @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @private * @private
* @return {GeoJSONGeometryCollection} GeoJSON geometry collection. * @return {GeoJSONGeometryCollection} GeoJSON geometry collection.
*/ */
ol.format.GeoJSON.writeGeometryCollectionGeometry_ = function(geometry) { ol.format.GeoJSON.writeGeometryCollectionGeometry_ = function(
geometry, opt_options) {
goog.asserts.assertInstanceof(geometry, ol.geom.GeometryCollection); goog.asserts.assertInstanceof(geometry, ol.geom.GeometryCollection);
var geometries = goog.array.map( var geometries = goog.array.map(
geometry.getGeometriesArray(), ol.format.GeoJSON.writeGeometry_); geometry.getGeometriesArray(), function(geometry) {
return ol.format.GeoJSON.writeGeometry_(geometry, opt_options);
});
return /** @type {GeoJSONGeometryCollection} */ ({ return /** @type {GeoJSONGeometryCollection} */ ({
'type': 'GeometryCollection', 'type': 'GeometryCollection',
'geometries': geometries 'geometries': geometries
@@ -484,6 +500,7 @@ ol.format.GeoJSON.prototype.readProjectionFromObject = function(object) {
* *
* @function * @function
* @param {ol.Feature} feature Feature. * @param {ol.Feature} feature Feature.
* @param {olx.format.WriteOptions} options Write options.
* @return {GeoJSONFeature} GeoJSON. * @return {GeoJSONFeature} GeoJSON.
* @api * @api
*/ */
@@ -493,7 +510,8 @@ ol.format.GeoJSON.prototype.writeFeature;
/** /**
* @inheritDoc * @inheritDoc
*/ */
ol.format.GeoJSON.prototype.writeFeatureObject = function(feature) { ol.format.GeoJSON.prototype.writeFeatureObject = function(
feature, opt_options) {
var object = { var object = {
'type': 'Feature' 'type': 'Feature'
}; };
@@ -504,7 +522,8 @@ ol.format.GeoJSON.prototype.writeFeatureObject = function(feature) {
var geometry = feature.getGeometry(); var geometry = feature.getGeometry();
if (goog.isDefAndNotNull(geometry)) { if (goog.isDefAndNotNull(geometry)) {
goog.object.set( goog.object.set(
object, 'geometry', ol.format.GeoJSON.writeGeometry_(geometry)); object, 'geometry',
ol.format.GeoJSON.writeGeometry_(geometry, opt_options));
} }
var properties = feature.getProperties(); var properties = feature.getProperties();
goog.object.remove(properties, 'geometry'); goog.object.remove(properties, 'geometry');
@@ -520,6 +539,7 @@ ol.format.GeoJSON.prototype.writeFeatureObject = function(feature) {
* *
* @function * @function
* @param {Array.<ol.Feature>} features Features. * @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions} options Write options.
* @return {GeoJSONObject} GeoJSON. * @return {GeoJSONObject} GeoJSON.
* @api * @api
*/ */
@@ -529,11 +549,12 @@ ol.format.GeoJSON.prototype.writeFeatures;
/** /**
* @inheritDoc * @inheritDoc
*/ */
ol.format.GeoJSON.prototype.writeFeaturesObject = function(features) { ol.format.GeoJSON.prototype.writeFeaturesObject =
function(features, opt_options) {
var objects = []; var objects = [];
var i, ii; var i, ii;
for (i = 0, ii = features.length; i < ii; ++i) { for (i = 0, ii = features.length; i < ii; ++i) {
objects.push(this.writeFeatureObject(features[i])); objects.push(this.writeFeatureObject(features[i], opt_options));
} }
return /** @type {GeoJSONFeatureCollection} */ ({ return /** @type {GeoJSONFeatureCollection} */ ({
'type': 'FeatureCollection', 'type': 'FeatureCollection',
@@ -547,6 +568,7 @@ ol.format.GeoJSON.prototype.writeFeaturesObject = function(features) {
* *
* @function * @function
* @param {ol.geom.Geometry} geometry Geometry. * @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions} options Write options.
* @return {GeoJSONGeometry|GeoJSONGeometryCollection} GeoJSON. * @return {GeoJSONGeometry|GeoJSONGeometryCollection} GeoJSON.
* @api * @api
*/ */