1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.geoimage;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.Cursor;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.FlowLayout;
|
---|
11 | import java.awt.GraphicsEnvironment;
|
---|
12 | import java.awt.GridBagConstraints;
|
---|
13 | import java.awt.GridBagLayout;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.awt.event.ActionListener;
|
---|
16 | import java.awt.event.FocusEvent;
|
---|
17 | import java.awt.event.FocusListener;
|
---|
18 | import java.awt.event.ItemEvent;
|
---|
19 | import java.awt.event.ItemListener;
|
---|
20 | import java.awt.event.WindowAdapter;
|
---|
21 | import java.awt.event.WindowEvent;
|
---|
22 | import java.io.File;
|
---|
23 | import java.io.FileInputStream;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.io.InputStream;
|
---|
26 | import java.text.DateFormat;
|
---|
27 | import java.text.ParseException;
|
---|
28 | import java.text.SimpleDateFormat;
|
---|
29 | import java.util.ArrayList;
|
---|
30 | import java.util.Collection;
|
---|
31 | import java.util.Collections;
|
---|
32 | import java.util.Comparator;
|
---|
33 | import java.util.Date;
|
---|
34 | import java.util.Dictionary;
|
---|
35 | import java.util.Hashtable;
|
---|
36 | import java.util.List;
|
---|
37 | import java.util.Locale;
|
---|
38 | import java.util.Objects;
|
---|
39 | import java.util.TimeZone;
|
---|
40 | import java.util.zip.GZIPInputStream;
|
---|
41 |
|
---|
42 | import javax.swing.AbstractAction;
|
---|
43 | import javax.swing.AbstractListModel;
|
---|
44 | import javax.swing.BorderFactory;
|
---|
45 | import javax.swing.JButton;
|
---|
46 | import javax.swing.JCheckBox;
|
---|
47 | import javax.swing.JFileChooser;
|
---|
48 | import javax.swing.JLabel;
|
---|
49 | import javax.swing.JList;
|
---|
50 | import javax.swing.JOptionPane;
|
---|
51 | import javax.swing.JPanel;
|
---|
52 | import javax.swing.JScrollPane;
|
---|
53 | import javax.swing.JSeparator;
|
---|
54 | import javax.swing.JSlider;
|
---|
55 | import javax.swing.ListSelectionModel;
|
---|
56 | import javax.swing.SwingConstants;
|
---|
57 | import javax.swing.event.ChangeEvent;
|
---|
58 | import javax.swing.event.ChangeListener;
|
---|
59 | import javax.swing.event.DocumentEvent;
|
---|
60 | import javax.swing.event.DocumentListener;
|
---|
61 | import javax.swing.event.ListSelectionEvent;
|
---|
62 | import javax.swing.event.ListSelectionListener;
|
---|
63 | import javax.swing.filechooser.FileFilter;
|
---|
64 |
|
---|
65 | import org.openstreetmap.josm.Main;
|
---|
66 | import org.openstreetmap.josm.actions.DiskAccessAction;
|
---|
67 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
68 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
69 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
70 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
---|
71 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
72 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
73 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
74 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
75 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
76 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
77 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
78 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
79 | import org.openstreetmap.josm.io.GpxReader;
|
---|
80 | import org.openstreetmap.josm.io.JpgImporter;
|
---|
81 | import org.openstreetmap.josm.tools.ExifReader;
|
---|
82 | import org.openstreetmap.josm.tools.GBC;
|
---|
83 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
84 | import org.openstreetmap.josm.tools.Pair;
|
---|
85 | import org.openstreetmap.josm.tools.Utils;
|
---|
86 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
87 | import org.xml.sax.SAXException;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * This class displays the window to select the GPX file and the offset (timezone + delta).
|
---|
91 | * Then it correlates the images of the layer with that GPX file.
|
---|
92 | */
|
---|
93 | public class CorrelateGpxWithImages extends AbstractAction {
|
---|
94 |
|
---|
95 | private static List<GpxData> loadedGpxData = new ArrayList<>();
|
---|
96 |
|
---|
97 | private final transient GeoImageLayer yLayer;
|
---|
98 | private transient Timezone timezone;
|
---|
99 | private transient Offset delta;
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Constructs a new {@code CorrelateGpxWithImages} action.
|
---|
103 | * @param layer The image layer
|
---|
104 | */
|
---|
105 | public CorrelateGpxWithImages(GeoImageLayer layer) {
|
---|
106 | super(tr("Correlate to GPX"), ImageProvider.get("dialogs/geoimage/gpx2img"));
|
---|
107 | this.yLayer = layer;
|
---|
108 | }
|
---|
109 |
|
---|
110 | private final class SyncDialogWindowListener extends WindowAdapter {
|
---|
111 | private static final int CANCEL = -1;
|
---|
112 | private static final int DONE = 0;
|
---|
113 | private static final int AGAIN = 1;
|
---|
114 | private static final int NOTHING = 2;
|
---|
115 |
|
---|
116 | private int checkAndSave() {
|
---|
117 | if (syncDialog.isVisible())
|
---|
118 | // nothing happened: JOSM was minimized or similar
|
---|
119 | return NOTHING;
|
---|
120 | int answer = syncDialog.getValue();
|
---|
121 | if (answer != 1)
|
---|
122 | return CANCEL;
|
---|
123 |
|
---|
124 | // Parse values again, to display an error if the format is not recognized
|
---|
125 | try {
|
---|
126 | timezone = Timezone.parseTimezone(tfTimezone.getText().trim());
|
---|
127 | } catch (ParseException e) {
|
---|
128 | JOptionPane.showMessageDialog(Main.parent, e.getMessage(),
|
---|
129 | tr("Invalid timezone"), JOptionPane.ERROR_MESSAGE);
|
---|
130 | return AGAIN;
|
---|
131 | }
|
---|
132 |
|
---|
133 | try {
|
---|
134 | delta = Offset.parseOffset(tfOffset.getText().trim());
|
---|
135 | } catch (ParseException e) {
|
---|
136 | JOptionPane.showMessageDialog(Main.parent, e.getMessage(),
|
---|
137 | tr("Invalid offset"), JOptionPane.ERROR_MESSAGE);
|
---|
138 | return AGAIN;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (lastNumMatched == 0 && new ExtendedDialog(
|
---|
142 | Main.parent,
|
---|
143 | tr("Correlate images with GPX track"),
|
---|
144 | new String[] {tr("OK"), tr("Try Again")}).
|
---|
145 | setContent(tr("No images could be matched!")).
|
---|
146 | setButtonIcons(new String[] {"ok", "dialogs/refresh"}).
|
---|
147 | showDialog().getValue() == 2)
|
---|
148 | return AGAIN;
|
---|
149 | return DONE;
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public void windowDeactivated(WindowEvent e) {
|
---|
154 | int result = checkAndSave();
|
---|
155 | switch (result) {
|
---|
156 | case NOTHING:
|
---|
157 | break;
|
---|
158 | case CANCEL:
|
---|
159 | if (yLayer != null) {
|
---|
160 | if (yLayer.data != null) {
|
---|
161 | for (ImageEntry ie : yLayer.data) {
|
---|
162 | ie.discardTmp();
|
---|
163 | }
|
---|
164 | }
|
---|
165 | yLayer.updateBufferAndRepaint();
|
---|
166 | }
|
---|
167 | break;
|
---|
168 | case AGAIN:
|
---|
169 | actionPerformed(null);
|
---|
170 | break;
|
---|
171 | case DONE:
|
---|
172 | Main.pref.put("geoimage.timezone", timezone.formatTimezone());
|
---|
173 | Main.pref.put("geoimage.delta", delta.formatOffset());
|
---|
174 | Main.pref.put("geoimage.showThumbs", yLayer.useThumbs);
|
---|
175 |
|
---|
176 | yLayer.useThumbs = cbShowThumbs.isSelected();
|
---|
177 | yLayer.startLoadThumbs();
|
---|
178 |
|
---|
179 | // Search whether an other layer has yet defined some bounding box.
|
---|
180 | // If none, we'll zoom to the bounding box of the layer with the photos.
|
---|
181 | boolean boundingBoxedLayerFound = false;
|
---|
182 | for (Layer l: Main.map.mapView.getAllLayers()) {
|
---|
183 | if (l != yLayer) {
|
---|
184 | BoundingXYVisitor bbox = new BoundingXYVisitor();
|
---|
185 | l.visitBoundingBox(bbox);
|
---|
186 | if (bbox.getBounds() != null) {
|
---|
187 | boundingBoxedLayerFound = true;
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | if (!boundingBoxedLayerFound) {
|
---|
193 | BoundingXYVisitor bbox = new BoundingXYVisitor();
|
---|
194 | yLayer.visitBoundingBox(bbox);
|
---|
195 | Main.map.mapView.zoomTo(bbox);
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (yLayer.data != null) {
|
---|
199 | for (ImageEntry ie : yLayer.data) {
|
---|
200 | ie.applyTmp();
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | yLayer.updateBufferAndRepaint();
|
---|
205 |
|
---|
206 | break;
|
---|
207 | default:
|
---|
208 | throw new IllegalStateException();
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | private static class GpxDataWrapper {
|
---|
214 | private final String name;
|
---|
215 | private final GpxData data;
|
---|
216 | private final File file;
|
---|
217 |
|
---|
218 | GpxDataWrapper(String name, GpxData data, File file) {
|
---|
219 | this.name = name;
|
---|
220 | this.data = data;
|
---|
221 | this.file = file;
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | public String toString() {
|
---|
226 | return name;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | private ExtendedDialog syncDialog;
|
---|
231 | private final transient List<GpxDataWrapper> gpxLst = new ArrayList<>();
|
---|
232 | private JPanel outerPanel;
|
---|
233 | private JosmComboBox<GpxDataWrapper> cbGpx;
|
---|
234 | private JosmTextField tfTimezone;
|
---|
235 | private JosmTextField tfOffset;
|
---|
236 | private JCheckBox cbExifImg;
|
---|
237 | private JCheckBox cbTaggedImg;
|
---|
238 | private JCheckBox cbShowThumbs;
|
---|
239 | private JLabel statusBarText;
|
---|
240 |
|
---|
241 | // remember the last number of matched photos
|
---|
242 | private int lastNumMatched;
|
---|
243 |
|
---|
244 | /** This class is called when the user doesn't find the GPX file he needs in the files that have
|
---|
245 | * been loaded yet. It displays a FileChooser dialog to select the GPX file to be loaded.
|
---|
246 | */
|
---|
247 | private class LoadGpxDataActionListener implements ActionListener {
|
---|
248 |
|
---|
249 | @Override
|
---|
250 | public void actionPerformed(ActionEvent arg0) {
|
---|
251 | FileFilter filter = new FileFilter() {
|
---|
252 | @Override
|
---|
253 | public boolean accept(File f) {
|
---|
254 | return f.isDirectory() || Utils.hasExtension(f, "gpx", "gpx.gz");
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Override
|
---|
258 | public String getDescription() {
|
---|
259 | return tr("GPX Files (*.gpx *.gpx.gz)");
|
---|
260 | }
|
---|
261 | };
|
---|
262 | AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null, filter, JFileChooser.FILES_ONLY, null);
|
---|
263 | if (fc == null)
|
---|
264 | return;
|
---|
265 | File sel = fc.getSelectedFile();
|
---|
266 |
|
---|
267 | try {
|
---|
268 | outerPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
|
---|
269 |
|
---|
270 | for (int i = gpxLst.size() - 1; i >= 0; i--) {
|
---|
271 | GpxDataWrapper wrapper = gpxLst.get(i);
|
---|
272 | if (wrapper.file != null && sel.equals(wrapper.file)) {
|
---|
273 | cbGpx.setSelectedIndex(i);
|
---|
274 | if (!sel.getName().equals(wrapper.name)) {
|
---|
275 | JOptionPane.showMessageDialog(
|
---|
276 | Main.parent,
|
---|
277 | tr("File {0} is loaded yet under the name \"{1}\"", sel.getName(), wrapper.name),
|
---|
278 | tr("Error"),
|
---|
279 | JOptionPane.ERROR_MESSAGE
|
---|
280 | );
|
---|
281 | }
|
---|
282 | return;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | GpxData data = null;
|
---|
286 | try (InputStream iStream = createInputStream(sel)) {
|
---|
287 | GpxReader reader = new GpxReader(iStream);
|
---|
288 | reader.parse(false);
|
---|
289 | data = reader.getGpxData();
|
---|
290 | data.storageFile = sel;
|
---|
291 |
|
---|
292 | } catch (SAXException x) {
|
---|
293 | Main.error(x);
|
---|
294 | JOptionPane.showMessageDialog(
|
---|
295 | Main.parent,
|
---|
296 | tr("Error while parsing {0}", sel.getName())+": "+x.getMessage(),
|
---|
297 | tr("Error"),
|
---|
298 | JOptionPane.ERROR_MESSAGE
|
---|
299 | );
|
---|
300 | return;
|
---|
301 | } catch (IOException x) {
|
---|
302 | Main.error(x);
|
---|
303 | JOptionPane.showMessageDialog(
|
---|
304 | Main.parent,
|
---|
305 | tr("Could not read \"{0}\"", sel.getName())+'\n'+x.getMessage(),
|
---|
306 | tr("Error"),
|
---|
307 | JOptionPane.ERROR_MESSAGE
|
---|
308 | );
|
---|
309 | return;
|
---|
310 | }
|
---|
311 |
|
---|
312 | loadedGpxData.add(data);
|
---|
313 | if (gpxLst.get(0).file == null) {
|
---|
314 | gpxLst.remove(0);
|
---|
315 | }
|
---|
316 | gpxLst.add(new GpxDataWrapper(sel.getName(), data, sel));
|
---|
317 | cbGpx.setSelectedIndex(cbGpx.getItemCount() - 1);
|
---|
318 | } finally {
|
---|
319 | outerPanel.setCursor(Cursor.getDefaultCursor());
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | private InputStream createInputStream(File sel) throws IOException {
|
---|
324 | if (Utils.hasExtension(sel, "gpx.gz")) {
|
---|
325 | return new GZIPInputStream(new FileInputStream(sel));
|
---|
326 | } else {
|
---|
327 | return new FileInputStream(sel);
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * This action listener is called when the user has a photo of the time of his GPS receiver. It
|
---|
334 | * displays the list of photos of the layer, and upon selection displays the selected photo.
|
---|
335 | * From that photo, the user can key in the time of the GPS.
|
---|
336 | * Then values of timezone and delta are set.
|
---|
337 | * @author chris
|
---|
338 | *
|
---|
339 | */
|
---|
340 | private class SetOffsetActionListener implements ActionListener {
|
---|
341 | private JPanel panel;
|
---|
342 | private JLabel lbExifTime;
|
---|
343 | private JosmTextField tfGpsTime;
|
---|
344 | private JosmComboBox<String> cbTimezones;
|
---|
345 | private ImageDisplay imgDisp;
|
---|
346 | private JList<String> imgList;
|
---|
347 |
|
---|
348 | @Override
|
---|
349 | public void actionPerformed(ActionEvent arg0) {
|
---|
350 | SimpleDateFormat dateFormat = (SimpleDateFormat) DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);
|
---|
351 |
|
---|
352 | panel = new JPanel(new BorderLayout());
|
---|
353 | panel.add(new JLabel(tr("<html>Take a photo of your GPS receiver while it displays the time.<br>"
|
---|
354 | + "Display that photo here.<br>"
|
---|
355 | + "And then, simply capture the time you read on the photo and select a timezone<hr></html>")),
|
---|
356 | BorderLayout.NORTH);
|
---|
357 |
|
---|
358 | imgDisp = new ImageDisplay();
|
---|
359 | imgDisp.setPreferredSize(new Dimension(300, 225));
|
---|
360 | panel.add(imgDisp, BorderLayout.CENTER);
|
---|
361 |
|
---|
362 | JPanel panelTf = new JPanel(new GridBagLayout());
|
---|
363 |
|
---|
364 | GridBagConstraints gc = new GridBagConstraints();
|
---|
365 | gc.gridx = gc.gridy = 0;
|
---|
366 | gc.gridwidth = gc.gridheight = 1;
|
---|
367 | gc.weightx = gc.weighty = 0.0;
|
---|
368 | gc.fill = GridBagConstraints.NONE;
|
---|
369 | gc.anchor = GridBagConstraints.WEST;
|
---|
370 | panelTf.add(new JLabel(tr("Photo time (from exif):")), gc);
|
---|
371 |
|
---|
372 | lbExifTime = new JLabel();
|
---|
373 | gc.gridx = 1;
|
---|
374 | gc.weightx = 1.0;
|
---|
375 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
376 | gc.gridwidth = 2;
|
---|
377 | panelTf.add(lbExifTime, gc);
|
---|
378 |
|
---|
379 | gc.gridx = 0;
|
---|
380 | gc.gridy = 1;
|
---|
381 | gc.gridwidth = gc.gridheight = 1;
|
---|
382 | gc.weightx = gc.weighty = 0.0;
|
---|
383 | gc.fill = GridBagConstraints.NONE;
|
---|
384 | gc.anchor = GridBagConstraints.WEST;
|
---|
385 | panelTf.add(new JLabel(tr("Gps time (read from the above photo): ")), gc);
|
---|
386 |
|
---|
387 | tfGpsTime = new JosmTextField(12);
|
---|
388 | tfGpsTime.setEnabled(false);
|
---|
389 | tfGpsTime.setMinimumSize(new Dimension(155, tfGpsTime.getMinimumSize().height));
|
---|
390 | gc.gridx = 1;
|
---|
391 | gc.weightx = 1.0;
|
---|
392 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
393 | panelTf.add(tfGpsTime, gc);
|
---|
394 |
|
---|
395 | gc.gridx = 2;
|
---|
396 | gc.weightx = 0.2;
|
---|
397 | panelTf.add(new JLabel(" ["+dateFormat.toLocalizedPattern()+']'), gc);
|
---|
398 |
|
---|
399 | gc.gridx = 0;
|
---|
400 | gc.gridy = 2;
|
---|
401 | gc.gridwidth = gc.gridheight = 1;
|
---|
402 | gc.weightx = gc.weighty = 0.0;
|
---|
403 | gc.fill = GridBagConstraints.NONE;
|
---|
404 | gc.anchor = GridBagConstraints.WEST;
|
---|
405 | panelTf.add(new JLabel(tr("I am in the timezone of: ")), gc);
|
---|
406 |
|
---|
407 | String[] tmp = TimeZone.getAvailableIDs();
|
---|
408 | List<String> vtTimezones = new ArrayList<>(tmp.length);
|
---|
409 |
|
---|
410 | for (String tzStr : tmp) {
|
---|
411 | TimeZone tz = TimeZone.getTimeZone(tzStr);
|
---|
412 |
|
---|
413 | String tzDesc = new StringBuilder(tzStr).append(" (")
|
---|
414 | .append(new Timezone(tz.getRawOffset() / 3600000.0).formatTimezone())
|
---|
415 | .append(')').toString();
|
---|
416 | vtTimezones.add(tzDesc);
|
---|
417 | }
|
---|
418 |
|
---|
419 | Collections.sort(vtTimezones);
|
---|
420 |
|
---|
421 | cbTimezones = new JosmComboBox<>(vtTimezones.toArray(new String[0]));
|
---|
422 |
|
---|
423 | String tzId = Main.pref.get("geoimage.timezoneid", "");
|
---|
424 | TimeZone defaultTz;
|
---|
425 | if (tzId.isEmpty()) {
|
---|
426 | defaultTz = TimeZone.getDefault();
|
---|
427 | } else {
|
---|
428 | defaultTz = TimeZone.getTimeZone(tzId);
|
---|
429 | }
|
---|
430 |
|
---|
431 | cbTimezones.setSelectedItem(new StringBuilder(defaultTz.getID()).append(" (")
|
---|
432 | .append(new Timezone(defaultTz.getRawOffset() / 3600000.0).formatTimezone())
|
---|
433 | .append(')').toString());
|
---|
434 |
|
---|
435 | gc.gridx = 1;
|
---|
436 | gc.weightx = 1.0;
|
---|
437 | gc.gridwidth = 2;
|
---|
438 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
439 | panelTf.add(cbTimezones, gc);
|
---|
440 |
|
---|
441 | panel.add(panelTf, BorderLayout.SOUTH);
|
---|
442 |
|
---|
443 | JPanel panelLst = new JPanel(new BorderLayout());
|
---|
444 |
|
---|
445 | imgList = new JList<>(new AbstractListModel<String>() {
|
---|
446 | @Override
|
---|
447 | public String getElementAt(int i) {
|
---|
448 | return yLayer.data.get(i).getFile().getName();
|
---|
449 | }
|
---|
450 |
|
---|
451 | @Override
|
---|
452 | public int getSize() {
|
---|
453 | return yLayer.data != null ? yLayer.data.size() : 0;
|
---|
454 | }
|
---|
455 | });
|
---|
456 | imgList.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
457 | imgList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
---|
458 |
|
---|
459 | @Override
|
---|
460 | public void valueChanged(ListSelectionEvent arg0) {
|
---|
461 | int index = imgList.getSelectedIndex();
|
---|
462 | Integer orientation = null;
|
---|
463 | try {
|
---|
464 | orientation = ExifReader.readOrientation(yLayer.data.get(index).getFile());
|
---|
465 | } catch (Exception e) {
|
---|
466 | Main.warn(e);
|
---|
467 | }
|
---|
468 | imgDisp.setImage(yLayer.data.get(index).getFile(), orientation);
|
---|
469 | Date date = yLayer.data.get(index).getExifTime();
|
---|
470 | if (date != null) {
|
---|
471 | DateFormat df = DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);
|
---|
472 | lbExifTime.setText(df.format(date));
|
---|
473 | tfGpsTime.setText(df.format(date));
|
---|
474 | tfGpsTime.setCaretPosition(tfGpsTime.getText().length());
|
---|
475 | tfGpsTime.setEnabled(true);
|
---|
476 | tfGpsTime.requestFocus();
|
---|
477 | } else {
|
---|
478 | lbExifTime.setText(tr("No date"));
|
---|
479 | tfGpsTime.setText("");
|
---|
480 | tfGpsTime.setEnabled(false);
|
---|
481 | }
|
---|
482 | }
|
---|
483 | });
|
---|
484 | panelLst.add(new JScrollPane(imgList), BorderLayout.CENTER);
|
---|
485 |
|
---|
486 | JButton openButton = new JButton(tr("Open another photo"));
|
---|
487 | openButton.addActionListener(new ActionListener() {
|
---|
488 |
|
---|
489 | @Override
|
---|
490 | public void actionPerformed(ActionEvent ae) {
|
---|
491 | AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true, false, null,
|
---|
492 | JpgImporter.FILE_FILTER_WITH_FOLDERS, JFileChooser.FILES_ONLY, "geoimage.lastdirectory");
|
---|
493 | if (fc == null)
|
---|
494 | return;
|
---|
495 | File sel = fc.getSelectedFile();
|
---|
496 |
|
---|
497 | Integer orientation = null;
|
---|
498 | try {
|
---|
499 | orientation = ExifReader.readOrientation(sel);
|
---|
500 | } catch (Exception e) {
|
---|
501 | Main.warn(e);
|
---|
502 | }
|
---|
503 | imgDisp.setImage(sel, orientation);
|
---|
504 |
|
---|
505 | Date date = null;
|
---|
506 | try {
|
---|
507 | date = ExifReader.readTime(sel);
|
---|
508 | } catch (Exception e) {
|
---|
509 | Main.warn(e);
|
---|
510 | }
|
---|
511 | if (date != null) {
|
---|
512 | lbExifTime.setText(DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM).format(date));
|
---|
513 | tfGpsTime.setText(DateUtils.getDateFormat(DateFormat.SHORT).format(date)+' ');
|
---|
514 | tfGpsTime.setEnabled(true);
|
---|
515 | } else {
|
---|
516 | lbExifTime.setText(tr("No date"));
|
---|
517 | tfGpsTime.setText("");
|
---|
518 | tfGpsTime.setEnabled(false);
|
---|
519 | }
|
---|
520 | }
|
---|
521 | });
|
---|
522 | panelLst.add(openButton, BorderLayout.PAGE_END);
|
---|
523 |
|
---|
524 | panel.add(panelLst, BorderLayout.LINE_START);
|
---|
525 |
|
---|
526 | boolean isOk = false;
|
---|
527 | while (!isOk) {
|
---|
528 | int answer = JOptionPane.showConfirmDialog(
|
---|
529 | Main.parent, panel,
|
---|
530 | tr("Synchronize time from a photo of the GPS receiver"),
|
---|
531 | JOptionPane.OK_CANCEL_OPTION,
|
---|
532 | JOptionPane.QUESTION_MESSAGE
|
---|
533 | );
|
---|
534 | if (answer == JOptionPane.CANCEL_OPTION)
|
---|
535 | return;
|
---|
536 |
|
---|
537 | long delta;
|
---|
538 |
|
---|
539 | try {
|
---|
540 | delta = dateFormat.parse(lbExifTime.getText()).getTime()
|
---|
541 | - dateFormat.parse(tfGpsTime.getText()).getTime();
|
---|
542 | } catch (ParseException e) {
|
---|
543 | JOptionPane.showMessageDialog(Main.parent, tr("Error while parsing the date.\n"
|
---|
544 | + "Please use the requested format"),
|
---|
545 | tr("Invalid date"), JOptionPane.ERROR_MESSAGE);
|
---|
546 | continue;
|
---|
547 | }
|
---|
548 |
|
---|
549 | String selectedTz = (String) cbTimezones.getSelectedItem();
|
---|
550 | int pos = selectedTz.lastIndexOf('(');
|
---|
551 | tzId = selectedTz.substring(0, pos - 1);
|
---|
552 | String tzValue = selectedTz.substring(pos + 1, selectedTz.length() - 1);
|
---|
553 |
|
---|
554 | Main.pref.put("geoimage.timezoneid", tzId);
|
---|
555 | tfOffset.setText(Offset.milliseconds(delta).formatOffset());
|
---|
556 | tfTimezone.setText(tzValue);
|
---|
557 |
|
---|
558 | isOk = true;
|
---|
559 |
|
---|
560 | }
|
---|
561 | statusBarUpdater.updateStatusBar();
|
---|
562 | yLayer.updateBufferAndRepaint();
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | @Override
|
---|
567 | public void actionPerformed(ActionEvent arg0) {
|
---|
568 | // Construct the list of loaded GPX tracks
|
---|
569 | Collection<Layer> layerLst = Main.map.mapView.getAllLayers();
|
---|
570 | GpxDataWrapper defaultItem = null;
|
---|
571 | for (Layer cur : layerLst) {
|
---|
572 | if (cur instanceof GpxLayer) {
|
---|
573 | GpxLayer curGpx = (GpxLayer) cur;
|
---|
574 | GpxDataWrapper gdw = new GpxDataWrapper(curGpx.getName(), curGpx.data, curGpx.data.storageFile);
|
---|
575 | gpxLst.add(gdw);
|
---|
576 | if (cur == yLayer.gpxLayer) {
|
---|
577 | defaultItem = gdw;
|
---|
578 | }
|
---|
579 | }
|
---|
580 | }
|
---|
581 | for (GpxData data : loadedGpxData) {
|
---|
582 | gpxLst.add(new GpxDataWrapper(data.storageFile.getName(),
|
---|
583 | data,
|
---|
584 | data.storageFile));
|
---|
585 | }
|
---|
586 |
|
---|
587 | if (gpxLst.isEmpty()) {
|
---|
588 | gpxLst.add(new GpxDataWrapper(tr("<No GPX track loaded yet>"), null, null));
|
---|
589 | }
|
---|
590 |
|
---|
591 | JPanel panelCb = new JPanel();
|
---|
592 |
|
---|
593 | panelCb.add(new JLabel(tr("GPX track: ")));
|
---|
594 |
|
---|
595 | cbGpx = new JosmComboBox<>(gpxLst.toArray(new GpxDataWrapper[0]));
|
---|
596 | if (defaultItem != null) {
|
---|
597 | cbGpx.setSelectedItem(defaultItem);
|
---|
598 | }
|
---|
599 | cbGpx.addActionListener(statusBarUpdaterWithRepaint);
|
---|
600 | panelCb.add(cbGpx);
|
---|
601 |
|
---|
602 | JButton buttonOpen = new JButton(tr("Open another GPX trace"));
|
---|
603 | buttonOpen.addActionListener(new LoadGpxDataActionListener());
|
---|
604 | panelCb.add(buttonOpen);
|
---|
605 |
|
---|
606 | JPanel panelTf = new JPanel(new GridBagLayout());
|
---|
607 |
|
---|
608 | String prefTimezone = Main.pref.get("geoimage.timezone", "0:00");
|
---|
609 | if (prefTimezone == null) {
|
---|
610 | prefTimezone = "0:00";
|
---|
611 | }
|
---|
612 | try {
|
---|
613 | timezone = Timezone.parseTimezone(prefTimezone);
|
---|
614 | } catch (ParseException e) {
|
---|
615 | timezone = Timezone.ZERO;
|
---|
616 | }
|
---|
617 |
|
---|
618 | tfTimezone = new JosmTextField(10);
|
---|
619 | tfTimezone.setText(timezone.formatTimezone());
|
---|
620 |
|
---|
621 | try {
|
---|
622 | delta = Offset.parseOffset(Main.pref.get("geoimage.delta", "0"));
|
---|
623 | } catch (ParseException e) {
|
---|
624 | delta = Offset.ZERO;
|
---|
625 | }
|
---|
626 |
|
---|
627 | tfOffset = new JosmTextField(10);
|
---|
628 | tfOffset.setText(delta.formatOffset());
|
---|
629 |
|
---|
630 | JButton buttonViewGpsPhoto = new JButton(tr("<html>Use photo of an accurate clock,<br>"
|
---|
631 | + "e.g. GPS receiver display</html>"));
|
---|
632 | buttonViewGpsPhoto.setIcon(ImageProvider.get("clock"));
|
---|
633 | buttonViewGpsPhoto.addActionListener(new SetOffsetActionListener());
|
---|
634 |
|
---|
635 | JButton buttonAutoGuess = new JButton(tr("Auto-Guess"));
|
---|
636 | buttonAutoGuess.setToolTipText(tr("Matches first photo with first gpx point"));
|
---|
637 | buttonAutoGuess.addActionListener(new AutoGuessActionListener());
|
---|
638 |
|
---|
639 | JButton buttonAdjust = new JButton(tr("Manual adjust"));
|
---|
640 | buttonAdjust.addActionListener(new AdjustActionListener());
|
---|
641 |
|
---|
642 | JLabel labelPosition = new JLabel(tr("Override position for: "));
|
---|
643 |
|
---|
644 | int numAll = getSortedImgList(true, true).size();
|
---|
645 | int numExif = numAll - getSortedImgList(false, true).size();
|
---|
646 | int numTagged = numAll - getSortedImgList(true, false).size();
|
---|
647 |
|
---|
648 | cbExifImg = new JCheckBox(tr("Images with geo location in exif data ({0}/{1})", numExif, numAll));
|
---|
649 | cbExifImg.setEnabled(numExif != 0);
|
---|
650 |
|
---|
651 | cbTaggedImg = new JCheckBox(tr("Images that are already tagged ({0}/{1})", numTagged, numAll), true);
|
---|
652 | cbTaggedImg.setEnabled(numTagged != 0);
|
---|
653 |
|
---|
654 | labelPosition.setEnabled(cbExifImg.isEnabled() || cbTaggedImg.isEnabled());
|
---|
655 |
|
---|
656 | boolean ticked = yLayer.thumbsLoaded || Main.pref.getBoolean("geoimage.showThumbs", false);
|
---|
657 | cbShowThumbs = new JCheckBox(tr("Show Thumbnail images on the map"), ticked);
|
---|
658 | cbShowThumbs.setEnabled(!yLayer.thumbsLoaded);
|
---|
659 |
|
---|
660 | int y = 0;
|
---|
661 | GBC gbc = GBC.eol();
|
---|
662 | gbc.gridx = 0;
|
---|
663 | gbc.gridy = y++;
|
---|
664 | panelTf.add(panelCb, gbc);
|
---|
665 |
|
---|
666 | gbc = GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 12);
|
---|
667 | gbc.gridx = 0;
|
---|
668 | gbc.gridy = y++;
|
---|
669 | panelTf.add(new JSeparator(SwingConstants.HORIZONTAL), gbc);
|
---|
670 |
|
---|
671 | gbc = GBC.std();
|
---|
672 | gbc.gridx = 0;
|
---|
673 | gbc.gridy = y;
|
---|
674 | panelTf.add(new JLabel(tr("Timezone: ")), gbc);
|
---|
675 |
|
---|
676 | gbc = GBC.std().fill(GBC.HORIZONTAL);
|
---|
677 | gbc.gridx = 1;
|
---|
678 | gbc.gridy = y++;
|
---|
679 | gbc.weightx = 1.;
|
---|
680 | panelTf.add(tfTimezone, gbc);
|
---|
681 |
|
---|
682 | gbc = GBC.std();
|
---|
683 | gbc.gridx = 0;
|
---|
684 | gbc.gridy = y;
|
---|
685 | panelTf.add(new JLabel(tr("Offset:")), gbc);
|
---|
686 |
|
---|
687 | gbc = GBC.std().fill(GBC.HORIZONTAL);
|
---|
688 | gbc.gridx = 1;
|
---|
689 | gbc.gridy = y++;
|
---|
690 | gbc.weightx = 1.;
|
---|
691 | panelTf.add(tfOffset, gbc);
|
---|
692 |
|
---|
693 | gbc = GBC.std().insets(5, 5, 5, 5);
|
---|
694 | gbc.gridx = 2;
|
---|
695 | gbc.gridy = y-2;
|
---|
696 | gbc.gridheight = 2;
|
---|
697 | gbc.gridwidth = 2;
|
---|
698 | gbc.fill = GridBagConstraints.BOTH;
|
---|
699 | gbc.weightx = 0.5;
|
---|
700 | panelTf.add(buttonViewGpsPhoto, gbc);
|
---|
701 |
|
---|
702 | gbc = GBC.std().fill(GBC.BOTH).insets(5, 5, 5, 5);
|
---|
703 | gbc.gridx = 2;
|
---|
704 | gbc.gridy = y++;
|
---|
705 | gbc.weightx = 0.5;
|
---|
706 | panelTf.add(buttonAutoGuess, gbc);
|
---|
707 |
|
---|
708 | gbc.gridx = 3;
|
---|
709 | panelTf.add(buttonAdjust, gbc);
|
---|
710 |
|
---|
711 | gbc = GBC.eol().fill(GBC.HORIZONTAL).insets(0, 12, 0, 0);
|
---|
712 | gbc.gridx = 0;
|
---|
713 | gbc.gridy = y++;
|
---|
714 | panelTf.add(new JSeparator(SwingConstants.HORIZONTAL), gbc);
|
---|
715 |
|
---|
716 | gbc = GBC.eol();
|
---|
717 | gbc.gridx = 0;
|
---|
718 | gbc.gridy = y++;
|
---|
719 | panelTf.add(labelPosition, gbc);
|
---|
720 |
|
---|
721 | gbc = GBC.eol();
|
---|
722 | gbc.gridx = 1;
|
---|
723 | gbc.gridy = y++;
|
---|
724 | panelTf.add(cbExifImg, gbc);
|
---|
725 |
|
---|
726 | gbc = GBC.eol();
|
---|
727 | gbc.gridx = 1;
|
---|
728 | gbc.gridy = y++;
|
---|
729 | panelTf.add(cbTaggedImg, gbc);
|
---|
730 |
|
---|
731 | gbc = GBC.eol();
|
---|
732 | gbc.gridx = 0;
|
---|
733 | gbc.gridy = y++;
|
---|
734 | panelTf.add(cbShowThumbs, gbc);
|
---|
735 |
|
---|
736 | final JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
---|
737 | statusBar.setBorder(BorderFactory.createLoweredBevelBorder());
|
---|
738 | statusBarText = new JLabel(" ");
|
---|
739 | statusBarText.setFont(statusBarText.getFont().deriveFont(8));
|
---|
740 | statusBar.add(statusBarText);
|
---|
741 |
|
---|
742 | tfTimezone.addFocusListener(repaintTheMap);
|
---|
743 | tfOffset.addFocusListener(repaintTheMap);
|
---|
744 |
|
---|
745 | tfTimezone.getDocument().addDocumentListener(statusBarUpdater);
|
---|
746 | tfOffset.getDocument().addDocumentListener(statusBarUpdater);
|
---|
747 | cbExifImg.addItemListener(statusBarUpdaterWithRepaint);
|
---|
748 | cbTaggedImg.addItemListener(statusBarUpdaterWithRepaint);
|
---|
749 |
|
---|
750 | statusBarUpdater.updateStatusBar();
|
---|
751 |
|
---|
752 | outerPanel = new JPanel(new BorderLayout());
|
---|
753 | outerPanel.add(statusBar, BorderLayout.PAGE_END);
|
---|
754 |
|
---|
755 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
756 | syncDialog = new ExtendedDialog(
|
---|
757 | Main.parent,
|
---|
758 | tr("Correlate images with GPX track"),
|
---|
759 | new String[] {tr("Correlate"), tr("Cancel")},
|
---|
760 | false
|
---|
761 | );
|
---|
762 | syncDialog.setContent(panelTf, false);
|
---|
763 | syncDialog.setButtonIcons(new String[] {"ok", "cancel"});
|
---|
764 | syncDialog.setupDialog();
|
---|
765 | outerPanel.add(syncDialog.getContentPane(), BorderLayout.PAGE_START);
|
---|
766 | syncDialog.setContentPane(outerPanel);
|
---|
767 | syncDialog.pack();
|
---|
768 | syncDialog.addWindowListener(new SyncDialogWindowListener());
|
---|
769 | syncDialog.showDialog();
|
---|
770 | }
|
---|
771 | }
|
---|
772 |
|
---|
773 | private final transient StatusBarUpdater statusBarUpdater = new StatusBarUpdater(false);
|
---|
774 | private final transient StatusBarUpdater statusBarUpdaterWithRepaint = new StatusBarUpdater(true);
|
---|
775 |
|
---|
776 | private class StatusBarUpdater implements DocumentListener, ItemListener, ActionListener {
|
---|
777 | private final boolean doRepaint;
|
---|
778 |
|
---|
779 | StatusBarUpdater(boolean doRepaint) {
|
---|
780 | this.doRepaint = doRepaint;
|
---|
781 | }
|
---|
782 |
|
---|
783 | @Override
|
---|
784 | public void insertUpdate(DocumentEvent ev) {
|
---|
785 | updateStatusBar();
|
---|
786 | }
|
---|
787 |
|
---|
788 | @Override
|
---|
789 | public void removeUpdate(DocumentEvent ev) {
|
---|
790 | updateStatusBar();
|
---|
791 | }
|
---|
792 |
|
---|
793 | @Override
|
---|
794 | public void changedUpdate(DocumentEvent ev) {
|
---|
795 | // Do nothing
|
---|
796 | }
|
---|
797 |
|
---|
798 | @Override
|
---|
799 | public void itemStateChanged(ItemEvent e) {
|
---|
800 | updateStatusBar();
|
---|
801 | }
|
---|
802 |
|
---|
803 | @Override
|
---|
804 | public void actionPerformed(ActionEvent e) {
|
---|
805 | updateStatusBar();
|
---|
806 | }
|
---|
807 |
|
---|
808 | public void updateStatusBar() {
|
---|
809 | statusBarText.setText(statusText());
|
---|
810 | if (doRepaint) {
|
---|
811 | yLayer.updateBufferAndRepaint();
|
---|
812 | }
|
---|
813 | }
|
---|
814 |
|
---|
815 | private String statusText() {
|
---|
816 | try {
|
---|
817 | timezone = Timezone.parseTimezone(tfTimezone.getText().trim());
|
---|
818 | delta = Offset.parseOffset(tfOffset.getText().trim());
|
---|
819 | } catch (ParseException e) {
|
---|
820 | return e.getMessage();
|
---|
821 | }
|
---|
822 |
|
---|
823 | // The selection of images we are about to correlate may have changed.
|
---|
824 | // So reset all images.
|
---|
825 | if (yLayer.data != null) {
|
---|
826 | for (ImageEntry ie: yLayer.data) {
|
---|
827 | ie.discardTmp();
|
---|
828 | }
|
---|
829 | }
|
---|
830 |
|
---|
831 | // Construct a list of images that have a date, and sort them on the date.
|
---|
832 | List<ImageEntry> dateImgLst = getSortedImgList();
|
---|
833 | // Create a temporary copy for each image
|
---|
834 | for (ImageEntry ie : dateImgLst) {
|
---|
835 | ie.createTmp();
|
---|
836 | ie.tmp.setPos(null);
|
---|
837 | }
|
---|
838 |
|
---|
839 | GpxDataWrapper selGpx = selectedGPX(false);
|
---|
840 | if (selGpx == null)
|
---|
841 | return tr("No gpx selected");
|
---|
842 |
|
---|
843 | final long offsetMs = ((long) (timezone.getHours() * 3600 * 1000)) + delta.getMilliseconds(); // in milliseconds
|
---|
844 | lastNumMatched = matchGpxTrack(dateImgLst, selGpx.data, offsetMs);
|
---|
845 |
|
---|
846 | return trn("<html>Matched <b>{0}</b> of <b>{1}</b> photo to GPX track.</html>",
|
---|
847 | "<html>Matched <b>{0}</b> of <b>{1}</b> photos to GPX track.</html>",
|
---|
848 | dateImgLst.size(), lastNumMatched, dateImgLst.size());
|
---|
849 | }
|
---|
850 | }
|
---|
851 |
|
---|
852 | private final transient RepaintTheMapListener repaintTheMap = new RepaintTheMapListener();
|
---|
853 |
|
---|
854 | private class RepaintTheMapListener implements FocusListener {
|
---|
855 | @Override
|
---|
856 | public void focusGained(FocusEvent e) { // do nothing
|
---|
857 | }
|
---|
858 |
|
---|
859 | @Override
|
---|
860 | public void focusLost(FocusEvent e) {
|
---|
861 | yLayer.updateBufferAndRepaint();
|
---|
862 | }
|
---|
863 | }
|
---|
864 |
|
---|
865 | /**
|
---|
866 | * Presents dialog with sliders for manual adjust.
|
---|
867 | */
|
---|
868 | private class AdjustActionListener implements ActionListener {
|
---|
869 |
|
---|
870 | @Override
|
---|
871 | public void actionPerformed(ActionEvent arg0) {
|
---|
872 |
|
---|
873 | final Offset offset = Offset.milliseconds(
|
---|
874 | delta.getMilliseconds() + Math.round(timezone.getHours() * 60 * 60 * 1000));
|
---|
875 | final int dayOffset = offset.getDayOffset();
|
---|
876 | final Pair<Timezone, Offset> timezoneOffsetPair = offset.withoutDayOffset().splitOutTimezone();
|
---|
877 |
|
---|
878 | // Info Labels
|
---|
879 | final JLabel lblMatches = new JLabel();
|
---|
880 |
|
---|
881 | // Timezone Slider
|
---|
882 | // The slider allows to switch timezon from -12:00 to 12:00 in 30 minutes steps. Therefore the range is -24 to 24.
|
---|
883 | final JLabel lblTimezone = new JLabel();
|
---|
884 | final JSlider sldTimezone = new JSlider(-24, 24, 0);
|
---|
885 | sldTimezone.setPaintLabels(true);
|
---|
886 | Dictionary<Integer, JLabel> labelTable = new Hashtable<>();
|
---|
887 | // CHECKSTYLE.OFF: ParenPad
|
---|
888 | for (int i = -12; i <= 12; i += 6) {
|
---|
889 | labelTable.put(i * 2, new JLabel(new Timezone(i).formatTimezone()));
|
---|
890 | }
|
---|
891 | // CHECKSTYLE.ON: ParenPad
|
---|
892 | sldTimezone.setLabelTable(labelTable);
|
---|
893 |
|
---|
894 | // Minutes Slider
|
---|
895 | final JLabel lblMinutes = new JLabel();
|
---|
896 | final JSlider sldMinutes = new JSlider(-15, 15, 0);
|
---|
897 | sldMinutes.setPaintLabels(true);
|
---|
898 | sldMinutes.setMajorTickSpacing(5);
|
---|
899 |
|
---|
900 | // Seconds slider
|
---|
901 | final JLabel lblSeconds = new JLabel();
|
---|
902 | final JSlider sldSeconds = new JSlider(-600, 600, 0);
|
---|
903 | sldSeconds.setPaintLabels(true);
|
---|
904 | labelTable = new Hashtable<>();
|
---|
905 | // CHECKSTYLE.OFF: ParenPad
|
---|
906 | for (int i = -60; i <= 60; i += 30) {
|
---|
907 | labelTable.put(i * 10, new JLabel(Offset.seconds(i).formatOffset()));
|
---|
908 | }
|
---|
909 | // CHECKSTYLE.ON: ParenPad
|
---|
910 | sldSeconds.setLabelTable(labelTable);
|
---|
911 | sldSeconds.setMajorTickSpacing(300);
|
---|
912 |
|
---|
913 | // This is called whenever one of the sliders is moved.
|
---|
914 | // It updates the labels and also calls the "match photos" code
|
---|
915 | class SliderListener implements ChangeListener {
|
---|
916 | @Override
|
---|
917 | public void stateChanged(ChangeEvent e) {
|
---|
918 | timezone = new Timezone(sldTimezone.getValue() / 2.);
|
---|
919 |
|
---|
920 | lblTimezone.setText(tr("Timezone: {0}", timezone.formatTimezone()));
|
---|
921 | lblMinutes.setText(tr("Minutes: {0}", sldMinutes.getValue()));
|
---|
922 | lblSeconds.setText(tr("Seconds: {0}", Offset.milliseconds(100 * sldSeconds.getValue()).formatOffset()));
|
---|
923 |
|
---|
924 | delta = Offset.milliseconds(100 * sldSeconds.getValue()
|
---|
925 | + 1000L * 60 * sldMinutes.getValue()
|
---|
926 | + 1000L * 60 * 60 * 24 * dayOffset);
|
---|
927 |
|
---|
928 | tfTimezone.getDocument().removeDocumentListener(statusBarUpdater);
|
---|
929 | tfOffset.getDocument().removeDocumentListener(statusBarUpdater);
|
---|
930 |
|
---|
931 | tfTimezone.setText(timezone.formatTimezone());
|
---|
932 | tfOffset.setText(delta.formatOffset());
|
---|
933 |
|
---|
934 | tfTimezone.getDocument().addDocumentListener(statusBarUpdater);
|
---|
935 | tfOffset.getDocument().addDocumentListener(statusBarUpdater);
|
---|
936 |
|
---|
937 | lblMatches.setText(statusBarText.getText() + "<br>" + trn("(Time difference of {0} day)",
|
---|
938 | "Time difference of {0} days", Math.abs(dayOffset), Math.abs(dayOffset)));
|
---|
939 |
|
---|
940 | statusBarUpdater.updateStatusBar();
|
---|
941 | yLayer.updateBufferAndRepaint();
|
---|
942 | }
|
---|
943 | }
|
---|
944 |
|
---|
945 | // Put everything together
|
---|
946 | JPanel p = new JPanel(new GridBagLayout());
|
---|
947 | p.setPreferredSize(new Dimension(400, 230));
|
---|
948 | p.add(lblMatches, GBC.eol().fill());
|
---|
949 | p.add(lblTimezone, GBC.eol().fill());
|
---|
950 | p.add(sldTimezone, GBC.eol().fill().insets(0, 0, 0, 10));
|
---|
951 | p.add(lblMinutes, GBC.eol().fill());
|
---|
952 | p.add(sldMinutes, GBC.eol().fill().insets(0, 0, 0, 10));
|
---|
953 | p.add(lblSeconds, GBC.eol().fill());
|
---|
954 | p.add(sldSeconds, GBC.eol().fill());
|
---|
955 |
|
---|
956 | // If there's an error in the calculation the found values
|
---|
957 | // will be off range for the sliders. Catch this error
|
---|
958 | // and inform the user about it.
|
---|
959 | try {
|
---|
960 | sldTimezone.setValue((int) (timezoneOffsetPair.a.getHours() * 2));
|
---|
961 | sldMinutes.setValue((int) (timezoneOffsetPair.b.getSeconds() / 60));
|
---|
962 | final long deciSeconds = timezoneOffsetPair.b.getMilliseconds() / 100;
|
---|
963 | sldSeconds.setValue((int) (deciSeconds % 60));
|
---|
964 | } catch (Exception e) {
|
---|
965 | JOptionPane.showMessageDialog(Main.parent,
|
---|
966 | tr("An error occurred while trying to match the photos to the GPX track."
|
---|
967 | +" You can adjust the sliders to manually match the photos."),
|
---|
968 | tr("Matching photos to track failed"),
|
---|
969 | JOptionPane.WARNING_MESSAGE);
|
---|
970 | }
|
---|
971 |
|
---|
972 | // Call the sliderListener once manually so labels get adjusted
|
---|
973 | new SliderListener().stateChanged(null);
|
---|
974 | // Listeners added here, otherwise it tries to match three times
|
---|
975 | // (when setting the default values)
|
---|
976 | sldTimezone.addChangeListener(new SliderListener());
|
---|
977 | sldMinutes.addChangeListener(new SliderListener());
|
---|
978 | sldSeconds.addChangeListener(new SliderListener());
|
---|
979 |
|
---|
980 | // There is no way to cancel this dialog, all changes get applied
|
---|
981 | // immediately. Therefore "Close" is marked with an "OK" icon.
|
---|
982 | // Settings are only saved temporarily to the layer.
|
---|
983 | new ExtendedDialog(Main.parent,
|
---|
984 | tr("Adjust timezone and offset"),
|
---|
985 | new String[] {tr("Close")}).
|
---|
986 | setContent(p).setButtonIcons(new String[] {"ok"}).showDialog();
|
---|
987 | }
|
---|
988 | }
|
---|
989 |
|
---|
990 | static class NoGpxTimestamps extends Exception {
|
---|
991 | }
|
---|
992 |
|
---|
993 | /**
|
---|
994 | * Tries to auto-guess the timezone and offset.
|
---|
995 | *
|
---|
996 | * @param imgs the images to correlate
|
---|
997 | * @param gpx the gpx track to correlate to
|
---|
998 | * @return a pair of timezone and offset
|
---|
999 | * @throws IndexOutOfBoundsException when there are no images
|
---|
1000 | * @throws NoGpxTimestamps when the gpx track does not contain a timestamp
|
---|
1001 | */
|
---|
1002 | static Pair<Timezone, Offset> autoGuess(List<ImageEntry> imgs, GpxData gpx) throws IndexOutOfBoundsException, NoGpxTimestamps {
|
---|
1003 |
|
---|
1004 | // Init variables
|
---|
1005 | long firstExifDate = imgs.get(0).getExifTime().getTime();
|
---|
1006 |
|
---|
1007 | long firstGPXDate = -1;
|
---|
1008 | // Finds first GPX point
|
---|
1009 | outer: for (GpxTrack trk : gpx.tracks) {
|
---|
1010 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
1011 | for (WayPoint curWp : segment.getWayPoints()) {
|
---|
1012 | try {
|
---|
1013 | final Date parsedTime = curWp.setTimeFromAttribute();
|
---|
1014 | if (parsedTime != null) {
|
---|
1015 | firstGPXDate = parsedTime.getTime();
|
---|
1016 | break outer;
|
---|
1017 | }
|
---|
1018 | } catch (Exception e) {
|
---|
1019 | Main.warn(e);
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | if (firstGPXDate < 0) {
|
---|
1026 | throw new NoGpxTimestamps();
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | return Offset.milliseconds(firstExifDate - firstGPXDate).splitOutTimezone();
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | private class AutoGuessActionListener implements ActionListener {
|
---|
1033 |
|
---|
1034 | @Override
|
---|
1035 | public void actionPerformed(ActionEvent arg0) {
|
---|
1036 | GpxDataWrapper gpxW = selectedGPX(true);
|
---|
1037 | if (gpxW == null)
|
---|
1038 | return;
|
---|
1039 | GpxData gpx = gpxW.data;
|
---|
1040 |
|
---|
1041 | List<ImageEntry> imgs = getSortedImgList();
|
---|
1042 |
|
---|
1043 | try {
|
---|
1044 | final Pair<Timezone, Offset> r = autoGuess(imgs, gpx);
|
---|
1045 | timezone = r.a;
|
---|
1046 | delta = r.b;
|
---|
1047 | } catch (IndexOutOfBoundsException ex) {
|
---|
1048 | JOptionPane.showMessageDialog(Main.parent,
|
---|
1049 | tr("The selected photos do not contain time information."),
|
---|
1050 | tr("Photos do not contain time information"), JOptionPane.WARNING_MESSAGE);
|
---|
1051 | return;
|
---|
1052 | } catch (NoGpxTimestamps ex) {
|
---|
1053 | JOptionPane.showMessageDialog(Main.parent,
|
---|
1054 | tr("The selected GPX track does not contain timestamps. Please select another one."),
|
---|
1055 | tr("GPX Track has no time information"), JOptionPane.WARNING_MESSAGE);
|
---|
1056 | return;
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | tfTimezone.getDocument().removeDocumentListener(statusBarUpdater);
|
---|
1060 | tfOffset.getDocument().removeDocumentListener(statusBarUpdater);
|
---|
1061 |
|
---|
1062 | tfTimezone.setText(timezone.formatTimezone());
|
---|
1063 | tfOffset.setText(delta.formatOffset());
|
---|
1064 | tfOffset.requestFocus();
|
---|
1065 |
|
---|
1066 | tfTimezone.getDocument().addDocumentListener(statusBarUpdater);
|
---|
1067 | tfOffset.getDocument().addDocumentListener(statusBarUpdater);
|
---|
1068 |
|
---|
1069 | statusBarUpdater.updateStatusBar();
|
---|
1070 | yLayer.updateBufferAndRepaint();
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | private List<ImageEntry> getSortedImgList() {
|
---|
1075 | return getSortedImgList(cbExifImg.isSelected(), cbTaggedImg.isSelected());
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | /**
|
---|
1079 | * Returns a list of images that fulfill the given criteria.
|
---|
1080 | * Default setting is to return untagged images, but may be overwritten.
|
---|
1081 | * @param exif also returns images with exif-gps info
|
---|
1082 | * @param tagged also returns tagged images
|
---|
1083 | * @return matching images
|
---|
1084 | */
|
---|
1085 | private List<ImageEntry> getSortedImgList(boolean exif, boolean tagged) {
|
---|
1086 | if (yLayer.data == null) {
|
---|
1087 | return Collections.emptyList();
|
---|
1088 | }
|
---|
1089 | List<ImageEntry> dateImgLst = new ArrayList<>(yLayer.data.size());
|
---|
1090 | for (ImageEntry e : yLayer.data) {
|
---|
1091 | if (!e.hasExifTime()) {
|
---|
1092 | continue;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | if (e.getExifCoor() != null && !exif) {
|
---|
1096 | continue;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | if (e.isTagged() && e.getExifCoor() == null && !tagged) {
|
---|
1100 | continue;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | dateImgLst.add(e);
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | Collections.sort(dateImgLst, new Comparator<ImageEntry>() {
|
---|
1107 | @Override
|
---|
1108 | public int compare(ImageEntry arg0, ImageEntry arg1) {
|
---|
1109 | return arg0.getExifTime().compareTo(arg1.getExifTime());
|
---|
1110 | }
|
---|
1111 | });
|
---|
1112 |
|
---|
1113 | return dateImgLst;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | private GpxDataWrapper selectedGPX(boolean complain) {
|
---|
1117 | Object item = cbGpx.getSelectedItem();
|
---|
1118 |
|
---|
1119 | if (item == null || ((GpxDataWrapper) item).file == null) {
|
---|
1120 | if (complain) {
|
---|
1121 | JOptionPane.showMessageDialog(Main.parent, tr("You should select a GPX track"),
|
---|
1122 | tr("No selected GPX track"), JOptionPane.ERROR_MESSAGE);
|
---|
1123 | }
|
---|
1124 | return null;
|
---|
1125 | }
|
---|
1126 | return (GpxDataWrapper) item;
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | /**
|
---|
1130 | * Match a list of photos to a gpx track with a given offset.
|
---|
1131 | * All images need a exifTime attribute and the List must be sorted according to these times.
|
---|
1132 | * @param images images to match
|
---|
1133 | * @param selectedGpx selected GPX data
|
---|
1134 | * @param offset offset
|
---|
1135 | * @return number of matched points
|
---|
1136 | */
|
---|
1137 | static int matchGpxTrack(List<ImageEntry> images, GpxData selectedGpx, long offset) {
|
---|
1138 | int ret = 0;
|
---|
1139 |
|
---|
1140 | for (GpxTrack trk : selectedGpx.tracks) {
|
---|
1141 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
1142 |
|
---|
1143 | long prevWpTime = 0;
|
---|
1144 | WayPoint prevWp = null;
|
---|
1145 |
|
---|
1146 | for (WayPoint curWp : segment.getWayPoints()) {
|
---|
1147 | try {
|
---|
1148 | final Date parsedTime = curWp.setTimeFromAttribute();
|
---|
1149 | if (parsedTime != null) {
|
---|
1150 | final long curWpTime = parsedTime.getTime() + offset;
|
---|
1151 | ret += matchPoints(images, prevWp, prevWpTime, curWp, curWpTime, offset);
|
---|
1152 |
|
---|
1153 | prevWp = curWp;
|
---|
1154 | prevWpTime = curWpTime;
|
---|
1155 | continue;
|
---|
1156 | }
|
---|
1157 | } catch (Exception e) {
|
---|
1158 | Main.warn(e);
|
---|
1159 | }
|
---|
1160 | prevWp = null;
|
---|
1161 | prevWpTime = 0;
|
---|
1162 | }
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 | return ret;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | private static Double getElevation(WayPoint wp) {
|
---|
1169 | String value = wp.getString(GpxConstants.PT_ELE);
|
---|
1170 | if (value != null && !value.isEmpty()) {
|
---|
1171 | try {
|
---|
1172 | return new Double(value);
|
---|
1173 | } catch (NumberFormatException e) {
|
---|
1174 | Main.warn(e);
|
---|
1175 | }
|
---|
1176 | }
|
---|
1177 | return null;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | static int matchPoints(List<ImageEntry> images, WayPoint prevWp, long prevWpTime,
|
---|
1181 | WayPoint curWp, long curWpTime, long offset) {
|
---|
1182 | // Time between the track point and the previous one, 5 sec if first point, i.e. photos take
|
---|
1183 | // 5 sec before the first track point can be assumed to be take at the starting position
|
---|
1184 | long interval = prevWpTime > 0 ? Math.abs(curWpTime - prevWpTime) : 5*1000;
|
---|
1185 | int ret = 0;
|
---|
1186 |
|
---|
1187 | // i is the index of the timewise last photo that has the same or earlier EXIF time
|
---|
1188 | int i = getLastIndexOfListBefore(images, curWpTime);
|
---|
1189 |
|
---|
1190 | // no photos match
|
---|
1191 | if (i < 0)
|
---|
1192 | return 0;
|
---|
1193 |
|
---|
1194 | Double speed = null;
|
---|
1195 | Double prevElevation = null;
|
---|
1196 |
|
---|
1197 | if (prevWp != null) {
|
---|
1198 | double distance = prevWp.getCoor().greatCircleDistance(curWp.getCoor());
|
---|
1199 | // This is in km/h, 3.6 * m/s
|
---|
1200 | if (curWpTime > prevWpTime) {
|
---|
1201 | speed = 3600 * distance / (curWpTime - prevWpTime);
|
---|
1202 | }
|
---|
1203 | prevElevation = getElevation(prevWp);
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | Double curElevation = getElevation(curWp);
|
---|
1207 |
|
---|
1208 | // First trackpoint, then interval is set to five seconds, i.e. photos up to five seconds
|
---|
1209 | // before the first point will be geotagged with the starting point
|
---|
1210 | if (prevWpTime == 0 || curWpTime <= prevWpTime) {
|
---|
1211 | while (i >= 0) {
|
---|
1212 | final ImageEntry curImg = images.get(i);
|
---|
1213 | long time = curImg.getExifTime().getTime();
|
---|
1214 | if (time > curWpTime || time < curWpTime - interval) {
|
---|
1215 | break;
|
---|
1216 | }
|
---|
1217 | if (curImg.tmp.getPos() == null) {
|
---|
1218 | curImg.tmp.setPos(curWp.getCoor());
|
---|
1219 | curImg.tmp.setSpeed(speed);
|
---|
1220 | curImg.tmp.setElevation(curElevation);
|
---|
1221 | curImg.tmp.setGpsTime(new Date(curImg.getExifTime().getTime() - offset));
|
---|
1222 | curImg.flagNewGpsData();
|
---|
1223 | ret++;
|
---|
1224 | }
|
---|
1225 | i--;
|
---|
1226 | }
|
---|
1227 | return ret;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | // This code gives a simple linear interpolation of the coordinates between current and
|
---|
1231 | // previous track point assuming a constant speed in between
|
---|
1232 | while (i >= 0) {
|
---|
1233 | ImageEntry curImg = images.get(i);
|
---|
1234 | long imgTime = curImg.getExifTime().getTime();
|
---|
1235 | if (imgTime < prevWpTime) {
|
---|
1236 | break;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | if (curImg.tmp.getPos() == null && prevWp != null) {
|
---|
1240 | // The values of timeDiff are between 0 and 1, it is not seconds but a dimensionless variable
|
---|
1241 | double timeDiff = (double) (imgTime - prevWpTime) / interval;
|
---|
1242 | curImg.tmp.setPos(prevWp.getCoor().interpolate(curWp.getCoor(), timeDiff));
|
---|
1243 | curImg.tmp.setSpeed(speed);
|
---|
1244 | if (curElevation != null && prevElevation != null) {
|
---|
1245 | curImg.tmp.setElevation(prevElevation + (curElevation - prevElevation) * timeDiff);
|
---|
1246 | }
|
---|
1247 | curImg.tmp.setGpsTime(new Date(curImg.getExifTime().getTime() - offset));
|
---|
1248 | curImg.flagNewGpsData();
|
---|
1249 |
|
---|
1250 | ret++;
|
---|
1251 | }
|
---|
1252 | i--;
|
---|
1253 | }
|
---|
1254 | return ret;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | private static int getLastIndexOfListBefore(List<ImageEntry> images, long searchedTime) {
|
---|
1258 | int lstSize = images.size();
|
---|
1259 |
|
---|
1260 | // No photos or the first photo taken is later than the search period
|
---|
1261 | if (lstSize == 0 || searchedTime < images.get(0).getExifTime().getTime())
|
---|
1262 | return -1;
|
---|
1263 |
|
---|
1264 | // The search period is later than the last photo
|
---|
1265 | if (searchedTime > images.get(lstSize - 1).getExifTime().getTime())
|
---|
1266 | return lstSize-1;
|
---|
1267 |
|
---|
1268 | // The searched index is somewhere in the middle, do a binary search from the beginning
|
---|
1269 | int curIndex = 0;
|
---|
1270 | int startIndex = 0;
|
---|
1271 | int endIndex = lstSize-1;
|
---|
1272 | while (endIndex - startIndex > 1) {
|
---|
1273 | curIndex = (endIndex + startIndex) / 2;
|
---|
1274 | if (searchedTime > images.get(curIndex).getExifTime().getTime()) {
|
---|
1275 | startIndex = curIndex;
|
---|
1276 | } else {
|
---|
1277 | endIndex = curIndex;
|
---|
1278 | }
|
---|
1279 | }
|
---|
1280 | if (searchedTime < images.get(endIndex).getExifTime().getTime())
|
---|
1281 | return startIndex;
|
---|
1282 |
|
---|
1283 | // This final loop is to check if photos with the exact same EXIF time follows
|
---|
1284 | while ((endIndex < (lstSize-1)) && (images.get(endIndex).getExifTime().getTime()
|
---|
1285 | == images.get(endIndex + 1).getExifTime().getTime())) {
|
---|
1286 | endIndex++;
|
---|
1287 | }
|
---|
1288 | return endIndex;
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 | static final class Timezone {
|
---|
1292 |
|
---|
1293 | static final Timezone ZERO = new Timezone(0.0);
|
---|
1294 | private final double timezone;
|
---|
1295 |
|
---|
1296 | Timezone(double hours) {
|
---|
1297 | this.timezone = hours;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | public double getHours() {
|
---|
1301 | return timezone;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | String formatTimezone() {
|
---|
1305 | StringBuilder ret = new StringBuilder();
|
---|
1306 |
|
---|
1307 | double timezone = this.timezone;
|
---|
1308 | if (timezone < 0) {
|
---|
1309 | ret.append('-');
|
---|
1310 | timezone = -timezone;
|
---|
1311 | } else {
|
---|
1312 | ret.append('+');
|
---|
1313 | }
|
---|
1314 | ret.append((long) timezone).append(':');
|
---|
1315 | int minutes = (int) ((timezone % 1) * 60);
|
---|
1316 | if (minutes < 10) {
|
---|
1317 | ret.append('0');
|
---|
1318 | }
|
---|
1319 | ret.append(minutes);
|
---|
1320 |
|
---|
1321 | return ret.toString();
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | static Timezone parseTimezone(String timezone) throws ParseException {
|
---|
1325 |
|
---|
1326 | if (timezone.isEmpty())
|
---|
1327 | return ZERO;
|
---|
1328 |
|
---|
1329 | String error = tr("Error while parsing timezone.\nExpected format: {0}", "+H:MM");
|
---|
1330 |
|
---|
1331 | char sgnTimezone = '+';
|
---|
1332 | StringBuilder hTimezone = new StringBuilder();
|
---|
1333 | StringBuilder mTimezone = new StringBuilder();
|
---|
1334 | int state = 1; // 1=start/sign, 2=hours, 3=minutes.
|
---|
1335 | for (int i = 0; i < timezone.length(); i++) {
|
---|
1336 | char c = timezone.charAt(i);
|
---|
1337 | switch (c) {
|
---|
1338 | case ' ':
|
---|
1339 | if (state != 2 || hTimezone.length() != 0)
|
---|
1340 | throw new ParseException(error, i);
|
---|
1341 | break;
|
---|
1342 | case '+':
|
---|
1343 | case '-':
|
---|
1344 | if (state == 1) {
|
---|
1345 | sgnTimezone = c;
|
---|
1346 | state = 2;
|
---|
1347 | } else
|
---|
1348 | throw new ParseException(error, i);
|
---|
1349 | break;
|
---|
1350 | case ':':
|
---|
1351 | case '.':
|
---|
1352 | if (state == 2) {
|
---|
1353 | state = 3;
|
---|
1354 | } else
|
---|
1355 | throw new ParseException(error, i);
|
---|
1356 | break;
|
---|
1357 | case '0':
|
---|
1358 | case '1':
|
---|
1359 | case '2':
|
---|
1360 | case '3':
|
---|
1361 | case '4':
|
---|
1362 | case '5':
|
---|
1363 | case '6':
|
---|
1364 | case '7':
|
---|
1365 | case '8':
|
---|
1366 | case '9':
|
---|
1367 | switch (state) {
|
---|
1368 | case 1:
|
---|
1369 | case 2:
|
---|
1370 | state = 2;
|
---|
1371 | hTimezone.append(c);
|
---|
1372 | break;
|
---|
1373 | case 3:
|
---|
1374 | mTimezone.append(c);
|
---|
1375 | break;
|
---|
1376 | default:
|
---|
1377 | throw new ParseException(error, i);
|
---|
1378 | }
|
---|
1379 | break;
|
---|
1380 | default:
|
---|
1381 | throw new ParseException(error, i);
|
---|
1382 | }
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | int h = 0;
|
---|
1386 | int m = 0;
|
---|
1387 | try {
|
---|
1388 | h = Integer.parseInt(hTimezone.toString());
|
---|
1389 | if (mTimezone.length() > 0) {
|
---|
1390 | m = Integer.parseInt(mTimezone.toString());
|
---|
1391 | }
|
---|
1392 | } catch (NumberFormatException nfe) {
|
---|
1393 | // Invalid timezone
|
---|
1394 | throw (ParseException) new ParseException(error, 0).initCause(nfe);
|
---|
1395 | }
|
---|
1396 |
|
---|
1397 | if (h > 12 || m > 59)
|
---|
1398 | throw new ParseException(error, 0);
|
---|
1399 | else
|
---|
1400 | return new Timezone((h + m / 60.0) * (sgnTimezone == '-' ? -1 : 1));
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | @Override
|
---|
1404 | public boolean equals(Object o) {
|
---|
1405 | if (this == o) return true;
|
---|
1406 | if (!(o instanceof Timezone)) return false;
|
---|
1407 | Timezone timezone1 = (Timezone) o;
|
---|
1408 | return Double.compare(timezone1.timezone, timezone) == 0;
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | @Override
|
---|
1412 | public int hashCode() {
|
---|
1413 | return Objects.hash(timezone);
|
---|
1414 | }
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | static final class Offset {
|
---|
1418 |
|
---|
1419 | static final Offset ZERO = new Offset(0);
|
---|
1420 | private final long milliseconds;
|
---|
1421 |
|
---|
1422 | private Offset(long milliseconds) {
|
---|
1423 | this.milliseconds = milliseconds;
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | static Offset milliseconds(long milliseconds) {
|
---|
1427 | return new Offset(milliseconds);
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | static Offset seconds(long seconds) {
|
---|
1431 | return new Offset(1000 * seconds);
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | long getMilliseconds() {
|
---|
1435 | return milliseconds;
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 | long getSeconds() {
|
---|
1439 | return milliseconds / 1000;
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 | String formatOffset() {
|
---|
1443 | if (milliseconds % 1000 == 0) {
|
---|
1444 | return Long.toString(milliseconds / 1000);
|
---|
1445 | } else if (milliseconds % 100 == 0) {
|
---|
1446 | return String.format(Locale.ENGLISH, "%.1f", milliseconds / 1000.);
|
---|
1447 | } else {
|
---|
1448 | return String.format(Locale.ENGLISH, "%.3f", milliseconds / 1000.);
|
---|
1449 | }
|
---|
1450 | }
|
---|
1451 |
|
---|
1452 | static Offset parseOffset(String offset) throws ParseException {
|
---|
1453 | String error = tr("Error while parsing offset.\nExpected format: {0}", "number");
|
---|
1454 |
|
---|
1455 | if (!offset.isEmpty()) {
|
---|
1456 | try {
|
---|
1457 | if (offset.startsWith("+")) {
|
---|
1458 | offset = offset.substring(1);
|
---|
1459 | }
|
---|
1460 | return Offset.milliseconds(Math.round(Double.parseDouble(offset) * 1000));
|
---|
1461 | } catch (NumberFormatException nfe) {
|
---|
1462 | throw (ParseException) new ParseException(error, 0).initCause(nfe);
|
---|
1463 | }
|
---|
1464 | } else {
|
---|
1465 | return Offset.ZERO;
|
---|
1466 | }
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | int getDayOffset() {
|
---|
1470 | final double diffInH = getMilliseconds() / 1000. / 60 / 60; // hours
|
---|
1471 |
|
---|
1472 | // Find day difference
|
---|
1473 | return (int) Math.round(diffInH / 24);
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | Offset withoutDayOffset() {
|
---|
1477 | return milliseconds(getMilliseconds() - getDayOffset() * 24L * 60L * 60L * 1000L);
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | Pair<Timezone, Offset> splitOutTimezone() {
|
---|
1481 | // In hours
|
---|
1482 | double tz = withoutDayOffset().getSeconds() / 3600.0;
|
---|
1483 |
|
---|
1484 | // Due to imprecise clocks we might get a "+3:28" timezone, which should obviously be 3:30 with
|
---|
1485 | // -2 minutes offset. This determines the real timezone and finds offset.
|
---|
1486 | final double timezone = (double) Math.round(tz * 2) / 2; // hours, rounded to one decimal place
|
---|
1487 | final long delta = Math.round(getMilliseconds() - timezone * 60 * 60 * 1000); // milliseconds
|
---|
1488 | return Pair.create(new Timezone(timezone), Offset.milliseconds(delta));
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | @Override
|
---|
1492 | public boolean equals(Object o) {
|
---|
1493 | if (this == o) return true;
|
---|
1494 | if (!(o instanceof Offset)) return false;
|
---|
1495 | Offset offset = (Offset) o;
|
---|
1496 | return milliseconds == offset.milliseconds;
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | @Override
|
---|
1500 | public int hashCode() {
|
---|
1501 | return Objects.hash(milliseconds);
|
---|
1502 | }
|
---|
1503 | }
|
---|
1504 | }
|
---|