Skip to content

Commit

Permalink
Fix 2491. Avoid wrong coordinate changes
Browse files Browse the repository at this point in the history
  • Loading branch information
offtherailz committed Dec 15, 2017
1 parent b59c2dc commit 8252235
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
7 changes: 4 additions & 3 deletions web/client/components/map/openlayers/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ class OpenlayersMap extends React.Component {
map.forEachFeatureAtPixel(event.pixel, (feature, layer) => {
if (layer && layer.get('handleClickOnLayer')) {
layerInfo = layer.get('msId');
const geom = feature.getGeometry();
// TODO getFirstCoordinate makes sense only for points, maybe centroid is more appropriate
const getCoord = geom.getType() === "GeometryCollection" ? geom.getGeometries()[0].getFirstCoordinate() : geom.getFirstCoordinate();
coords = ol.proj.toLonLat(getCoord, this.props.projection);
}
const geom = feature.getGeometry();
const getCoord = geom.getType() === "GeometryCollection" ? geom.getGeometries()[0].getFirstCoordinate() : geom.getFirstCoordinate();
coords = ol.proj.toLonLat(getCoord, this.props.projection);
tLng = CoordinatesUtils.normalizeLng(coords[0]);
});
this.props.onClick({
Expand Down
80 changes: 80 additions & 0 deletions web/client/components/map/openlayers/__tests__/Map-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,86 @@ describe('OpenlayersMap', () => {
done();
}, 500);
});
it('click on feature preserves clicked point', (done) => {
const testHandlers = {
handler: () => {}
};
const spy = expect.spyOn(testHandlers, 'handler');
const comp = (<OpenlayersMap projection="EPSG:4326" center={{y: 43.9, x: 10.3}} zoom={11}
onClick={testHandlers.handler}/>);
const map = ReactDOM.render(comp, document.getElementById("map"));
expect(map).toExist();
setTimeout(() => {
map.map.forEachFeatureAtPixel = (pixel, callback) => {
callback.call(null, {
feature: new ol.Feature({
geometry: new ol.geom.Polygon([ [0, 0], [0, 1], [1, 1], [1, 0], [0, 0] ]),
name: 'My Point'
}),
getGeometry: () => {
return {
getFirstCoordinate: () => [10.3, 43.9],
getType: () => {
return 'Polygon';
}
};
}
}, {
get: (key) => key === "handleClickOnLayer" ? false : "ID"
});
};
map.map.dispatchEvent({
type: 'singleclick',
coordinate: [0.5, 0.5],
pixel: map.map.getPixelFromCoordinate([0.5, 0.5]),
originalEvent: {}
});
expect(spy.calls.length).toEqual(1);
expect(spy.calls[0].arguments[0].latlng.lat).toBe(0.5);
expect(spy.calls[0].arguments[0].latlng.lng).toBe(0.5);
done();
}, 500);
});
it('click on feature for handleClickOnLayer', (done) => {
const testHandlers = {
handler: () => {}
};
const spy = expect.spyOn(testHandlers, 'handler');
const comp = (<OpenlayersMap projection="EPSG:4326" center={{y: 43.9, x: 10.3}} zoom={11}
onClick={testHandlers.handler}/>);
const map = ReactDOM.render(comp, document.getElementById("map"));
expect(map).toExist();
setTimeout(() => {
map.map.forEachFeatureAtPixel = (pixel, callback) => {
callback.call(null, {
feature: new ol.Feature({
geometry: new ol.geom.Polygon([ [0, 0], [0, 1], [1, 1], [1, 0], [0, 0] ]),
name: 'My Point'
}),
getGeometry: () => {
return {
getFirstCoordinate: () => [10.3, 43.9], // this makes sense only for points, maybe centroid is more appropriate
getType: () => {
return 'Polygon';
}
};
}
}, {
get: (key) => key === "handleClickOnLayer" ? true : "ID"
});
};
map.map.dispatchEvent({
type: 'singleclick',
coordinate: [0.5, 0.5],
pixel: map.map.getPixelFromCoordinate([0.5, 0.5]),
originalEvent: {}
});
expect(spy.calls.length).toEqual(1);
expect(spy.calls[0].arguments[0].latlng.lat).toBe(43.9);
expect(spy.calls[0].arguments[0].latlng.lng).toBe(10.3);
done();
}, 500);
});

it('check layers init', () => {
var options = {
Expand Down

0 comments on commit 8252235

Please sign in to comment.