Skip to content

Commit

Permalink
Don't assume that vector layer geometries are EPSG:4326 (#1162)
Browse files Browse the repository at this point in the history
* Don't assume that vector layer geometries are EPSG:4326

* Add test
  • Loading branch information
manisandro authored and mbarto committed Oct 18, 2016
1 parent 8e22fd8 commit 3fad9fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
38 changes: 38 additions & 0 deletions web/client/components/map/openlayers/__tests__/Layer-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,44 @@ describe('Openlayers layer', () => {
expect(map.getLayers().getLength()).toBe(1);
});

it('creates a vector layer specifying the feature CRS for openlayers map', () => {
var options = {
crs: 'EPSG:4326',
features: {
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:4326'
}
},
'featureCrs': 'EPSG:3857',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[
[1447153.3803125600, 5311971.8469454700],
[1669792.3618991000, 5311971.8469454700],
[1669792.3618991000, 5465442.1833227500],
[1447153.3803125600, 5465442.1833227500]
]]
}
}
]
}
};
// create layers
var layer = ReactDOM.render(
<OpenlayersLayer type="vector"
options={options} map={map}/>, document.getElementById("container"));

expect(layer).toExist();
// count layers
expect(map.getLayers().getLength()).toBe(1);
});

it('change layer visibility for Google Layer', () => {
var google = {
maps: {
Expand Down
6 changes: 5 additions & 1 deletion web/client/components/map/openlayers/plugins/VectorLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,17 @@ var styleFunction = function(feature) {
Layers.registerType('vector', {
create: (options) => {
let features;
let featuresCrs = options.featuresCrs || 'EPSG:4326';
let layerCrs = options.crs || 'EPSG:3857';
if (options.features) {
let featureCollection = options.features;
if (Array.isArray(options.features)) {
featureCollection = { "type": "FeatureCollection", features: featureCollection};
}
features = (new ol.format.GeoJSON()).readFeatures(featureCollection);
features.forEach((f) => f.getGeometry().transform('EPSG:4326', options.crs || 'EPSG:3857'));
if (featuresCrs !== layerCrs) {
features.forEach((f) => f.getGeometry().transform(featuresCrs, layerCrs));
}
}

const source = new ol.source.Vector({
Expand Down

0 comments on commit 3fad9fd

Please sign in to comment.