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
+24 -2
View File
@@ -26,6 +26,27 @@ ol.format.Feature = function() {
ol.format.Feature.prototype.getExtensions = goog.abstractMethod; 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. * @return {ol.format.FormatType} Format.
*/ */
@@ -103,13 +124,14 @@ ol.format.Feature.prototype.writeGeometry = goog.abstractMethod;
/** /**
* @param {ol.geom.Geometry} geometry Geometry. * @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 * @param {(olx.format.WriteOptions|olx.format.ReadOptions)=} opt_options
* Options. * Options.
* @return {ol.geom.Geometry} Transformed geometry. * @return {ol.geom.Geometry} Transformed geometry.
* @protected * @protected
*/ */
ol.format.Feature.transformGeometry = function( ol.format.Feature.transformWithOptions = function(
geometry, write, opt_options) { geometry, write, opt_options) {
var featureProjection = goog.isDef(opt_options) ? var featureProjection = goog.isDef(opt_options) ?
ol.proj.get(opt_options.featureProjection) : null; ol.proj.get(opt_options.featureProjection) : null;
+2 -2
View File
@@ -75,7 +75,7 @@ ol.format.GeoJSON.readGeometry_ = function(object, opt_options) {
} }
var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type]; var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type];
goog.asserts.assert(goog.isDef(geometryReader)); goog.asserts.assert(goog.isDef(geometryReader));
return ol.format.Feature.transformGeometry( return ol.format.Feature.transformWithOptions(
geometryReader(object), false, opt_options); 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()]; var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()];
goog.asserts.assert(goog.isDef(geometryWriter)); goog.asserts.assert(goog.isDef(geometryWriter));
return geometryWriter( return geometryWriter(
ol.format.Feature.transformGeometry(geometry, true, opt_options)); ol.format.Feature.transformWithOptions(geometry, true, opt_options));
}; };
+26 -12
View File
@@ -164,7 +164,7 @@ ol.format.GML.readGeometry = function(node, objectStack) {
var geometry = ol.xml.pushParseAndPop(/** @type {ol.geom.Geometry} */(null), var geometry = ol.xml.pushParseAndPop(/** @type {ol.geom.Geometry} */(null),
ol.format.GML.GEOMETRY_PARSERS_, node, objectStack); ol.format.GML.GEOMETRY_PARSERS_, node, objectStack);
if (goog.isDefAndNotNull(geometry)) { if (goog.isDefAndNotNull(geometry)) {
return ol.format.Feature.transformGeometry(geometry, false, context); return ol.format.Feature.transformWithOptions(geometry, false, context);
} else { } else {
return undefined; return undefined;
} }
@@ -1040,12 +1040,8 @@ ol.format.GML.RING_PARSERS_ = {
* @inheritDoc * @inheritDoc
*/ */
ol.format.GML.prototype.readGeometryFromNode = function(node, opt_options) { ol.format.GML.prototype.readGeometryFromNode = function(node, opt_options) {
var obj = {}; var geometry = ol.format.GML.readGeometry(node,
if (goog.isDef(opt_options)) { [this.getReadOptions(node, goog.isDef(opt_options) ? opt_options : {})]);
goog.object.extend(obj, opt_options);
//FIXME Get dataProjection from data
}
var geometry = ol.format.GML.readGeometry(node, [obj]);
return (goog.isDef(geometry) ? geometry : null); return (goog.isDef(geometry) ? geometry : null);
}; };
@@ -1071,13 +1067,21 @@ ol.format.GML.prototype.readFeaturesFromNode = function(node, opt_options) {
'featureNS': this.featureNS_ 'featureNS': this.featureNS_
}; };
if (goog.isDef(opt_options)) { if (goog.isDef(opt_options)) {
goog.object.extend(options, opt_options); goog.object.extend(options, this.getReadOptions(node, opt_options));
//FIXME Get dataProjection from data
} }
return ol.format.GML.readFeatures_(node, [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 {Node} node Node.
* @param {ol.geom.Point} value Point geometry. * @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)); goog.asserts.assert(goog.isObject(context));
var item = goog.object.clone(context); var item = goog.object.clone(context);
item.node = node; 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} */ ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
(item), ol.format.GML.GEOMETRY_SERIALIZERS_, (item), ol.format.GML.GEOMETRY_SERIALIZERS_,
ol.format.GML.GEOMETRY_NODE_FACTORY_, ol.format.GML.GEOMETRY_NODE_FACTORY_, [value], objectStack);
[ol.format.Feature.transformGeometry(geometry, true, context)],
objectStack);
}; };
+3 -26
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 * @inheritDoc
*/ */
@@ -78,9 +58,8 @@ ol.format.JSONFeature.prototype.getType = function() {
* @inheritDoc * @inheritDoc
*/ */
ol.format.JSONFeature.prototype.readFeature = function(source, opt_options) { ol.format.JSONFeature.prototype.readFeature = function(source, opt_options) {
var obj = this.getObject_(source);
return this.readFeatureFromObject( 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 * @inheritDoc
*/ */
ol.format.JSONFeature.prototype.readFeatures = function(source, opt_options) { ol.format.JSONFeature.prototype.readFeatures = function(source, opt_options) {
var obj = this.getObject_(source);
return this.readFeaturesFromObject( 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 * @inheritDoc
*/ */
ol.format.JSONFeature.prototype.readGeometry = function(source, opt_options) { ol.format.JSONFeature.prototype.readGeometry = function(source, opt_options) {
var obj = this.getObject_(source);
return this.readGeometryFromObject( return this.readGeometryFromObject(
obj, this.getReadOptions_(obj, opt_options)); this.getObject_(source), this.getReadOptions(source, opt_options));
}; };
+1 -1
View File
@@ -35,7 +35,6 @@ describe('ol.format.GML', function() {
it('can read, transform and write a point geometry', function() { it('can read, transform and write a point geometry', function() {
var config = { var config = {
dataProjection: 'CRS:84',
featureProjection: 'EPSG:3857' featureProjection: 'EPSG:3857'
}; };
var text = var text =
@@ -48,6 +47,7 @@ describe('ol.format.GML', function() {
var coordinates = g.getCoordinates(); var coordinates = g.getCoordinates();
expect(coordinates.splice(0, 2)).to.eql( expect(coordinates.splice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857')); ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
config.dataProjection = 'CRS:84';
var serialized = format.writeGeometry(g, config); var serialized = format.writeGeometry(g, config);
var pos = serialized.firstElementChild.firstElementChild.textContent; var pos = serialized.firstElementChild.firstElementChild.textContent;
var coordinate = pos.split(' '); var coordinate = pos.split(' ');