Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept feature projection in GeoJSON constructor #5917

Merged
merged 1 commit into from Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion externs/olx.js
Expand Up @@ -1666,7 +1666,8 @@ olx.format.WriteOptions.prototype.decimals;

/**
* @typedef {{defaultDataProjection: ol.ProjectionLike,
* geometryName: (string|undefined)}}
* geometryName: (string|undefined),
* featureProjection: ol.ProjectionLike}}
*/
olx.format.GeoJSONOptions;

Expand All @@ -1679,6 +1680,15 @@ olx.format.GeoJSONOptions;
olx.format.GeoJSONOptions.prototype.defaultDataProjection;


/**
* Projection for features read or written by the format. Options passed to
* read or write methods will take precedence.
* @type {ol.ProjectionLike}
* @api stable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to mark this option stable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to call this stable. I'll merge as is, and if there is disagreement, we can change it before the release.

*/
olx.format.GeoJSONOptions.prototype.featureProjection;


/**
* Geometry name to use when creating features.
* @type {string|undefined}
Expand Down
24 changes: 11 additions & 13 deletions src/ol/format/feature.js
@@ -1,6 +1,7 @@
goog.provide('ol.format.Feature');

goog.require('ol.geom.Geometry');
goog.require('ol.obj');
goog.require('ol.proj');


Expand All @@ -24,6 +25,12 @@ ol.format.Feature = function() {
*/
this.defaultDataProjection = null;

/**
* @protected
* @type {ol.proj.Projection}
*/
this.defaultFeatureProjection = null;

};


Expand Down Expand Up @@ -64,19 +71,10 @@ ol.format.Feature.prototype.getReadOptions = function(source, opt_options) {
* Updated options.
*/
ol.format.Feature.prototype.adaptOptions = function(options) {
var updatedOptions;
if (options) {
updatedOptions = {
featureProjection: options.featureProjection,
dataProjection: options.dataProjection ?
options.dataProjection : this.defaultDataProjection,
rightHanded: options.rightHanded
};
if (options.decimals) {
updatedOptions.decimals = options.decimals;
}
}
return updatedOptions;
return ol.obj.assign({
dataProjection: this.defaultDataProjection,
featureProjection: this.defaultFeatureProjection
}, options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did rightHanded and decimals go?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If rightHanded or decimals are provided in option, these will be applied to the return.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes, I somehow didn't see the options being passed in. Thanks for the clarification, @tschaub.

};


Expand Down
4 changes: 4 additions & 0 deletions src/ol/format/geojson.js
Expand Up @@ -42,6 +42,10 @@ ol.format.GeoJSON = function(opt_options) {
options.defaultDataProjection : 'EPSG:4326');


if (options.featureProjection) {
this.defaultFeatureProjection = ol.proj.get(options.featureProjection);
}

/**
* Name of the geometry attribute for features.
* @type {string|undefined}
Expand Down
33 changes: 33 additions & 0 deletions test/spec/ol/format/geojson.test.js
Expand Up @@ -208,6 +208,24 @@ describe('ol.format.GeoJSON', function() {
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
});

it('uses featureProjection passed to the constructor', function() {
var format = new ol.format.GeoJSON({featureProjection: 'EPSG:3857'});
var feature = format.readFeatures(pointGeoJSON);
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('gives precedence to options passed to the read method', function() {
var format = new ol.format.GeoJSON({featureProjection: 'EPSG:1234'});
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'
Expand Down Expand Up @@ -578,6 +596,21 @@ describe('ol.format.GeoJSON', function() {
format.readGeometry(geojson).getCoordinates());
});

it('accepts featureProjection', function() {
var point = new ol.geom.Point(ol.proj.fromLonLat([10, 20]));
var geojson = format.writeGeometry(point, {featureProjection: 'EPSG:3857'});
var obj = JSON.parse(geojson);
expect(obj.coordinates).to.eql([10, 20]);
});

it('respects featureProjection passed to constructor', function() {
var format = new ol.format.GeoJSON({featureProjection: 'EPSG:3857'});
var point = new ol.geom.Point(ol.proj.fromLonLat([10, 20]));
var geojson = format.writeGeometry(point);
var obj = JSON.parse(geojson);
expect(obj.coordinates).to.eql([10, 20]);
});

it('encodes linestring', function() {
var linestring = new ol.geom.LineString([[10, 20], [30, 40]]);
var geojson = format.writeGeometry(linestring);
Expand Down