Skip to content

Commit

Permalink
Merge pull request #3388 from fredj/polyline-layout
Browse files Browse the repository at this point in the history
Add new geometry layout option for polyline format
  • Loading branch information
fredj committed Mar 24, 2015
2 parents ae2c914 + e669b20 commit 5c6c45f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
12 changes: 11 additions & 1 deletion externs/olx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,8 @@ olx.format.GeoJSONOptions.prototype.geometryName;


/**
* @typedef {{factor: (number|undefined)}}
* @typedef {{factor: (number|undefined),
* geometryLayout: (ol.geom.GeometryLayout|undefined)}}
* @api
*/
olx.format.PolylineOptions;
Expand All @@ -1633,6 +1634,15 @@ olx.format.PolylineOptions;
olx.format.PolylineOptions.prototype.factor;


/**
* Layout of the feature geometries created by the format reader.
* Default is `ol.geom.GeometryLayout.XY`.
* @type {ol.geom.GeometryLayout|undefined}
* @api
*/
olx.format.PolylineOptions.prototype.geometryLayout;


/**
* @typedef {{defaultDataProjection: ol.proj.ProjectionLike}}
* @api
Expand Down
18 changes: 14 additions & 4 deletions src/ol/format/polylineformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.TextFeature');
goog.require('ol.geom.LineString');
goog.require('ol.geom.SimpleGeometry');
goog.require('ol.geom.flat.flip');
goog.require('ol.geom.flat.inflate');
goog.require('ol.proj');
Expand Down Expand Up @@ -38,6 +39,13 @@ ol.format.Polyline = function(opt_options) {
* @type {number}
*/
this.factor_ = goog.isDef(options.factor) ? options.factor : 1e5;

/**
* @private
* @type {ol.geom.GeometryLayout}
*/
this.geometryLayout_ = goog.isDef(options.geometryLayout) ?
options.geometryLayout : ol.geom.GeometryLayout.XY;
};
goog.inherits(ol.format.Polyline, ol.format.TextFeature);

Expand Down Expand Up @@ -316,15 +324,17 @@ ol.format.Polyline.prototype.readGeometry;
*/
ol.format.Polyline.prototype.readGeometryFromText =
function(text, opt_options) {
var flatCoordinates = ol.format.Polyline.decodeDeltas(text, 2, this.factor_);
var stride = ol.geom.SimpleGeometry.getStrideForLayout(this.geometryLayout_);
var flatCoordinates = ol.format.Polyline.decodeDeltas(
text, stride, this.factor_);
ol.geom.flat.flip.flipXY(
flatCoordinates, 0, flatCoordinates.length, 2, flatCoordinates);
flatCoordinates, 0, flatCoordinates.length, stride, flatCoordinates);
var coordinates = ol.geom.flat.inflate.coordinates(
flatCoordinates, 0, flatCoordinates.length, 2);
flatCoordinates, 0, flatCoordinates.length, stride);

return /** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(
new ol.geom.LineString(coordinates), false,
new ol.geom.LineString(coordinates, this.geometryLayout_), false,
this.adaptOptions(opt_options)));
};

Expand Down
7 changes: 3 additions & 4 deletions src/ol/geom/simplegeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ ol.geom.SimpleGeometry.getLayoutForStride_ = function(stride) {

/**
* @param {ol.geom.GeometryLayout} layout Layout.
* @private
* @return {number} Stride.
*/
ol.geom.SimpleGeometry.getStrideForLayout_ = function(layout) {
ol.geom.SimpleGeometry.getStrideForLayout = function(layout) {
if (layout == ol.geom.GeometryLayout.XY) {
return 2;
} else if (layout == ol.geom.GeometryLayout.XYZ) {
Expand Down Expand Up @@ -200,7 +199,7 @@ ol.geom.SimpleGeometry.prototype.getStride = function() {
*/
ol.geom.SimpleGeometry.prototype.setFlatCoordinatesInternal =
function(layout, flatCoordinates) {
this.stride = ol.geom.SimpleGeometry.getStrideForLayout_(layout);
this.stride = ol.geom.SimpleGeometry.getStrideForLayout(layout);
this.layout = layout;
this.flatCoordinates = flatCoordinates;
};
Expand All @@ -217,7 +216,7 @@ ol.geom.SimpleGeometry.prototype.setLayout =
/** @type {number} */
var stride;
if (goog.isDef(layout)) {
stride = ol.geom.SimpleGeometry.getStrideForLayout_(layout);
stride = ol.geom.SimpleGeometry.getStrideForLayout(layout);
} else {
var i;
for (i = 0; i < nesting; ++i) {
Expand Down
19 changes: 19 additions & 0 deletions test/spec/ol/format/polylineformat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,25 @@ describe('ol.format.Polyline', function() {
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});

it('parses XYZ linestring', function() {
var xyz = ol.format.Polyline.encodeDeltas([
38.500, -120.200, 100,
40.700, -120.950, 200,
43.252, -126.453, 20
], 3);
var format = new ol.format.Polyline({
geometryLayout: ol.geom.GeometryLayout.XYZ
});

var geometry = format.readGeometry(xyz);
expect(geometry.getLayout()).to.eql(ol.geom.GeometryLayout.XYZ);
expect(geometry.getCoordinates()).to.eql([
[-120.200, 38.500, 100],
[-120.950, 40.700, 200],
[-126.453, 43.252, 20]
]);
});

it('transforms and returns the expected geometry', function() {
var geometry = format.readGeometry(encodedFlatPoints, {
featureProjection: 'EPSG:3857'
Expand Down

0 comments on commit 5c6c45f

Please sign in to comment.