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

Add point measurement support #1339

Merged
merged 1 commit into from
Dec 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions web/client/actions/measurement.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ function changeMeasurement(measurement) {
function changeMeasurementState(measureState) {
return {
type: CHANGE_MEASUREMENT_STATE,
pointMeasureEnabled: measureState.pointMeasureEnabled,
lineMeasureEnabled: measureState.lineMeasureEnabled,
areaMeasureEnabled: measureState.areaMeasureEnabled,
bearingMeasureEnabled: measureState.bearingMeasureEnabled,
geomType: measureState.geomType,
point: measureState.point,
len: measureState.len,
area: measureState.area,
bearing: measureState.bearing,
Expand Down
25 changes: 20 additions & 5 deletions web/client/components/map/leaflet/MeasurementSupport.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

const React = require('react');
const assign = require('object-assign');
var L = require('leaflet');
var CoordinatesUtils = require('../../../utils/CoordinatesUtils');

Expand Down Expand Up @@ -49,13 +50,19 @@ const MeasurementSupport = React.createClass({
this.props.map.addLayer(evt.layer);
// preserve the currently created layer to remove it later on
this.lastLayer = evt.layer;

if (this.props.measurement.geomType === 'Point') {
let pos = this.drawControl._marker.getLatLng();
let point = {x: pos.lng, y: pos.lat, srs: 'EPSG:4326'};
let newMeasureState = assign({}, this.props.measurement, {point: point});
this.props.changeMeasurementState(newMeasureState);
}
}
},
render() {
return null;
},
mapClickHandler: function() {
var latLngs;
var area;
var newMeasureState;
var bearingMarkers;
Expand All @@ -75,9 +82,11 @@ const MeasurementSupport = React.createClass({
} else {
// update measurement results for every new vertex drawn

// calculate possible length / area
latLngs = this.drawControl._poly.getLatLngs();
area = L.GeometryUtil.geodesicArea(latLngs);
// calculate area
if (this.props.measurement.geomType === 'Polygon') {
let latLngs = this.drawControl._poly.getLatLngs();
area = L.GeometryUtil.geodesicArea(latLngs);
}

// calculate bearing
if (this.props.measurement.geomType === 'Bearing') {
Expand All @@ -101,10 +110,12 @@ const MeasurementSupport = React.createClass({
}

newMeasureState = {
pointMeasureEnabled: this.props.measurement.pointMeasureEnabled,
lineMeasureEnabled: this.props.measurement.lineMeasureEnabled,
areaMeasureEnabled: this.props.measurement.areaMeasureEnabled,
bearingMeasureEnabled: this.props.measurement.bearingMeasureEnabled,
geomType: this.props.measurement.geomType,
point: null, // handled in onDraw.created
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably we also need a pointMeasureEnabled: ... here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, commit amended. BTW I'll have a followup PR which will clean up the code a bit.

len: this.props.measurement.geomType === 'LineString' ? this.drawControl._measurementRunningTotal : 0,
area: this.props.measurement.geomType === 'Polygon' ? area : 0,
bearing: bearing
Expand All @@ -120,7 +131,11 @@ const MeasurementSupport = React.createClass({
this.props.map.on('draw:drawstart', this.onDraw.drawStart, this);
this.props.map.on('click', this.mapClickHandler, this);

if (newProps.measurement.geomType === 'LineString' ||
if (newProps.measurement.geomType === 'Point') {
this.drawControl = new L.Draw.Marker(this.props.map, {
repeatMode: false
});
} else if (newProps.measurement.geomType === 'LineString' ||
newProps.measurement.geomType === 'Bearing') {
this.drawControl = new L.Draw.Polyline(this.props.map, {
shapeOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,38 @@ describe('Leaflet MeasurementSupport', () => {
});
});

it('test map onClick handler for POINT tool', () => {
let newMeasureState;
let map = L.map("map", {
center: [51.505, -0.09],
zoom: 13
});
let proj = "EPSG:3857";
let measurement = {
geomType: null
};
const cmp = ReactDOM.render(
<MeasurementSupport
map={map}
projection={proj}
measurement={measurement}
changeMeasurementState={(data) => {newMeasureState = data; }}
/>
, msNode);
expect(cmp).toExist();

cmp.setProps({
measurement: {
geomType: "Point"
}
}, () => {
document.getElementById('map').addEventListener('click', () => {
expect(newMeasureState).toExist();
});
document.getElementById('map').click();
});
});

it('test map onClick handler for LINE tool', () => {
let newMeasureState;
let map = L.map("map", {
Expand Down
6 changes: 6 additions & 0 deletions web/client/components/map/openlayers/MeasurementSupport.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const MeasurementSupport = React.createClass({
this.removeDrawInteraction();
}
},
getPointCoordinate: function(coordinate) {
return CoordinatesUtils.reproject(coordinate, this.props.projection, 'EPSG:4326');
},
render() {
return null;
},
Expand Down Expand Up @@ -134,10 +137,13 @@ const MeasurementSupport = React.createClass({
}

newMeasureState = {
pointMeasureEnabled: this.props.measurement.pointMeasureEnabled,
lineMeasureEnabled: this.props.measurement.lineMeasureEnabled,
areaMeasureEnabled: this.props.measurement.areaMeasureEnabled,
bearingMeasureEnabled: this.props.measurement.bearingMeasureEnabled,
geomType: this.props.measurement.geomType,
point: this.props.measurement.geomType === 'Point' ?
this.getPointCoordinate(sketchCoords) : null,
len: this.props.measurement.geomType === 'LineString' ?
this.calculateGeodesicDistance(sketchCoords) : 0,
area: this.props.measurement.geomType === 'Polygon' ?
Expand Down
3 changes: 3 additions & 0 deletions web/client/reducers/measurement.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ function measurement(state = {
switch (action.type) {
case CHANGE_MEASUREMENT_TOOL:
return assign({}, state, {
pointMeasureEnabled: ((action.geomType !== state.geomType) && (action.geomType === 'Point')),
lineMeasureEnabled: ((action.geomType !== state.geomType) && (action.geomType === 'LineString')),
areaMeasureEnabled: ((action.geomType !== state.geomType) && (action.geomType === 'Polygon')),
bearingMeasureEnabled: ((action.geomType !== state.geomType) && (action.geomType === 'Bearing')),
geomType: (action.geomType === state.geomType) ? null : action.geomType
});
case CHANGE_MEASUREMENT_STATE:
return assign({}, state, {
pointMeasureEnabled: action.pointMeasureEnabled,
lineMeasureEnabled: action.lineMeasureEnabled,
areaMeasureEnabled: action.areaMeasureEnabled,
bearingMeasureEnabled: action.bearingMeasureEnabled,
geomType: action.geomType,
point: action.point,
len: action.len,
area: action.area,
bearing: action.bearing,
Expand Down