Implement read/write transforms for ol.format.Polyline
This commit is contained in:
@@ -146,3 +146,24 @@ ol.format.Feature.transformWithOptions = function(
|
|||||||
return geometry;
|
return geometry;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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.Feature.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;
|
||||||
|
};
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ goog.require('goog.asserts');
|
|||||||
goog.require('goog.dom.NodeType');
|
goog.require('goog.dom.NodeType');
|
||||||
goog.require('goog.object');
|
goog.require('goog.object');
|
||||||
goog.require('ol.Feature');
|
goog.require('ol.Feature');
|
||||||
|
goog.require('ol.format.Feature');
|
||||||
goog.require('ol.format.XMLFeature');
|
goog.require('ol.format.XMLFeature');
|
||||||
goog.require('ol.format.XSD');
|
goog.require('ol.format.XSD');
|
||||||
goog.require('ol.geom.LineString');
|
goog.require('ol.geom.LineString');
|
||||||
@@ -866,7 +867,7 @@ ol.format.GPX.prototype.writeFeaturesNode = function(features, opt_options) {
|
|||||||
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');
|
||||||
|
|
||||||
// for convenience set a default dataProjection
|
// for convenience set a default dataProjection
|
||||||
opt_options = ol.format.XMLFeature.setDefaultDataProjection(
|
opt_options = ol.format.Feature.setDefaultDataProjection(
|
||||||
opt_options, this.readProjectionFromDocument(null));
|
opt_options, this.readProjectionFromDocument(null));
|
||||||
features = ol.format.XMLFeature.transformFeaturesWithOptions(
|
features = ol.format.XMLFeature.transformFeaturesWithOptions(
|
||||||
features, true, opt_options);
|
features, true, opt_options);
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ goog.require('ol.Feature');
|
|||||||
goog.require('ol.array');
|
goog.require('ol.array');
|
||||||
goog.require('ol.color');
|
goog.require('ol.color');
|
||||||
goog.require('ol.feature');
|
goog.require('ol.feature');
|
||||||
|
goog.require('ol.format.Feature');
|
||||||
goog.require('ol.format.XMLFeature');
|
goog.require('ol.format.XMLFeature');
|
||||||
goog.require('ol.format.XSD');
|
goog.require('ol.format.XSD');
|
||||||
goog.require('ol.geom.Geometry');
|
goog.require('ol.geom.Geometry');
|
||||||
@@ -2523,7 +2524,7 @@ ol.format.KML.prototype.writeFeaturesNode = function(features, opt_options) {
|
|||||||
ol.format.KML.SCHEMA_LOCATION_);
|
ol.format.KML.SCHEMA_LOCATION_);
|
||||||
|
|
||||||
// for convenience set a default dataProjection
|
// for convenience set a default dataProjection
|
||||||
opt_options = ol.format.XMLFeature.setDefaultDataProjection(
|
opt_options = ol.format.Feature.setDefaultDataProjection(
|
||||||
opt_options, this.readProjectionFromDocument(null));
|
opt_options, this.readProjectionFromDocument(null));
|
||||||
features = ol.format.XMLFeature.transformFeaturesWithOptions(
|
features = ol.format.XMLFeature.transformFeaturesWithOptions(
|
||||||
features, true, opt_options);
|
features, true, opt_options);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ goog.provide('ol.format.Polyline');
|
|||||||
|
|
||||||
goog.require('goog.asserts');
|
goog.require('goog.asserts');
|
||||||
goog.require('ol.Feature');
|
goog.require('ol.Feature');
|
||||||
|
goog.require('ol.format.Feature');
|
||||||
goog.require('ol.format.TextFeature');
|
goog.require('ol.format.TextFeature');
|
||||||
goog.require('ol.geom.LineString');
|
goog.require('ol.geom.LineString');
|
||||||
goog.require('ol.geom.flat.inflate');
|
goog.require('ol.geom.flat.inflate');
|
||||||
@@ -248,6 +249,7 @@ ol.format.Polyline.encodeUnsignedInteger = function(num) {
|
|||||||
*
|
*
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
@@ -257,8 +259,8 @@ ol.format.Polyline.prototype.readFeature;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.format.Polyline.prototype.readFeatureFromText = function(text) {
|
ol.format.Polyline.prototype.readFeatureFromText = function(text, opt_options) {
|
||||||
var geometry = this.readGeometryFromText(text);
|
var geometry = this.readGeometryFromText(text, opt_options);
|
||||||
return new ol.Feature(geometry);
|
return new ol.Feature(geometry);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -269,6 +271,7 @@ ol.format.Polyline.prototype.readFeatureFromText = function(text) {
|
|||||||
*
|
*
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
@@ -278,8 +281,9 @@ ol.format.Polyline.prototype.readFeatures;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.format.Polyline.prototype.readFeaturesFromText = function(text) {
|
ol.format.Polyline.prototype.readFeaturesFromText =
|
||||||
var feature = this.readFeatureFromText(text);
|
function(text, opt_options) {
|
||||||
|
var feature = this.readFeatureFromText(text, opt_options);
|
||||||
return [feature];
|
return [feature];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -289,6 +293,7 @@ ol.format.Polyline.prototype.readFeaturesFromText = function(text) {
|
|||||||
*
|
*
|
||||||
* @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.geom.Geometry} Geometry.
|
* @return {ol.geom.Geometry} Geometry.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -298,11 +303,18 @@ ol.format.Polyline.prototype.readGeometry;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @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 flatCoordinates = ol.format.Polyline.decodeDeltas(text, 2, this.factor_);
|
||||||
var coordinates = ol.geom.flat.inflate.coordinates(
|
var coordinates = ol.geom.flat.inflate.coordinates(
|
||||||
flatCoordinates, 0, flatCoordinates.length, 2);
|
flatCoordinates, 0, flatCoordinates.length, 2);
|
||||||
return new ol.geom.LineString(coordinates);
|
|
||||||
|
// for convenience set a default dataProjection
|
||||||
|
opt_options = ol.format.Feature.setDefaultDataProjection(
|
||||||
|
opt_options, this.readProjectionFromText(null));
|
||||||
|
|
||||||
|
return ol.format.Feature.transformWithOptions(
|
||||||
|
new ol.geom.LineString(coordinates), false, false, opt_options);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -328,10 +340,10 @@ ol.format.Polyline.prototype.readProjectionFromText = function(text) {
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.format.Polyline.prototype.writeFeatureText = function(feature) {
|
ol.format.Polyline.prototype.writeFeatureText = function(feature, opt_options) {
|
||||||
var geometry = feature.getGeometry();
|
var geometry = feature.getGeometry();
|
||||||
if (goog.isDefAndNotNull(geometry)) {
|
if (goog.isDefAndNotNull(geometry)) {
|
||||||
return this.writeGeometryText(geometry);
|
return this.writeGeometryText(geometry, opt_options);
|
||||||
} else {
|
} else {
|
||||||
goog.asserts.fail();
|
goog.asserts.fail();
|
||||||
return '';
|
return '';
|
||||||
@@ -342,9 +354,10 @@ ol.format.Polyline.prototype.writeFeatureText = function(feature) {
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.format.Polyline.prototype.writeFeaturesText = function(features) {
|
ol.format.Polyline.prototype.writeFeaturesText =
|
||||||
|
function(features, opt_options) {
|
||||||
goog.asserts.assert(features.length == 1);
|
goog.asserts.assert(features.length == 1);
|
||||||
return this.writeFeatureText(features[0]);
|
return this.writeFeatureText(features[0], opt_options);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -353,6 +366,7 @@ ol.format.Polyline.prototype.writeFeaturesText = function(features) {
|
|||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {ol.geom.Geometry} geometry Geometry.
|
* @param {ol.geom.Geometry} geometry Geometry.
|
||||||
|
* @param {olx.format.WriteOptions=} opt_options Write options.
|
||||||
* @return {string} Geometry.
|
* @return {string} Geometry.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -362,8 +376,14 @@ ol.format.Polyline.prototype.writeGeometry;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.format.Polyline.prototype.writeGeometryText = function(geometry) {
|
ol.format.Polyline.prototype.writeGeometryText =
|
||||||
|
function(geometry, opt_options) {
|
||||||
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
|
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
|
||||||
|
// for convenience set a default dataProjection
|
||||||
|
opt_options = ol.format.Feature.setDefaultDataProjection(
|
||||||
|
opt_options, this.readProjectionFromText(null));
|
||||||
|
geometry = ol.format.Feature.transformWithOptions(
|
||||||
|
geometry, true, true, opt_options);
|
||||||
var flatCoordinates = geometry.getFlatCoordinates();
|
var flatCoordinates = geometry.getFlatCoordinates();
|
||||||
var stride = geometry.getStride();
|
var stride = geometry.getStride();
|
||||||
return ol.format.Polyline.encodeDeltas(flatCoordinates, stride, this.factor_);
|
return ol.format.Polyline.encodeDeltas(flatCoordinates, stride, this.factor_);
|
||||||
|
|||||||
@@ -277,24 +277,3 @@ 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;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -11,13 +11,17 @@ describe('ol.format.Polyline', function() {
|
|||||||
|
|
||||||
function resetTestingData() {
|
function resetTestingData() {
|
||||||
format = new ol.format.Polyline();
|
format = new ol.format.Polyline();
|
||||||
points = [[38.50000, -120.20000],
|
points = [[-120.20000, 38.50000],
|
||||||
[40.70000, -120.95000],
|
[-120.95000, 40.70000],
|
||||||
[43.25200, -126.45300]];
|
[-126.45300, 43.25200]];
|
||||||
flatPoints = [38.50000, -120.20000,
|
flatPoints = [-120.20000, 38.50000,
|
||||||
40.70000, -120.95000,
|
-120.95000, 40.70000,
|
||||||
43.25200, -126.45300];
|
-126.45300, 43.25200];
|
||||||
encodedFlatPoints = '_p~iF~ps|U_ulLnnqC_mqNvxq`@';
|
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];
|
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];
|
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);
|
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() {
|
describe('#readFeatures', function() {
|
||||||
@@ -268,6 +282,19 @@ describe('ol.format.Polyline', function() {
|
|||||||
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
|
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() {
|
describe('#readGeometry', function() {
|
||||||
@@ -278,6 +305,14 @@ describe('ol.format.Polyline', function() {
|
|||||||
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
|
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() {
|
describe('#readProjection', function() {
|
||||||
@@ -296,6 +331,13 @@ describe('ol.format.Polyline', function() {
|
|||||||
expect(format.writeFeature(feature)).to.be(encodedFlatPoints);
|
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() {
|
describe('#writeFeature', function() {
|
||||||
@@ -305,6 +347,13 @@ describe('ol.format.Polyline', function() {
|
|||||||
expect(format.writeFeatures(features)).to.be(encodedFlatPoints);
|
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() {
|
describe('#writeGeometry', function() {
|
||||||
@@ -314,6 +363,13 @@ describe('ol.format.Polyline', function() {
|
|||||||
expect(format.writeGeometry(geometry)).to.be(encodedFlatPoints);
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user