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