Make options complete in ol.format.Feature already

This commit is contained in:
Andreas Hocevar
2014-07-30 22:19:23 +02:00
parent 4825cba48a
commit c4fdbacc12
5 changed files with 56 additions and 43 deletions

View File

@@ -26,6 +26,27 @@ ol.format.Feature = function() {
ol.format.Feature.prototype.getExtensions = goog.abstractMethod;
/**
* Adds the data projection to the read options.
* @param {Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Options.
* @return {olx.format.ReadOptions|undefined} Options.
* @protected
*/
ol.format.Feature.prototype.getReadOptions = function(
source, opt_options) {
var options;
if (goog.isDef(opt_options)) {
options = {
dataProjection: goog.isDef(opt_options.dataProjection) ?
opt_options.dataProjection : this.readProjection(source),
featureProjection: opt_options.featureProjection
};
}
return options;
};
/**
* @return {ol.format.FormatType} Format.
*/
@@ -103,13 +124,14 @@ ol.format.Feature.prototype.writeGeometry = goog.abstractMethod;
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @param {boolean} write Set to true for writing, false for reading.
* @param {boolean} write Set to true for writing, false for reading. For
* writing, the geometry will be cloned before transforming.
* @param {(olx.format.WriteOptions|olx.format.ReadOptions)=} opt_options
* Options.
* @return {ol.geom.Geometry} Transformed geometry.
* @protected
*/
ol.format.Feature.transformGeometry = function(
ol.format.Feature.transformWithOptions = function(
geometry, write, opt_options) {
var featureProjection = goog.isDef(opt_options) ?
ol.proj.get(opt_options.featureProjection) : null;

View File

@@ -75,7 +75,7 @@ ol.format.GeoJSON.readGeometry_ = function(object, opt_options) {
}
var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type];
goog.asserts.assert(goog.isDef(geometryReader));
return ol.format.Feature.transformGeometry(
return ol.format.Feature.transformWithOptions(
geometryReader(object), false, opt_options);
};
@@ -177,7 +177,7 @@ ol.format.GeoJSON.writeGeometry_ = function(geometry, opt_options) {
var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()];
goog.asserts.assert(goog.isDef(geometryWriter));
return geometryWriter(
ol.format.Feature.transformGeometry(geometry, true, opt_options));
ol.format.Feature.transformWithOptions(geometry, true, opt_options));
};

View File

@@ -164,7 +164,7 @@ ol.format.GML.readGeometry = function(node, objectStack) {
var geometry = ol.xml.pushParseAndPop(/** @type {ol.geom.Geometry} */(null),
ol.format.GML.GEOMETRY_PARSERS_, node, objectStack);
if (goog.isDefAndNotNull(geometry)) {
return ol.format.Feature.transformGeometry(geometry, false, context);
return ol.format.Feature.transformWithOptions(geometry, false, context);
} else {
return undefined;
}
@@ -1040,12 +1040,8 @@ ol.format.GML.RING_PARSERS_ = {
* @inheritDoc
*/
ol.format.GML.prototype.readGeometryFromNode = function(node, opt_options) {
var obj = {};
if (goog.isDef(opt_options)) {
goog.object.extend(obj, opt_options);
//FIXME Get dataProjection from data
}
var geometry = ol.format.GML.readGeometry(node, [obj]);
var geometry = ol.format.GML.readGeometry(node,
[this.getReadOptions(node, goog.isDef(opt_options) ? opt_options : {})]);
return (goog.isDef(geometry) ? geometry : null);
};
@@ -1071,13 +1067,21 @@ ol.format.GML.prototype.readFeaturesFromNode = function(node, opt_options) {
'featureNS': this.featureNS_
};
if (goog.isDef(opt_options)) {
goog.object.extend(options, opt_options);
//FIXME Get dataProjection from data
goog.object.extend(options, this.getReadOptions(node, opt_options));
}
return ol.format.GML.readFeatures_(node, [options]);
};
/**
* @inheritDoc
*/
ol.format.GML.prototype.readProjectionFromNode = function(node) {
//TODO read this from data
return ol.proj.get(this.srsName_);
};
/**
* @param {Node} node Node.
* @param {ol.geom.Point} value Point geometry.
@@ -1455,11 +1459,21 @@ ol.format.GML.writeGeometry = function(node, geometry, objectStack) {
goog.asserts.assert(goog.isObject(context));
var item = goog.object.clone(context);
item.node = node;
var value;
if (goog.isArray(geometry)) {
if (goog.isDef(context.dataProjection)) {
value = ol.proj.transformExtent(
geometry, context.featureProjection, context.dataProjection);
} else {
value = geometry;
}
} else {
goog.asserts.assertInstanceof(geometry, ol.geom.Geometry);
value = ol.format.Feature.transformWithOptions(geometry, true, context);
}
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
(item), ol.format.GML.GEOMETRY_SERIALIZERS_,
ol.format.GML.GEOMETRY_NODE_FACTORY_,
[ol.format.Feature.transformGeometry(geometry, true, context)],
objectStack);
ol.format.GML.GEOMETRY_NODE_FACTORY_, [value], objectStack);
};

View File

@@ -46,26 +46,6 @@ ol.format.JSONFeature.prototype.getObject_ = function(source) {
};
/**
* Adds the data projection to the read options.
* @param {Object} obj Data object.
* @param {olx.format.ReadOptions=} opt_options Options.
* @return {olx.format.ReadOptions|undefined} Options.
* @private
*/
ol.format.JSONFeature.prototype.getReadOptions_ = function(obj, opt_options) {
var options;
if (goog.isDef(opt_options)) {
options = {
dataProjection: goog.isDef(opt_options.dataProjection) ?
opt_options.dataProjection : this.readProjectionFromObject(obj),
featureProjection: opt_options.featureProjection
};
}
return options;
};
/**
* @inheritDoc
*/
@@ -78,9 +58,8 @@ ol.format.JSONFeature.prototype.getType = function() {
* @inheritDoc
*/
ol.format.JSONFeature.prototype.readFeature = function(source, opt_options) {
var obj = this.getObject_(source);
return this.readFeatureFromObject(
obj, this.getReadOptions_(obj, opt_options));
this.getObject_(source), this.getReadOptions(source, opt_options));
};
@@ -88,9 +67,8 @@ ol.format.JSONFeature.prototype.readFeature = function(source, opt_options) {
* @inheritDoc
*/
ol.format.JSONFeature.prototype.readFeatures = function(source, opt_options) {
var obj = this.getObject_(source);
return this.readFeaturesFromObject(
obj, this.getReadOptions_(obj, opt_options));
this.getObject_(source), this.getReadOptions(source, opt_options));
};
@@ -116,9 +94,8 @@ ol.format.JSONFeature.prototype.readFeaturesFromObject = goog.abstractMethod;
* @inheritDoc
*/
ol.format.JSONFeature.prototype.readGeometry = function(source, opt_options) {
var obj = this.getObject_(source);
return this.readGeometryFromObject(
obj, this.getReadOptions_(obj, opt_options));
this.getObject_(source), this.getReadOptions(source, opt_options));
};

View File

@@ -35,7 +35,6 @@ describe('ol.format.GML', function() {
it('can read, transform and write a point geometry', function() {
var config = {
dataProjection: 'CRS:84',
featureProjection: 'EPSG:3857'
};
var text =
@@ -48,6 +47,7 @@ describe('ol.format.GML', function() {
var coordinates = g.getCoordinates();
expect(coordinates.splice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
config.dataProjection = 'CRS:84';
var serialized = format.writeGeometry(g, config);
var pos = serialized.firstElementChild.firstElementChild.textContent;
var coordinate = pos.split(' ');