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