1 | // License: GPL. See LICENSE file for details.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui.layer;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
8 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
9 |
|
---|
10 | import java.awt.BasicStroke;
|
---|
11 | import java.awt.Color;
|
---|
12 | import java.awt.Component;
|
---|
13 | import java.awt.Dimension;
|
---|
14 | import java.awt.Graphics2D;
|
---|
15 | import java.awt.GridBagLayout;
|
---|
16 | import java.awt.Point;
|
---|
17 | import java.awt.RenderingHints;
|
---|
18 | import java.awt.event.ActionEvent;
|
---|
19 | import java.awt.event.MouseAdapter;
|
---|
20 | import java.awt.event.MouseEvent;
|
---|
21 | import java.awt.event.MouseListener;
|
---|
22 | import java.awt.geom.Area;
|
---|
23 | import java.awt.geom.Rectangle2D;
|
---|
24 | import java.io.File;
|
---|
25 | import java.net.MalformedURLException;
|
---|
26 | import java.net.URL;
|
---|
27 | import java.text.DateFormat;
|
---|
28 | import java.util.ArrayList;
|
---|
29 | import java.util.Arrays;
|
---|
30 | import java.util.Collection;
|
---|
31 | import java.util.Collections;
|
---|
32 | import java.util.Comparator;
|
---|
33 | import java.util.LinkedList;
|
---|
34 | import java.util.List;
|
---|
35 | import java.util.Map;
|
---|
36 | import java.util.concurrent.Future;
|
---|
37 |
|
---|
38 | import javax.swing.AbstractAction;
|
---|
39 | import javax.swing.Action;
|
---|
40 | import javax.swing.Icon;
|
---|
41 | import javax.swing.JComponent;
|
---|
42 | import javax.swing.JFileChooser;
|
---|
43 | import javax.swing.JLabel;
|
---|
44 | import javax.swing.JList;
|
---|
45 | import javax.swing.JMenuItem;
|
---|
46 | import javax.swing.JOptionPane;
|
---|
47 | import javax.swing.JPanel;
|
---|
48 | import javax.swing.JScrollPane;
|
---|
49 | import javax.swing.JTable;
|
---|
50 | import javax.swing.ListSelectionModel;
|
---|
51 | import javax.swing.SwingUtilities;
|
---|
52 | import javax.swing.event.ListSelectionEvent;
|
---|
53 | import javax.swing.event.ListSelectionListener;
|
---|
54 | import javax.swing.filechooser.FileFilter;
|
---|
55 | import javax.swing.table.TableCellRenderer;
|
---|
56 |
|
---|
57 | import org.openstreetmap.josm.Main;
|
---|
58 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
59 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTaskList;
|
---|
60 | import org.openstreetmap.josm.data.Bounds;
|
---|
61 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
62 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
63 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
64 | import org.openstreetmap.josm.data.gpx.GpxRoute;
|
---|
65 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
66 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
---|
67 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
68 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
69 | import org.openstreetmap.josm.data.osm.Node;
|
---|
70 | import org.openstreetmap.josm.data.osm.Way;
|
---|
71 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
72 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
73 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
74 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
75 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
76 | import org.openstreetmap.josm.gui.MapView;
|
---|
77 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
78 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
79 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
80 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
81 | import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker;
|
---|
82 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
83 | import org.openstreetmap.josm.gui.preferences.GPXSettingsPanel;
|
---|
84 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
85 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
86 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
87 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
88 | import org.openstreetmap.josm.io.JpgImporter;
|
---|
89 | import org.openstreetmap.josm.tools.AudioUtil;
|
---|
90 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
91 | import org.openstreetmap.josm.tools.GBC;
|
---|
92 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
93 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
94 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
95 | import org.openstreetmap.josm.tools.Utils;
|
---|
96 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
97 |
|
---|
98 | public class GpxLayer extends Layer {
|
---|
99 |
|
---|
100 | private static final String PREF_DOWNLOAD_ALONG_TRACK_DISTANCE = "gpxLayer.downloadAlongTrack.distance";
|
---|
101 | private static final String PREF_DOWNLOAD_ALONG_TRACK_AREA = "gpxLayer.downloadAlongTrack.area";
|
---|
102 | private static final String PREF_DOWNLOAD_ALONG_TRACK_NEAR = "gpxLayer.downloadAlongTrack.near";
|
---|
103 |
|
---|
104 | public GpxData data;
|
---|
105 | protected static final double PHI = Math.toRadians(15);
|
---|
106 | private boolean computeCacheInSync;
|
---|
107 | private int computeCacheMaxLineLengthUsed;
|
---|
108 | private Color computeCacheColorUsed;
|
---|
109 | private boolean computeCacheColorDynamic;
|
---|
110 | private colorModes computeCacheColored;
|
---|
111 | private int computeCacheColorTracksTune;
|
---|
112 | private boolean isLocalFile;
|
---|
113 | // used by ChooseTrackVisibilityAction to determine which tracks to show/hide
|
---|
114 | private boolean[] trackVisibility = new boolean[0];
|
---|
115 |
|
---|
116 | private final List<GpxTrack> lastTracks = new ArrayList<GpxTrack>(); // List of tracks at last paint
|
---|
117 | private int lastUpdateCount;
|
---|
118 |
|
---|
119 | private static class Markers {
|
---|
120 | public boolean timedMarkersOmitted = false;
|
---|
121 | public boolean untimedMarkersOmitted = false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public GpxLayer(GpxData d) {
|
---|
125 | super((String) d.attr.get("name"));
|
---|
126 | data = d;
|
---|
127 | computeCacheInSync = false;
|
---|
128 | ensureTrackVisibilityLength();
|
---|
129 | }
|
---|
130 |
|
---|
131 | public GpxLayer(GpxData d, String name) {
|
---|
132 | this(d);
|
---|
133 | this.setName(name);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public GpxLayer(GpxData d, String name, boolean isLocal) {
|
---|
137 | this(d);
|
---|
138 | this.setName(name);
|
---|
139 | this.isLocalFile = isLocal;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * returns a human readable string that shows the timespan of the given track
|
---|
144 | */
|
---|
145 | private static String getTimespanForTrack(GpxTrack trk) {
|
---|
146 | WayPoint earliest = null, latest = null;
|
---|
147 |
|
---|
148 | for (GpxTrackSegment seg : trk.getSegments()) {
|
---|
149 | for (WayPoint pnt : seg.getWayPoints()) {
|
---|
150 | if (latest == null) {
|
---|
151 | latest = earliest = pnt;
|
---|
152 | } else {
|
---|
153 | if (pnt.compareTo(earliest) < 0) {
|
---|
154 | earliest = pnt;
|
---|
155 | } else {
|
---|
156 | latest = pnt;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | String ts = "";
|
---|
163 |
|
---|
164 | if (earliest != null && latest != null) {
|
---|
165 | DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
|
---|
166 | String earliestDate = df.format(earliest.getTime());
|
---|
167 | String latestDate = df.format(latest.getTime());
|
---|
168 |
|
---|
169 | if (earliestDate.equals(latestDate)) {
|
---|
170 | DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT);
|
---|
171 | ts += earliestDate + " ";
|
---|
172 | ts += tf.format(earliest.getTime()) + " - " + tf.format(latest.getTime());
|
---|
173 | } else {
|
---|
174 | DateFormat dtf = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
|
---|
175 | ts += dtf.format(earliest.getTime()) + " - " + dtf.format(latest.getTime());
|
---|
176 | }
|
---|
177 |
|
---|
178 | int diff = (int) (latest.time - earliest.time);
|
---|
179 | ts += String.format(" (%d:%02d)", diff / 3600, (diff % 3600) / 60);
|
---|
180 | }
|
---|
181 | return ts;
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override
|
---|
185 | public Icon getIcon() {
|
---|
186 | return ImageProvider.get("layer", "gpx_small");
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public Object getInfoComponent() {
|
---|
191 | StringBuilder info = new StringBuilder();
|
---|
192 |
|
---|
193 | if (data.attr.containsKey("name")) {
|
---|
194 | info.append(tr("Name: {0}", data.attr.get(GpxData.META_NAME))).append("<br>");
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (data.attr.containsKey("desc")) {
|
---|
198 | info.append(tr("Description: {0}", data.attr.get(GpxData.META_DESC))).append("<br>");
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (data.tracks.size() > 0) {
|
---|
202 | info.append("<table><thead align='center'><tr><td colspan='5'>"
|
---|
203 | + trn("{0} track", "{0} tracks", data.tracks.size(), data.tracks.size())
|
---|
204 | + "</td></tr><tr align='center'><td>" + tr("Name") + "</td><td>"
|
---|
205 | + tr("Description") + "</td><td>" + tr("Timespan")
|
---|
206 | + "</td><td>" + tr("Length") + "</td><td>" + tr("URL")
|
---|
207 | + "</td></tr></thead>");
|
---|
208 |
|
---|
209 | for (GpxTrack trk : data.tracks) {
|
---|
210 | info.append("<tr><td>");
|
---|
211 | if (trk.getAttributes().containsKey("name")) {
|
---|
212 | info.append(trk.getAttributes().get("name"));
|
---|
213 | }
|
---|
214 | info.append("</td><td>");
|
---|
215 | if (trk.getAttributes().containsKey("desc")) {
|
---|
216 | info.append(" ").append(trk.getAttributes().get("desc"));
|
---|
217 | }
|
---|
218 | info.append("</td><td>");
|
---|
219 | info.append(getTimespanForTrack(trk));
|
---|
220 | info.append("</td><td>");
|
---|
221 | info.append(NavigatableComponent.getSystemOfMeasurement().getDistText(trk.length()));
|
---|
222 | info.append("</td><td>");
|
---|
223 | if (trk.getAttributes().containsKey("url")) {
|
---|
224 | info.append(trk.getAttributes().get("url"));
|
---|
225 | }
|
---|
226 | info.append("</td></tr>");
|
---|
227 | }
|
---|
228 |
|
---|
229 | info.append("</table><br><br>");
|
---|
230 |
|
---|
231 | }
|
---|
232 |
|
---|
233 | info.append(tr("Length: {0}", NavigatableComponent.getSystemOfMeasurement().getDistText(data.length()))).append("<br>");
|
---|
234 |
|
---|
235 | info.append(trn("{0} route, ", "{0} routes, ", data.routes.size(), data.routes.size())).append(
|
---|
236 | trn("{0} waypoint", "{0} waypoints", data.waypoints.size(), data.waypoints.size())).append("<br>");
|
---|
237 |
|
---|
238 | final JScrollPane sp = new JScrollPane(new HtmlPanel(info.toString()), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
---|
239 | sp.setPreferredSize(new Dimension(sp.getPreferredSize().width, 350));
|
---|
240 | SwingUtilities.invokeLater(new Runnable() {
|
---|
241 | @Override
|
---|
242 | public void run() {
|
---|
243 | sp.getVerticalScrollBar().setValue(0);
|
---|
244 | }
|
---|
245 | });
|
---|
246 | return sp;
|
---|
247 | }
|
---|
248 |
|
---|
249 | @Override
|
---|
250 | public Color getColor(boolean ignoreCustom) {
|
---|
251 | Color c = Main.pref.getColor(marktr("gps point"), "layer " + getName(), Color.gray);
|
---|
252 |
|
---|
253 | return ignoreCustom || getColorMode() == colorModes.none ? c : null;
|
---|
254 | }
|
---|
255 |
|
---|
256 | public colorModes getColorMode() {
|
---|
257 | try {
|
---|
258 | int i=Main.pref.getInteger("draw.rawgps.colors", "layer " + getName(), 0);
|
---|
259 | return colorModes.values()[i];
|
---|
260 | } catch (Exception e) {
|
---|
261 | }
|
---|
262 | return colorModes.none;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /* for preferences */
|
---|
266 | static public Color getGenericColor() {
|
---|
267 | return Main.pref.getColor(marktr("gps point"), Color.gray);
|
---|
268 | }
|
---|
269 |
|
---|
270 | @Override
|
---|
271 | public Action[] getMenuEntries() {
|
---|
272 | if (Main.applet)
|
---|
273 | return new Action[] {
|
---|
274 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
275 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
276 | SeparatorLayerAction.INSTANCE,
|
---|
277 | new CustomizeColor(this),
|
---|
278 | new CustomizeDrawing(this),
|
---|
279 | new ConvertToDataLayerAction(),
|
---|
280 | SeparatorLayerAction.INSTANCE,
|
---|
281 | new ChooseTrackVisibilityAction(),
|
---|
282 | new RenameLayerAction(getAssociatedFile(), this),
|
---|
283 | SeparatorLayerAction.INSTANCE,
|
---|
284 | new LayerListPopup.InfoAction(this) };
|
---|
285 | return new Action[] {
|
---|
286 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
287 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
288 | SeparatorLayerAction.INSTANCE,
|
---|
289 | new LayerSaveAction(this),
|
---|
290 | new LayerSaveAsAction(this),
|
---|
291 | new CustomizeColor(this),
|
---|
292 | new CustomizeDrawing(this),
|
---|
293 | new ImportImages(),
|
---|
294 | new ImportAudio(),
|
---|
295 | new MarkersFromNamedPoins(),
|
---|
296 | new ConvertToDataLayerAction(),
|
---|
297 | new DownloadAlongTrackAction(),
|
---|
298 | SeparatorLayerAction.INSTANCE,
|
---|
299 | new ChooseTrackVisibilityAction(),
|
---|
300 | new RenameLayerAction(getAssociatedFile(), this),
|
---|
301 | SeparatorLayerAction.INSTANCE,
|
---|
302 | new LayerListPopup.InfoAction(this) };
|
---|
303 | }
|
---|
304 |
|
---|
305 | @Override
|
---|
306 | public String getToolTipText() {
|
---|
307 | StringBuilder info = new StringBuilder().append("<html>");
|
---|
308 |
|
---|
309 | if (data.attr.containsKey("name")) {
|
---|
310 | info.append(tr("Name: {0}", data.attr.get(GpxData.META_NAME))).append("<br>");
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (data.attr.containsKey("desc")) {
|
---|
314 | info.append(tr("Description: {0}", data.attr.get(GpxData.META_DESC))).append("<br>");
|
---|
315 | }
|
---|
316 |
|
---|
317 | info.append(trn("{0} track, ", "{0} tracks, ", data.tracks.size(), data.tracks.size()));
|
---|
318 | info.append(trn("{0} route, ", "{0} routes, ", data.routes.size(), data.routes.size()));
|
---|
319 | info.append(trn("{0} waypoint", "{0} waypoints", data.waypoints.size(), data.waypoints.size())).append("<br>");
|
---|
320 |
|
---|
321 | info.append(tr("Length: {0}", NavigatableComponent.getSystemOfMeasurement().getDistText(data.length())));
|
---|
322 | info.append("<br>");
|
---|
323 |
|
---|
324 | return info.append("</html>").toString();
|
---|
325 | }
|
---|
326 |
|
---|
327 | @Override
|
---|
328 | public boolean isMergable(Layer other) {
|
---|
329 | return other instanceof GpxLayer;
|
---|
330 | }
|
---|
331 |
|
---|
332 | private int sumUpdateCount() {
|
---|
333 | int updateCount = 0;
|
---|
334 | for (GpxTrack track: data.tracks) {
|
---|
335 | updateCount += track.getUpdateCount();
|
---|
336 | }
|
---|
337 | return updateCount;
|
---|
338 | }
|
---|
339 |
|
---|
340 | @Override
|
---|
341 | public boolean isChanged() {
|
---|
342 | if (data.tracks.equals(lastTracks))
|
---|
343 | return sumUpdateCount() != lastUpdateCount;
|
---|
344 | else
|
---|
345 | return true;
|
---|
346 | }
|
---|
347 |
|
---|
348 | @Override
|
---|
349 | public void mergeFrom(Layer from) {
|
---|
350 | data.mergeFrom(((GpxLayer) from).data);
|
---|
351 | computeCacheInSync = false;
|
---|
352 | }
|
---|
353 |
|
---|
354 | private final static Color[] colors = new Color[256];
|
---|
355 | static {
|
---|
356 | for (int i = 0; i < colors.length; i++) {
|
---|
357 | colors[i] = Color.getHSBColor(i / 300.0f, 1, 1);
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | private final static Color[] colors_cyclic = new Color[256];
|
---|
362 | static {
|
---|
363 | for (int i = 0; i < colors_cyclic.length; i++) {
|
---|
364 | // red yellow green blue red
|
---|
365 | int[] h = new int[] { 0, 59, 127, 244, 360};
|
---|
366 | int[] s = new int[] { 100, 84, 99, 100 };
|
---|
367 | int[] b = new int[] { 90, 93, 74, 83 };
|
---|
368 |
|
---|
369 | float angle = 4 - i / 256f * 4;
|
---|
370 | int quadrant = (int) angle;
|
---|
371 | angle -= quadrant;
|
---|
372 | quadrant = Utils.mod(quadrant+1, 4);
|
---|
373 |
|
---|
374 | float vh = h[quadrant] * w(angle) + h[quadrant+1] * (1 - w(angle));
|
---|
375 | float vs = s[quadrant] * w(angle) + s[Utils.mod(quadrant+1, 4)] * (1 - w(angle));
|
---|
376 | float vb = b[quadrant] * w(angle) + b[Utils.mod(quadrant+1, 4)] * (1 - w(angle));
|
---|
377 |
|
---|
378 | colors_cyclic[i] = Color.getHSBColor(vh/360f, vs/100f, vb/100f);
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * transition function:
|
---|
384 | * w(0)=1, w(1)=0, 0<=w(x)<=1
|
---|
385 | * @param x number: 0<=x<=1
|
---|
386 | * @return the weighted value
|
---|
387 | */
|
---|
388 | private static float w(float x) {
|
---|
389 | if (x < 0.5)
|
---|
390 | return 1 - 2*x*x;
|
---|
391 | else
|
---|
392 | return 2*(1-x)*(1-x);
|
---|
393 | }
|
---|
394 |
|
---|
395 | // lookup array to draw arrows without doing any math
|
---|
396 | private final static int ll0 = 9;
|
---|
397 | private final static int sl4 = 5;
|
---|
398 | private final static int sl9 = 3;
|
---|
399 | private final static int[][] dir = { { +sl4, +ll0, +ll0, +sl4 }, { -sl9, +ll0, +sl9, +ll0 }, { -ll0, +sl4, -sl4, +ll0 },
|
---|
400 | { -ll0, -sl9, -ll0, +sl9 }, { -sl4, -ll0, -ll0, -sl4 }, { +sl9, -ll0, -sl9, -ll0 },
|
---|
401 | { +ll0, -sl4, +sl4, -ll0 }, { +ll0, +sl9, +ll0, -sl9 }, { +sl4, +ll0, +ll0, +sl4 },
|
---|
402 | { -sl9, +ll0, +sl9, +ll0 }, { -ll0, +sl4, -sl4, +ll0 }, { -ll0, -sl9, -ll0, +sl9 } };
|
---|
403 |
|
---|
404 | // the different color modes
|
---|
405 | enum colorModes {
|
---|
406 | none, velocity, dilution, direction, time
|
---|
407 | }
|
---|
408 |
|
---|
409 | @Override
|
---|
410 | public void paint(Graphics2D g, MapView mv, Bounds box) {
|
---|
411 | lastUpdateCount = sumUpdateCount();
|
---|
412 | lastTracks.clear();
|
---|
413 | lastTracks.addAll(data.tracks);
|
---|
414 |
|
---|
415 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
---|
416 | Main.pref.getBoolean("mappaint.gpx.use-antialiasing", false) ?
|
---|
417 | RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
|
---|
418 |
|
---|
419 | /****************************************************************
|
---|
420 | ********** STEP 1 - GET CONFIG VALUES **************************
|
---|
421 | ****************************************************************/
|
---|
422 | // Long startTime = System.currentTimeMillis();
|
---|
423 | Color neutralColor = getColor(true);
|
---|
424 | String spec="layer "+getName();
|
---|
425 |
|
---|
426 | // also draw lines between points belonging to different segments
|
---|
427 | boolean forceLines = Main.pref.getBoolean("draw.rawgps.lines.force", spec, false);
|
---|
428 | // draw direction arrows on the lines
|
---|
429 | boolean direction = Main.pref.getBoolean("draw.rawgps.direction", spec, false);
|
---|
430 | // don't draw lines if longer than x meters
|
---|
431 | int lineWidth = Main.pref.getInteger("draw.rawgps.linewidth", spec, 0);
|
---|
432 |
|
---|
433 | int maxLineLength;
|
---|
434 | boolean lines;
|
---|
435 | if (this.isLocalFile) {
|
---|
436 | maxLineLength = Main.pref.getInteger("draw.rawgps.max-line-length.local", spec, -1);
|
---|
437 | lines = Main.pref.getBoolean("draw.rawgps.lines.local", spec, true);
|
---|
438 | } else {
|
---|
439 | maxLineLength = Main.pref.getInteger("draw.rawgps.max-line-length", spec, 200);
|
---|
440 | lines = Main.pref.getBoolean("draw.rawgps.lines", spec, true);
|
---|
441 | }
|
---|
442 | // paint large dots for points
|
---|
443 | boolean large = Main.pref.getBoolean("draw.rawgps.large", spec, false);
|
---|
444 | int largesize = Main.pref.getInteger("draw.rawgps.large.size", spec, 3);
|
---|
445 | boolean hdopcircle = Main.pref.getBoolean("draw.rawgps.hdopcircle", spec, false);
|
---|
446 | // color the lines
|
---|
447 | colorModes colored = getColorMode();
|
---|
448 | // paint direction arrow with alternate math. may be faster
|
---|
449 | boolean alternatedirection = Main.pref.getBoolean("draw.rawgps.alternatedirection", spec, false);
|
---|
450 | // don't draw arrows nearer to each other than this
|
---|
451 | int delta = Main.pref.getInteger("draw.rawgps.min-arrow-distance", spec, 40);
|
---|
452 | // allows to tweak line coloring for different speed levels.
|
---|
453 | int colorTracksTune = Main.pref.getInteger("draw.rawgps.colorTracksTune", spec, 45);
|
---|
454 | boolean colorModeDynamic = Main.pref.getBoolean("draw.rawgps.colors.dynamic", spec, false);
|
---|
455 | int hdopfactor = Main.pref.getInteger("hdop.factor", 25);
|
---|
456 |
|
---|
457 | if(lineWidth != 0)
|
---|
458 | {
|
---|
459 | g.setStroke(new BasicStroke(lineWidth,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
|
---|
460 | }
|
---|
461 |
|
---|
462 | /****************************************************************
|
---|
463 | ********** STEP 2a - CHECK CACHE VALIDITY **********************
|
---|
464 | ****************************************************************/
|
---|
465 | if ((computeCacheMaxLineLengthUsed != maxLineLength) || (!neutralColor.equals(computeCacheColorUsed))
|
---|
466 | || (computeCacheColored != colored) || (computeCacheColorTracksTune != colorTracksTune)
|
---|
467 | || (computeCacheColorDynamic != colorModeDynamic)) {
|
---|
468 | computeCacheMaxLineLengthUsed = maxLineLength;
|
---|
469 | computeCacheInSync = false;
|
---|
470 | computeCacheColorUsed = neutralColor;
|
---|
471 | computeCacheColored = colored;
|
---|
472 | computeCacheColorTracksTune = colorTracksTune;
|
---|
473 | computeCacheColorDynamic = colorModeDynamic;
|
---|
474 | }
|
---|
475 |
|
---|
476 | /****************************************************************
|
---|
477 | ********** STEP 2b - RE-COMPUTE CACHE DATA *********************
|
---|
478 | ****************************************************************/
|
---|
479 | if (!computeCacheInSync) { // don't compute if the cache is good
|
---|
480 | double minval = +1e10;
|
---|
481 | double maxval = -1e10;
|
---|
482 | WayPoint oldWp = null;
|
---|
483 | if (colorModeDynamic) {
|
---|
484 | if (colored == colorModes.velocity) {
|
---|
485 | for (GpxTrack trk : data.tracks) {
|
---|
486 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
487 | if(!forceLines) {
|
---|
488 | oldWp = null;
|
---|
489 | }
|
---|
490 | for (WayPoint trkPnt : segment.getWayPoints()) {
|
---|
491 | LatLon c = trkPnt.getCoor();
|
---|
492 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
493 | continue;
|
---|
494 | }
|
---|
495 | if (oldWp != null && trkPnt.time > oldWp.time) {
|
---|
496 | double vel = c.greatCircleDistance(oldWp.getCoor())
|
---|
497 | / (trkPnt.time - oldWp.time);
|
---|
498 | if(vel > maxval) {
|
---|
499 | maxval = vel;
|
---|
500 | }
|
---|
501 | if(vel < minval) {
|
---|
502 | minval = vel;
|
---|
503 | }
|
---|
504 | }
|
---|
505 | oldWp = trkPnt;
|
---|
506 | }
|
---|
507 | }
|
---|
508 | }
|
---|
509 | } else if (colored == colorModes.dilution) {
|
---|
510 | for (GpxTrack trk : data.tracks) {
|
---|
511 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
512 | for (WayPoint trkPnt : segment.getWayPoints()) {
|
---|
513 | Object val = trkPnt.attr.get("hdop");
|
---|
514 | if (val != null) {
|
---|
515 | double hdop = ((Float) val).doubleValue();
|
---|
516 | if(hdop > maxval) {
|
---|
517 | maxval = hdop;
|
---|
518 | }
|
---|
519 | if(hdop < minval) {
|
---|
520 | minval = hdop;
|
---|
521 | }
|
---|
522 | }
|
---|
523 | }
|
---|
524 | }
|
---|
525 | }
|
---|
526 | }
|
---|
527 | oldWp = null;
|
---|
528 | }
|
---|
529 | if (colored == colorModes.time) {
|
---|
530 | for (GpxTrack trk : data.tracks) {
|
---|
531 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
532 | for (WayPoint trkPnt : segment.getWayPoints()) {
|
---|
533 | double t=trkPnt.time;
|
---|
534 | if (t==0) {
|
---|
535 | continue; // skip non-dated trackpoints
|
---|
536 | }
|
---|
537 | if(t > maxval) {
|
---|
538 | maxval = t;
|
---|
539 | }
|
---|
540 | if(t < minval) {
|
---|
541 | minval = t;
|
---|
542 | }
|
---|
543 | }
|
---|
544 | }
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | for (GpxTrack trk : data.tracks) {
|
---|
549 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
550 | if (!forceLines) { // don't draw lines between segments, unless forced to
|
---|
551 | oldWp = null;
|
---|
552 | }
|
---|
553 | for (WayPoint trkPnt : segment.getWayPoints()) {
|
---|
554 | LatLon c = trkPnt.getCoor();
|
---|
555 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
556 | continue;
|
---|
557 | }
|
---|
558 | trkPnt.customColoring = neutralColor;
|
---|
559 | if(colored == colorModes.dilution && trkPnt.attr.get("hdop") != null) {
|
---|
560 | float hdop = ((Float) trkPnt.attr.get("hdop")).floatValue();
|
---|
561 | int hdoplvl =(int) Math.round(colorModeDynamic ? ((hdop-minval)*255/(maxval-minval))
|
---|
562 | : (hdop <= 0 ? 0 : hdop * hdopfactor));
|
---|
563 | // High hdop is bad, but high values in colors are green.
|
---|
564 | // Therefore inverse the logic
|
---|
565 | int hdopcolor = 255 - (hdoplvl > 255 ? 255 : hdoplvl);
|
---|
566 | trkPnt.customColoring = colors[hdopcolor];
|
---|
567 | }
|
---|
568 | if (oldWp != null) {
|
---|
569 | double dist = c.greatCircleDistance(oldWp.getCoor());
|
---|
570 | boolean noDraw=false;
|
---|
571 | switch (colored) {
|
---|
572 | case velocity:
|
---|
573 | double dtime = trkPnt.time - oldWp.time;
|
---|
574 | if(dtime > 0) {
|
---|
575 | float vel = (float) (dist / dtime);
|
---|
576 | int velColor =(int) Math.round(colorModeDynamic ? ((vel-minval)*255/(maxval-minval))
|
---|
577 | : (vel <= 0 ? 0 : vel / colorTracksTune * 255));
|
---|
578 | trkPnt.customColoring = colors[Math.max(0, Math.min(velColor, 255))];
|
---|
579 | } else {
|
---|
580 | trkPnt.customColoring = colors[255];
|
---|
581 | }
|
---|
582 | break;
|
---|
583 | case direction:
|
---|
584 | double dirColor = oldWp.getCoor().heading(trkPnt.getCoor()) / (2.0 * Math.PI) * 256;
|
---|
585 | // Bad case first
|
---|
586 | if (dirColor != dirColor || dirColor < 0.0 || dirColor >= 256.0) {
|
---|
587 | trkPnt.customColoring = colors_cyclic[0];
|
---|
588 | } else {
|
---|
589 | trkPnt.customColoring = colors_cyclic[(int) (dirColor)];
|
---|
590 | }
|
---|
591 | break;
|
---|
592 | case time:
|
---|
593 | if (trkPnt.time>0){
|
---|
594 | int tColor = (int) Math.round((trkPnt.time-minval)*255/(maxval-minval));
|
---|
595 | trkPnt.customColoring = colors[tColor];
|
---|
596 | } else {
|
---|
597 | trkPnt.customColoring = neutralColor;
|
---|
598 | }
|
---|
599 | break;
|
---|
600 | }
|
---|
601 |
|
---|
602 | if (!noDraw && (maxLineLength == -1 || dist <= maxLineLength)) {
|
---|
603 | trkPnt.drawLine = true;
|
---|
604 | trkPnt.dir = (int) oldWp.getCoor().heading(trkPnt.getCoor());
|
---|
605 | } else {
|
---|
606 | trkPnt.drawLine = false;
|
---|
607 | }
|
---|
608 | } else { // make sure we reset outdated data
|
---|
609 | trkPnt.drawLine = false;
|
---|
610 | }
|
---|
611 | oldWp = trkPnt;
|
---|
612 | }
|
---|
613 | }
|
---|
614 | }
|
---|
615 | computeCacheInSync = true;
|
---|
616 | }
|
---|
617 |
|
---|
618 | LinkedList<WayPoint> visibleSegments = new LinkedList<WayPoint>();
|
---|
619 | WayPoint last = null;
|
---|
620 | int i = 0;
|
---|
621 | ensureTrackVisibilityLength();
|
---|
622 | for (GpxTrack trk: data.tracks) {
|
---|
623 | // hide tracks that were de-selected in ChooseTrackVisibilityAction
|
---|
624 | if(!trackVisibility[i++]) {
|
---|
625 | continue;
|
---|
626 | }
|
---|
627 |
|
---|
628 | for (GpxTrackSegment trkSeg: trk.getSegments()) {
|
---|
629 | for(WayPoint pt : trkSeg.getWayPoints())
|
---|
630 | {
|
---|
631 | Bounds b = new Bounds(pt.getCoor());
|
---|
632 | // last should never be null when this is true!
|
---|
633 | if(pt.drawLine) {
|
---|
634 | b.extend(last.getCoor());
|
---|
635 | }
|
---|
636 | if(b.intersects(box))
|
---|
637 | {
|
---|
638 | if(last != null && (visibleSegments.isEmpty()
|
---|
639 | || visibleSegments.getLast() != last)) {
|
---|
640 | if(last.drawLine) {
|
---|
641 | WayPoint l = new WayPoint(last);
|
---|
642 | l.drawLine = false;
|
---|
643 | visibleSegments.add(l);
|
---|
644 | } else {
|
---|
645 | visibleSegments.add(last);
|
---|
646 | }
|
---|
647 | }
|
---|
648 | visibleSegments.add(pt);
|
---|
649 | }
|
---|
650 | last = pt;
|
---|
651 | }
|
---|
652 | }
|
---|
653 | }
|
---|
654 | if(visibleSegments.isEmpty())
|
---|
655 | return;
|
---|
656 |
|
---|
657 | /****************************************************************
|
---|
658 | ********** STEP 3a - DRAW LINES ********************************
|
---|
659 | ****************************************************************/
|
---|
660 | if (lines) {
|
---|
661 | Point old = null;
|
---|
662 | for (WayPoint trkPnt : visibleSegments) {
|
---|
663 | LatLon c = trkPnt.getCoor();
|
---|
664 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
665 | continue;
|
---|
666 | }
|
---|
667 | Point screen = mv.getPoint(trkPnt.getEastNorth());
|
---|
668 | if (trkPnt.drawLine) {
|
---|
669 | // skip points that are on the same screenposition
|
---|
670 | if (old != null && ((old.x != screen.x) || (old.y != screen.y))) {
|
---|
671 | g.setColor(trkPnt.customColoring);
|
---|
672 | g.drawLine(old.x, old.y, screen.x, screen.y);
|
---|
673 | }
|
---|
674 | }
|
---|
675 | old = screen;
|
---|
676 | } // end for trkpnt
|
---|
677 | } // end if lines
|
---|
678 |
|
---|
679 | /****************************************************************
|
---|
680 | ********** STEP 3b - DRAW NICE ARROWS **************************
|
---|
681 | ****************************************************************/
|
---|
682 | if (lines && direction && !alternatedirection) {
|
---|
683 | Point old = null;
|
---|
684 | Point oldA = null; // last arrow painted
|
---|
685 | for (WayPoint trkPnt : visibleSegments) {
|
---|
686 | LatLon c = trkPnt.getCoor();
|
---|
687 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
688 | continue;
|
---|
689 | }
|
---|
690 | if (trkPnt.drawLine) {
|
---|
691 | Point screen = mv.getPoint(trkPnt.getEastNorth());
|
---|
692 | // skip points that are on the same screenposition
|
---|
693 | if (old != null
|
---|
694 | && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
|
---|
695 | || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
|
---|
696 | g.setColor(trkPnt.customColoring);
|
---|
697 | double t = Math.atan2(screen.y - old.y, screen.x - old.x) + Math.PI;
|
---|
698 | g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t - PHI)),
|
---|
699 | (int) (screen.y + 10 * Math.sin(t - PHI)));
|
---|
700 | g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t + PHI)),
|
---|
701 | (int) (screen.y + 10 * Math.sin(t + PHI)));
|
---|
702 | oldA = screen;
|
---|
703 | }
|
---|
704 | old = screen;
|
---|
705 | }
|
---|
706 | } // end for trkpnt
|
---|
707 | } // end if lines
|
---|
708 |
|
---|
709 | /****************************************************************
|
---|
710 | ********** STEP 3c - DRAW FAST ARROWS **************************
|
---|
711 | ****************************************************************/
|
---|
712 | if (lines && direction && alternatedirection) {
|
---|
713 | Point old = null;
|
---|
714 | Point oldA = null; // last arrow painted
|
---|
715 | for (WayPoint trkPnt : visibleSegments) {
|
---|
716 | LatLon c = trkPnt.getCoor();
|
---|
717 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
718 | continue;
|
---|
719 | }
|
---|
720 | if (trkPnt.drawLine) {
|
---|
721 | Point screen = mv.getPoint(trkPnt.getEastNorth());
|
---|
722 | // skip points that are on the same screenposition
|
---|
723 | if (old != null
|
---|
724 | && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
|
---|
725 | || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
|
---|
726 | g.setColor(trkPnt.customColoring);
|
---|
727 | g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][0], screen.y
|
---|
728 | + dir[trkPnt.dir][1]);
|
---|
729 | g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][2], screen.y
|
---|
730 | + dir[trkPnt.dir][3]);
|
---|
731 | oldA = screen;
|
---|
732 | }
|
---|
733 | old = screen;
|
---|
734 | }
|
---|
735 | } // end for trkpnt
|
---|
736 | } // end if lines
|
---|
737 |
|
---|
738 | /****************************************************************
|
---|
739 | ********** STEP 3d - DRAW LARGE POINTS AND HDOP CIRCLE *********
|
---|
740 | ****************************************************************/
|
---|
741 | if (large || hdopcircle) {
|
---|
742 | g.setColor(neutralColor);
|
---|
743 | for (WayPoint trkPnt : visibleSegments) {
|
---|
744 | LatLon c = trkPnt.getCoor();
|
---|
745 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
746 | continue;
|
---|
747 | }
|
---|
748 | Point screen = mv.getPoint(trkPnt.getEastNorth());
|
---|
749 | g.setColor(trkPnt.customColoring);
|
---|
750 | if (hdopcircle && trkPnt.attr.get("hdop") != null) {
|
---|
751 | // hdop value
|
---|
752 | float hdop = ((Float)trkPnt.attr.get("hdop")).floatValue();
|
---|
753 | if (hdop < 0) {
|
---|
754 | hdop = 0;
|
---|
755 | }
|
---|
756 | // hdop pixels
|
---|
757 | int hdopp = mv.getPoint(new LatLon(trkPnt.getCoor().lat(), trkPnt.getCoor().lon() + 2*6*hdop*360/40000000)).x - screen.x;
|
---|
758 | g.drawArc(screen.x-hdopp/2, screen.y-hdopp/2, hdopp, hdopp, 0, 360);
|
---|
759 | }
|
---|
760 | if (large) {
|
---|
761 | g.fillRect(screen.x-1, screen.y-1, largesize, largesize);
|
---|
762 | }
|
---|
763 | } // end for trkpnt
|
---|
764 | } // end if large || hdopcircle
|
---|
765 |
|
---|
766 | /****************************************************************
|
---|
767 | ********** STEP 3e - DRAW SMALL POINTS FOR LINES ***************
|
---|
768 | ****************************************************************/
|
---|
769 | if (!large && lines) {
|
---|
770 | g.setColor(neutralColor);
|
---|
771 | for (WayPoint trkPnt : visibleSegments) {
|
---|
772 | LatLon c = trkPnt.getCoor();
|
---|
773 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
774 | continue;
|
---|
775 | }
|
---|
776 | if (!trkPnt.drawLine) {
|
---|
777 | Point screen = mv.getPoint(trkPnt.getEastNorth());
|
---|
778 | g.drawRect(screen.x, screen.y, 0, 0);
|
---|
779 | }
|
---|
780 | } // end for trkpnt
|
---|
781 | } // end if large
|
---|
782 |
|
---|
783 | /****************************************************************
|
---|
784 | ********** STEP 3f - DRAW SMALL POINTS INSTEAD OF LINES ********
|
---|
785 | ****************************************************************/
|
---|
786 | if (!large && !lines) {
|
---|
787 | g.setColor(neutralColor);
|
---|
788 | for (WayPoint trkPnt : visibleSegments) {
|
---|
789 | LatLon c = trkPnt.getCoor();
|
---|
790 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
791 | continue;
|
---|
792 | }
|
---|
793 | Point screen = mv.getPoint(trkPnt.getEastNorth());
|
---|
794 | g.setColor(trkPnt.customColoring);
|
---|
795 | g.drawRect(screen.x, screen.y, 0, 0);
|
---|
796 | } // end for trkpnt
|
---|
797 | } // end if large
|
---|
798 |
|
---|
799 | // Long duration = System.currentTimeMillis() - startTime;
|
---|
800 | // System.out.println(duration);
|
---|
801 | } // end paint
|
---|
802 |
|
---|
803 | @Override
|
---|
804 | public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
805 | v.visit(data.recalculateBounds());
|
---|
806 | }
|
---|
807 |
|
---|
808 | public class ConvertToDataLayerAction extends AbstractAction {
|
---|
809 | public ConvertToDataLayerAction() {
|
---|
810 | super(tr("Convert to data layer"), ImageProvider.get("converttoosm"));
|
---|
811 | }
|
---|
812 |
|
---|
813 | @Override
|
---|
814 | public void actionPerformed(ActionEvent e) {
|
---|
815 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
816 | msg
|
---|
817 | .add(
|
---|
818 | new JLabel(
|
---|
819 | tr("<html>Upload of unprocessed GPS data as map data is considered harmful.<br>If you want to upload traces, look here:</html>")),
|
---|
820 | GBC.eol());
|
---|
821 | msg.add(new UrlLabel(tr("http://www.openstreetmap.org/traces")), GBC.eop());
|
---|
822 | if (!ConditionalOptionPaneUtil.showConfirmationDialog("convert_to_data", Main.parent, msg, tr("Warning"),
|
---|
823 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_OPTION))
|
---|
824 | return;
|
---|
825 | DataSet ds = new DataSet();
|
---|
826 | for (GpxTrack trk : data.tracks) {
|
---|
827 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
828 | List<Node> nodes = new ArrayList<Node>();
|
---|
829 | for (WayPoint p : segment.getWayPoints()) {
|
---|
830 | Node n = new Node(p.getCoor());
|
---|
831 | String timestr = p.getString("time");
|
---|
832 | if (timestr != null) {
|
---|
833 | n.setTimestamp(DateUtils.fromString(timestr));
|
---|
834 | }
|
---|
835 | ds.addPrimitive(n);
|
---|
836 | nodes.add(n);
|
---|
837 | }
|
---|
838 | Way w = new Way();
|
---|
839 | w.setNodes(nodes);
|
---|
840 | ds.addPrimitive(w);
|
---|
841 | }
|
---|
842 | }
|
---|
843 | Main.main
|
---|
844 | .addLayer(new OsmDataLayer(ds, tr("Converted from: {0}", GpxLayer.this.getName()), getAssociatedFile()));
|
---|
845 | Main.main.removeLayer(GpxLayer.this);
|
---|
846 | }
|
---|
847 | }
|
---|
848 |
|
---|
849 | @Override
|
---|
850 | public File getAssociatedFile() {
|
---|
851 | return data.storageFile;
|
---|
852 | }
|
---|
853 |
|
---|
854 | @Override
|
---|
855 | public void setAssociatedFile(File file) {
|
---|
856 | data.storageFile = file;
|
---|
857 | }
|
---|
858 |
|
---|
859 | /** ensures the trackVisibility array has the correct length without losing data.
|
---|
860 | * additional entries are initialized to true;
|
---|
861 | */
|
---|
862 | final private void ensureTrackVisibilityLength() {
|
---|
863 | final int l = data.tracks.size();
|
---|
864 | if(l == trackVisibility.length)
|
---|
865 | return;
|
---|
866 | final boolean[] back = trackVisibility.clone();
|
---|
867 | final int m = Math.min(l, back.length);
|
---|
868 | trackVisibility = new boolean[l];
|
---|
869 | for(int i=0; i < m; i++) {
|
---|
870 | trackVisibility[i] = back[i];
|
---|
871 | }
|
---|
872 | for(int i=m; i < l; i++) {
|
---|
873 | trackVisibility[i] = true;
|
---|
874 | }
|
---|
875 | }
|
---|
876 |
|
---|
877 | /**
|
---|
878 | * allows the user to choose which of the downloaded tracks should be displayed.
|
---|
879 | * they can be chosen from the gpx layer context menu.
|
---|
880 | */
|
---|
881 | public class ChooseTrackVisibilityAction extends AbstractAction {
|
---|
882 | public ChooseTrackVisibilityAction() {
|
---|
883 | super(tr("Choose visible tracks"), ImageProvider.get("dialogs/filter"));
|
---|
884 | }
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * gathers all available data for the tracks and returns them as array of arrays
|
---|
888 | * in the expected column order */
|
---|
889 | private Object[][] buildTableContents() {
|
---|
890 | Object[][] tracks = new Object[data.tracks.size()][5];
|
---|
891 | int i = 0;
|
---|
892 | for (GpxTrack trk : data.tracks) {
|
---|
893 | Map<String, Object> attr = trk.getAttributes();
|
---|
894 | String name = (String) (attr.containsKey("name") ? attr.get("name") : "");
|
---|
895 | String desc = (String) (attr.containsKey("desc") ? attr.get("desc") : "");
|
---|
896 | String time = getTimespanForTrack(trk);
|
---|
897 | String length = NavigatableComponent.getSystemOfMeasurement().getDistText(trk.length());
|
---|
898 | String url = (String) (attr.containsKey("url") ? attr.get("url") : "");
|
---|
899 | tracks[i] = new String[] {name, desc, time, length, url};
|
---|
900 | i++;
|
---|
901 | }
|
---|
902 | return tracks;
|
---|
903 | }
|
---|
904 |
|
---|
905 | /**
|
---|
906 | * Builds an non-editable table whose 5th column will open a browser when double clicked.
|
---|
907 | * The table will fill its parent. */
|
---|
908 | private JTable buildTable(String[] headers, Object[][] content) {
|
---|
909 | final JTable t = new JTable(content, headers) {
|
---|
910 | @Override
|
---|
911 | public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
|
---|
912 | Component c = super.prepareRenderer(renderer, row, col);
|
---|
913 | if (c instanceof JComponent) {
|
---|
914 | JComponent jc = (JComponent)c;
|
---|
915 | jc.setToolTipText((String)getValueAt(row, col));
|
---|
916 | }
|
---|
917 | return c;
|
---|
918 | }
|
---|
919 |
|
---|
920 | @Override
|
---|
921 | public boolean isCellEditable(int rowIndex, int colIndex) {
|
---|
922 | return false;
|
---|
923 | }
|
---|
924 | };
|
---|
925 | // default column widths
|
---|
926 | t.getColumnModel().getColumn(0).setPreferredWidth(220);
|
---|
927 | t.getColumnModel().getColumn(1).setPreferredWidth(300);
|
---|
928 | t.getColumnModel().getColumn(2).setPreferredWidth(200);
|
---|
929 | t.getColumnModel().getColumn(3).setPreferredWidth(50);
|
---|
930 | t.getColumnModel().getColumn(4).setPreferredWidth(100);
|
---|
931 | // make the link clickable
|
---|
932 | final MouseListener urlOpener = new MouseAdapter() {
|
---|
933 | @Override
|
---|
934 | public void mouseClicked(MouseEvent e) {
|
---|
935 | if (e.getClickCount() != 2)
|
---|
936 | return;
|
---|
937 | JTable t = (JTable)e.getSource();
|
---|
938 | int col = t.convertColumnIndexToModel(t.columnAtPoint(e.getPoint()));
|
---|
939 | if(col != 4) // only accept clicks on the URL column
|
---|
940 | return;
|
---|
941 | int row = t.rowAtPoint(e.getPoint());
|
---|
942 | String url = (String) t.getValueAt(row, col);
|
---|
943 | if(url == "")
|
---|
944 | return;
|
---|
945 | OpenBrowser.displayUrl(url);
|
---|
946 | }
|
---|
947 | };
|
---|
948 | t.addMouseListener(urlOpener);
|
---|
949 | t.setFillsViewportHeight(true);
|
---|
950 | return t;
|
---|
951 | }
|
---|
952 |
|
---|
953 | /** selects all rows (=tracks) in the table that are currently visible */
|
---|
954 | private void selectVisibleTracksInTable(JTable table) {
|
---|
955 | // don't select any tracks if the layer is not visible
|
---|
956 | if(!isVisible())
|
---|
957 | return;
|
---|
958 | ListSelectionModel s = table.getSelectionModel();
|
---|
959 | s.clearSelection();
|
---|
960 | for(int i=0; i < trackVisibility.length; i++)
|
---|
961 | if(trackVisibility[i]) {
|
---|
962 | s.addSelectionInterval(i, i);
|
---|
963 | }
|
---|
964 | }
|
---|
965 |
|
---|
966 | /** listens to selection changes in the table and redraws the map */
|
---|
967 | private void listenToSelectionChanges(JTable table) {
|
---|
968 | table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
|
---|
969 | public void valueChanged(ListSelectionEvent e) {
|
---|
970 | if(!(e.getSource() instanceof ListSelectionModel))
|
---|
971 | return;
|
---|
972 |
|
---|
973 | ListSelectionModel s = (ListSelectionModel) e.getSource();
|
---|
974 | for(int i = 0; i < data.tracks.size(); i++) {
|
---|
975 | trackVisibility[i] = s.isSelectedIndex(i);
|
---|
976 | }
|
---|
977 | Main.map.mapView.preferenceChanged(null);
|
---|
978 | Main.map.repaint(100);
|
---|
979 | }
|
---|
980 | });
|
---|
981 | }
|
---|
982 |
|
---|
983 | @Override
|
---|
984 | public void actionPerformed(ActionEvent arg0) {
|
---|
985 | final JPanel msg = new JPanel(new GridBagLayout());
|
---|
986 | msg.add(new JLabel(tr("<html>Select all tracks that you want to be displayed. You can drag select a "
|
---|
987 | + "range of tracks or use CTRL+Click to select specific ones. The map is updated live in the "
|
---|
988 | + "background. Open the URLs by double clicking them.</html>")),
|
---|
989 | GBC.eol().fill(GBC.HORIZONTAL));
|
---|
990 |
|
---|
991 | // build table
|
---|
992 | final boolean[] trackVisibilityBackup = trackVisibility.clone();
|
---|
993 | final String[] headers = {tr("Name"), tr("Description"), tr("Timespan"), tr("Length"), tr("URL")};
|
---|
994 | final JTable table = buildTable(headers, buildTableContents());
|
---|
995 | selectVisibleTracksInTable(table);
|
---|
996 | listenToSelectionChanges(table);
|
---|
997 |
|
---|
998 | // make the table scrollable
|
---|
999 | JScrollPane scrollPane = new JScrollPane(table);
|
---|
1000 | msg.add(scrollPane, GBC.eol().fill(GBC.BOTH));
|
---|
1001 |
|
---|
1002 | // build dialog
|
---|
1003 | ExtendedDialog ed = new ExtendedDialog(
|
---|
1004 | Main.parent, tr("Set track visibility for {0}", getName()),
|
---|
1005 | new String[] {tr("Show all"), tr("Show selected only"), tr("Cancel")});
|
---|
1006 | ed.setButtonIcons(new String[] {"dialogs/layerlist/eye", "dialogs/filter", "cancel"});
|
---|
1007 | ed.setContent(msg, false);
|
---|
1008 | ed.setDefaultButton(2);
|
---|
1009 | ed.setCancelButton(3);
|
---|
1010 | ed.configureContextsensitiveHelp("/Action/ChooseTrackVisibility", true);
|
---|
1011 | ed.setRememberWindowGeometry(
|
---|
1012 | getClass().getName() + ".geometry",
|
---|
1013 | WindowGeometry.centerInWindow(Main.parent, new Dimension(1000, 500))
|
---|
1014 | );
|
---|
1015 | ed.showDialog();
|
---|
1016 | int v = ed.getValue();
|
---|
1017 | // cancel for unknown buttons and copy back original settings
|
---|
1018 | if(v != 1 && v != 2) {
|
---|
1019 | for(int i = 0; i < data.tracks.size(); i++) {
|
---|
1020 | trackVisibility[i] = trackVisibilityBackup[i];
|
---|
1021 | }
|
---|
1022 | Main.map.repaint();
|
---|
1023 | return;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | // set visibility (1 = show all, 2 = filter). If no tracks are selected
|
---|
1027 | // set all of them visible and...
|
---|
1028 | ListSelectionModel s = table.getSelectionModel();
|
---|
1029 | final boolean all = v == 1 || s.isSelectionEmpty();
|
---|
1030 | for(int i = 0; i < data.tracks.size(); i++) {
|
---|
1031 | trackVisibility[i] = all || s.isSelectedIndex(i);
|
---|
1032 | }
|
---|
1033 | // ...sync with layer visibility instead to avoid having two ways to hide everything
|
---|
1034 | setVisible(v == 1 || !s.isSelectionEmpty());
|
---|
1035 | Main.map.repaint();
|
---|
1036 | }
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | /**
|
---|
1040 | * Action that issues a series of download requests to the API, following the GPX track.
|
---|
1041 | *
|
---|
1042 | * @author fred
|
---|
1043 | */
|
---|
1044 | public class DownloadAlongTrackAction extends AbstractAction {
|
---|
1045 | final static int NEAR_TRACK=0;
|
---|
1046 | final static int NEAR_WAYPOINTS=1;
|
---|
1047 | final static int NEAR_BOTH=2;
|
---|
1048 | final Integer dist[] = { 5000, 500, 50 };
|
---|
1049 | final Integer area[] = { 20, 10, 5, 1 };
|
---|
1050 |
|
---|
1051 | public DownloadAlongTrackAction() {
|
---|
1052 | super(tr("Download from OSM along this track"), ImageProvider.get("downloadalongtrack"));
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | @Override
|
---|
1056 | public void actionPerformed(ActionEvent e) {
|
---|
1057 | /*
|
---|
1058 | * build selection dialog
|
---|
1059 | */
|
---|
1060 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
1061 |
|
---|
1062 | msg.add(new JLabel(tr("Download everything within:")), GBC.eol());
|
---|
1063 | String s[] = new String[dist.length];
|
---|
1064 | for (int i = 0; i < dist.length; ++i) {
|
---|
1065 | s[i] = tr("{0} meters", dist[i]);
|
---|
1066 | }
|
---|
1067 | JList buffer = new JList(s);
|
---|
1068 | buffer.setSelectedIndex(Main.pref.getInteger(PREF_DOWNLOAD_ALONG_TRACK_DISTANCE, 0));
|
---|
1069 | msg.add(buffer, GBC.eol());
|
---|
1070 |
|
---|
1071 | msg.add(new JLabel(tr("Maximum area per request:")), GBC.eol());
|
---|
1072 | s = new String[area.length];
|
---|
1073 | for (int i = 0; i < area.length; ++i) {
|
---|
1074 | s[i] = tr("{0} sq km", area[i]);
|
---|
1075 | }
|
---|
1076 | JList maxRect = new JList(s);
|
---|
1077 | maxRect.setSelectedIndex(Main.pref.getInteger(PREF_DOWNLOAD_ALONG_TRACK_AREA, 0));
|
---|
1078 | msg.add(maxRect, GBC.eol());
|
---|
1079 |
|
---|
1080 | msg.add(new JLabel(tr("Download near:")), GBC.eol());
|
---|
1081 | JList downloadNear = new JList(new String[] { tr("track only"), tr("waypoints only"), tr("track and waypoints") });
|
---|
1082 |
|
---|
1083 | downloadNear.setSelectedIndex(Main.pref.getInteger(PREF_DOWNLOAD_ALONG_TRACK_NEAR, 0));
|
---|
1084 | msg.add(downloadNear, GBC.eol());
|
---|
1085 |
|
---|
1086 | int ret = JOptionPane.showConfirmDialog(
|
---|
1087 | Main.parent,
|
---|
1088 | msg,
|
---|
1089 | tr("Download from OSM along this track"),
|
---|
1090 | JOptionPane.OK_CANCEL_OPTION,
|
---|
1091 | JOptionPane.QUESTION_MESSAGE
|
---|
1092 | );
|
---|
1093 | switch(ret) {
|
---|
1094 | case JOptionPane.CANCEL_OPTION:
|
---|
1095 | case JOptionPane.CLOSED_OPTION:
|
---|
1096 | return;
|
---|
1097 | default:
|
---|
1098 | // continue
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | Main.pref.putInteger(PREF_DOWNLOAD_ALONG_TRACK_DISTANCE, buffer.getSelectedIndex());
|
---|
1102 | Main.pref.putInteger(PREF_DOWNLOAD_ALONG_TRACK_AREA, maxRect.getSelectedIndex());
|
---|
1103 | final int near = downloadNear.getSelectedIndex();
|
---|
1104 | Main.pref.putInteger(PREF_DOWNLOAD_ALONG_TRACK_NEAR, near);
|
---|
1105 |
|
---|
1106 | /*
|
---|
1107 | * Find the average latitude for the data we're contemplating, so we can know how many
|
---|
1108 | * metres per degree of longitude we have.
|
---|
1109 | */
|
---|
1110 | double latsum = 0;
|
---|
1111 | int latcnt = 0;
|
---|
1112 |
|
---|
1113 | if (near == NEAR_TRACK || near == NEAR_BOTH) {
|
---|
1114 | for (GpxTrack trk : data.tracks) {
|
---|
1115 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
1116 | for (WayPoint p : segment.getWayPoints()) {
|
---|
1117 | latsum += p.getCoor().lat();
|
---|
1118 | latcnt++;
|
---|
1119 | }
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | if (near == NEAR_WAYPOINTS || near == NEAR_BOTH) {
|
---|
1125 | for (WayPoint p : data.waypoints) {
|
---|
1126 | latsum += p.getCoor().lat();
|
---|
1127 | latcnt++;
|
---|
1128 | }
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | double avglat = latsum / latcnt;
|
---|
1132 | double scale = Math.cos(Math.toRadians(avglat));
|
---|
1133 |
|
---|
1134 | /*
|
---|
1135 | * Compute buffer zone extents and maximum bounding box size. Note that the maximum we
|
---|
1136 | * ever offer is a bbox area of 0.002, while the API theoretically supports 0.25, but as
|
---|
1137 | * soon as you touch any built-up area, that kind of bounding box will download forever
|
---|
1138 | * and then stop because it has more than 50k nodes.
|
---|
1139 | */
|
---|
1140 | Integer i = buffer.getSelectedIndex();
|
---|
1141 | final int buffer_dist = dist[i < 0 ? 0 : i];
|
---|
1142 | i = maxRect.getSelectedIndex();
|
---|
1143 | final double max_area = area[i < 0 ? 0 : i] / 10000.0 / scale;
|
---|
1144 | final double buffer_y = buffer_dist / 100000.0;
|
---|
1145 | final double buffer_x = buffer_y / scale;
|
---|
1146 |
|
---|
1147 | final int totalTicks = latcnt;
|
---|
1148 | // guess if a progress bar might be useful.
|
---|
1149 | final boolean displayProgress = totalTicks > 2000 && buffer_y < 0.01;
|
---|
1150 |
|
---|
1151 | class CalculateDownloadArea extends PleaseWaitRunnable {
|
---|
1152 | private Area a = new Area();
|
---|
1153 | private boolean cancel = false;
|
---|
1154 | private int ticks = 0;
|
---|
1155 | private Rectangle2D r = new Rectangle2D.Double();
|
---|
1156 |
|
---|
1157 | public CalculateDownloadArea() {
|
---|
1158 | super(tr("Calculating Download Area"),
|
---|
1159 | (displayProgress ? null : NullProgressMonitor.INSTANCE),
|
---|
1160 | false);
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | @Override
|
---|
1164 | protected void cancel() {
|
---|
1165 | cancel = true;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | @Override
|
---|
1169 | protected void finish() {
|
---|
1170 | if(cancel)
|
---|
1171 | return;
|
---|
1172 | confirmAndDownloadAreas(a, max_area, progressMonitor);
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | /**
|
---|
1176 | * increase tick count by one, report progress every 100 ticks
|
---|
1177 | */
|
---|
1178 | private void tick() {
|
---|
1179 | ticks++;
|
---|
1180 | if(ticks % 100 == 0) {
|
---|
1181 | progressMonitor.worked(100);
|
---|
1182 | }
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | /**
|
---|
1186 | * calculate area for single, given way point and return new LatLon if the
|
---|
1187 | * way point has been used to modify the area.
|
---|
1188 | */
|
---|
1189 | private LatLon calcAreaForWayPoint(WayPoint p, LatLon previous) {
|
---|
1190 | tick();
|
---|
1191 | LatLon c = p.getCoor();
|
---|
1192 | if (previous == null || c.greatCircleDistance(previous) > buffer_dist) {
|
---|
1193 | // we add a buffer around the point.
|
---|
1194 | r.setRect(c.lon() - buffer_x, c.lat() - buffer_y, 2 * buffer_x, 2 * buffer_y);
|
---|
1195 | a.add(new Area(r));
|
---|
1196 | return c;
|
---|
1197 | }
|
---|
1198 | return previous;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | @Override
|
---|
1202 | protected void realRun() {
|
---|
1203 | progressMonitor.setTicksCount(totalTicks);
|
---|
1204 | /*
|
---|
1205 | * Collect the combined area of all gpx points plus buffer zones around them. We ignore
|
---|
1206 | * points that lie closer to the previous point than the given buffer size because
|
---|
1207 | * otherwise this operation takes ages.
|
---|
1208 | */
|
---|
1209 | LatLon previous = null;
|
---|
1210 | if (near == NEAR_TRACK || near == NEAR_BOTH) {
|
---|
1211 | for (GpxTrack trk : data.tracks) {
|
---|
1212 | for (GpxTrackSegment segment : trk.getSegments()) {
|
---|
1213 | for (WayPoint p : segment.getWayPoints()) {
|
---|
1214 | if(cancel)
|
---|
1215 | return;
|
---|
1216 | previous = calcAreaForWayPoint(p, previous);
|
---|
1217 | }
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 | if (near == NEAR_WAYPOINTS || near == NEAR_BOTH) {
|
---|
1222 | for (WayPoint p : data.waypoints) {
|
---|
1223 | if(cancel)
|
---|
1224 | return;
|
---|
1225 | previous = calcAreaForWayPoint(p, previous);
|
---|
1226 | }
|
---|
1227 | }
|
---|
1228 | }
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | Main.worker.submit(new CalculateDownloadArea());
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /**
|
---|
1235 | * Area "a" contains the hull that we would like to download data for. however we
|
---|
1236 | * can only download rectangles, so the following is an attempt at finding a number of
|
---|
1237 | * rectangles to download.
|
---|
1238 | *
|
---|
1239 | * The idea is simply: Start out with the full bounding box. If it is too large, then
|
---|
1240 | * split it in half and repeat recursively for each half until you arrive at something
|
---|
1241 | * small enough to download. The algorithm is improved by always using the intersection
|
---|
1242 | * between the rectangle and the actual desired area. For example, if you have a track
|
---|
1243 | * that goes like this: +----+ | /| | / | | / | |/ | +----+ then we would first look at
|
---|
1244 | * downloading the whole rectangle (assume it's too big), after that we split it in half
|
---|
1245 | * (upper and lower half), but we donot request the full upper and lower rectangle, only
|
---|
1246 | * the part of the upper/lower rectangle that actually has something in it.
|
---|
1247 | *
|
---|
1248 | * This functions calculates the rectangles, asks the user to continue and downloads
|
---|
1249 | * the areas if applicable.
|
---|
1250 | */
|
---|
1251 | private void confirmAndDownloadAreas(Area a, double max_area, ProgressMonitor progressMonitor) {
|
---|
1252 | List<Rectangle2D> toDownload = new ArrayList<Rectangle2D>();
|
---|
1253 |
|
---|
1254 | addToDownload(a, a.getBounds(), toDownload, max_area);
|
---|
1255 |
|
---|
1256 | if(toDownload.size() == 0)
|
---|
1257 | return;
|
---|
1258 |
|
---|
1259 | JPanel msg = new JPanel(new GridBagLayout());
|
---|
1260 |
|
---|
1261 | msg.add(new JLabel(
|
---|
1262 | tr("<html>This action will require {0} individual<br>"
|
---|
1263 | + "download requests. Do you wish<br>to continue?</html>",
|
---|
1264 | toDownload.size())), GBC.eol());
|
---|
1265 |
|
---|
1266 | if (toDownload.size() > 1) {
|
---|
1267 | // hide progress dialog before displaying another pop up. Really closing the
|
---|
1268 | // dialog will be handled by PleaseWaitRunnable.
|
---|
1269 | if (progressMonitor instanceof PleaseWaitProgressMonitor) {
|
---|
1270 | ((PleaseWaitProgressMonitor) progressMonitor).getDialog().setVisible(false);
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | int ret = JOptionPane.showConfirmDialog(
|
---|
1274 | Main.parent,
|
---|
1275 | msg,
|
---|
1276 | tr("Download from OSM along this track"),
|
---|
1277 | JOptionPane.OK_CANCEL_OPTION,
|
---|
1278 | JOptionPane.PLAIN_MESSAGE
|
---|
1279 | );
|
---|
1280 | switch(ret) {
|
---|
1281 | case JOptionPane.CANCEL_OPTION:
|
---|
1282 | case JOptionPane.CLOSED_OPTION:
|
---|
1283 | return;
|
---|
1284 | default:
|
---|
1285 | // continue
|
---|
1286 | }
|
---|
1287 | }
|
---|
1288 | final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
|
---|
1289 | final Future<?> future = new DownloadOsmTaskList().download(false, toDownload, monitor);
|
---|
1290 | Main.worker.submit(
|
---|
1291 | new Runnable() {
|
---|
1292 | @Override
|
---|
1293 | public void run() {
|
---|
1294 | try {
|
---|
1295 | future.get();
|
---|
1296 | } catch(Exception e) {
|
---|
1297 | e.printStackTrace();
|
---|
1298 | return;
|
---|
1299 | }
|
---|
1300 | monitor.close();
|
---|
1301 | }
|
---|
1302 | }
|
---|
1303 | );
|
---|
1304 | }
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | private static void addToDownload(Area a, Rectangle2D r, Collection<Rectangle2D> results, double max_area) {
|
---|
1308 | Area tmp = new Area(r);
|
---|
1309 | // intersect with sought-after area
|
---|
1310 | tmp.intersect(a);
|
---|
1311 | if (tmp.isEmpty())
|
---|
1312 | return;
|
---|
1313 | Rectangle2D bounds = tmp.getBounds2D();
|
---|
1314 | if (bounds.getWidth() * bounds.getHeight() > max_area) {
|
---|
1315 | // the rectangle gets too large; split it and make recursive call.
|
---|
1316 | Rectangle2D r1;
|
---|
1317 | Rectangle2D r2;
|
---|
1318 | if (bounds.getWidth() > bounds.getHeight()) {
|
---|
1319 | // rectangles that are wider than high are split into a left and right half,
|
---|
1320 | r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth() / 2, bounds.getHeight());
|
---|
1321 | r2 = new Rectangle2D.Double(bounds.getX() + bounds.getWidth() / 2, bounds.getY(),
|
---|
1322 | bounds.getWidth() / 2, bounds.getHeight());
|
---|
1323 | } else {
|
---|
1324 | // others into a top and bottom half.
|
---|
1325 | r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() / 2);
|
---|
1326 | r2 = new Rectangle2D.Double(bounds.getX(), bounds.getY() + bounds.getHeight() / 2, bounds.getWidth(),
|
---|
1327 | bounds.getHeight() / 2);
|
---|
1328 | }
|
---|
1329 | addToDownload(a, r1, results, max_area);
|
---|
1330 | addToDownload(a, r2, results, max_area);
|
---|
1331 | } else {
|
---|
1332 | results.add(bounds);
|
---|
1333 | }
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | /**
|
---|
1337 | * Makes a new marker layer derived from this GpxLayer containing at least one audio marker
|
---|
1338 | * which the given audio file is associated with. Markers are derived from the following (a)
|
---|
1339 | * explict waypoints in the GPX layer, or (b) named trackpoints in the GPX layer, or (d)
|
---|
1340 | * timestamp on the wav file (e) (in future) voice recognised markers in the sound recording (f)
|
---|
1341 | * a single marker at the beginning of the track
|
---|
1342 | * @param wavFile : the file to be associated with the markers in the new marker layer
|
---|
1343 | * @param markers : keeps track of warning messages to avoid repeated warnings
|
---|
1344 | */
|
---|
1345 | private void importAudio(File wavFile, MarkerLayer ml, double firstStartTime, Markers markers) {
|
---|
1346 | URL url = null;
|
---|
1347 | try {
|
---|
1348 | url = wavFile.toURI().toURL();
|
---|
1349 | } catch (MalformedURLException e) {
|
---|
1350 | System.err.println("Unable to convert filename " + wavFile.getAbsolutePath() + " to URL");
|
---|
1351 | }
|
---|
1352 | Collection<WayPoint> waypoints = new ArrayList<WayPoint>();
|
---|
1353 | boolean timedMarkersOmitted = false;
|
---|
1354 | boolean untimedMarkersOmitted = false;
|
---|
1355 | double snapDistance = Main.pref.getDouble("marker.audiofromuntimedwaypoints.distance", 1.0e-3); /*
|
---|
1356 | * about
|
---|
1357 | * 25
|
---|
1358 | * m
|
---|
1359 | */
|
---|
1360 | WayPoint wayPointFromTimeStamp = null;
|
---|
1361 |
|
---|
1362 | // determine time of first point in track
|
---|
1363 | double firstTime = -1.0;
|
---|
1364 | if (data.tracks != null && !data.tracks.isEmpty()) {
|
---|
1365 | for (GpxTrack track : data.tracks) {
|
---|
1366 | for (GpxTrackSegment seg : track.getSegments()) {
|
---|
1367 | for (WayPoint w : seg.getWayPoints()) {
|
---|
1368 | firstTime = w.time;
|
---|
1369 | break;
|
---|
1370 | }
|
---|
1371 | if (firstTime >= 0.0) {
|
---|
1372 | break;
|
---|
1373 | }
|
---|
1374 | }
|
---|
1375 | if (firstTime >= 0.0) {
|
---|
1376 | break;
|
---|
1377 | }
|
---|
1378 | }
|
---|
1379 | }
|
---|
1380 | if (firstTime < 0.0) {
|
---|
1381 | JOptionPane.showMessageDialog(
|
---|
1382 | Main.parent,
|
---|
1383 | tr("No GPX track available in layer to associate audio with."),
|
---|
1384 | tr("Error"),
|
---|
1385 | JOptionPane.ERROR_MESSAGE
|
---|
1386 | );
|
---|
1387 | return;
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | // (a) try explicit timestamped waypoints - unless suppressed
|
---|
1391 | if (Main.pref.getBoolean("marker.audiofromexplicitwaypoints", true) && data.waypoints != null
|
---|
1392 | && !data.waypoints.isEmpty()) {
|
---|
1393 | for (WayPoint w : data.waypoints) {
|
---|
1394 | if (w.time > firstTime) {
|
---|
1395 | waypoints.add(w);
|
---|
1396 | } else if (w.time > 0.0) {
|
---|
1397 | timedMarkersOmitted = true;
|
---|
1398 | }
|
---|
1399 | }
|
---|
1400 | }
|
---|
1401 |
|
---|
1402 | // (b) try explicit waypoints without timestamps - unless suppressed
|
---|
1403 | if (Main.pref.getBoolean("marker.audiofromuntimedwaypoints", true) && data.waypoints != null
|
---|
1404 | && !data.waypoints.isEmpty()) {
|
---|
1405 | for (WayPoint w : data.waypoints) {
|
---|
1406 | if (waypoints.contains(w)) {
|
---|
1407 | continue;
|
---|
1408 | }
|
---|
1409 | WayPoint wNear = nearestPointOnTrack(w.getEastNorth(), snapDistance);
|
---|
1410 | if (wNear != null) {
|
---|
1411 | WayPoint wc = new WayPoint(w.getCoor());
|
---|
1412 | wc.time = wNear.time;
|
---|
1413 | if (w.attr.containsKey("name")) {
|
---|
1414 | wc.attr.put("name", w.getString("name"));
|
---|
1415 | }
|
---|
1416 | waypoints.add(wc);
|
---|
1417 | } else {
|
---|
1418 | untimedMarkersOmitted = true;
|
---|
1419 | }
|
---|
1420 | }
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | // (c) use explicitly named track points, again unless suppressed
|
---|
1424 | if ((Main.pref.getBoolean("marker.audiofromnamedtrackpoints", false)) && data.tracks != null
|
---|
1425 | && !data.tracks.isEmpty()) {
|
---|
1426 | for (GpxTrack track : data.tracks) {
|
---|
1427 | for (GpxTrackSegment seg : track.getSegments()) {
|
---|
1428 | for (WayPoint w : seg.getWayPoints()) {
|
---|
1429 | if (w.attr.containsKey("name") || w.attr.containsKey("desc")) {
|
---|
1430 | waypoints.add(w);
|
---|
1431 | }
|
---|
1432 | }
|
---|
1433 | }
|
---|
1434 | }
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | // (d) use timestamp of file as location on track
|
---|
1438 | if ((Main.pref.getBoolean("marker.audiofromwavtimestamps", false)) && data.tracks != null
|
---|
1439 | && !data.tracks.isEmpty()) {
|
---|
1440 | double lastModified = wavFile.lastModified() / 1000.0; // lastModified is in
|
---|
1441 | // milliseconds
|
---|
1442 | double duration = AudioUtil.getCalibratedDuration(wavFile);
|
---|
1443 | double startTime = lastModified - duration;
|
---|
1444 | startTime = firstStartTime + (startTime - firstStartTime)
|
---|
1445 | / Main.pref.getDouble("audio.calibration", "1.0" /* default, ratio */);
|
---|
1446 | WayPoint w1 = null;
|
---|
1447 | WayPoint w2 = null;
|
---|
1448 |
|
---|
1449 | for (GpxTrack track : data.tracks) {
|
---|
1450 | for (GpxTrackSegment seg : track.getSegments()) {
|
---|
1451 | for (WayPoint w : seg.getWayPoints()) {
|
---|
1452 | if (startTime < w.time) {
|
---|
1453 | w2 = w;
|
---|
1454 | break;
|
---|
1455 | }
|
---|
1456 | w1 = w;
|
---|
1457 | }
|
---|
1458 | if (w2 != null) {
|
---|
1459 | break;
|
---|
1460 | }
|
---|
1461 | }
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 | if (w1 == null || w2 == null) {
|
---|
1465 | timedMarkersOmitted = true;
|
---|
1466 | } else {
|
---|
1467 | wayPointFromTimeStamp = new WayPoint(w1.getCoor().interpolate(w2.getCoor(),
|
---|
1468 | (startTime - w1.time) / (w2.time - w1.time)));
|
---|
1469 | wayPointFromTimeStamp.time = startTime;
|
---|
1470 | String name = wavFile.getName();
|
---|
1471 | int dot = name.lastIndexOf(".");
|
---|
1472 | if (dot > 0) {
|
---|
1473 | name = name.substring(0, dot);
|
---|
1474 | }
|
---|
1475 | wayPointFromTimeStamp.attr.put("name", name);
|
---|
1476 | waypoints.add(wayPointFromTimeStamp);
|
---|
1477 | }
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | // (e) analyse audio for spoken markers here, in due course
|
---|
1481 |
|
---|
1482 | // (f) simply add a single marker at the start of the track
|
---|
1483 | if ((Main.pref.getBoolean("marker.audiofromstart") || waypoints.isEmpty()) && data.tracks != null
|
---|
1484 | && !data.tracks.isEmpty()) {
|
---|
1485 | boolean gotOne = false;
|
---|
1486 | for (GpxTrack track : data.tracks) {
|
---|
1487 | for (GpxTrackSegment seg : track.getSegments()) {
|
---|
1488 | for (WayPoint w : seg.getWayPoints()) {
|
---|
1489 | WayPoint wStart = new WayPoint(w.getCoor());
|
---|
1490 | wStart.attr.put("name", "start");
|
---|
1491 | wStart.time = w.time;
|
---|
1492 | waypoints.add(wStart);
|
---|
1493 | gotOne = true;
|
---|
1494 | break;
|
---|
1495 | }
|
---|
1496 | if (gotOne) {
|
---|
1497 | break;
|
---|
1498 | }
|
---|
1499 | }
|
---|
1500 | if (gotOne) {
|
---|
1501 | break;
|
---|
1502 | }
|
---|
1503 | }
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | /* we must have got at least one waypoint now */
|
---|
1507 |
|
---|
1508 | Collections.sort((ArrayList<WayPoint>) waypoints, new Comparator<WayPoint>() {
|
---|
1509 | @Override
|
---|
1510 | public int compare(WayPoint a, WayPoint b) {
|
---|
1511 | return a.time <= b.time ? -1 : 1;
|
---|
1512 | }
|
---|
1513 | });
|
---|
1514 |
|
---|
1515 | firstTime = -1.0; /* this time of the first waypoint, not first trackpoint */
|
---|
1516 | for (WayPoint w : waypoints) {
|
---|
1517 | if (firstTime < 0.0) {
|
---|
1518 | firstTime = w.time;
|
---|
1519 | }
|
---|
1520 | double offset = w.time - firstTime;
|
---|
1521 | AudioMarker am = new AudioMarker(w.getCoor(), w, url, ml, w.time, offset);
|
---|
1522 | /*
|
---|
1523 | * timeFromAudio intended for future use to shift markers of this type on
|
---|
1524 | * synchronization
|
---|
1525 | */
|
---|
1526 | if (w == wayPointFromTimeStamp) {
|
---|
1527 | am.timeFromAudio = true;
|
---|
1528 | }
|
---|
1529 | ml.data.add(am);
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | if (timedMarkersOmitted && !markers.timedMarkersOmitted) {
|
---|
1533 | JOptionPane
|
---|
1534 | .showMessageDialog(
|
---|
1535 | Main.parent,
|
---|
1536 | tr("Some waypoints with timestamps from before the start of the track or after the end were omitted or moved to the start."));
|
---|
1537 | markers.timedMarkersOmitted = timedMarkersOmitted;
|
---|
1538 | }
|
---|
1539 | if (untimedMarkersOmitted && !markers.untimedMarkersOmitted) {
|
---|
1540 | JOptionPane
|
---|
1541 | .showMessageDialog(
|
---|
1542 | Main.parent,
|
---|
1543 | tr("Some waypoints which were too far from the track to sensibly estimate their time were omitted."));
|
---|
1544 | markers.untimedMarkersOmitted = untimedMarkersOmitted;
|
---|
1545 | }
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | /**
|
---|
1549 | * Makes a WayPoint at the projection of point P onto the track providing P is less than
|
---|
1550 | * tolerance away from the track
|
---|
1551 | *
|
---|
1552 | * @param P : the point to determine the projection for
|
---|
1553 | * @param tolerance : must be no further than this from the track
|
---|
1554 | * @return the closest point on the track to P, which may be the first or last point if off the
|
---|
1555 | * end of a segment, or may be null if nothing close enough
|
---|
1556 | */
|
---|
1557 | public WayPoint nearestPointOnTrack(EastNorth P, double tolerance) {
|
---|
1558 | /*
|
---|
1559 | * assume the coordinates of P are xp,yp, and those of a section of track between two
|
---|
1560 | * trackpoints are R=xr,yr and S=xs,ys. Let N be the projected point.
|
---|
1561 | *
|
---|
1562 | * The equation of RS is Ax + By + C = 0 where A = ys - yr B = xr - xs C = - Axr - Byr
|
---|
1563 | *
|
---|
1564 | * Also, note that the distance RS^2 is A^2 + B^2
|
---|
1565 | *
|
---|
1566 | * If RS^2 == 0.0 ignore the degenerate section of track
|
---|
1567 | *
|
---|
1568 | * PN^2 = (Axp + Byp + C)^2 / RS^2 that is the distance from P to the line
|
---|
1569 | *
|
---|
1570 | * so if PN^2 is less than PNmin^2 (initialized to tolerance) we can reject the line;
|
---|
1571 | * otherwise... determine if the projected poijnt lies within the bounds of the line: PR^2 -
|
---|
1572 | * PN^2 <= RS^2 and PS^2 - PN^2 <= RS^2
|
---|
1573 | *
|
---|
1574 | * where PR^2 = (xp - xr)^2 + (yp-yr)^2 and PS^2 = (xp - xs)^2 + (yp-ys)^2
|
---|
1575 | *
|
---|
1576 | * If so, calculate N as xn = xr + (RN/RS) B yn = y1 + (RN/RS) A
|
---|
1577 | *
|
---|
1578 | * where RN = sqrt(PR^2 - PN^2)
|
---|
1579 | */
|
---|
1580 |
|
---|
1581 | double PNminsq = tolerance * tolerance;
|
---|
1582 | EastNorth bestEN = null;
|
---|
1583 | double bestTime = 0.0;
|
---|
1584 | double px = P.east();
|
---|
1585 | double py = P.north();
|
---|
1586 | double rx = 0.0, ry = 0.0, sx, sy, x, y;
|
---|
1587 | if (data.tracks == null)
|
---|
1588 | return null;
|
---|
1589 | for (GpxTrack track : data.tracks) {
|
---|
1590 | for (GpxTrackSegment seg : track.getSegments()) {
|
---|
1591 | WayPoint R = null;
|
---|
1592 | for (WayPoint S : seg.getWayPoints()) {
|
---|
1593 | EastNorth c = S.getEastNorth();
|
---|
1594 | if (R == null) {
|
---|
1595 | R = S;
|
---|
1596 | rx = c.east();
|
---|
1597 | ry = c.north();
|
---|
1598 | x = px - rx;
|
---|
1599 | y = py - ry;
|
---|
1600 | double PRsq = x * x + y * y;
|
---|
1601 | if (PRsq < PNminsq) {
|
---|
1602 | PNminsq = PRsq;
|
---|
1603 | bestEN = c;
|
---|
1604 | bestTime = R.time;
|
---|
1605 | }
|
---|
1606 | } else {
|
---|
1607 | sx = c.east();
|
---|
1608 | sy = c.north();
|
---|
1609 | double A = sy - ry;
|
---|
1610 | double B = rx - sx;
|
---|
1611 | double C = -A * rx - B * ry;
|
---|
1612 | double RSsq = A * A + B * B;
|
---|
1613 | if (RSsq == 0.0) {
|
---|
1614 | continue;
|
---|
1615 | }
|
---|
1616 | double PNsq = A * px + B * py + C;
|
---|
1617 | PNsq = PNsq * PNsq / RSsq;
|
---|
1618 | if (PNsq < PNminsq) {
|
---|
1619 | x = px - rx;
|
---|
1620 | y = py - ry;
|
---|
1621 | double PRsq = x * x + y * y;
|
---|
1622 | x = px - sx;
|
---|
1623 | y = py - sy;
|
---|
1624 | double PSsq = x * x + y * y;
|
---|
1625 | if (PRsq - PNsq <= RSsq && PSsq - PNsq <= RSsq) {
|
---|
1626 | double RNoverRS = Math.sqrt((PRsq - PNsq) / RSsq);
|
---|
1627 | double nx = rx - RNoverRS * B;
|
---|
1628 | double ny = ry + RNoverRS * A;
|
---|
1629 | bestEN = new EastNorth(nx, ny);
|
---|
1630 | bestTime = R.time + RNoverRS * (S.time - R.time);
|
---|
1631 | PNminsq = PNsq;
|
---|
1632 | }
|
---|
1633 | }
|
---|
1634 | R = S;
|
---|
1635 | rx = sx;
|
---|
1636 | ry = sy;
|
---|
1637 | }
|
---|
1638 | }
|
---|
1639 | if (R != null) {
|
---|
1640 | EastNorth c = R.getEastNorth();
|
---|
1641 | /* if there is only one point in the seg, it will do this twice, but no matter */
|
---|
1642 | rx = c.east();
|
---|
1643 | ry = c.north();
|
---|
1644 | x = px - rx;
|
---|
1645 | y = py - ry;
|
---|
1646 | double PRsq = x * x + y * y;
|
---|
1647 | if (PRsq < PNminsq) {
|
---|
1648 | PNminsq = PRsq;
|
---|
1649 | bestEN = c;
|
---|
1650 | bestTime = R.time;
|
---|
1651 | }
|
---|
1652 | }
|
---|
1653 | }
|
---|
1654 | }
|
---|
1655 | if (bestEN == null)
|
---|
1656 | return null;
|
---|
1657 | WayPoint best = new WayPoint(Main.getProjection().eastNorth2latlon(bestEN));
|
---|
1658 | best.time = bestTime;
|
---|
1659 | return best;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | private class CustomizeDrawing extends AbstractAction implements LayerAction, MultiLayerAction {
|
---|
1663 | List<Layer> layers;
|
---|
1664 |
|
---|
1665 | public CustomizeDrawing(List<Layer> l) {
|
---|
1666 | this();
|
---|
1667 | layers = l;
|
---|
1668 | }
|
---|
1669 |
|
---|
1670 | public CustomizeDrawing(Layer l) {
|
---|
1671 | this();
|
---|
1672 | layers = new LinkedList<Layer>();
|
---|
1673 | layers.add(l);
|
---|
1674 | }
|
---|
1675 |
|
---|
1676 | private CustomizeDrawing() {
|
---|
1677 | super(tr("Customize track drawing"), ImageProvider.get("mapmode/addsegment"));
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | @Override
|
---|
1681 | public boolean supportLayers(List<Layer> layers) {
|
---|
1682 | for(Layer layer: layers) {
|
---|
1683 | if(!(layer instanceof GpxLayer))
|
---|
1684 | return false;
|
---|
1685 | }
|
---|
1686 | return true;
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | @Override
|
---|
1690 | public Component createMenuComponent() {
|
---|
1691 | return new JMenuItem(this);
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | @Override
|
---|
1695 | public Action getMultiLayerAction(List<Layer> layers) {
|
---|
1696 | return new CustomizeDrawing(layers);
|
---|
1697 | }
|
---|
1698 |
|
---|
1699 | @Override
|
---|
1700 | public void actionPerformed(ActionEvent e) {
|
---|
1701 | boolean hasLocal = false, hasNonlocal = false;
|
---|
1702 | for (Layer layer : layers) {
|
---|
1703 | if (layer instanceof GpxLayer) {
|
---|
1704 | if (((GpxLayer) layer).isLocalFile) {
|
---|
1705 | hasLocal = true;
|
---|
1706 | } else {
|
---|
1707 | hasNonlocal = true;
|
---|
1708 | }
|
---|
1709 | }
|
---|
1710 | }
|
---|
1711 | GPXSettingsPanel panel=new GPXSettingsPanel(getName(), hasLocal, hasNonlocal);
|
---|
1712 |
|
---|
1713 | int answer = JOptionPane.showConfirmDialog(Main.parent, panel,
|
---|
1714 | tr("Customize track drawing"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
|
---|
1715 | if (answer == JOptionPane.CANCEL_OPTION || answer == JOptionPane.CLOSED_OPTION) return;
|
---|
1716 | for(Layer layer : layers) {
|
---|
1717 | // save preferences for all layers
|
---|
1718 | boolean f=false;
|
---|
1719 | if (layer instanceof GpxLayer) {
|
---|
1720 | f=((GpxLayer)layer).isLocalFile;
|
---|
1721 | }
|
---|
1722 | panel.savePreferences(layer.getName(),f);
|
---|
1723 | }
|
---|
1724 | Main.map.repaint();
|
---|
1725 | }
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | private class MarkersFromNamedPoins extends AbstractAction {
|
---|
1729 |
|
---|
1730 | public MarkersFromNamedPoins() {
|
---|
1731 | super(tr("Markers From Named Points"), ImageProvider.get("addmarkers"));
|
---|
1732 | putValue("help", ht("/Action/MarkersFromNamedPoints"));
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | @Override
|
---|
1736 | public void actionPerformed(ActionEvent e) {
|
---|
1737 | GpxData namedTrackPoints = new GpxData();
|
---|
1738 | for (GpxTrack track : data.tracks) {
|
---|
1739 | for (GpxTrackSegment seg : track.getSegments()) {
|
---|
1740 | for (WayPoint point : seg.getWayPoints())
|
---|
1741 | if (point.attr.containsKey("name") || point.attr.containsKey("desc")) {
|
---|
1742 | namedTrackPoints.waypoints.add(point);
|
---|
1743 | }
|
---|
1744 | }
|
---|
1745 | }
|
---|
1746 |
|
---|
1747 | MarkerLayer ml = new MarkerLayer(namedTrackPoints, tr("Named Trackpoints from {0}", getName()),
|
---|
1748 | getAssociatedFile(), GpxLayer.this);
|
---|
1749 | if (ml.data.size() > 0) {
|
---|
1750 | Main.main.addLayer(ml);
|
---|
1751 | }
|
---|
1752 |
|
---|
1753 | }
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | private class ImportAudio extends AbstractAction {
|
---|
1757 |
|
---|
1758 | public ImportAudio() {
|
---|
1759 | super(tr("Import Audio"), ImageProvider.get("importaudio"));
|
---|
1760 | putValue("help", ht("/Action/ImportAudio"));
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 | private void warnCantImportIntoServerLayer(GpxLayer layer) {
|
---|
1764 | String msg = tr("<html>The data in the GPX layer ''{0}'' has been downloaded from the server.<br>"
|
---|
1765 | + "Because its way points do not include a timestamp we cannot correlate them with audio data.</html>",
|
---|
1766 | layer.getName()
|
---|
1767 | );
|
---|
1768 | HelpAwareOptionPane.showOptionDialog(
|
---|
1769 | Main.parent,
|
---|
1770 | msg,
|
---|
1771 | tr("Import not possible"),
|
---|
1772 | JOptionPane.WARNING_MESSAGE,
|
---|
1773 | ht("/Action/ImportAudio#CantImportIntoGpxLayerFromServer")
|
---|
1774 | );
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 | @Override
|
---|
1778 | public void actionPerformed(ActionEvent e) {
|
---|
1779 | if (GpxLayer.this.data.fromServer) {
|
---|
1780 | warnCantImportIntoServerLayer(GpxLayer.this);
|
---|
1781 | return;
|
---|
1782 | }
|
---|
1783 | String dir = Main.pref.get("markers.lastaudiodirectory");
|
---|
1784 | JFileChooser fc = new JFileChooser(dir);
|
---|
1785 | fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
---|
1786 | fc.setAcceptAllFileFilterUsed(false);
|
---|
1787 | fc.setFileFilter(new FileFilter() {
|
---|
1788 | @Override
|
---|
1789 | public boolean accept(File f) {
|
---|
1790 | return f.isDirectory() || f.getName().toLowerCase().endsWith(".wav");
|
---|
1791 | }
|
---|
1792 |
|
---|
1793 | @Override
|
---|
1794 | public String getDescription() {
|
---|
1795 | return tr("Wave Audio files (*.wav)");
|
---|
1796 | }
|
---|
1797 | });
|
---|
1798 | fc.setMultiSelectionEnabled(true);
|
---|
1799 | if (fc.showOpenDialog(Main.parent) == JFileChooser.APPROVE_OPTION) {
|
---|
1800 | if (!fc.getCurrentDirectory().getAbsolutePath().equals(dir)) {
|
---|
1801 | Main.pref.put("markers.lastaudiodirectory", fc.getCurrentDirectory().getAbsolutePath());
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | File sel[] = fc.getSelectedFiles();
|
---|
1805 | // sort files in increasing order of timestamp (this is the end time, but so
|
---|
1806 | // long as they don't overlap, that's fine)
|
---|
1807 | if (sel.length > 1) {
|
---|
1808 | Arrays.sort(sel, new Comparator<File>() {
|
---|
1809 | @Override
|
---|
1810 | public int compare(File a, File b) {
|
---|
1811 | return a.lastModified() <= b.lastModified() ? -1 : 1;
|
---|
1812 | }
|
---|
1813 | });
|
---|
1814 | }
|
---|
1815 |
|
---|
1816 | String names = null;
|
---|
1817 | for (int i = 0; i < sel.length; i++) {
|
---|
1818 | if (names == null) {
|
---|
1819 | names = " (";
|
---|
1820 | } else {
|
---|
1821 | names += ", ";
|
---|
1822 | }
|
---|
1823 | names += sel[i].getName();
|
---|
1824 | }
|
---|
1825 | if (names != null) {
|
---|
1826 | names += ")";
|
---|
1827 | } else {
|
---|
1828 | names = "";
|
---|
1829 | }
|
---|
1830 | MarkerLayer ml = new MarkerLayer(new GpxData(), tr("Audio markers from {0}", getName()) + names,
|
---|
1831 | getAssociatedFile(), GpxLayer.this);
|
---|
1832 | double firstStartTime = sel[0].lastModified() / 1000.0 /* ms -> seconds */
|
---|
1833 | - AudioUtil.getCalibratedDuration(sel[0]);
|
---|
1834 |
|
---|
1835 | Markers m = new Markers();
|
---|
1836 | for (int i = 0; i < sel.length; i++) {
|
---|
1837 | importAudio(sel[i], ml, firstStartTime, m);
|
---|
1838 | }
|
---|
1839 | Main.main.addLayer(ml);
|
---|
1840 | Main.map.repaint();
|
---|
1841 | }
|
---|
1842 |
|
---|
1843 | }
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 | private class ImportImages extends AbstractAction {
|
---|
1847 |
|
---|
1848 | public ImportImages() {
|
---|
1849 | super(tr("Import images"), ImageProvider.get("dialogs/geoimage"));
|
---|
1850 | putValue("help", ht("/Action/ImportImages"));
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | private void warnCantImportIntoServerLayer(GpxLayer layer) {
|
---|
1854 | String msg = tr("<html>The data in the GPX layer ''{0}'' has been downloaded from the server.<br>"
|
---|
1855 | + "Because its way points do not include a timestamp we cannot correlate them with images.</html>",
|
---|
1856 | layer.getName()
|
---|
1857 | );
|
---|
1858 | HelpAwareOptionPane.showOptionDialog(
|
---|
1859 | Main.parent,
|
---|
1860 | msg,
|
---|
1861 | tr("Import not possible"),
|
---|
1862 | JOptionPane.WARNING_MESSAGE,
|
---|
1863 | ht("/Action/ImportImages#CantImportIntoGpxLayerFromServer")
|
---|
1864 | );
|
---|
1865 | }
|
---|
1866 |
|
---|
1867 | private void addRecursiveFiles(LinkedList<File> files, File[] sel) {
|
---|
1868 | for (File f : sel) {
|
---|
1869 | if (f.isDirectory()) {
|
---|
1870 | addRecursiveFiles(files, f.listFiles());
|
---|
1871 | } else if (f.getName().toLowerCase().endsWith(".jpg")) {
|
---|
1872 | files.add(f);
|
---|
1873 | }
|
---|
1874 | }
|
---|
1875 | }
|
---|
1876 |
|
---|
1877 | @Override
|
---|
1878 | public void actionPerformed(ActionEvent e) {
|
---|
1879 |
|
---|
1880 | if (GpxLayer.this.data.fromServer) {
|
---|
1881 | warnCantImportIntoServerLayer(GpxLayer.this);
|
---|
1882 | return;
|
---|
1883 | }
|
---|
1884 | String curDir = Main.pref.get("geoimage.lastdirectory", Main.pref.get("lastDirectory"));
|
---|
1885 | if (curDir.equals("")) {
|
---|
1886 | curDir = ".";
|
---|
1887 | }
|
---|
1888 | JFileChooser fc = new JFileChooser(new File(curDir));
|
---|
1889 |
|
---|
1890 | fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
---|
1891 | fc.setMultiSelectionEnabled(true);
|
---|
1892 | fc.setAcceptAllFileFilterUsed(false);
|
---|
1893 | JpgImporter importer = new JpgImporter(GpxLayer.this);
|
---|
1894 | fc.setFileFilter(importer.filter);
|
---|
1895 | fc.showOpenDialog(Main.parent);
|
---|
1896 | LinkedList<File> files = new LinkedList<File>();
|
---|
1897 | File[] sel = fc.getSelectedFiles();
|
---|
1898 | if (sel == null || sel.length == 0)
|
---|
1899 | return;
|
---|
1900 | if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {
|
---|
1901 | Main.pref.put("geoimage.lastdirectory", fc.getCurrentDirectory().getAbsolutePath());
|
---|
1902 | }
|
---|
1903 | addRecursiveFiles(files, sel);
|
---|
1904 | importer.importDataHandleExceptions(files, NullProgressMonitor.INSTANCE);
|
---|
1905 | }
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | @Override
|
---|
1909 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
1910 | if (newValue == null) return;
|
---|
1911 | if (data.waypoints != null) {
|
---|
1912 | for (WayPoint wp : data.waypoints){
|
---|
1913 | wp.invalidateEastNorthCache();
|
---|
1914 | }
|
---|
1915 | }
|
---|
1916 | if (data.tracks != null){
|
---|
1917 | for (GpxTrack track: data.tracks) {
|
---|
1918 | for (GpxTrackSegment segment: track.getSegments()) {
|
---|
1919 | for (WayPoint wp: segment.getWayPoints()) {
|
---|
1920 | wp.invalidateEastNorthCache();
|
---|
1921 | }
|
---|
1922 | }
|
---|
1923 | }
|
---|
1924 | }
|
---|
1925 | if (data.routes != null) {
|
---|
1926 | for (GpxRoute route: data.routes) {
|
---|
1927 | if (route.routePoints == null) {
|
---|
1928 | continue;
|
---|
1929 | }
|
---|
1930 | for (WayPoint wp: route.routePoints) {
|
---|
1931 | wp.invalidateEastNorthCache();
|
---|
1932 | }
|
---|
1933 | }
|
---|
1934 | }
|
---|
1935 | }
|
---|
1936 | }
|
---|