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