Skip to content

Commit

Permalink
improve DistanceTool to work correctly during Map zoom in and zoom ou…
Browse files Browse the repository at this point in the history
…t actions (#333)

* improve DistanceTool to work correctly during Map zoom in and zoom out
actions and also display last segment length

Signed-off-by: Nikolaos Pringouris <[email protected]>
  • Loading branch information
nprigour authored and fgdrf committed Feb 22, 2019
1 parent 5c9f993 commit a747cde
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public DistanceTool() {
super(MOUSE | MOTION);
}

List<Point> points = new ArrayList<Point>();
List<Coordinate> points = new ArrayList<Coordinate>();
DistanceFeedbackCommand command;
private Point now;
private Coordinate now;

double lastSegmentDistance = 0;

@Override
protected void onMouseMoved( MapMouseEvent e ) {
now = e.getPoint();
now = getContext().pixelToWorld(e.x, e.y);
if (command == null || points.isEmpty())
return;
Rectangle area = command.getValidArea();
Expand All @@ -66,7 +68,7 @@ protected void onMouseMoved( MapMouseEvent e ) {
}

public void onMouseReleased( MapMouseEvent e ) {
Point current = e.getPoint();
Coordinate current = getContext().pixelToWorld(e.x, e.y);
if (points.isEmpty() || !current.equals(points.get(points.size() - 1)))
points.add(current);
if (command == null || !command.isValid()) {
Expand Down Expand Up @@ -137,25 +139,24 @@ public void run() {
private double distance() throws TransformException {
if (points.isEmpty())
return 0;
Iterator<Point> iter = points.iterator();
Point start = iter.next();
Iterator<Coordinate> iter = points.iterator();
Coordinate start = iter.next();
double distance = 0;
double lastSegment = 0;
while( iter.hasNext() ) {
Point current = iter.next();
Coordinate begin = getContext().pixelToWorld(start.x, start.y);
Coordinate end = getContext().pixelToWorld(current.x, current.y);
distance += JTS.orthodromicDistance(begin, end, getContext().getCRS());
Coordinate current = iter.next();
lastSegment = JTS.orthodromicDistance(start, current, getContext().getCRS());
distance += lastSegment;
start = current;
}

if (now != null) {
Point current = now;
Coordinate begin = getContext().pixelToWorld(start.x, start.y);
Coordinate end = getContext().pixelToWorld(current.x, current.y);
distance += JTS.orthodromicDistance(begin, end, getContext().getCRS());
start = current;
Coordinate current = now;
lastSegment = JTS.orthodromicDistance(start, current, getContext().getCRS());
distance += lastSegment;
}

this.lastSegmentDistance = lastSegment;

return distance;
}

Expand Down Expand Up @@ -187,12 +188,15 @@ private void displayOnStatusBar( double distance ) {
}

final Measure<Double, Length> distanceInMeter = Measure.valueOf(distance, SI.METER);

final Measure<Double, Length> lastSegmentInMeter = Measure.valueOf(lastSegmentDistance, SI.METER);

Measure<Double, Length> result = null;
Measure<Double, Length> resultLastSegment = null;
if (units.equals( org.locationtech.udig.ui.preferences.PreferenceConstants.IMPERIAL_UNITS)){
Measure<Double, Length> distanceInMiles = distanceInMeter.to(NonSI.MILE);
Measure<Double, Length> lastSegmentInMiles = lastSegmentInMeter.to(NonSI.MILE);
double distInMilesValue = distanceInMiles.getValue().doubleValue();

double lastSegmentInMilesValue = lastSegmentInMiles.getValue().doubleValue();

if (distInMilesValue > Measure.valueOf(1, NonSI.MILE).doubleValue(NonSI.MILE)) {
// everything longer than a mile
Expand All @@ -204,9 +208,21 @@ private void displayOnStatusBar( double distance ) {
// shorter than a foot
result = distanceInMiles.to(NonSI.INCH);
}

if (lastSegmentInMilesValue > Measure.valueOf(1, NonSI.MILE).doubleValue(NonSI.MILE)) {
// everything longer than a mile
resultLastSegment = lastSegmentInMiles;
} else if (lastSegmentInMilesValue > Measure.valueOf(1, NonSI.FOOT).doubleValue(NonSI.MILE)) {
// everything longer that a foot
resultLastSegment = lastSegmentInMiles.to(NonSI.FOOT);
} else {
// shorter than a foot
resultLastSegment = lastSegmentInMiles.to(NonSI.INCH);
}
} else {
double distanceInMeterValue = distanceInMeter.getValue().doubleValue();

double lastSegmentInMeterValue = lastSegmentInMeter.getValue().doubleValue();

if (distanceInMeterValue > Measure.valueOf(1000, SI.METER).doubleValue(SI.METER)) {
result = distanceInMeter.to(SI.KILOMETER);
} else if (distanceInMeterValue > Measure.valueOf(1, SI.METER).doubleValue(SI.METER)) {
Expand All @@ -216,9 +232,21 @@ private void displayOnStatusBar( double distance ) {
} else {
result = distanceInMeter.to(SI.MILLIMETER);
}

if (lastSegmentInMeterValue > Measure.valueOf(1000, SI.METER).doubleValue(SI.METER)) {
resultLastSegment = lastSegmentInMeter.to(SI.KILOMETER);
} else if (lastSegmentInMeterValue > Measure.valueOf(1, SI.METER).doubleValue(SI.METER)) {
resultLastSegment = lastSegmentInMeter.to(SI.METER);
} else if (lastSegmentInMeterValue > Measure.valueOf(1, SI.CENTIMETER).doubleValue(SI.METER)) {
resultLastSegment = lastSegmentInMeter.to(SI.CENTIMETER);
} else {
resultLastSegment = lastSegmentInMeter.to(SI.MILLIMETER);
}
}

final String message = MessageFormat.format(Messages.DistanceTool_distance, round(result.getValue(), 2) + " " + result.getUnit());
final String message = MessageFormat.format(Messages.DistanceTool_distance,
round(result.getValue(), 2) + " " + result.getUnit(),
round(resultLastSegment.getValue(), 2) + " " + resultLastSegment.getUnit());

getContext().updateUI(new Runnable(){
public void run() {
Expand Down Expand Up @@ -257,16 +285,17 @@ public void run( IProgressMonitor monitor ) throws Exception {
if (points.isEmpty())
return;
graphics.setColor(Color.BLACK);
Iterator<Point> iter = points.iterator();
Point start = iter.next();
Iterator<Coordinate> iter = points.iterator();
Point start = getContext().worldToPixel(iter.next());
while( iter.hasNext() ) {
Point current = iter.next();
Point current = getContext().worldToPixel(iter.next());
graphics.drawLine(start.x, start.y, current.x, current.y);
start = current;
}
if (start == null || now == null)
return;
graphics.drawLine(start.x, start.y, now.x, now.y);
Point nowPoint = getContext().worldToPixel(now);
graphics.drawLine(start.x, start.y, nowPoint.x, nowPoint.y);

displayResult();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DistanceTool_error=Unable to calculate the distance
DistanceTool_distance=Distance \= {0}
DistanceTool_distance=Distance \= {0} (last segment \= {1})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DistanceTool_distance = Entfernung \= {0}
DistanceTool_distance = Gesamtstrecke \= {0} (aktuelles Segment \= {1})
DistanceTool_error = Kann Entfernung nicht berechnen.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DistanceTool_distance = Distanza \= {0}
DistanceTool_distance = Distanza \= {0} (ultimo segmento \= {1})
DistanceTool_error = Impossibile calcolare la distanza

This file was deleted.

This file was deleted.

0 comments on commit a747cde

Please sign in to comment.