1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.mapillary;
|
---|
3 |
|
---|
4 | import java.text.SimpleDateFormat;
|
---|
5 | import java.util.Calendar;
|
---|
6 | import java.util.Date;
|
---|
7 | import java.util.Locale;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Abstract superclass for all image objects. At the moment there are just 2,
|
---|
14 | * {@link MapillaryImportedImage} and {@link MapillaryImage}.
|
---|
15 | *
|
---|
16 | * @author nokutu
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class MapillaryAbstractImage {
|
---|
20 | /**
|
---|
21 | * If two values for field ca differ by less than EPSILON both values are considered equal.
|
---|
22 | */
|
---|
23 | private static final float EPSILON = 1e-5f;
|
---|
24 |
|
---|
25 | /** The time the image was captured, in Epoch format. */
|
---|
26 | protected long capturedAt;
|
---|
27 | /** Sequence of pictures containing this object. */
|
---|
28 | private MapillarySequence sequence;
|
---|
29 | /** Position of the picture. */
|
---|
30 | public final LatLon latLon;
|
---|
31 | /** Direction of the picture. */
|
---|
32 | public final double ca;
|
---|
33 | /** Temporal position of the picture until it is uploaded. */
|
---|
34 | public LatLon tempLatLon;
|
---|
35 | /**
|
---|
36 | * When the object is being dragged in the map, the temporal position is
|
---|
37 | * stored here.
|
---|
38 | */
|
---|
39 | public LatLon movingLatLon;
|
---|
40 | /** Temporal direction of the picture until it is uploaded */
|
---|
41 | public double tempCa;
|
---|
42 | /**
|
---|
43 | * When the object direction is being moved in the map, the temporal direction
|
---|
44 | * is stored here
|
---|
45 | */
|
---|
46 | protected double movingCa;
|
---|
47 | /** Whether the image must be drown in the map or not */
|
---|
48 | private boolean visible;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Creates a new object in the given position and with the given direction.
|
---|
52 | *
|
---|
53 | * @param lat
|
---|
54 | * The latitude where the picture was taken.
|
---|
55 | * @param lon
|
---|
56 | * The longitude where the picture was taken.
|
---|
57 | * @param ca
|
---|
58 | * The direction of the picture (0 means north).
|
---|
59 | */
|
---|
60 | protected MapillaryAbstractImage(final double lat, final double lon, final double ca) {
|
---|
61 | this.latLon = new LatLon(lat, lon);
|
---|
62 | this.tempLatLon = this.latLon;
|
---|
63 | this.movingLatLon = this.latLon;
|
---|
64 | this.ca = ca;
|
---|
65 | this.tempCa = ca;
|
---|
66 | this.movingCa = ca;
|
---|
67 | this.visible = true;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Returns the direction towards the image has been taken.
|
---|
72 | *
|
---|
73 | * @return The direction of the image (0 means north and goes clockwise).
|
---|
74 | */
|
---|
75 | public double getCa() {
|
---|
76 | return this.movingCa;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Returns the Epoch time when the image was captured.
|
---|
81 | *
|
---|
82 | * @return The long containing the Epoch time when the image was captured.
|
---|
83 | */
|
---|
84 | public long getCapturedAt() {
|
---|
85 | return this.capturedAt;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Returns the date the picture was taken in DMY format.
|
---|
90 | *
|
---|
91 | * @return A String object containing the date when the picture was taken.
|
---|
92 | */
|
---|
93 | public String getDate() {
|
---|
94 | final StringBuilder format = new StringBuilder(26);
|
---|
95 | if (Main.pref.getBoolean("iso.dates")) {
|
---|
96 | format.append("yyyy-MM-dd");
|
---|
97 | } else {
|
---|
98 | format.append("dd/MM/yyyy");
|
---|
99 | }
|
---|
100 | if (Main.pref.getBoolean("mapillary.display-hour", true)) {
|
---|
101 | if (Main.pref.getBoolean("mapillary.format-24")) {
|
---|
102 | format.append(" - HH:mm:ss (z)");
|
---|
103 | } else {
|
---|
104 | format.append(" - h:mm:ss a (z)");
|
---|
105 | }
|
---|
106 | }
|
---|
107 | return getDate(format.toString());
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Returns the date the picture was taken in the given format.
|
---|
112 | *
|
---|
113 | * @param format
|
---|
114 | * Format of the date. See {@link SimpleDateFormat}.
|
---|
115 | * @return A String containing the date the picture was taken using the given
|
---|
116 | * format.
|
---|
117 | * @throws NullPointerException if parameter format is <code>null</code>
|
---|
118 | */
|
---|
119 | public String getDate(String format) {
|
---|
120 | final Date date = new Date(getCapturedAt());
|
---|
121 |
|
---|
122 | final SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.UK);
|
---|
123 | formatter.setTimeZone(Calendar.getInstance().getTimeZone());
|
---|
124 | return formatter.format(date);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Returns a LatLon object containing the current coordinates of the object.
|
---|
129 | * When you are dragging the image this changes.
|
---|
130 | *
|
---|
131 | * @return The LatLon object with the position of the object.
|
---|
132 | */
|
---|
133 | public LatLon getLatLon() {
|
---|
134 | return this.movingLatLon;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Returns the sequence which contains this image.
|
---|
139 | *
|
---|
140 | * @return The MapillarySequence object that contains this MapillaryImage.
|
---|
141 | */
|
---|
142 | public MapillarySequence getSequence() {
|
---|
143 | if (this.sequence == null) {
|
---|
144 | this.sequence = new MapillarySequence();
|
---|
145 | this.sequence.add(this);
|
---|
146 | }
|
---|
147 |
|
---|
148 | return this.sequence;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Returns the last fixed direction of the object.
|
---|
153 | *
|
---|
154 | * @return The last fixed direction of the object. 0 means north.
|
---|
155 | */
|
---|
156 | public double getTempCa() {
|
---|
157 | return this.tempCa;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Returns the last fixed coordinates of the object.
|
---|
162 | *
|
---|
163 | * @return A LatLon object containing.
|
---|
164 | */
|
---|
165 | public LatLon getTempLatLon() {
|
---|
166 | return this.tempLatLon;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Returns whether the object has been modified or not.
|
---|
171 | *
|
---|
172 | * @return true if the object has been modified; false otherwise.
|
---|
173 | */
|
---|
174 | public boolean isModified() {
|
---|
175 | return !this.getLatLon().equals(this.latLon) || Math.abs(this.getCa() - this.ca) < EPSILON;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Returns whether the image is visible on the map or not.
|
---|
180 | *
|
---|
181 | * @return True if the image is visible; false otherwise.
|
---|
182 | */
|
---|
183 | public boolean isVisible() {
|
---|
184 | return this.visible;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Moves the image temporally to another position
|
---|
189 | *
|
---|
190 | * @param x The movement of the image in longitude units.
|
---|
191 | * @param y The movement of the image in latitude units.
|
---|
192 | */
|
---|
193 | public void move(final double x, final double y) {
|
---|
194 | this.movingLatLon = new LatLon(this.tempLatLon.getY() + y, this.tempLatLon.getX() + x);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * If the MapillaryImage belongs to a MapillarySequence, returns the next
|
---|
199 | * MapillarySequence in it.
|
---|
200 | *
|
---|
201 | * @return The following MapillaryImage, or null if there is none.
|
---|
202 | */
|
---|
203 | public MapillaryAbstractImage next() {
|
---|
204 | synchronized (MapillaryAbstractImage.class) {
|
---|
205 | if (this.getSequence() == null)
|
---|
206 | return null;
|
---|
207 | return this.getSequence().next(this);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * If the MapillaryImage belongs to a MapillarySequence, returns the previous
|
---|
213 | * MapillarySequence in it.
|
---|
214 | *
|
---|
215 | * @return The previous MapillaryImage, or null if there is none.
|
---|
216 | */
|
---|
217 | public MapillaryAbstractImage previous() {
|
---|
218 | synchronized (MapillaryAbstractImage.class) {
|
---|
219 | if (this.getSequence() == null)
|
---|
220 | return null;
|
---|
221 | return this.getSequence().previous(this);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Sets the Epoch time when the picture was captured.
|
---|
227 | *
|
---|
228 | * @param capturedAt Epoch time when the image was captured.
|
---|
229 | */
|
---|
230 | public void setCapturedAt(final long capturedAt) {
|
---|
231 | this.capturedAt = capturedAt;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Sets the MapillarySequence object which contains the MapillaryImage.
|
---|
236 | *
|
---|
237 | * @param sequence The MapillarySequence that contains the MapillaryImage.
|
---|
238 | */
|
---|
239 | public void setSequence(final MapillarySequence sequence) {
|
---|
240 | this.sequence = sequence;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Set's whether the image should be visible on the map or not.
|
---|
245 | *
|
---|
246 | * @param visible true if the image is set to be visible; false otherwise.
|
---|
247 | */
|
---|
248 | public void setVisible(final boolean visible) {
|
---|
249 | this.visible = visible;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Called when the mouse button is released, meaning that the picture has
|
---|
254 | * stopped being dragged, so the temporal values are saved.
|
---|
255 | */
|
---|
256 | public void stopMoving() {
|
---|
257 | this.tempLatLon = this.movingLatLon;
|
---|
258 | this.tempCa = this.movingCa;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Turns the image direction.
|
---|
263 | *
|
---|
264 | * @param ca The angle the image is moving.
|
---|
265 | */
|
---|
266 | public void turn(final double ca) {
|
---|
267 | this.movingCa = this.tempCa + ca;
|
---|
268 | }
|
---|
269 | }
|
---|