]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/compress/MarkPointsInRectangleFunction.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / function / compress / MarkPointsInRectangleFunction.java
1 package tim.prune.function.compress;
2
3 import javax.swing.JOptionPane;
4
5 import tim.prune.App;
6 import tim.prune.GenericFunction;
7 import tim.prune.I18nManager;
8 import tim.prune.UpdateMessageBroker;
9 import tim.prune.data.DataPoint;
10
11 /**
12  * Function to mark all the points in the selected rectangle
13  */
14 public class MarkPointsInRectangleFunction extends GenericFunction
15 {
16         /** Minimum and maximum latitude values of rectangle */
17         private double _minLat = 0.0, _maxLat = 0.0;
18         /** Minimum and maximum longitude values of rectangle */
19         private double _minLon = 0.0, _maxLon = 0.0;
20
21
22         /**
23          * Constructor
24          * @param inApp App object
25          */
26         public MarkPointsInRectangleFunction(App inApp)
27         {
28                 super(inApp);
29         }
30
31         /**
32          * Set the coordinates of the rectangle
33          * @param inLon1 first longitude value
34          * @param inLat1 first latitude value
35          * @param inLon2 second longitude value
36          * @param inLat2 second latitude value
37          */
38         public void setRectCoords(double inLon1, double inLat1, double inLon2, double inLat2)
39         {
40                 if (inLon1 == inLon2 || inLat1 == inLat2)
41                 {
42                         // Coordinates not valid
43                         _minLat = _maxLat = _minLon = _maxLon = 0.0;
44                 }
45                 else
46                 {
47                         if (inLon2 > inLon1) {
48                                 _minLon = inLon1; _maxLon = inLon2;
49                         }
50                         else {
51                                 _minLon = inLon2; _maxLon = inLon1;
52                         }
53                         if (inLat2 > inLat1) {
54                                 _minLat = inLat1; _maxLat = inLat2;
55                         }
56                         else {
57                                 _minLat = inLat2; _maxLat = inLat1;
58                         }
59                 }
60         }
61
62         /**
63          * Begin the function using the set parameters
64          */
65         public void begin()
66         {
67                 if (_maxLon == _minLon || _maxLat == _minLat) {
68                         return;
69                 }
70
71                 // Loop over all points in track
72                 final int numPoints = _app.getTrackInfo().getTrack().getNumPoints();
73                 int numMarked = 0;
74                 for (int i=0; i<numPoints; i++)
75                 {
76                         DataPoint point = _app.getTrackInfo().getTrack().getPoint(i);
77                         // For each point, see if it's within the rectangle
78                         final double pointLon = point.getLongitude().getDouble();
79                         final double pointLat = point.getLatitude().getDouble();
80                         final boolean insideRect = (pointLon >= _minLon && pointLon <= _maxLon
81                                 && pointLat >= _minLat && pointLat <= _maxLat);
82                         // If so, then mark it
83                         point.setMarkedForDeletion(insideRect);
84                         if (insideRect) {
85                                 numMarked++;
86                         }
87                 }
88
89                 // Inform subscribers to update display
90                 UpdateMessageBroker.informSubscribers();
91                 // Confirm message showing how many marked
92                 if (numMarked > 0) {
93                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.compress.confirm1")
94                                 + " " + numMarked + " " + I18nManager.getText("dialog.compress.confirm2"),
95                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
96                 }
97         }
98
99         /** @return name key */
100         public String getNameKey() {
101                 return "menu.track.markrectangle";
102         }
103 }