Implement read/write transforms for ol.format.KML
This commit is contained in:
@@ -864,17 +864,13 @@ ol.format.GPX.prototype.writeFeatures;
|
|||||||
ol.format.GPX.prototype.writeFeaturesNode = function(features, opt_options) {
|
ol.format.GPX.prototype.writeFeaturesNode = function(features, opt_options) {
|
||||||
//FIXME Serialize metadata
|
//FIXME Serialize metadata
|
||||||
var gpx = ol.xml.createElementNS('http://www.topografix.com/GPX/1/1', 'gpx');
|
var gpx = ol.xml.createElementNS('http://www.topografix.com/GPX/1/1', 'gpx');
|
||||||
if (goog.isDef(opt_options)) {
|
|
||||||
if (!goog.isDef(opt_options.dataProjection)) {
|
// for convenience set a default dataProjection
|
||||||
// for convenience set a default dataProjection
|
opt_options = ol.format.XMLFeature.setDefaultDataProjection(
|
||||||
opt_options = {
|
opt_options, this.readProjectionFromDocument(null));
|
||||||
featureProjection: opt_options.featureProjection,
|
|
||||||
dataProjection: this.readProjectionFromDocument(null)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
features = ol.format.XMLFeature.transformFeaturesWithOptions(
|
features = ol.format.XMLFeature.transformFeaturesWithOptions(
|
||||||
features, true, opt_options);
|
features, true, opt_options);
|
||||||
|
|
||||||
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
|
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
|
||||||
({node: gpx}), ol.format.GPX.GPX_SERIALIZERS_,
|
({node: gpx}), ol.format.GPX.GPX_SERIALIZERS_,
|
||||||
ol.format.GPX.GPX_NODE_FACTORY_, features, []);
|
ol.format.GPX.GPX_NODE_FACTORY_, features, []);
|
||||||
|
|||||||
@@ -1487,6 +1487,7 @@ ol.format.KML.prototype.readSharedStyleMap_ = function(node, objectStack) {
|
|||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @param {olx.format.ReadOptions=} opt_options Read options.
|
||||||
* @return {ol.Feature} Feature.
|
* @return {ol.Feature} Feature.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -1496,7 +1497,7 @@ ol.format.KML.prototype.readFeature;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @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);
|
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
|
||||||
if (!goog.array.contains(ol.format.KML.NAMESPACE_URIS_, node.namespaceURI)) {
|
if (!goog.array.contains(ol.format.KML.NAMESPACE_URIS_, node.namespaceURI)) {
|
||||||
return null;
|
return null;
|
||||||
@@ -1504,6 +1505,8 @@ ol.format.KML.prototype.readFeatureFromNode = function(node) {
|
|||||||
goog.asserts.assert(node.localName == 'Placemark');
|
goog.asserts.assert(node.localName == 'Placemark');
|
||||||
var feature = this.readPlacemark_(node, []);
|
var feature = this.readPlacemark_(node, []);
|
||||||
if (goog.isDef(feature)) {
|
if (goog.isDef(feature)) {
|
||||||
|
ol.format.XMLFeature.transformFeaturesWithOptions(
|
||||||
|
[feature], false, this.getReadOptions(node, opt_options));
|
||||||
return feature;
|
return feature;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
@@ -1516,6 +1519,7 @@ ol.format.KML.prototype.readFeatureFromNode = function(node) {
|
|||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @param {olx.format.ReadOptions=} opt_options Read options.
|
||||||
* @return {Array.<ol.Feature>} Features.
|
* @return {Array.<ol.Feature>} Features.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -1525,7 +1529,7 @@ ol.format.KML.prototype.readFeatures;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @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);
|
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
|
||||||
if (!goog.array.contains(ol.format.KML.NAMESPACE_URIS_, node.namespaceURI)) {
|
if (!goog.array.contains(ol.format.KML.NAMESPACE_URIS_, node.namespaceURI)) {
|
||||||
return [];
|
return [];
|
||||||
@@ -1535,6 +1539,8 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) {
|
|||||||
if (localName == 'Document' || localName == 'Folder') {
|
if (localName == 'Document' || localName == 'Folder') {
|
||||||
features = this.readDocumentOrFolder_(node, []);
|
features = this.readDocumentOrFolder_(node, []);
|
||||||
if (goog.isDef(features)) {
|
if (goog.isDef(features)) {
|
||||||
|
ol.format.XMLFeature.transformFeaturesWithOptions(
|
||||||
|
features, false, this.getReadOptions(node, opt_options));
|
||||||
return features;
|
return features;
|
||||||
} else {
|
} else {
|
||||||
return [];
|
return [];
|
||||||
@@ -1542,6 +1548,8 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) {
|
|||||||
} else if (localName == 'Placemark') {
|
} else if (localName == 'Placemark') {
|
||||||
var feature = this.readPlacemark_(node, []);
|
var feature = this.readPlacemark_(node, []);
|
||||||
if (goog.isDef(feature)) {
|
if (goog.isDef(feature)) {
|
||||||
|
ol.format.XMLFeature.transformFeaturesWithOptions(
|
||||||
|
[feature], false, this.getReadOptions(node, opt_options));
|
||||||
return [feature];
|
return [feature];
|
||||||
} else {
|
} else {
|
||||||
return [];
|
return [];
|
||||||
@@ -1551,7 +1559,7 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) {
|
|||||||
var n;
|
var n;
|
||||||
for (n = node.firstElementChild; !goog.isNull(n);
|
for (n = node.firstElementChild; !goog.isNull(n);
|
||||||
n = n.nextElementSibling) {
|
n = n.nextElementSibling) {
|
||||||
var fs = this.readFeaturesFromNode(n);
|
var fs = this.readFeaturesFromNode(n, opt_options);
|
||||||
if (goog.isDef(fs)) {
|
if (goog.isDef(fs)) {
|
||||||
goog.array.extend(features, fs);
|
goog.array.extend(features, fs);
|
||||||
}
|
}
|
||||||
@@ -2494,6 +2502,7 @@ ol.format.KML.OUTER_BOUNDARY_NODE_FACTORY_ =
|
|||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {Array.<ol.Feature>} features Features.
|
* @param {Array.<ol.Feature>} features Features.
|
||||||
|
* @param {olx.format.WriteOptions=} opt_options Options.
|
||||||
* @return {Node} Result.
|
* @return {Node} Result.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -2503,7 +2512,7 @@ ol.format.KML.prototype.writeFeatures;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @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 kml = ol.xml.createElementNS(ol.format.KML.NAMESPACE_URIS_[4], 'kml');
|
||||||
var xmlnsUri = 'http://www.w3.org/2000/xmlns/';
|
var xmlnsUri = 'http://www.w3.org/2000/xmlns/';
|
||||||
var xmlSchemaInstanceUri = 'http://www.w3.org/2001/XMLSchema-instance';
|
var xmlSchemaInstanceUri = 'http://www.w3.org/2001/XMLSchema-instance';
|
||||||
@@ -2513,6 +2522,12 @@ ol.format.KML.prototype.writeFeaturesNode = function(features) {
|
|||||||
ol.xml.setAttributeNS(kml, xmlSchemaInstanceUri, 'xsi:schemaLocation',
|
ol.xml.setAttributeNS(kml, xmlSchemaInstanceUri, 'xsi:schemaLocation',
|
||||||
ol.format.KML.SCHEMA_LOCATION_);
|
ol.format.KML.SCHEMA_LOCATION_);
|
||||||
|
|
||||||
|
// for convenience set a default dataProjection
|
||||||
|
opt_options = ol.format.XMLFeature.setDefaultDataProjection(
|
||||||
|
opt_options, this.readProjectionFromDocument(null));
|
||||||
|
features = ol.format.XMLFeature.transformFeaturesWithOptions(
|
||||||
|
features, true, opt_options);
|
||||||
|
|
||||||
var /** @type {ol.xml.NodeStackItem} */ context = {node: kml};
|
var /** @type {ol.xml.NodeStackItem} */ context = {node: kml};
|
||||||
var properties = {};
|
var properties = {};
|
||||||
if (features.length > 1) {
|
if (features.length > 1) {
|
||||||
|
|||||||
@@ -277,3 +277,24 @@ ol.format.XMLFeature.transformFeaturesWithOptions = function(
|
|||||||
}
|
}
|
||||||
return features;
|
return features;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {(olx.format.WriteOptions|olx.format.ReadOptions)=} opt_options
|
||||||
|
* Options.
|
||||||
|
* @param {ol.proj.ProjectionLike} defaultDataProjection Default projection.
|
||||||
|
* @protected
|
||||||
|
* @return {(olx.format.WriteOptions|olx.format.ReadOptions)=} Updated options.
|
||||||
|
*/
|
||||||
|
ol.format.XMLFeature.setDefaultDataProjection = function(
|
||||||
|
opt_options, defaultDataProjection) {
|
||||||
|
if (goog.isDef(opt_options)) {
|
||||||
|
if (!goog.isDef(opt_options.dataProjection)) {
|
||||||
|
opt_options = {
|
||||||
|
featureProjection: opt_options.featureProjection,
|
||||||
|
dataProjection: defaultDataProjection
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return opt_options;
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
goog.provide('ol.test.format.GPX');
|
goog.provide('ol.test.format.GPX');
|
||||||
|
|
||||||
goog.require('ol.proj');
|
|
||||||
|
|
||||||
describe('ol.format.GPX', function() {
|
describe('ol.format.GPX', function() {
|
||||||
|
|
||||||
var format;
|
var format;
|
||||||
@@ -559,4 +557,5 @@ goog.require('ol.format.GPX');
|
|||||||
goog.require('ol.geom.LineString');
|
goog.require('ol.geom.LineString');
|
||||||
goog.require('ol.geom.MultiLineString');
|
goog.require('ol.geom.MultiLineString');
|
||||||
goog.require('ol.geom.Point');
|
goog.require('ol.geom.Point');
|
||||||
|
goog.require('ol.proj');
|
||||||
goog.require('ol.xml');
|
goog.require('ol.xml');
|
||||||
|
|||||||
@@ -119,6 +119,76 @@ describe('ol.format.KML', function() {
|
|||||||
expect(g.getCoordinates()).to.eql([1, 2, 3]);
|
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() {
|
it('can write XY Point geometries', function() {
|
||||||
var layout = ol.geom.GeometryLayout.XY;
|
var layout = ol.geom.GeometryLayout.XY;
|
||||||
var point = new ol.geom.Point([1, 2], layout);
|
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));
|
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() {
|
it('can write XYM Point geometries', function() {
|
||||||
var layout = ol.geom.GeometryLayout.XYM;
|
var layout = ol.geom.GeometryLayout.XYM;
|
||||||
var point = new ol.geom.Point([1, 2, 100], layout);
|
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);
|
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() {
|
it('can read a multiple features from a Document', function() {
|
||||||
var text =
|
var text =
|
||||||
'<Document xmlns="http://earth.google.com/kml/2.2">' +
|
'<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.Fill');
|
||||||
goog.require('ol.style.Icon');
|
goog.require('ol.style.Icon');
|
||||||
goog.require('ol.style.IconOrigin');
|
goog.require('ol.style.IconOrigin');
|
||||||
|
goog.require('ol.proj');
|
||||||
goog.require('ol.style.Stroke');
|
goog.require('ol.style.Stroke');
|
||||||
goog.require('ol.style.Style');
|
goog.require('ol.style.Style');
|
||||||
goog.require('ol.style.Text');
|
goog.require('ol.style.Text');
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
goog.provide('ol.test.format.TopoJSON');
|
goog.provide('ol.test.format.TopoJSON');
|
||||||
|
|
||||||
goog.require('ol.proj');
|
|
||||||
|
|
||||||
var aruba = {
|
var aruba = {
|
||||||
type: 'Topology',
|
type: 'Topology',
|
||||||
transform: {
|
transform: {
|
||||||
@@ -160,4 +158,5 @@ goog.require('ol.Feature');
|
|||||||
goog.require('ol.geom.MultiPolygon');
|
goog.require('ol.geom.MultiPolygon');
|
||||||
goog.require('ol.geom.Polygon');
|
goog.require('ol.geom.Polygon');
|
||||||
goog.require('ol.format.Feature');
|
goog.require('ol.format.Feature');
|
||||||
|
goog.require('ol.proj');
|
||||||
goog.require('ol.format.TopoJSON');
|
goog.require('ol.format.TopoJSON');
|
||||||
|
|||||||
Reference in New Issue
Block a user