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

improve DistanceTool to work correctly during Map zoom in and zoom out actions #333

Merged
merged 3 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
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.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DistanceTool_distance = Distancia \= {0}
DistanceTool_distance = Distancia \= {0} (??�ltimo segmento \= {1})
DistanceTool_error = No se puede calcular la distancia
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DistanceTool_error=Ezin da distantzia kalkulatu
DistanceTool_distance=Distantzia \= {0}
DistanceTool_distance=Distantzia \= {0} (azken segmentua \= {1})
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
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DistanceTool_error=\uac70\ub9ac\ub97c \uacc4\uc0b0\ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4
DistanceTool_distance=\uac70\ub9ac \= {0}
DistanceTool_distance=\uac70\ub9ac \= {0} (\uB9C8\uC9C0\uB9C9 \uC138\uADF8\uBA3C\uD2B8 \= {1})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DistanceTool_error=\u041d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435
DistanceTool_distance=\u0420\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \= {0}
DistanceTool_distance=\u0420\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \= {0} (\u043F\u043E\u0441\u043B\u0435\u0434\u043D\u0438\u0439 \u0441\u0435\u0433\u043C\u0435\u043D\u0442 \= {1})
fgdrf marked this conversation as resolved.
Show resolved Hide resolved