Merge pull request #2407 from ahocevar/format-projection

Options for feature readers and writers to support transforms
This commit is contained in:
Tobias Sauerwein
2014-08-21 17:58:10 +02:00
29 changed files with 1282 additions and 218 deletions

View File

@@ -11,9 +11,7 @@ goog.require('ol.tilegrid.XYZ');
var waterLayer = new ol.layer.Vector({
source: new ol.source.TileVector({
format: new ol.format.TopoJSON({
defaultProjection: 'EPSG:4326'
}),
format: new ol.format.TopoJSON(),
projection: 'EPSG:3857',
tileGrid: new ol.tilegrid.XYZ({
maxZoom: 19
@@ -31,9 +29,7 @@ var waterLayer = new ol.layer.Vector({
var roadStyleCache = {};
var roadLayer = new ol.layer.Vector({
source: new ol.source.TileVector({
format: new ol.format.TopoJSON({
defaultProjection: 'EPSG:4326'
}),
format: new ol.format.TopoJSON(),
projection: 'EPSG:3857',
tileGrid: new ol.tilegrid.XYZ({
maxZoom: 19

View File

@@ -1162,7 +1162,58 @@ olx.control.ZoomToExtentOptions.prototype.extent;
/**
* @typedef {{defaultProjection: ol.proj.ProjectionLike,
* @typedef {{dataProjection: (ol.proj.ProjectionLike|undefined),
* featureProjection: (ol.proj.ProjectionLike|undefined)}}
*/
olx.format.ReadOptions;
/**
* Projection of the data we are reading. If not provided, the projection will
* be derived from the data (where possible) or the `defaultDataProjection` of
* the format is assigned (where set). If the projection can not be derived from
* the data and if no `defaultDataProjection` is set for a format, the features
* will not be reprojected.
* @type {ol.proj.ProjectionLike|undefined}
*/
olx.format.ReadOptions.prototype.dataProjection;
/**
* Projection of the feature geometries created by the format reader. If not
* provided, features will be returned in the `dataProjection`.
* @type {ol.proj.ProjectionLike|undefined}
*/
olx.format.ReadOptions.prototype.featureProjection;
/**
* @typedef {{dataProjection: (ol.proj.ProjectionLike|undefined),
* featureProjection: ol.proj.ProjectionLike}}
*/
olx.format.WriteOptions;
/**
* Projection of the data we are writing. If not provided, the
* `defaultDataProjection` of the format is assigned (where set). If no
* `defaultDataProjection` is set for a format, the features will be returned
* in the `featureProjection`.
* @type {ol.proj.ProjectionLike|undefined}
*/
olx.format.WriteOptions.prototype.dataProjection;
/**
* Projection of the feature geometries that will be serialized by the format
* writer.
* @type {ol.proj.ProjectionLike}
*/
olx.format.WriteOptions.prototype.featureProjection;
/**
* @typedef {{defaultDataProjection: ol.proj.ProjectionLike,
* geometryName: (string|undefined)}}
* @api
*/
@@ -1170,10 +1221,10 @@ olx.format.GeoJSONOptions;
/**
* Default projection.
* Default data projection.
* @type {ol.proj.ProjectionLike}
*/
olx.format.GeoJSONOptions.prototype.defaultProjection;
olx.format.GeoJSONOptions.prototype.defaultDataProjection;
/**
@@ -1198,19 +1249,18 @@ olx.format.PolylineOptions;
olx.format.PolylineOptions.prototype.factor;
/**
* @typedef {{defaultProjection: ol.proj.ProjectionLike}}
* @typedef {{defaultDataProjection: ol.proj.ProjectionLike}}
* @api
*/
olx.format.TopoJSONOptions;
/**
* Default projection.
* Default data projection.
* @type {ol.proj.ProjectionLike}
*/
olx.format.TopoJSONOptions.prototype.defaultProjection;
olx.format.TopoJSONOptions.prototype.defaultDataProjection;
/**

View File

@@ -1,6 +1,8 @@
goog.provide('ol.format.Feature');
goog.require('goog.functions');
goog.require('goog.array');
goog.require('ol.geom.Geometry');
goog.require('ol.proj');
@@ -16,6 +18,12 @@ goog.require('goog.functions');
* @constructor
*/
ol.format.Feature = function() {
/**
* @protected
* @type {ol.proj.Projection}
*/
this.defaultDataProjection = null;
};
@@ -25,6 +33,50 @@ 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 this.adaptOptions(options);
};
/**
* Sets the `defaultDataProjection` on the options, if no `dataProjection`
* is set.
* @param {olx.format.WriteOptions|olx.format.ReadOptions|undefined} options
* Options.
* @protected
* @return {olx.format.WriteOptions|olx.format.ReadOptions|undefined}
* Updated options.
*/
ol.format.Feature.prototype.adaptOptions = function(
options) {
var updatedOptions;
if (goog.isDef(options)) {
updatedOptions = {
featureProjection: options.featureProjection,
dataProjection: goog.isDefAndNotNull(options.dataProjection) ?
options.dataProjection : this.defaultDataProjection
};
}
return updatedOptions;
};
/**
* @return {ol.format.FormatType} Format.
*/
@@ -35,6 +87,7 @@ ol.format.Feature.prototype.getType = goog.abstractMethod;
* Read a single feature from a source.
*
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
*/
ol.format.Feature.prototype.readFeature = goog.abstractMethod;
@@ -44,6 +97,7 @@ ol.format.Feature.prototype.readFeature = goog.abstractMethod;
* Read all features from a source.
*
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
*/
ol.format.Feature.prototype.readFeatures = goog.abstractMethod;
@@ -53,6 +107,7 @@ ol.format.Feature.prototype.readFeatures = goog.abstractMethod;
* Read a single geometry from a source.
*
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.geom.Geometry} Geometry.
*/
ol.format.Feature.prototype.readGeometry = goog.abstractMethod;
@@ -71,6 +126,7 @@ ol.format.Feature.prototype.readProjection = goog.abstractMethod;
* Encode a feature in this format.
*
* @param {ol.Feature} feature Feature.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @return {ArrayBuffer|Node|Object|string} Result.
*/
ol.format.Feature.prototype.writeFeature = goog.abstractMethod;
@@ -80,6 +136,7 @@ ol.format.Feature.prototype.writeFeature = goog.abstractMethod;
* Encode an array of features in this format.
*
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @return {ArrayBuffer|Node|Object|string} Result.
*/
ol.format.Feature.prototype.writeFeatures = goog.abstractMethod;
@@ -89,6 +146,41 @@ ol.format.Feature.prototype.writeFeatures = goog.abstractMethod;
* Write a single geometry in this format.
*
* @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @return {ArrayBuffer|Node|Object|string} Node.
*/
ol.format.Feature.prototype.writeGeometry = goog.abstractMethod;
/**
* @param {ol.geom.Geometry|ol.Extent} geometry Geometry.
* @param {boolean} write Set to true for writing, false for reading.
* @param {(olx.format.WriteOptions|olx.format.ReadOptions)=} opt_options
* Options.
* @return {ol.geom.Geometry|ol.Extent} Transformed geometry.
* @protected
*/
ol.format.Feature.transformWithOptions = function(
geometry, write, opt_options) {
var featureProjection = goog.isDef(opt_options) ?
ol.proj.get(opt_options.featureProjection) : null;
var dataProjection = goog.isDef(opt_options) ?
ol.proj.get(opt_options.dataProjection) : null;
if (!goog.isNull(featureProjection) && !goog.isNull(dataProjection) &&
!ol.proj.equivalent(featureProjection, dataProjection)) {
if (geometry instanceof ol.geom.Geometry) {
return (write ? geometry.clone() : geometry).transform(
write ? featureProjection : dataProjection,
write ? dataProjection : featureProjection);
} else {
// FIXME this is necessary because ol.format.GML treats extents
// as geometries
return ol.proj.transformExtent(
write ? goog.array.clone(geometry) : geometry,
write ? featureProjection : dataProjection,
write ? dataProjection : featureProjection);
}
} else {
return geometry;
}
};

View File

@@ -7,6 +7,7 @@ goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.JSONFeature');
goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.GeometryType');
@@ -36,11 +37,11 @@ ol.format.GeoJSON = function(opt_options) {
goog.base(this);
/**
* @private
* @type {ol.proj.Projection}
* @inheritDoc
*/
this.defaultProjection_ = ol.proj.get(options.defaultProjection ?
options.defaultProjection : 'EPSG:4326');
this.defaultDataProjection = ol.proj.get(
goog.isDefAndNotNull(options.defaultDataProjection) ?
options.defaultDataProjection : 'EPSG:4326');
/**
@@ -64,28 +65,39 @@ ol.format.GeoJSON.EXTENSIONS_ = ['.geojson'];
/**
* @param {GeoJSONObject} object Object.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @private
* @return {ol.geom.Geometry} Geometry.
*/
ol.format.GeoJSON.readGeometry_ = function(object) {
ol.format.GeoJSON.readGeometry_ = function(object, opt_options) {
if (goog.isNull(object)) {
return null;
}
var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type];
goog.asserts.assert(goog.isDef(geometryReader));
return geometryReader(object);
return /** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(
geometryReader(object), false, opt_options));
};
/**
* @param {GeoJSONGeometryCollection} object Object.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @private
* @return {ol.geom.GeometryCollection} Geometry collection.
*/
ol.format.GeoJSON.readGeometryCollectionGeometry_ = function(object) {
ol.format.GeoJSON.readGeometryCollectionGeometry_ = function(
object, opt_options) {
goog.asserts.assert(object.type == 'GeometryCollection');
var geometries = goog.array.map(
object.geometries, ol.format.GeoJSON.readGeometry_);
var geometries = goog.array.map(object.geometries,
/**
* @param {GeoJSONObject} geometry Geometry.
* @return {ol.geom.Geometry} geometry Geometry.
*/
function(geometry) {
return ol.format.GeoJSON.readGeometry_(geometry, opt_options);
});
return new ol.geom.GeometryCollection(geometries);
};
@@ -158,13 +170,15 @@ ol.format.GeoJSON.readPolygonGeometry_ = function(object) {
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @private
* @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()];
goog.asserts.assert(goog.isDef(geometryWriter));
return geometryWriter(geometry);
return geometryWriter(/** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(geometry, true, opt_options)));
};
@@ -183,13 +197,17 @@ ol.format.GeoJSON.writeEmptyGeometryCollectionGeometry_ = function(geometry) {
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @private
* @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);
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} */ ({
'type': 'GeometryCollection',
'geometries': geometries
@@ -330,6 +348,7 @@ ol.format.GeoJSON.prototype.getExtensions = function() {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
* @api
*/
@@ -342,6 +361,7 @@ ol.format.GeoJSON.prototype.readFeature;
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -351,10 +371,12 @@ ol.format.GeoJSON.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readFeatureFromObject = function(object) {
ol.format.GeoJSON.prototype.readFeatureFromObject = function(
object, opt_options) {
var geoJSONFeature = /** @type {GeoJSONFeature} */ (object);
goog.asserts.assert(geoJSONFeature.type == 'Feature');
var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry);
var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry,
opt_options);
var feature = new ol.Feature();
if (goog.isDef(this.geometryName_)) {
feature.setGeometryName(this.geometryName_);
@@ -373,10 +395,11 @@ ol.format.GeoJSON.prototype.readFeatureFromObject = function(object) {
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readFeaturesFromObject = function(object) {
ol.format.GeoJSON.prototype.readFeaturesFromObject = function(
object, opt_options) {
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
if (geoJSONObject.type == 'Feature') {
return [this.readFeatureFromObject(object)];
return [this.readFeatureFromObject(object, opt_options)];
} else if (geoJSONObject.type == 'FeatureCollection') {
var geoJSONFeatureCollection = /** @type {GeoJSONFeatureCollection} */
(object);
@@ -385,7 +408,8 @@ ol.format.GeoJSON.prototype.readFeaturesFromObject = function(object) {
var geoJSONFeatures = geoJSONFeatureCollection.features;
var i, ii;
for (i = 0, ii = geoJSONFeatures.length; i < ii; ++i) {
features.push(this.readFeatureFromObject(geoJSONFeatures[i]));
features.push(this.readFeatureFromObject(geoJSONFeatures[i],
opt_options));
}
return features;
} else {
@@ -400,6 +424,7 @@ ol.format.GeoJSON.prototype.readFeaturesFromObject = function(object) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.geom.Geometry} Geometry.
* @api
*/
@@ -409,9 +434,10 @@ ol.format.GeoJSON.prototype.readGeometry;
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readGeometryFromObject = function(object) {
ol.format.GeoJSON.prototype.readGeometryFromObject = function(
object, opt_options) {
return ol.format.GeoJSON.readGeometry_(
/** @type {GeoJSONGeometry} */ (object));
/** @type {GeoJSONGeometry} */ (object), opt_options);
};
@@ -446,7 +472,7 @@ ol.format.GeoJSON.prototype.readProjectionFromObject = function(object) {
return null;
}
} else {
return this.defaultProjection_;
return this.defaultDataProjection;
}
};
@@ -456,6 +482,7 @@ ol.format.GeoJSON.prototype.readProjectionFromObject = function(object) {
*
* @function
* @param {ol.Feature} feature Feature.
* @param {olx.format.WriteOptions} options Write options.
* @return {GeoJSONFeature} GeoJSON.
* @api
*/
@@ -465,7 +492,8 @@ ol.format.GeoJSON.prototype.writeFeature;
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.writeFeatureObject = function(feature) {
ol.format.GeoJSON.prototype.writeFeatureObject = function(
feature, opt_options) {
var object = {
'type': 'Feature'
};
@@ -476,7 +504,8 @@ ol.format.GeoJSON.prototype.writeFeatureObject = function(feature) {
var geometry = feature.getGeometry();
if (goog.isDefAndNotNull(geometry)) {
goog.object.set(
object, 'geometry', ol.format.GeoJSON.writeGeometry_(geometry));
object, 'geometry',
ol.format.GeoJSON.writeGeometry_(geometry, opt_options));
}
var properties = feature.getProperties();
goog.object.remove(properties, 'geometry');
@@ -492,6 +521,7 @@ ol.format.GeoJSON.prototype.writeFeatureObject = function(feature) {
*
* @function
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions} options Write options.
* @return {GeoJSONObject} GeoJSON.
* @api
*/
@@ -501,11 +531,12 @@ ol.format.GeoJSON.prototype.writeFeatures;
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.writeFeaturesObject = function(features) {
ol.format.GeoJSON.prototype.writeFeaturesObject =
function(features, opt_options) {
var objects = [];
var i, ii;
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} */ ({
'type': 'FeatureCollection',
@@ -519,6 +550,7 @@ ol.format.GeoJSON.prototype.writeFeaturesObject = function(features) {
*
* @function
* @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions} options Write options.
* @return {GeoJSONGeometry|GeoJSONGeometryCollection} GeoJSON.
* @api
*/

View File

@@ -1,3 +1,6 @@
// FIXME Envelopes should not be treated as geometries! readEnvelope_ is part
// of GEOMETRY_PARSERS_ and methods using GEOMETRY_PARSERS_ do not expect
// envelopes/extents, only geometries!
goog.provide('ol.format.GML');
goog.require('goog.asserts');
@@ -8,6 +11,7 @@ goog.require('goog.string');
goog.require('ol.Feature');
goog.require('ol.array');
goog.require('ol.extent');
goog.require('ol.format.Feature');
goog.require('ol.format.XMLFeature');
goog.require('ol.format.XSD');
goog.require('ol.geom.Geometry');
@@ -162,7 +166,8 @@ 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 geometry;
return /** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(geometry, false, context));
} else {
return undefined;
}
@@ -1037,9 +1042,10 @@ ol.format.GML.RING_PARSERS_ = {
/**
* @inheritDoc
*/
ol.format.GML.prototype.readGeometryFromNode = function(node) {
var geometry = ol.format.GML.readGeometry(node, [{}]);
return (goog.isDef(geometry)) ? geometry : null;
ol.format.GML.prototype.readGeometryFromNode = function(node, opt_options) {
var geometry = ol.format.GML.readGeometry(node,
[this.getReadOptions(node, goog.isDef(opt_options) ? opt_options : {})]);
return (goog.isDef(geometry) ? geometry : null);
};
@@ -1048,6 +1054,7 @@ ol.format.GML.prototype.readGeometryFromNode = function(node) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -1057,12 +1064,24 @@ ol.format.GML.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.GML.prototype.readFeaturesFromNode = function(node) {
var objectStack = [{
ol.format.GML.prototype.readFeaturesFromNode = function(node, opt_options) {
var options = {
'featureType': this.featureType_,
'featureNS': this.featureNS_
}];
return ol.format.GML.readFeatures_(node, objectStack);
};
if (goog.isDef(opt_options)) {
goog.object.extend(options, this.getReadOptions(node, opt_options));
}
return ol.format.GML.readFeatures_(node, [options]);
};
/**
* @inheritDoc
*/
ol.format.GML.prototype.readProjectionFromNode = function(node) {
return ol.proj.get(goog.isDef(this.srsName_) ? this.srsName_ :
node.firstElementChild.getAttribute('srsName'));
};
@@ -1443,9 +1462,22 @@ 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_, [geometry], objectStack);
ol.format.GML.GEOMETRY_NODE_FACTORY_, [value], objectStack);
};
@@ -1673,14 +1705,15 @@ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack,
/**
* @inheritDoc
*/
ol.format.GML.prototype.writeGeometryNode = function(geometry) {
ol.format.GML.prototype.writeGeometryNode = function(geometry, opt_options) {
var geom = ol.xml.createElementNS('http://www.opengis.net/gml', 'geom');
var context = {node: geom, srsName: this.srsName_,
curve: this.curve_, surface: this.surface_,
multiSurface: this.multiSurface_, multiCurve: this.multiCurve_};
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
(context), ol.format.GML.GEOMETRY_SERIALIZERS_,
ol.format.GML.GEOMETRY_NODE_FACTORY_, [geometry], []);
if (goog.isDef(opt_options)) {
goog.object.extend(context, opt_options);
}
ol.format.GML.writeGeometry(geom, geometry, [context]);
return geom;
};
@@ -1690,6 +1723,7 @@ ol.format.GML.prototype.writeGeometryNode = function(geometry) {
*
* @function
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Options.
* @return {Node} Result.
* @api
*/
@@ -1699,7 +1733,7 @@ ol.format.GML.prototype.writeFeatures;
/**
* @inheritDoc
*/
ol.format.GML.prototype.writeFeaturesNode = function(features) {
ol.format.GML.prototype.writeFeaturesNode = function(features, opt_options) {
var node = ol.xml.createElementNS('http://www.opengis.net/gml',
'featureMembers');
ol.xml.setAttributeNS(node, 'http://www.w3.org/2001/XMLSchema-instance',
@@ -1713,6 +1747,9 @@ ol.format.GML.prototype.writeFeaturesNode = function(features) {
featureNS: this.featureNS_,
featureType: this.featureType_
};
if (goog.isDef(opt_options)) {
goog.object.extend(context, opt_options);
}
ol.format.GML.writeFeatureMembers_(node, features, [context]);
return node;
};

View File

@@ -5,6 +5,7 @@ goog.require('goog.asserts');
goog.require('goog.dom.NodeType');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.XMLFeature');
goog.require('ol.format.XSD');
goog.require('ol.geom.LineString');
@@ -30,6 +31,11 @@ ol.format.GPX = function(opt_options) {
goog.base(this);
/**
* @inheritDoc
*/
this.defaultDataProjection = ol.proj.get('EPSG:4326');
/**
* @type {function(ol.Feature, Node)|undefined}
* @private
@@ -175,6 +181,7 @@ ol.format.GPX.parseTrkSeg_ = function(node, objectStack) {
ol.format.GPX.readRte_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'rte');
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
var values = ol.xml.pushParseAndPop({
'flatCoordinates': []
}, ol.format.GPX.RTE_PARSERS_, node, objectStack);
@@ -186,6 +193,7 @@ ol.format.GPX.readRte_ = function(node, objectStack) {
goog.object.remove(values, 'flatCoordinates');
var geometry = new ol.geom.LineString(null);
geometry.setFlatCoordinates(ol.geom.GeometryLayout.XYZM, flatCoordinates);
ol.format.Feature.transformWithOptions(geometry, false, options);
var feature = new ol.Feature(geometry);
feature.setProperties(values);
return feature;
@@ -201,6 +209,7 @@ ol.format.GPX.readRte_ = function(node, objectStack) {
ol.format.GPX.readTrk_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'trk');
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
var values = ol.xml.pushParseAndPop({
'flatCoordinates': [],
'ends': []
@@ -216,6 +225,7 @@ ol.format.GPX.readTrk_ = function(node, objectStack) {
var geometry = new ol.geom.MultiLineString(null);
geometry.setFlatCoordinates(
ol.geom.GeometryLayout.XYZM, flatCoordinates, ends);
ol.format.Feature.transformWithOptions(geometry, false, options);
var feature = new ol.Feature(geometry);
feature.setProperties(values);
return feature;
@@ -231,6 +241,7 @@ ol.format.GPX.readTrk_ = function(node, objectStack) {
ol.format.GPX.readWpt_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'wpt');
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
var values = ol.xml.pushParseAndPop(
{}, ol.format.GPX.WPT_PARSERS_, node, objectStack);
if (!goog.isDef(values)) {
@@ -239,6 +250,7 @@ ol.format.GPX.readWpt_ = function(node, objectStack) {
var coordinates = ol.format.GPX.appendCoordinate_([], node, values);
var geometry = new ol.geom.Point(
coordinates, ol.geom.GeometryLayout.XYZM);
ol.format.Feature.transformWithOptions(geometry, false, options);
var feature = new ol.Feature(geometry);
feature.setProperties(values);
return feature;
@@ -415,6 +427,7 @@ ol.format.GPX.prototype.handleReadExtensions_ = function(features) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
* @api
*/
@@ -424,7 +437,7 @@ ol.format.GPX.prototype.readFeature;
/**
* @inheritDoc
*/
ol.format.GPX.prototype.readFeatureFromNode = function(node) {
ol.format.GPX.prototype.readFeatureFromNode = function(node, opt_options) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
if (!goog.array.contains(ol.format.GPX.NAMESPACE_URIS_, node.namespaceURI)) {
return null;
@@ -433,7 +446,7 @@ ol.format.GPX.prototype.readFeatureFromNode = function(node) {
if (!goog.isDef(featureReader)) {
return null;
}
var feature = featureReader(node, []);
var feature = featureReader(node, [this.getReadOptions(node, opt_options)]);
if (!goog.isDef(feature)) {
return null;
}
@@ -447,6 +460,7 @@ ol.format.GPX.prototype.readFeatureFromNode = function(node) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -456,7 +470,7 @@ ol.format.GPX.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.GPX.prototype.readFeaturesFromNode = function(node) {
ol.format.GPX.prototype.readFeaturesFromNode = function(node, opt_options) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
if (!goog.array.contains(ol.format.GPX.NAMESPACE_URIS_, node.namespaceURI)) {
return [];
@@ -464,7 +478,7 @@ ol.format.GPX.prototype.readFeaturesFromNode = function(node) {
if (node.localName == 'gpx') {
var features = ol.xml.pushParseAndPop(
/** @type {Array.<ol.Feature>} */ ([]), ol.format.GPX.GPX_PARSERS_,
node, []);
node, [this.getReadOptions(node, opt_options)]);
if (goog.isDef(features)) {
this.handleReadExtensions_(features);
return features;
@@ -491,7 +505,7 @@ ol.format.GPX.prototype.readProjection;
* @inheritDoc
*/
ol.format.GPX.prototype.readProjectionFromDocument = function(doc) {
return ol.proj.get('EPSG:4326');
return this.defaultDataProjection;
};
@@ -499,7 +513,7 @@ ol.format.GPX.prototype.readProjectionFromDocument = function(doc) {
* @inheritDoc
*/
ol.format.GPX.prototype.readProjectionFromNode = function(node) {
return ol.proj.get('EPSG:4326');
return this.defaultDataProjection;
};
@@ -574,11 +588,14 @@ ol.format.GPX.writeWptType_ = function(node, coordinate, objectStack) {
* @private
*/
ol.format.GPX.writeRte_ = function(node, feature, objectStack) {
var options = /** @type {olx.format.WriteOptions} */ (objectStack[0]);
var properties = feature.getProperties();
var context = {node: node, 'properties': properties};
var geometry = feature.getGeometry();
if (goog.isDef(geometry)) {
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
geometry = /** @type {ol.geom.LineString} */
(ol.format.Feature.transformWithOptions(geometry, true, options));
goog.object.set(context, 'geometryLayout', geometry.getLayout());
goog.object.set(properties, 'rtept', geometry.getCoordinates());
}
@@ -598,11 +615,14 @@ ol.format.GPX.writeRte_ = function(node, feature, objectStack) {
* @private
*/
ol.format.GPX.writeTrk_ = function(node, feature, objectStack) {
var options = /** @type {olx.format.WriteOptions} */ (objectStack[0]);
var properties = feature.getProperties();
var context = {node: node, 'properties': properties};
var geometry = feature.getGeometry();
if (goog.isDef(geometry)) {
goog.asserts.assertInstanceof(geometry, ol.geom.MultiLineString);
geometry = /** @type {ol.geom.MultiLineString} */
(ol.format.Feature.transformWithOptions(geometry, true, options));
goog.object.set(properties, 'trkseg', geometry.getLineStrings());
}
var parentNode = objectStack[objectStack.length - 1].node;
@@ -636,12 +656,15 @@ ol.format.GPX.writeTrkSeg_ = function(node, lineString, objectStack) {
* @private
*/
ol.format.GPX.writeWpt_ = function(node, feature, objectStack) {
var options = /** @type {olx.format.WriteOptions} */ (objectStack[0]);
var context = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isObject(context));
goog.object.set(context, 'properties', feature.getProperties());
var geometry = feature.getGeometry();
if (goog.isDef(geometry)) {
goog.asserts.assertInstanceof(geometry, ol.geom.Point);
geometry = /** @type {ol.geom.Point} */
(ol.format.Feature.transformWithOptions(geometry, true, options));
goog.object.set(context, 'geometryLayout', geometry.getLayout());
ol.format.GPX.writeWptType_(node, geometry.getCoordinates(), objectStack);
}
@@ -845,6 +868,7 @@ ol.format.GPX.GPX_SERIALIZERS_ = ol.xml.makeStructureNS(
*
* @function
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @return {Node} Result.
* @api
*/
@@ -854,11 +878,12 @@ ol.format.GPX.prototype.writeFeatures;
/**
* @inheritDoc
*/
ol.format.GPX.prototype.writeFeaturesNode = function(features) {
ol.format.GPX.prototype.writeFeaturesNode = function(features, opt_options) {
//FIXME Serialize metadata
var gpx = ol.xml.createElementNS('http://www.topografix.com/GPX/1/1', 'gpx');
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
({node: gpx}), ol.format.GPX.GPX_SERIALIZERS_,
ol.format.GPX.GPX_NODE_FACTORY_, features, []);
ol.format.GPX.GPX_NODE_FACTORY_, features, [opt_options]);
return gpx;
};

View File

@@ -5,6 +5,7 @@ goog.require('goog.asserts');
goog.require('goog.string');
goog.require('goog.string.newlines');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.TextFeature');
goog.require('ol.geom.LineString');
goog.require('ol.proj');
@@ -38,6 +39,11 @@ ol.format.IGC = function(opt_options) {
goog.base(this);
/**
* @inheritDoc
*/
this.defaultDataProjection = ol.proj.get('EPSG:4326');
/**
* @private
* @type {ol.format.IGCZ}
@@ -95,6 +101,7 @@ ol.format.IGC.prototype.getExtensions = function() {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
* @api
*/
@@ -104,7 +111,7 @@ ol.format.IGC.prototype.readFeature;
/**
* @inheritDoc
*/
ol.format.IGC.prototype.readFeatureFromText = function(text) {
ol.format.IGC.prototype.readFeatureFromText = function(text, opt_options) {
var altitudeMode = this.altitudeMode_;
var lines = goog.string.newlines.splitLines(text);
/** @type {Object.<string, string>} */
@@ -169,7 +176,8 @@ ol.format.IGC.prototype.readFeatureFromText = function(text) {
var layout = altitudeMode == ol.format.IGCZ.NONE ?
ol.geom.GeometryLayout.XYM : ol.geom.GeometryLayout.XYZM;
lineString.setFlatCoordinates(layout, flatCoordinates);
var feature = new ol.Feature(lineString);
var feature = new ol.Feature(ol.format.Feature.transformWithOptions(
lineString, false, opt_options));
feature.setProperties(properties);
return feature;
};
@@ -181,6 +189,7 @@ ol.format.IGC.prototype.readFeatureFromText = function(text) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -190,8 +199,8 @@ ol.format.IGC.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.IGC.prototype.readFeaturesFromText = function(text) {
var feature = this.readFeatureFromText(text);
ol.format.IGC.prototype.readFeaturesFromText = function(text, opt_options) {
var feature = this.readFeatureFromText(text, opt_options);
if (!goog.isNull(feature)) {
return [feature];
} else {
@@ -215,5 +224,5 @@ ol.format.IGC.prototype.readProjection;
* @inheritDoc
*/
ol.format.IGC.prototype.readProjectionFromText = function(text) {
return ol.proj.get('EPSG:4326');
return this.defaultDataProjection;
};

View File

@@ -51,21 +51,24 @@ ol.format.JSONFeature.prototype.getType = function() {
/**
* @inheritDoc
*/
ol.format.JSONFeature.prototype.readFeature = function(source) {
return this.readFeatureFromObject(this.getObject_(source));
ol.format.JSONFeature.prototype.readFeature = function(source, opt_options) {
return this.readFeatureFromObject(
this.getObject_(source), this.getReadOptions(source, opt_options));
};
/**
* @inheritDoc
*/
ol.format.JSONFeature.prototype.readFeatures = function(source) {
return this.readFeaturesFromObject(this.getObject_(source));
ol.format.JSONFeature.prototype.readFeatures = function(source, opt_options) {
return this.readFeaturesFromObject(
this.getObject_(source), this.getReadOptions(source, opt_options));
};
/**
* @param {Object} object Object.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @protected
* @return {ol.Feature} Feature.
*/
@@ -74,6 +77,7 @@ ol.format.JSONFeature.prototype.readFeatureFromObject = goog.abstractMethod;
/**
* @param {Object} object Object.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @protected
* @return {Array.<ol.Feature>} Features.
*/
@@ -83,13 +87,15 @@ ol.format.JSONFeature.prototype.readFeaturesFromObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSONFeature.prototype.readGeometry = function(source) {
return this.readGeometryFromObject(this.getObject_(source));
ol.format.JSONFeature.prototype.readGeometry = function(source, opt_options) {
return this.readGeometryFromObject(
this.getObject_(source), this.getReadOptions(source, opt_options));
};
/**
* @param {Object} object Object.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @protected
* @return {ol.geom.Geometry} Geometry.
*/
@@ -115,13 +121,14 @@ ol.format.JSONFeature.prototype.readProjectionFromObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSONFeature.prototype.writeFeature = function(feature) {
return this.writeFeatureObject(feature);
ol.format.JSONFeature.prototype.writeFeature = function(feature, opt_options) {
return this.writeFeatureObject(feature, this.adaptOptions(opt_options));
};
/**
* @param {ol.Feature} feature Feature.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @protected
* @return {Object} Object.
*/
@@ -131,13 +138,15 @@ ol.format.JSONFeature.prototype.writeFeatureObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSONFeature.prototype.writeFeatures = function(features) {
return this.writeFeaturesObject(features);
ol.format.JSONFeature.prototype.writeFeatures = function(
features, opt_options) {
return this.writeFeaturesObject(features, this.adaptOptions(opt_options));
};
/**
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @protected
* @return {Object} Object.
*/
@@ -147,13 +156,15 @@ ol.format.JSONFeature.prototype.writeFeaturesObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSONFeature.prototype.writeGeometry = function(geometry) {
return this.writeGeometryObject(geometry);
ol.format.JSONFeature.prototype.writeGeometry = function(
geometry, opt_options) {
return this.writeGeometryObject(geometry, this.adaptOptions(opt_options));
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @protected
* @return {Object} Object.
*/

View File

@@ -17,6 +17,7 @@ goog.require('ol.Feature');
goog.require('ol.array');
goog.require('ol.color');
goog.require('ol.feature');
goog.require('ol.format.Feature');
goog.require('ol.format.XMLFeature');
goog.require('ol.format.XSD');
goog.require('ol.geom.Geometry');
@@ -71,6 +72,11 @@ ol.format.KML = function(opt_options) {
goog.base(this);
/**
* @inheritDoc
*/
this.defaultDataProjection = ol.proj.get('EPSG:4326');
var defaultStyle = goog.isDef(options.defaultStyle) ?
options.defaultStyle : ol.format.KML.DEFAULT_STYLE_ARRAY_;
@@ -1434,6 +1440,10 @@ ol.format.KML.prototype.readPlacemark_ = function(node, objectStack) {
if (!goog.isNull(id)) {
feature.setId(id);
}
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
if (goog.isDefAndNotNull(object.geometry)) {
ol.format.Feature.transformWithOptions(object.geometry, false, options);
}
feature.setProperties(object);
if (this.extractStyles_) {
feature.setStyle(this.featureStyleFunction_);
@@ -1497,6 +1507,7 @@ ol.format.KML.prototype.readSharedStyleMap_ = function(node, objectStack) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
* @api
*/
@@ -1506,13 +1517,14 @@ ol.format.KML.prototype.readFeature;
/**
* @inheritDoc
*/
ol.format.KML.prototype.readFeatureFromNode = function(node) {
ol.format.KML.prototype.readFeatureFromNode = function(node, opt_options) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
if (!goog.array.contains(ol.format.KML.NAMESPACE_URIS_, node.namespaceURI)) {
return null;
}
goog.asserts.assert(node.localName == 'Placemark');
var feature = this.readPlacemark_(node, []);
var feature = this.readPlacemark_(
node, [this.getReadOptions(node, opt_options)]);
if (goog.isDef(feature)) {
return feature;
} else {
@@ -1526,6 +1538,7 @@ ol.format.KML.prototype.readFeatureFromNode = function(node) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -1535,7 +1548,7 @@ ol.format.KML.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.KML.prototype.readFeaturesFromNode = function(node) {
ol.format.KML.prototype.readFeaturesFromNode = function(node, opt_options) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
if (!goog.array.contains(ol.format.KML.NAMESPACE_URIS_, node.namespaceURI)) {
return [];
@@ -1543,14 +1556,16 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) {
var features;
var localName = ol.xml.getLocalName(node);
if (localName == 'Document' || localName == 'Folder') {
features = this.readDocumentOrFolder_(node, []);
features = this.readDocumentOrFolder_(
node, [this.getReadOptions(node, opt_options)]);
if (goog.isDef(features)) {
return features;
} else {
return [];
}
} else if (localName == 'Placemark') {
var feature = this.readPlacemark_(node, []);
var feature = this.readPlacemark_(
node, [this.getReadOptions(node, opt_options)]);
if (goog.isDef(feature)) {
return [feature];
} else {
@@ -1561,7 +1576,7 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) {
var n;
for (n = node.firstElementChild; !goog.isNull(n);
n = n.nextElementSibling) {
var fs = this.readFeaturesFromNode(n);
var fs = this.readFeaturesFromNode(n, opt_options);
if (goog.isDef(fs)) {
goog.array.extend(features, fs);
}
@@ -1655,7 +1670,7 @@ ol.format.KML.prototype.readProjection;
* @inheritDoc
*/
ol.format.KML.prototype.readProjectionFromDocument = function(doc) {
return ol.proj.get('EPSG:4326');
return this.defaultDataProjection;
};
@@ -1663,7 +1678,7 @@ ol.format.KML.prototype.readProjectionFromDocument = function(doc) {
* @inheritDoc
*/
ol.format.KML.prototype.readProjectionFromNode = function(node) {
return ol.proj.get('EPSG:4326');
return this.defaultDataProjection;
};
@@ -1963,9 +1978,14 @@ ol.format.KML.writePlacemark_ = function(node, feature, objectStack) {
ol.xml.OBJECT_PROPERTY_NODE_FACTORY, values, objectStack, orderedKeys);
// serialize geometry
var options = /** @type {olx.format.WriteOptions} */ (objectStack[0]);
var geometry = feature.getGeometry();
if (goog.isDefAndNotNull(geometry)) {
geometry =
ol.format.Feature.transformWithOptions(geometry, true, options);
}
ol.xml.pushSerializeAndPop(context, ol.format.KML.PLACEMARK_SERIALIZERS_,
ol.format.KML.GEOMETRY_NODE_FACTORY_,
[feature.getGeometry()], objectStack);
ol.format.KML.GEOMETRY_NODE_FACTORY_, [geometry], objectStack);
};
@@ -2504,6 +2524,7 @@ ol.format.KML.OUTER_BOUNDARY_NODE_FACTORY_ =
*
* @function
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Options.
* @return {Node} Result.
* @api
*/
@@ -2513,7 +2534,7 @@ ol.format.KML.prototype.writeFeatures;
/**
* @inheritDoc
*/
ol.format.KML.prototype.writeFeaturesNode = function(features) {
ol.format.KML.prototype.writeFeaturesNode = function(features, opt_options) {
var kml = ol.xml.createElementNS(ol.format.KML.NAMESPACE_URIS_[4], 'kml');
var xmlnsUri = 'http://www.w3.org/2000/xmlns/';
var xmlSchemaInstanceUri = 'http://www.w3.org/2001/XMLSchema-instance';
@@ -2533,6 +2554,6 @@ ol.format.KML.prototype.writeFeaturesNode = function(features) {
var orderedKeys = ol.format.KML.KML_SEQUENCE_[kml.namespaceURI];
var values = ol.xml.makeSequence(properties, orderedKeys);
ol.xml.pushSerializeAndPop(context, ol.format.KML.KML_SERIALIZERS_,
ol.xml.OBJECT_PROPERTY_NODE_FACTORY, values, [], orderedKeys);
ol.xml.OBJECT_PROPERTY_NODE_FACTORY, values, [opt_options], orderedKeys);
return kml;
};

View File

@@ -6,6 +6,7 @@ goog.require('goog.asserts');
goog.require('goog.dom.NodeType');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.XMLFeature');
goog.require('ol.geom.LineString');
goog.require('ol.geom.Point');
@@ -26,6 +27,11 @@ goog.require('ol.xml');
*/
ol.format.OSMXML = function() {
goog.base(this);
/**
* @inheritDoc
*/
this.defaultDataProjection = ol.proj.get('EPSG:4326');
};
goog.inherits(ol.format.OSMXML, ol.format.XMLFeature);
@@ -54,6 +60,7 @@ ol.format.OSMXML.prototype.getExtensions = function() {
ol.format.OSMXML.readNode_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'node');
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
var state = /** @type {Object} */ (objectStack[objectStack.length - 1]);
var id = node.getAttribute('id');
var coordinates = /** @type {Array.<number>} */ ([
@@ -67,6 +74,7 @@ ol.format.OSMXML.readNode_ = function(node, objectStack) {
}, ol.format.OSMXML.NODE_PARSERS_, node, objectStack);
if (!goog.object.isEmpty(values.tags)) {
var geometry = new ol.geom.Point(coordinates);
ol.format.Feature.transformWithOptions(geometry, false, options);
var feature = new ol.Feature(geometry);
feature.setId(id);
feature.setProperties(values.tags);
@@ -83,6 +91,7 @@ ol.format.OSMXML.readNode_ = function(node, objectStack) {
ol.format.OSMXML.readWay_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'way');
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
var id = node.getAttribute('id');
var values = ol.xml.pushParseAndPop({
ndrefs: [],
@@ -104,6 +113,7 @@ ol.format.OSMXML.readWay_ = function(node, objectStack) {
geometry = new ol.geom.LineString(null);
geometry.setFlatCoordinates(ol.geom.GeometryLayout.XY, flatCoordinates);
}
ol.format.Feature.transformWithOptions(geometry, false, options);
var feature = new ol.Feature(geometry);
feature.setId(id);
feature.setProperties(values.tags);
@@ -189,6 +199,7 @@ ol.format.OSMXML.NODE_PARSERS_ = ol.xml.makeParsersNS(
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -198,13 +209,14 @@ ol.format.OSMXML.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.OSMXML.prototype.readFeaturesFromNode = function(node) {
ol.format.OSMXML.prototype.readFeaturesFromNode = function(node, opt_options) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
var options = this.getReadOptions(node, opt_options);
if (node.localName == 'osm') {
var state = ol.xml.pushParseAndPop({
nodes: {},
features: []
}, ol.format.OSMXML.PARSERS_, node, []);
}, ol.format.OSMXML.PARSERS_, node, [options]);
if (goog.isDef(state.features)) {
return state.features;
}
@@ -228,7 +240,7 @@ ol.format.OSMXML.prototype.readProjection;
* @inheritDoc
*/
ol.format.OSMXML.prototype.readProjectionFromDocument = function(doc) {
return ol.proj.get('EPSG:4326');
return this.defaultDataProjection;
};
@@ -236,5 +248,5 @@ ol.format.OSMXML.prototype.readProjectionFromDocument = function(doc) {
* @inheritDoc
*/
ol.format.OSMXML.prototype.readProjectionFromNode = function(node) {
return ol.proj.get('EPSG:4326');
return this.defaultDataProjection;
};

View File

@@ -2,6 +2,7 @@ goog.provide('ol.format.Polyline');
goog.require('goog.asserts');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.TextFeature');
goog.require('ol.geom.LineString');
goog.require('ol.geom.flat.inflate');
@@ -22,6 +23,11 @@ ol.format.Polyline = function(opt_options) {
goog.base(this);
/**
* @inheritDoc
*/
this.defaultDataProjection = ol.proj.get('EPSG:4326');
/**
* @private
* @type {number}
@@ -248,6 +254,7 @@ ol.format.Polyline.encodeUnsignedInteger = function(num) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
* @api
*/
@@ -257,8 +264,8 @@ ol.format.Polyline.prototype.readFeature;
/**
* @inheritDoc
*/
ol.format.Polyline.prototype.readFeatureFromText = function(text) {
var geometry = this.readGeometryFromText(text);
ol.format.Polyline.prototype.readFeatureFromText = function(text, opt_options) {
var geometry = this.readGeometryFromText(text, opt_options);
return new ol.Feature(geometry);
};
@@ -269,6 +276,7 @@ ol.format.Polyline.prototype.readFeatureFromText = function(text) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -278,8 +286,9 @@ ol.format.Polyline.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.Polyline.prototype.readFeaturesFromText = function(text) {
var feature = this.readFeatureFromText(text);
ol.format.Polyline.prototype.readFeaturesFromText =
function(text, opt_options) {
var feature = this.readFeatureFromText(text, opt_options);
return [feature];
};
@@ -289,6 +298,7 @@ ol.format.Polyline.prototype.readFeaturesFromText = function(text) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.geom.Geometry} Geometry.
* @api
*/
@@ -298,11 +308,16 @@ ol.format.Polyline.prototype.readGeometry;
/**
* @inheritDoc
*/
ol.format.Polyline.prototype.readGeometryFromText = function(text) {
ol.format.Polyline.prototype.readGeometryFromText =
function(text, opt_options) {
var flatCoordinates = ol.format.Polyline.decodeDeltas(text, 2, this.factor_);
var coordinates = ol.geom.flat.inflate.coordinates(
flatCoordinates, 0, flatCoordinates.length, 2);
return new ol.geom.LineString(coordinates);
return /** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(
new ol.geom.LineString(coordinates), false,
this.adaptOptions(opt_options)));
};
@@ -321,17 +336,17 @@ ol.format.Polyline.prototype.readProjection;
* @inheritDoc
*/
ol.format.Polyline.prototype.readProjectionFromText = function(text) {
return ol.proj.get('EPSG:4326');
return this.defaultDataProjection;
};
/**
* @inheritDoc
*/
ol.format.Polyline.prototype.writeFeatureText = function(feature) {
ol.format.Polyline.prototype.writeFeatureText = function(feature, opt_options) {
var geometry = feature.getGeometry();
if (goog.isDefAndNotNull(geometry)) {
return this.writeGeometryText(geometry);
return this.writeGeometryText(geometry, opt_options);
} else {
goog.asserts.fail();
return '';
@@ -342,9 +357,10 @@ ol.format.Polyline.prototype.writeFeatureText = function(feature) {
/**
* @inheritDoc
*/
ol.format.Polyline.prototype.writeFeaturesText = function(features) {
ol.format.Polyline.prototype.writeFeaturesText =
function(features, opt_options) {
goog.asserts.assert(features.length == 1);
return this.writeFeatureText(features[0]);
return this.writeFeatureText(features[0], opt_options);
};
@@ -353,6 +369,7 @@ ol.format.Polyline.prototype.writeFeaturesText = function(features) {
*
* @function
* @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @return {string} Geometry.
* @api
*/
@@ -362,8 +379,12 @@ ol.format.Polyline.prototype.writeGeometry;
/**
* @inheritDoc
*/
ol.format.Polyline.prototype.writeGeometryText = function(geometry) {
ol.format.Polyline.prototype.writeGeometryText =
function(geometry, opt_options) {
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
geometry = /** @type {ol.geom.LineString} */
(ol.format.Feature.transformWithOptions(
geometry, true, this.adaptOptions(opt_options)));
var flatCoordinates = geometry.getFlatCoordinates();
var stride = geometry.getStride();
return ol.format.Polyline.encodeDeltas(flatCoordinates, stride, this.factor_);

View File

@@ -47,13 +47,15 @@ ol.format.TextFeature.prototype.getType = function() {
/**
* @inheritDoc
*/
ol.format.TextFeature.prototype.readFeature = function(source) {
return this.readFeatureFromText(this.getText_(source));
ol.format.TextFeature.prototype.readFeature = function(source, opt_options) {
return this.readFeatureFromText(
this.getText_(source), this.adaptOptions(opt_options));
};
/**
* @param {string} text Text.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @protected
* @return {ol.Feature} Feature.
*/
@@ -63,13 +65,15 @@ ol.format.TextFeature.prototype.readFeatureFromText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.TextFeature.prototype.readFeatures = function(source) {
return this.readFeaturesFromText(this.getText_(source));
ol.format.TextFeature.prototype.readFeatures = function(source, opt_options) {
return this.readFeaturesFromText(
this.getText_(source), this.adaptOptions(opt_options));
};
/**
* @param {string} text Text.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @protected
* @return {Array.<ol.Feature>} Features.
*/
@@ -79,13 +83,15 @@ ol.format.TextFeature.prototype.readFeaturesFromText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.TextFeature.prototype.readGeometry = function(source) {
return this.readGeometryFromText(this.getText_(source));
ol.format.TextFeature.prototype.readGeometry = function(source, opt_options) {
return this.readGeometryFromText(
this.getText_(source), this.adaptOptions(opt_options));
};
/**
* @param {string} text Text.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @protected
* @return {ol.geom.Geometry} Geometry.
*/
@@ -111,13 +117,14 @@ ol.format.TextFeature.prototype.readProjectionFromText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.TextFeature.prototype.writeFeature = function(feature) {
return this.writeFeatureText(feature);
ol.format.TextFeature.prototype.writeFeature = function(feature, opt_options) {
return this.writeFeatureText(feature, this.adaptOptions(opt_options));
};
/**
* @param {ol.Feature} feature Features.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @protected
* @return {string} Text.
*/
@@ -127,13 +134,15 @@ ol.format.TextFeature.prototype.writeFeatureText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.TextFeature.prototype.writeFeatures = function(features) {
return this.writeFeaturesText(features);
ol.format.TextFeature.prototype.writeFeatures = function(
features, opt_options) {
return this.writeFeaturesText(features, this.adaptOptions(opt_options));
};
/**
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @protected
* @return {string} Text.
*/
@@ -143,13 +152,15 @@ ol.format.TextFeature.prototype.writeFeaturesText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.TextFeature.prototype.writeGeometry = function(geometry) {
return this.writeGeometryText(geometry);
ol.format.TextFeature.prototype.writeGeometry = function(
geometry, opt_options) {
return this.writeGeometryText(geometry, this.adaptOptions(opt_options));
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @protected
* @return {string} Text.
*/

View File

@@ -3,6 +3,7 @@ goog.provide('ol.format.TopoJSON');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.JSONFeature');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
@@ -30,11 +31,11 @@ ol.format.TopoJSON = function(opt_options) {
goog.base(this);
/**
* @private
* @type {ol.proj.Projection}
* @inheritDoc
*/
this.defaultProjection_ =
ol.proj.get(options.defaultProjection || 'EPSG:4326');
this.defaultDataProjection = ol.proj.get(
goog.isDefAndNotNull(options.defaultDataProjection) ?
options.defaultDataProjection : 'EPSG:4326');
};
goog.inherits(ol.format.TopoJSON, ol.format.JSONFeature);
@@ -217,17 +218,18 @@ ol.format.TopoJSON.prototype.getExtensions = function() {
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs.
* @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Array of features.
* @private
*/
ol.format.TopoJSON.readFeaturesFromGeometryCollection_ = function(
collection, arcs, scale, translate) {
collection, arcs, scale, translate, opt_options) {
var geometries = collection.geometries;
var features = [];
var i, ii;
for (i = 0, ii = geometries.length; i < ii; ++i) {
features[i] = ol.format.TopoJSON.readFeatureFromGeometry_(
geometries[i], arcs, scale, translate);
geometries[i], arcs, scale, translate, opt_options);
}
return features;
};
@@ -240,11 +242,12 @@ ol.format.TopoJSON.readFeaturesFromGeometryCollection_ = function(
* @param {Array.<Array.<ol.Coordinate>>} arcs Array of arcs.
* @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
* @private
*/
ol.format.TopoJSON.readFeatureFromGeometry_ = function(object, arcs,
scale, translate) {
scale, translate, opt_options) {
var geometry;
var type = object.type;
var geometryReader = ol.format.TopoJSON.GEOMETRY_READERS_[type];
@@ -255,7 +258,8 @@ ol.format.TopoJSON.readFeatureFromGeometry_ = function(object, arcs,
geometry = geometryReader(object, arcs);
}
var feature = new ol.Feature();
feature.setGeometry(geometry);
feature.setGeometry(/** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(geometry, false, opt_options)));
if (goog.isDef(object.id)) {
feature.setId(object.id);
}
@@ -280,7 +284,8 @@ ol.format.TopoJSON.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.TopoJSON.prototype.readFeaturesFromObject = function(object) {
ol.format.TopoJSON.prototype.readFeaturesFromObject = function(
object, opt_options) {
if (object.type == 'Topology') {
var topoJSONTopology = /** @type {TopoJSONTopology} */ (object);
var transform, scale = null, translate = null;
@@ -305,12 +310,12 @@ ol.format.TopoJSON.prototype.readFeaturesFromObject = function(object) {
(topoJSONFeatures[i]);
features.push.apply(features,
ol.format.TopoJSON.readFeaturesFromGeometryCollection_(
feature, arcs, scale, translate));
feature, arcs, scale, translate, opt_options));
} else {
feature = /** @type {TopoJSONGeometry} */
(topoJSONFeatures[i]);
features.push(ol.format.TopoJSON.readFeatureFromGeometry_(
feature, arcs, scale, translate));
feature, arcs, scale, translate, opt_options));
}
}
return features;
@@ -385,7 +390,7 @@ ol.format.TopoJSON.transformVertex_ = function(vertex, scale, translate) {
* @api
*/
ol.format.TopoJSON.prototype.readProjection = function(object) {
return this.defaultProjection_;
return this.defaultDataProjection;
};

View File

@@ -100,6 +100,7 @@ ol.format.WFS.schemaLocation_ = 'http://www.opengis.net/wfs ' +
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -109,11 +110,14 @@ ol.format.WFS.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.WFS.prototype.readFeaturesFromNode = function(node) {
var objectStack = [{
ol.format.WFS.prototype.readFeaturesFromNode = function(node, opt_options) {
var context = {
'featureType': this.featureType_,
'featureNS': this.featureNS_
}];
};
goog.object.extend(context, this.getReadOptions(node,
goog.isDef(opt_options) ? opt_options : {}));
var objectStack = [context];
var features = ol.xml.pushParseAndPop([],
ol.format.GML.FEATURE_COLLECTION_PARSERS, node, objectStack);
if (!goog.isDef(features)) {

View File

@@ -3,6 +3,7 @@ goog.provide('ol.format.WKT');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.TextFeature');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryCollection');
@@ -208,6 +209,7 @@ ol.format.WKT.prototype.parse_ = function(wkt) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.Feature} Feature.
* @api
*/
@@ -217,8 +219,8 @@ ol.format.WKT.prototype.readFeature;
/**
* @inheritDoc
*/
ol.format.WKT.prototype.readFeatureFromText = function(text) {
var geom = this.readGeometryFromText(text);
ol.format.WKT.prototype.readFeatureFromText = function(text, opt_options) {
var geom = this.readGeometryFromText(text, opt_options);
if (goog.isDef(geom)) {
var feature = new ol.Feature();
feature.setGeometry(geom);
@@ -233,6 +235,7 @@ ol.format.WKT.prototype.readFeatureFromText = function(text) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -242,9 +245,9 @@ ol.format.WKT.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.WKT.prototype.readFeaturesFromText = function(text) {
ol.format.WKT.prototype.readFeaturesFromText = function(text, opt_options) {
var geometries = [];
var geometry = this.readGeometryFromText(text);
var geometry = this.readGeometryFromText(text, opt_options);
if (this.splitCollection_ &&
geometry.getType() == ol.geom.GeometryType.GEOMETRY_COLLECTION) {
geometries = (/** @type {ol.geom.GeometryCollection} */ (geometry))
@@ -267,6 +270,7 @@ ol.format.WKT.prototype.readFeaturesFromText = function(text) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {ol.geom.Geometry} Geometry.
* @api
*/
@@ -276,8 +280,14 @@ ol.format.WKT.prototype.readGeometry;
/**
* @inheritDoc
*/
ol.format.WKT.prototype.readGeometryFromText = function(text) {
return this.parse_(text) || null;
ol.format.WKT.prototype.readGeometryFromText = function(text, opt_options) {
var geometry = this.parse_(text);
if (goog.isDef(geometry)) {
return /** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(geometry, false, opt_options));
} else {
return null;
}
};
@@ -294,6 +304,7 @@ ol.format.WKT.prototype.readProjectionFromText = function(text) {
*
* @function
* @param {ol.Feature} feature Feature.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @return {string} WKT string.
* @api
*/
@@ -303,10 +314,10 @@ ol.format.WKT.prototype.writeFeature;
/**
* @inheritDoc
*/
ol.format.WKT.prototype.writeFeatureText = function(feature) {
ol.format.WKT.prototype.writeFeatureText = function(feature, opt_options) {
var geometry = feature.getGeometry();
if (goog.isDef(geometry)) {
return this.writeGeometryText(geometry);
return this.writeGeometryText(geometry, opt_options);
}
return '';
};
@@ -317,6 +328,7 @@ ol.format.WKT.prototype.writeFeatureText = function(feature) {
*
* @function
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Write options.
* @return {string} WKT string.
* @api
*/
@@ -326,16 +338,16 @@ ol.format.WKT.prototype.writeFeatures;
/**
* @inheritDoc
*/
ol.format.WKT.prototype.writeFeaturesText = function(features) {
ol.format.WKT.prototype.writeFeaturesText = function(features, opt_options) {
if (features.length == 1) {
return this.writeFeatureText(features[0]);
return this.writeFeatureText(features[0], opt_options);
}
var geometries = [];
for (var i = 0, ii = features.length; i < ii; ++i) {
geometries.push(features[i].getGeometry());
}
var collection = new ol.geom.GeometryCollection(geometries);
return this.writeGeometryText(collection);
return this.writeGeometryText(collection, opt_options);
};
@@ -353,8 +365,9 @@ ol.format.WKT.prototype.writeGeometry;
/**
* @inheritDoc
*/
ol.format.WKT.prototype.writeGeometryText = function(geometry) {
return ol.format.WKT.encode_(geometry);
ol.format.WKT.prototype.writeGeometryText = function(geometry, opt_options) {
return ol.format.WKT.encode_(/** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(geometry, true, opt_options)));
};

View File

@@ -5,6 +5,7 @@ goog.require('goog.asserts');
goog.require('goog.dom.NodeType');
goog.require('ol.format.Feature');
goog.require('ol.format.FormatType');
goog.require('ol.proj');
goog.require('ol.xml');
@@ -35,14 +36,15 @@ ol.format.XMLFeature.prototype.getType = function() {
/**
* @inheritDoc
*/
ol.format.XMLFeature.prototype.readFeature = function(source) {
ol.format.XMLFeature.prototype.readFeature = function(source, opt_options) {
if (ol.xml.isDocument(source)) {
return this.readFeatureFromDocument(/** @type {Document} */ (source));
return this.readFeatureFromDocument(
/** @type {Document} */ (source), opt_options);
} else if (ol.xml.isNode(source)) {
return this.readFeatureFromNode(/** @type {Node} */ (source));
return this.readFeatureFromNode(/** @type {Node} */ (source), opt_options);
} else if (goog.isString(source)) {
var doc = ol.xml.load(source);
return this.readFeatureFromDocument(doc);
return this.readFeatureFromDocument(doc, opt_options);
} else {
goog.asserts.fail();
return null;
@@ -52,10 +54,12 @@ ol.format.XMLFeature.prototype.readFeature = function(source) {
/**
* @param {Document} doc Document.
* @param {olx.format.ReadOptions=} opt_options Options.
* @return {ol.Feature} Feature.
*/
ol.format.XMLFeature.prototype.readFeatureFromDocument = function(doc) {
var features = this.readFeaturesFromDocument(doc);
ol.format.XMLFeature.prototype.readFeatureFromDocument = function(
doc, opt_options) {
var features = this.readFeaturesFromDocument(doc, opt_options);
if (features.length > 0) {
return features[0];
} else {
@@ -66,6 +70,7 @@ ol.format.XMLFeature.prototype.readFeatureFromDocument = function(doc) {
/**
* @param {Node} node Node.
* @param {olx.format.ReadOptions=} opt_options Options.
* @return {ol.Feature} Feature.
*/
ol.format.XMLFeature.prototype.readFeatureFromNode = goog.abstractMethod;
@@ -74,14 +79,15 @@ ol.format.XMLFeature.prototype.readFeatureFromNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XMLFeature.prototype.readFeatures = function(source) {
ol.format.XMLFeature.prototype.readFeatures = function(source, opt_options) {
if (ol.xml.isDocument(source)) {
return this.readFeaturesFromDocument(/** @type {Document} */ (source));
return this.readFeaturesFromDocument(
/** @type {Document} */ (source), opt_options);
} else if (ol.xml.isNode(source)) {
return this.readFeaturesFromNode(/** @type {Node} */ (source));
return this.readFeaturesFromNode(/** @type {Node} */ (source), opt_options);
} else if (goog.isString(source)) {
var doc = ol.xml.load(source);
return this.readFeaturesFromDocument(doc);
return this.readFeaturesFromDocument(doc, opt_options);
} else {
goog.asserts.fail();
return [];
@@ -91,16 +97,18 @@ ol.format.XMLFeature.prototype.readFeatures = function(source) {
/**
* @param {Document} doc Document.
* @param {olx.format.ReadOptions=} opt_options Options.
* @protected
* @return {Array.<ol.Feature>} Features.
*/
ol.format.XMLFeature.prototype.readFeaturesFromDocument = function(doc) {
ol.format.XMLFeature.prototype.readFeaturesFromDocument = function(
doc, opt_options) {
/** @type {Array.<ol.Feature>} */
var features = [];
var n;
for (n = doc.firstChild; !goog.isNull(n); n = n.nextSibling) {
if (n.nodeType == goog.dom.NodeType.ELEMENT) {
goog.array.extend(features, this.readFeaturesFromNode(n));
goog.array.extend(features, this.readFeaturesFromNode(n, opt_options));
}
}
return features;
@@ -109,6 +117,7 @@ ol.format.XMLFeature.prototype.readFeaturesFromDocument = function(doc) {
/**
* @param {Node} node Node.
* @param {olx.format.ReadOptions=} opt_options Options.
* @protected
* @return {Array.<ol.Feature>} Features.
*/
@@ -118,14 +127,15 @@ ol.format.XMLFeature.prototype.readFeaturesFromNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XMLFeature.prototype.readGeometry = function(source) {
ol.format.XMLFeature.prototype.readGeometry = function(source, opt_options) {
if (ol.xml.isDocument(source)) {
return this.readGeometryFromDocument(/** @type {Document} */ (source));
return this.readGeometryFromDocument(
/** @type {Document} */ (source), opt_options);
} else if (ol.xml.isNode(source)) {
return this.readGeometryFromNode(/** @type {Node} */ (source));
return this.readGeometryFromNode(/** @type {Node} */ (source), opt_options);
} else if (goog.isString(source)) {
var doc = ol.xml.load(source);
return this.readGeometryFromDocument(doc);
return this.readGeometryFromDocument(doc, opt_options);
} else {
goog.asserts.fail();
return null;
@@ -135,6 +145,7 @@ ol.format.XMLFeature.prototype.readGeometry = function(source) {
/**
* @param {Document} doc Document.
* @param {olx.format.ReadOptions=} opt_options Options.
* @protected
* @return {ol.geom.Geometry} Geometry.
*/
@@ -143,6 +154,7 @@ ol.format.XMLFeature.prototype.readGeometryFromDocument = goog.abstractMethod;
/**
* @param {Node} node Node.
* @param {olx.format.ReadOptions=} opt_options Options.
* @protected
* @return {ol.geom.Geometry} Geometry.
*/
@@ -186,13 +198,14 @@ ol.format.XMLFeature.prototype.readProjectionFromNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XMLFeature.prototype.writeFeature = function(feature) {
return this.writeFeatureNode(feature);
ol.format.XMLFeature.prototype.writeFeature = function(feature, opt_options) {
return this.writeFeatureNode(feature, this.adaptOptions(opt_options));
};
/**
* @param {ol.Feature} feature Feature.
* @param {olx.format.WriteOptions=} opt_options Options.
* @protected
* @return {Node} Node.
*/
@@ -202,13 +215,14 @@ ol.format.XMLFeature.prototype.writeFeatureNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XMLFeature.prototype.writeFeatures = function(features) {
return this.writeFeaturesNode(features);
ol.format.XMLFeature.prototype.writeFeatures = function(features, opt_options) {
return this.writeFeaturesNode(features, this.adaptOptions(opt_options));
};
/**
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Options.
* @protected
* @return {Node} Node.
*/
@@ -218,13 +232,14 @@ ol.format.XMLFeature.prototype.writeFeaturesNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XMLFeature.prototype.writeGeometry = function(geometry) {
return this.writeGeometryNode(geometry);
ol.format.XMLFeature.prototype.writeGeometry = function(geometry, opt_options) {
return this.writeGeometryNode(geometry, this.adaptOptions(opt_options));
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @param {olx.format.WriteOptions=} opt_options Options.
* @protected
* @return {Node} Node.
*/

View File

@@ -23,7 +23,7 @@ ol.source.GeoJSON = function(opt_options) {
attributions: options.attributions,
extent: options.extent,
format: new ol.format.GeoJSON({
defaultProjection: options.defaultProjection
defaultDataProjection: options.defaultProjection
}),
logo: options.logo,
object: options.object,

View File

@@ -23,7 +23,7 @@ ol.source.TopoJSON = function(opt_options) {
attributions: options.attributions,
extent: options.extent,
format: new ol.format.TopoJSON({
defaultProjection: options.defaultProjection
defaultDataProjection: options.defaultProjection
}),
logo: options.logo,
object: options.object,

View File

@@ -174,6 +174,36 @@ describe('ol.format.GeoJSON', function() {
expect(features[2].getGeometry()).to.be.an(ol.geom.Polygon);
});
it('can read and transform a point', function() {
var feature = format.readFeatures(pointGeoJSON, {
featureProjection: 'EPSG:3857'
});
expect(feature[0].getGeometry()).to.be.an(ol.geom.Point);
expect(feature[0].getGeometry().getCoordinates()).to.eql(
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
});
it('can read and transform a feature collection', function() {
var features = format.readFeatures(featureCollectionGeoJSON, {
featureProjection: 'EPSG:3857'
});
expect(features[0].getGeometry()).to.be.an(ol.geom.Point);
expect(features[0].getGeometry().getCoordinates()).to.eql(
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
expect(features[1].getGeometry().getCoordinates()).to.eql([
ol.proj.transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857')
]);
expect(features[2].getGeometry().getCoordinates()).to.eql([[
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857')
]]);
});
it('can create a feature with a specific geometryName', function() {
var feature = new ol.format.GeoJSON({geometryName: 'the_geom'}).
readFeature(pointGeoJSON);
@@ -452,6 +482,22 @@ describe('ol.format.GeoJSON', function() {
}
});
it('transforms and encodes feature collection', function() {
var str = JSON.stringify(data),
array = format.readFeatures(str);
var geojson = format.writeFeatures(array, {
featureProjection: 'EPSG:3857'
});
var result = format.readFeatures(geojson);
var got, exp;
for (var i = 0, ii = array.length; i < ii; ++i) {
got = array[i];
exp = result[i];
expect(got.getGeometry().transform('EPSG:3857', 'EPSG:4326')
.getCoordinates()).to.eql(exp.getGeometry().getCoordinates());
}
});
});
describe('#writeGeometry', function() {
@@ -507,6 +553,20 @@ describe('ol.format.GeoJSON', function() {
});
});
it('transforms and encodes a point', function() {
var point = new ol.geom.Point([2, 3]);
var geojson = format.writeGeometry(point, {
featureProjection: 'EPSG:3857'
});
var newPoint = format.readGeometry(geojson, {
featureProjection: 'EPSG:3857'
});
expect(point.getCoordinates()[0]).to.eql(newPoint.getCoordinates()[0]);
expect(
Math.abs(point.getCoordinates()[1] - newPoint.getCoordinates()[1]))
.to.be.lessThan(0.0000001);
});
});
});

View File

@@ -1,19 +1,20 @@
goog.provide('ol.test.format.GML');
var readGeometry = function(format, text) {
var readGeometry = function(format, text, opt_options) {
var doc = ol.xml.load(text);
// we need an intermediate node for testing purposes
var node = goog.dom.createElement(goog.dom.TagName.PRE);
node.appendChild(doc.documentElement);
return format.readGeometryFromNode(node);
return format.readGeometryFromNode(node, opt_options);
};
describe('ol.format.GML', function() {
var format, formatWGS84;
var format, formatWGS84, formatNoSrs;
beforeEach(function() {
format = new ol.format.GML({srsName: 'CRS:84'});
formatWGS84 = new ol.format.GML({srsName: 'urn:x-ogc:def:crs:EPSG:4326'});
formatNoSrs = new ol.format.GML();
});
describe('#readGeometry', function() {
@@ -33,6 +34,44 @@ describe('ol.format.GML', function() {
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
it('can read, transform and write a point geometry', function() {
var config = {
featureProjection: 'EPSG:3857'
};
var text =
'<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:pos>1 2</gml:pos>' +
'</gml:Point>';
var g = readGeometry(format, text, config);
expect(g).to.be.an(ol.geom.Point);
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(' ');
expect(coordinate[0]).to.roughlyEqual(1, 1e-9);
expect(coordinate[1]).to.roughlyEqual(2, 1e-9);
});
it('can detect SRS, read and transform a point geometry', function() {
var config = {
featureProjection: 'EPSG:3857'
};
var text =
'<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:pos>1 2</gml:pos>' +
'</gml:Point>';
var g = readGeometry(formatNoSrs, text, config);
expect(g).to.be.an(ol.geom.Point);
var coordinates = g.getCoordinates();
expect(coordinates.splice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
});
it('can read and write a point geometry in EPSG:4326', function() {
var text =
'<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
@@ -63,6 +102,32 @@ describe('ol.format.GML', function() {
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
it('can read, transform and write a linestring geometry', function() {
var config = {
dataProjection: 'CRS:84',
featureProjection: 'EPSG:3857'
};
var text =
'<gml:LineString xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:posList>1 2 3 4</gml:posList>' +
'</gml:LineString>';
var g = readGeometry(format, text, config);
expect(g).to.be.an(ol.geom.LineString);
var coordinates = g.getCoordinates();
expect(coordinates[0].slice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
expect(coordinates[1].slice(0, 2)).to.eql(
ol.proj.transform([3, 4], 'CRS:84', 'EPSG:3857'));
var serialized = format.writeGeometry(g, config);
var poss = serialized.firstElementChild.firstElementChild.textContent;
var coordinate = poss.split(' ');
expect(coordinate[0]).to.roughlyEqual(1, 1e-9);
expect(coordinate[1]).to.roughlyEqual(2, 1e-9);
expect(coordinate[2]).to.roughlyEqual(3, 1e-9);
expect(coordinate[3]).to.roughlyEqual(4, 1e-9);
});
it('can read and write a linestring geometry in EPSG:4326', function() {
var text =
'<gml:LineString xmlns:gml="http://www.opengis.net/gml" ' +
@@ -779,7 +844,7 @@ describe('ol.format.GML', function() {
describe('when parsing more than one geometry', function() {
var features, feature;
var features;
before(function(done) {
afterLoadText('spec/ol/format/gml/more-geoms.xml', function(xml) {
try {
@@ -805,7 +870,7 @@ describe('ol.format.GML', function() {
describe('when parsing an attribute name equal to featureType', function() {
var features, feature;
var features;
before(function(done) {
afterLoadText('spec/ol/format/gml/repeated-name.xml', function(xml) {
try {
@@ -842,3 +907,4 @@ goog.require('ol.geom.MultiPolygon');
goog.require('ol.xml');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.proj');

View File

@@ -1,6 +1,5 @@
goog.provide('ol.test.format.GPX');
describe('ol.format.GPX', function() {
var format;
@@ -80,6 +79,34 @@ describe('ol.format.GPX', function() {
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can transform, read and write a rte', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <rte>' +
' <rtept lat="1" lon="2"/>' +
' <rtept lat="5" lon="6"/>' +
' </rte>' +
'</gpx>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.LineString);
var p1 = ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857');
p1.push(0, 0);
var p2 = ol.proj.transform([6, 5], 'EPSG:4326', 'EPSG:3857');
p2.push(0, 0);
expect(g.getCoordinates()).to.eql([p1, p2]);
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
var serialized = format.writeFeatures(fs, {
featureProjection: 'EPSG:3857'
});
expect(serialized).to.xmleql(ol.xml.load(text));
});
});
describe('trk', function() {
@@ -181,6 +208,42 @@ describe('ol.format.GPX', function() {
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can tranform, read and write a trk with a trkseg', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <trk>' +
' <trkseg>' +
' <trkpt lat="1" lon="2">' +
' <ele>3</ele>' +
' <time>2010-01-10T09:29:12Z</time>' +
' </trkpt>' +
' <trkpt lat="5" lon="6">' +
' <ele>7</ele>' +
' <time>2010-01-10T09:30:12Z</time>' +
' </trkpt>' +
' </trkseg>' +
' </trk>' +
'</gpx>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.MultiLineString);
var p1 = ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857');
p1.push(3, 1263115752);
var p2 = ol.proj.transform([6, 5], 'EPSG:4326', 'EPSG:3857');
p2.push(7, 1263115812);
expect(g.getCoordinates()).to.eql([[p1, p2]]);
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
var serialized = format.writeFeatures(fs, {
featureProjection: 'EPSG:3857'
});
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can read and write a trk with multiple trksegs', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
@@ -243,6 +306,29 @@ describe('ol.format.GPX', function() {
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can transform, read and write a wpt', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <wpt lat="1" lon="2"/>' +
'</gpx>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
var expectedPoint = ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857');
expectedPoint.push(0, 0);
expect(g.getCoordinates()).to.eql(expectedPoint);
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
var serialized = format.writeFeatures(fs, {
featureProjection: 'EPSG:3857'
});
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can read and write a wpt with ele', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
@@ -471,4 +557,5 @@ goog.require('ol.format.GPX');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.Point');
goog.require('ol.proj');
goog.require('ol.xml');

View File

@@ -0,0 +1,121 @@
goog.provide('ol.test.format.IGC');
describe('ol.format.IGC', function() {
var format;
var igc =
'AFLY05094\n' +
'HFDTE190411\n' +
'HFFXA100\n' +
'HFPLTPILOT:Tom Payne\n' +
'HFGTYGLIDERTYPE:Axis Mercury\n' +
'HFGIDGLIDERID:\n' +
'HFDTM100GPSDATUM:WGS84\n' +
'HFGPSGPS:FURUNO GH-80\n' +
'HFRFWFIRMWAREVERSION:1.22\n' +
'HFRHWHARDWAREVERSION:1.00\n' +
'HFFTYFRTYPE:FLYTEC,5020\n' +
'I013638TAS\n' +
'B0848484556256N00651095EA0205102039000\n' +
'B0855534556037N00651011EA0259302513000\n' +
'B0903354554964N00648049EA0272402758000\n' +
'GAB890A77AFE5CE63979AF6B1BED7F07D\n' +
'G62BB282E44D63A1149EF2F5E8AF6F2F1\n' +
'GEC14381987B15F81003EDE1E01A47843\n' +
'G60189641B00B00800019000000000000';
beforeEach(function() {
format = new ol.format.IGC();
});
describe('#readFeature', function() {
it('does not read invalid features', function() {
expect(format.readFeature('invalid')).to.be(null);
});
it('does read a feature', function() {
var feature = format.readFeature(igc);
expect(feature).to.be.an(ol.Feature);
var geom = feature.getGeometry();
expect(geom.getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(geom.getCoordinates()).to.eql([
[6.851583333333333, 45.9376, 1303202928],
[6.850183333333334, 45.93395, 1303203353],
[6.800816666666667, 45.916066666666666, 1303203815]]);
});
it('does transform and read a feature', function() {
var feature = format.readFeature(igc, {
featureProjection: 'EPSG:3857'
});
expect(feature).to.be.an(ol.Feature);
var geom = feature.getGeometry();
expect(geom.getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
var expectedPoint1 = ol.proj.transform(
[6.851583333333333, 45.9376], 'EPSG:4326', 'EPSG:3857');
expectedPoint1.push(1303202928);
var expectedPoint2 = ol.proj.transform(
[6.850183333333334, 45.93395], 'EPSG:4326', 'EPSG:3857');
expectedPoint2.push(1303203353);
var expectedPoint3 = ol.proj.transform(
[6.800816666666667, 45.916066666666666], 'EPSG:4326', 'EPSG:3857');
expectedPoint3.push(1303203815);
expect(geom.getCoordinates()).to.eql(
[expectedPoint1, expectedPoint2, expectedPoint3]);
});
});
describe('#readFeatures', function() {
it('does not read invalid features', function() {
expect(format.readFeatures('invalid')).to.be.empty();
});
it('does read features', function() {
var features = format.readFeatures(igc);
expect(features.length).to.eql(1);
var feature = features[0];
expect(feature).to.be.an(ol.Feature);
var geom = feature.getGeometry();
expect(geom.getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(geom.getCoordinates()).to.eql([
[6.851583333333333, 45.9376, 1303202928],
[6.850183333333334, 45.93395, 1303203353],
[6.800816666666667, 45.916066666666666, 1303203815]]);
});
it('does transform and read features', function() {
var features = format.readFeatures(igc, {
featureProjection: 'EPSG:3857'
});
expect(features.length).to.eql(1);
var feature = features[0];
expect(feature).to.be.an(ol.Feature);
var geom = feature.getGeometry();
expect(geom.getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
var expectedPoint1 = ol.proj.transform(
[6.851583333333333, 45.9376], 'EPSG:4326', 'EPSG:3857');
expectedPoint1.push(1303202928);
var expectedPoint2 = ol.proj.transform(
[6.850183333333334, 45.93395], 'EPSG:4326', 'EPSG:3857');
expectedPoint2.push(1303203353);
var expectedPoint3 = ol.proj.transform(
[6.800816666666667, 45.916066666666666], 'EPSG:4326', 'EPSG:3857');
expectedPoint3.push(1303203815);
expect(geom.getCoordinates()).to.eql(
[expectedPoint1, expectedPoint2, expectedPoint3]);
});
});
});
goog.require('ol.format.IGC');
goog.require('ol.Feature');
goog.require('ol.geom.GeometryType');
goog.require('ol.proj');

View File

@@ -1,29 +0,0 @@
goog.provide('ol.test.format.IGC');
describe('ol.format.IGC', function() {
var format;
beforeEach(function() {
format = new ol.format.IGC();
});
describe('#readFeature', function() {
it('does not read invalid features', function() {
expect(format.readFeature('invalid')).to.be(null);
});
});
describe('#readFeatures', function() {
it('does not read invalid features', function() {
expect(format.readFeatures('invalid')).to.be.empty();
});
});
});
goog.require('ol.format.IGC');

View File

@@ -119,6 +119,76 @@ describe('ol.format.KML', function() {
expect(g.getCoordinates()).to.eql([1, 2, 3]);
});
it('can transform and read Point geometries', function() {
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</kml>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857');
expectedPoint.push(3);
expect(g.getCoordinates()).to.eql(expectedPoint);
});
it('can read a single Point geometry', function() {
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</kml>';
var f = format.readFeature(text);
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([1, 2, 3]);
});
it('can transform and read a single Point geometry', function() {
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</kml>';
var f = format.readFeature(text, {
featureProjection: 'EPSG:3857'
});
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857');
expectedPoint.push(3);
expect(g.getCoordinates()).to.eql(expectedPoint);
});
it('can write XY Point geometries', function() {
var layout = ol.geom.GeometryLayout.XY;
var point = new ol.geom.Point([1, 2], layout);
@@ -159,6 +229,29 @@ describe('ol.format.KML', function() {
expect(node).to.xmleql(ol.xml.load(text));
});
it('can transform and write XYZ Point geometries', function() {
var layout = ol.geom.GeometryLayout.XYZ;
var point = new ol.geom.Point([1, 2, 3], layout).transform(
'EPSG:4326', 'EPSG:3857');
var features = [new ol.Feature(point)];
var node = format.writeFeatures(features, {
featureProjection: 'EPSG:3857'
});
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</kml>';
expect(node).to.xmleql(ol.xml.load(text));
});
it('can write XYM Point geometries', function() {
var layout = ol.geom.GeometryLayout.XYM;
var point = new ol.geom.Point([1, 2, 100], layout);
@@ -1890,6 +1983,28 @@ describe('ol.format.KML', function() {
expect(fs[0]).to.be.an(ol.Feature);
});
it('can transform and read a single feature from a Document', function() {
var text =
'<Document xmlns="http://earth.google.com/kml/2.2">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</Document>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857');
expectedPoint.push(3);
expect(g.getCoordinates()).to.eql(expectedPoint);
});
it('can read a multiple features from a Document', function() {
var text =
'<Document xmlns="http://earth.google.com/kml/2.2">' +
@@ -2317,6 +2432,7 @@ goog.require('ol.geom.Polygon');
goog.require('ol.style.Fill');
goog.require('ol.style.Icon');
goog.require('ol.style.IconOrigin');
goog.require('ol.proj');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
goog.require('ol.style.Text');

View File

@@ -0,0 +1,105 @@
goog.provide('ol.test.format.OSMXML');
describe('ol.format.OSMXML', function() {
var format;
beforeEach(function() {
format = new ol.format.OSMXML();
});
describe('#readFeatures', function() {
it('can read an empty document', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(0);
});
it('can read nodes', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(2);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([2, 1]);
});
it('can read nodes and ways', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
' <way id="3">' +
' <tag k="name" v="3"/>' +
' <nd ref="1" />' +
' <nd ref="2" />' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(3);
var point = fs[0];
expect(point).to.be.an(ol.Feature);
var g = point.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([2, 1]);
var line = fs[2];
expect(line).to.be.an(ol.Feature);
g = line.getGeometry();
expect(g).to.be.an(ol.geom.LineString);
expect(g.getCoordinates()).to.eql([[2, 1], [4, 3]]);
});
it('can transform and read nodes', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(2);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql(
ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857'));
});
});
});
goog.require('goog.dom.xml');
goog.require('ol.Feature');
goog.require('ol.format.OSMXML');
goog.require('ol.geom.Point');
goog.require('ol.geom.LineString');
goog.require('ol.proj');

View File

@@ -11,13 +11,17 @@ describe('ol.format.Polyline', function() {
function resetTestingData() {
format = new ol.format.Polyline();
points = [[38.50000, -120.20000],
[40.70000, -120.95000],
[43.25200, -126.45300]];
flatPoints = [38.50000, -120.20000,
40.70000, -120.95000,
43.25200, -126.45300];
encodedFlatPoints = '_p~iF~ps|U_ulLnnqC_mqNvxq`@';
points = [[-120.20000, 38.50000],
[-120.95000, 40.70000],
[-126.45300, 43.25200]];
flatPoints = [-120.20000, 38.50000,
-120.95000, 40.70000,
-126.45300, 43.25200];
encodedFlatPoints = '~ps|U_p~iFnnqC_ulLvxq`@_mqN';
points3857 = [
ol.proj.transform([-120.20000, 38.50000], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([-120.95000, 40.70000], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([-126.45300, 43.25200], 'EPSG:4326', 'EPSG:3857')];
floats = [0.00, 0.15, -0.01, -0.16, 0.16, 0.01];
smallFloats = [0.00000, 0.00015, -0.00001, -0.00016, 0.00016, 0.00001];
@@ -253,6 +257,16 @@ describe('ol.format.Polyline', function() {
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('transforms and returns the expected feature', function() {
var feature = format.readFeature(encodedFlatPoints, {
featureProjection: 'EPSG:3857'
});
expect(feature).to.be.an(ol.Feature);
var geometry = feature.getGeometry();
expect(geometry).to.be.an(ol.geom.LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readFeatures', function() {
@@ -268,6 +282,19 @@ describe('ol.format.Polyline', function() {
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('transforms and returns the expected features', function() {
var features = format.readFeatures(encodedFlatPoints, {
featureProjection: 'EPSG:3857'
});
expect(features).to.be.an(Array);
expect(features).to.have.length(1);
var feature = features[0];
expect(feature).to.be.an(ol.Feature);
var geometry = feature.getGeometry();
expect(geometry).to.be.an(ol.geom.LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readGeometry', function() {
@@ -278,6 +305,14 @@ describe('ol.format.Polyline', function() {
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('transforms and returns the expected geometry', function() {
var geometry = format.readGeometry(encodedFlatPoints, {
featureProjection: 'EPSG:3857'
});
expect(geometry).to.be.an(ol.geom.LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readProjection', function() {
@@ -296,6 +331,13 @@ describe('ol.format.Polyline', function() {
expect(format.writeFeature(feature)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function() {
var feature = new ol.Feature(new ol.geom.LineString(points3857));
expect(format.writeFeature(feature, {
featureProjection: 'EPSG:3857'
})).to.be(encodedFlatPoints);
});
});
describe('#writeFeature', function() {
@@ -305,6 +347,13 @@ describe('ol.format.Polyline', function() {
expect(format.writeFeatures(features)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function() {
var features = [new ol.Feature(new ol.geom.LineString(points3857))];
expect(format.writeFeatures(features, {
featureProjection: 'EPSG:3857'
})).to.be(encodedFlatPoints);
});
});
describe('#writeGeometry', function() {
@@ -314,6 +363,13 @@ describe('ol.format.Polyline', function() {
expect(format.writeGeometry(geometry)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function() {
var geometry = new ol.geom.LineString(points3857);
expect(format.writeGeometry(geometry, {
featureProjection: 'EPSG:3857'
})).to.be(encodedFlatPoints);
});
});
});

View File

@@ -89,6 +89,41 @@ describe('ol.format.TopoJSON', function() {
});
});
it('parses simple.json and transforms', function(done) {
afterLoadText('spec/ol/format/topojson/simple.json', function(text) {
var features = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(features.length).to.be(3);
var point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(features[0].getGeometry().getCoordinates()).to.eql(
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
var line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getCoordinates()).to.eql([
ol.proj.transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857')
]);
var polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getCoordinates()).to.eql([[
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857')
]]);
done();
});
});
it('parses world-110m.json', function(done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function(text) {
@@ -123,4 +158,5 @@ goog.require('ol.Feature');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon');
goog.require('ol.format.Feature');
goog.require('ol.proj');
goog.require('ol.format.TopoJSON');

View File

@@ -4,15 +4,17 @@ describe('ol.format.WFS', function() {
describe('when parsing TOPP states GML from WFS', function() {
var features, feature;
before(function(done) {
proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
afterLoadText('spec/ol/format/wfs/topp-states-wfs.xml', function(xml) {
try {
var features, feature, xml;
var config = {
'featureNS': 'http://www.openplans.org/topp',
'featureType': 'states'
};
before(function(done) {
proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
afterLoadText('spec/ol/format/wfs/topp-states-wfs.xml', function(data) {
try {
xml = data;
features = new ol.format.WFS(config).readFeatures(xml);
} catch (e) {
done(e);
@@ -32,6 +34,20 @@ describe('ol.format.WFS', function() {
expect(feature.getGeometry()).to.be.an(ol.geom.MultiPolygon);
});
it('transforms and creates a polygon for Illinois', function() {
features = new ol.format.WFS(config).readFeatures(xml, {
featureProjection: 'EPSG:3857'
});
feature = features[0];
expect(feature.getId()).to.equal('states.1');
expect(feature.get('STATE_NAME')).to.equal('Illinois');
var geom = feature.getGeometry();
expect(geom).to.be.an(ol.geom.MultiPolygon);
var p = ol.proj.transform([-88.071, 37.511], 'EPSG:4326', 'EPSG:3857');
p.push(0);
expect(geom.getFirstCoordinate()).to.eql(p);
});
});
describe('when parsing FeatureCollection', function() {
@@ -388,3 +404,4 @@ goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon');
goog.require('ol.format.WFS');
goog.require('ol.proj');

View File

@@ -15,6 +15,21 @@ describe('ol.format.WKT', function() {
expect(geom.getCoordinates()).to.eql([30, 10]);
});
it('Point transformed / read / written correctly', function() {
var wkt = 'POINT(1 2)';
var geom = format.readGeometry(wkt, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(geom.getCoordinates()).to.eql(
ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'));
var newWkt = format.writeGeometry(geom, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(newWkt).to.eql(wkt);
});
it('MultiPoint read / written correctly', function() {
// there are two forms to test
var wkt = 'MULTIPOINT((10 40),(40 30),(20 20),(30 10))';
@@ -231,7 +246,67 @@ describe('ol.format.WKT', function() {
expect(format.writeFeatures(features)).to.eql(wkt);
});
it('Point feature read / written correctly', function() {
var wkt = 'POINT(30 10)';
var feature = format.readFeature(wkt);
var geom = feature.getGeometry();
expect(geom.getCoordinates()).to.eql([30, 10]);
expect(format.writeFeature(feature)).to.eql(wkt);
});
it('Point feature transformed / read / written correctly', function() {
var wkt = 'POINT(1 2)';
var feature = format.readFeature(wkt, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
var geom = feature.getGeometry();
expect(geom.getCoordinates()).to.eql(
ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'));
var newWkt = format.writeFeature(feature, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(newWkt).to.eql(wkt);
});
it('Features read / written correctly', function() {
var wkt = 'GEOMETRYCOLLECTION(POINT(1 2),POINT(3 4))';
var features = format.readFeatures(wkt);
expect(features.length).to.eql(2);
var point1 = features[0].getGeometry();
var point2 = features[1].getGeometry();
expect(point1.getType()).to.eql(ol.geom.GeometryType.POINT);
expect(point2.getType()).to.eql(ol.geom.GeometryType.POINT);
expect(point1.getCoordinates()).to.eql([1, 2]);
expect(point2.getCoordinates()).to.eql([3, 4]);
expect(format.writeFeatures(features)).to.eql(wkt);
});
it('Features transformed / read / written correctly', function() {
var wkt = 'GEOMETRYCOLLECTION(POINT(1 2),POINT(4 5))';
var features = format.readFeatures(wkt, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(features.length).to.eql(2);
var point1 = features[0].getGeometry();
var point2 = features[1].getGeometry();
expect(point1.getType()).to.eql(ol.geom.GeometryType.POINT);
expect(point2.getType()).to.eql(ol.geom.GeometryType.POINT);
expect(point1.getCoordinates()).to.eql(
ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'));
expect(point2.getCoordinates()).to.eql(
ol.proj.transform([4, 5], 'EPSG:4326', 'EPSG:3857'));
var newWkt = format.writeFeatures(features, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(newWkt).to.eql(wkt);
});
});
goog.require('ol.geom.GeometryType');
goog.require('ol.format.WKT');
goog.require('ol.proj');