1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.Graphics2D;
|
---|
9 | import java.io.File;
|
---|
10 | import java.text.DateFormat;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.Date;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import javax.swing.Action;
|
---|
19 | import javax.swing.Icon;
|
---|
20 | import javax.swing.JScrollPane;
|
---|
21 | import javax.swing.SwingUtilities;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
25 | import org.openstreetmap.josm.actions.SaveActionBase;
|
---|
26 | import org.openstreetmap.josm.data.Bounds;
|
---|
27 | import org.openstreetmap.josm.data.SystemOfMeasurement;
|
---|
28 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
29 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
30 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
31 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
32 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
33 | import org.openstreetmap.josm.data.preferences.ColorProperty;
|
---|
34 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
35 | import org.openstreetmap.josm.gui.MapView;
|
---|
36 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
37 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
38 | import org.openstreetmap.josm.gui.layer.gpx.ChooseTrackVisibilityAction;
|
---|
39 | import org.openstreetmap.josm.gui.layer.gpx.ConvertToDataLayerAction;
|
---|
40 | import org.openstreetmap.josm.gui.layer.gpx.CustomizeDrawingAction;
|
---|
41 | import org.openstreetmap.josm.gui.layer.gpx.DownloadAlongTrackAction;
|
---|
42 | import org.openstreetmap.josm.gui.layer.gpx.DownloadWmsAlongTrackAction;
|
---|
43 | import org.openstreetmap.josm.gui.layer.gpx.GpxDrawHelper;
|
---|
44 | import org.openstreetmap.josm.gui.layer.gpx.ImportAudioAction;
|
---|
45 | import org.openstreetmap.josm.gui.layer.gpx.ImportImagesAction;
|
---|
46 | import org.openstreetmap.josm.gui.layer.gpx.MarkersFromNamedPointsAction;
|
---|
47 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
48 | import org.openstreetmap.josm.io.GpxImporter;
|
---|
49 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
50 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * A layer that displays data from a Gpx file / the OSM gpx downloads.
|
---|
54 | */
|
---|
55 | public class GpxLayer extends Layer {
|
---|
56 |
|
---|
57 | /** GPX data */
|
---|
58 | public GpxData data;
|
---|
59 | private final boolean isLocalFile;
|
---|
60 | /**
|
---|
61 | * used by {@link ChooseTrackVisibilityAction} to determine which tracks to show/hide
|
---|
62 | */
|
---|
63 | public boolean[] trackVisibility = new boolean[0];
|
---|
64 |
|
---|
65 | private final List<GpxTrack> lastTracks = new ArrayList<>(); // List of tracks at last paint
|
---|
66 | private int lastUpdateCount;
|
---|
67 |
|
---|
68 | private final GpxDrawHelper drawHelper;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Constructs a new {@code GpxLayer} without name.
|
---|
72 | * @param d GPX data
|
---|
73 | */
|
---|
74 | public GpxLayer(GpxData d) {
|
---|
75 | this(d, null, false);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Constructs a new {@code GpxLayer} with a given name.
|
---|
80 | * @param d GPX data
|
---|
81 | * @param name layer name
|
---|
82 | */
|
---|
83 | public GpxLayer(GpxData d, String name) {
|
---|
84 | this(d, name, false);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Constructs a new {@code GpxLayer} with a given name, thah can be attached to a local file.
|
---|
89 | * @param d GPX data
|
---|
90 | * @param name layer name
|
---|
91 | * @param isLocal whether data is attached to a local file
|
---|
92 | */
|
---|
93 | public GpxLayer(GpxData d, String name, boolean isLocal) {
|
---|
94 | super(d.getString(GpxConstants.META_NAME));
|
---|
95 | data = d;
|
---|
96 | drawHelper = new GpxDrawHelper(data);
|
---|
97 | SystemOfMeasurement.addSoMChangeListener(drawHelper);
|
---|
98 | ensureTrackVisibilityLength();
|
---|
99 | setName(name);
|
---|
100 | isLocalFile = isLocal;
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | protected ColorProperty getBaseColorProperty() {
|
---|
105 | return GpxDrawHelper.DEFAULT_COLOR;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns a human readable string that shows the timespan of the given track
|
---|
110 | * @param trk The GPX track for which timespan is displayed
|
---|
111 | * @return The timespan as a string
|
---|
112 | */
|
---|
113 | public static String getTimespanForTrack(GpxTrack trk) {
|
---|
114 | Date[] bounds = GpxData.getMinMaxTimeForTrack(trk);
|
---|
115 | String ts = "";
|
---|
116 | if (bounds != null) {
|
---|
117 | DateFormat df = DateUtils.getDateFormat(DateFormat.SHORT);
|
---|
118 | String earliestDate = df.format(bounds[0]);
|
---|
119 | String latestDate = df.format(bounds[1]);
|
---|
120 |
|
---|
121 | if (earliestDate.equals(latestDate)) {
|
---|
122 | DateFormat tf = DateUtils.getTimeFormat(DateFormat.SHORT);
|
---|
123 | ts += earliestDate + ' ';
|
---|
124 | ts += tf.format(bounds[0]) + " - " + tf.format(bounds[1]);
|
---|
125 | } else {
|
---|
126 | DateFormat dtf = DateUtils.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM);
|
---|
127 | ts += dtf.format(bounds[0]) + " - " + dtf.format(bounds[1]);
|
---|
128 | }
|
---|
129 |
|
---|
130 | int diff = (int) (bounds[1].getTime() - bounds[0].getTime()) / 1000;
|
---|
131 | ts += String.format(" (%d:%02d)", diff / 3600, (diff % 3600) / 60);
|
---|
132 | }
|
---|
133 | return ts;
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public Icon getIcon() {
|
---|
138 | return ImageProvider.get("layer", "gpx_small");
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public Object getInfoComponent() {
|
---|
143 | StringBuilder info = new StringBuilder(48).append("<html>");
|
---|
144 |
|
---|
145 | if (data.attr.containsKey("name")) {
|
---|
146 | info.append(tr("Name: {0}", data.get(GpxConstants.META_NAME))).append("<br>");
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (data.attr.containsKey("desc")) {
|
---|
150 | info.append(tr("Description: {0}", data.get(GpxConstants.META_DESC))).append("<br>");
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (!data.tracks.isEmpty()) {
|
---|
154 | info.append("<table><thead align='center'><tr><td colspan='5'>")
|
---|
155 | .append(trn("{0} track", "{0} tracks", data.tracks.size(), data.tracks.size()))
|
---|
156 | .append("</td></tr><tr align='center'><td>").append(tr("Name")).append("</td><td>")
|
---|
157 | .append(tr("Description")).append("</td><td>").append(tr("Timespan"))
|
---|
158 | .append("</td><td>").append(tr("Length")).append("</td><td>").append(tr("URL"))
|
---|
159 | .append("</td></tr></thead>");
|
---|
160 |
|
---|
161 | for (GpxTrack trk : data.tracks) {
|
---|
162 | info.append("<tr><td>");
|
---|
163 | if (trk.getAttributes().containsKey(GpxConstants.GPX_NAME)) {
|
---|
164 | info.append(trk.get(GpxConstants.GPX_NAME));
|
---|
165 | }
|
---|
166 | info.append("</td><td>");
|
---|
167 | if (trk.getAttributes().containsKey(GpxConstants.GPX_DESC)) {
|
---|
168 | info.append(' ').append(trk.get(GpxConstants.GPX_DESC));
|
---|
169 | }
|
---|
170 | info.append("</td><td>");
|
---|
171 | info.append(getTimespanForTrack(trk));
|
---|
172 | info.append("</td><td>");
|
---|
173 | info.append(SystemOfMeasurement.getSystemOfMeasurement().getDistText(trk.length()));
|
---|
174 | info.append("</td><td>");
|
---|
175 | if (trk.getAttributes().containsKey("url")) {
|
---|
176 | info.append(trk.get("url"));
|
---|
177 | }
|
---|
178 | info.append("</td></tr>");
|
---|
179 | }
|
---|
180 | info.append("</table><br><br>");
|
---|
181 | }
|
---|
182 |
|
---|
183 | info.append(tr("Length: {0}", SystemOfMeasurement.getSystemOfMeasurement().getDistText(data.length()))).append("<br>")
|
---|
184 | .append(trn("{0} route, ", "{0} routes, ", data.routes.size(), data.routes.size()))
|
---|
185 | .append(trn("{0} waypoint", "{0} waypoints", data.waypoints.size(), data.waypoints.size())).append("<br></html>");
|
---|
186 |
|
---|
187 | final JScrollPane sp = new JScrollPane(new HtmlPanel(info.toString()));
|
---|
188 | sp.setPreferredSize(new Dimension(sp.getPreferredSize().width+20, 370));
|
---|
189 | SwingUtilities.invokeLater(() -> sp.getVerticalScrollBar().setValue(0));
|
---|
190 | return sp;
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public boolean isInfoResizable() {
|
---|
195 | return true;
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public Action[] getMenuEntries() {
|
---|
200 | return new Action[] {
|
---|
201 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
202 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
203 | LayerListDialog.getInstance().createMergeLayerAction(this),
|
---|
204 | SeparatorLayerAction.INSTANCE,
|
---|
205 | new LayerSaveAction(this),
|
---|
206 | new LayerSaveAsAction(this),
|
---|
207 | new CustomizeColor(this),
|
---|
208 | new CustomizeDrawingAction(this),
|
---|
209 | new ImportImagesAction(this),
|
---|
210 | new ImportAudioAction(this),
|
---|
211 | new MarkersFromNamedPointsAction(this),
|
---|
212 | new ConvertToDataLayerAction.FromGpxLayer(this),
|
---|
213 | new DownloadAlongTrackAction(data),
|
---|
214 | new DownloadWmsAlongTrackAction(data),
|
---|
215 | SeparatorLayerAction.INSTANCE,
|
---|
216 | new ChooseTrackVisibilityAction(this),
|
---|
217 | new RenameLayerAction(getAssociatedFile(), this),
|
---|
218 | SeparatorLayerAction.INSTANCE,
|
---|
219 | new LayerListPopup.InfoAction(this) };
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Determines if data is attached to a local file.
|
---|
224 | * @return {@code true} if data is attached to a local file, {@code false} otherwise
|
---|
225 | */
|
---|
226 | public boolean isLocalFile() {
|
---|
227 | return isLocalFile;
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | public String getToolTipText() {
|
---|
232 | StringBuilder info = new StringBuilder(48).append("<html>");
|
---|
233 |
|
---|
234 | if (data.attr.containsKey(GpxConstants.META_NAME)) {
|
---|
235 | info.append(tr("Name: {0}", data.get(GpxConstants.META_NAME))).append("<br>");
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (data.attr.containsKey(GpxConstants.META_DESC)) {
|
---|
239 | info.append(tr("Description: {0}", data.get(GpxConstants.META_DESC))).append("<br>");
|
---|
240 | }
|
---|
241 |
|
---|
242 | info.append(trn("{0} track, ", "{0} tracks, ", data.tracks.size(), data.tracks.size()))
|
---|
243 | .append(trn("{0} route, ", "{0} routes, ", data.routes.size(), data.routes.size()))
|
---|
244 | .append(trn("{0} waypoint", "{0} waypoints", data.waypoints.size(), data.waypoints.size())).append("<br>")
|
---|
245 | .append(tr("Length: {0}", SystemOfMeasurement.getSystemOfMeasurement().getDistText(data.length())))
|
---|
246 | .append("<br></html>");
|
---|
247 | return info.toString();
|
---|
248 | }
|
---|
249 |
|
---|
250 | @Override
|
---|
251 | public boolean isMergable(Layer other) {
|
---|
252 | return other instanceof GpxLayer;
|
---|
253 | }
|
---|
254 |
|
---|
255 | private int sumUpdateCount() {
|
---|
256 | int updateCount = 0;
|
---|
257 | for (GpxTrack track: data.tracks) {
|
---|
258 | updateCount += track.getUpdateCount();
|
---|
259 | }
|
---|
260 | return updateCount;
|
---|
261 | }
|
---|
262 |
|
---|
263 | @Override
|
---|
264 | public boolean isChanged() {
|
---|
265 | if (data.tracks.equals(lastTracks))
|
---|
266 | return sumUpdateCount() != lastUpdateCount;
|
---|
267 | else
|
---|
268 | return true;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Shows/hides all tracks of a given date range by setting them to visible/invisible.
|
---|
273 | * @param fromDate The min date
|
---|
274 | * @param toDate The max date
|
---|
275 | * @param showWithoutDate Include tracks that don't have any date set..
|
---|
276 | */
|
---|
277 | public void filterTracksByDate(Date fromDate, Date toDate, boolean showWithoutDate) {
|
---|
278 | int i = 0;
|
---|
279 | long from = fromDate.getTime();
|
---|
280 | long to = toDate.getTime();
|
---|
281 | for (GpxTrack trk : data.tracks) {
|
---|
282 | Date[] t = GpxData.getMinMaxTimeForTrack(trk);
|
---|
283 |
|
---|
284 | if (t == null) continue;
|
---|
285 | long tm = t[1].getTime();
|
---|
286 | trackVisibility[i] = (tm == 0 && showWithoutDate) || (from <= tm && tm <= to);
|
---|
287 | i++;
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | @Override
|
---|
292 | public void mergeFrom(Layer from) {
|
---|
293 | if (!(from instanceof GpxLayer))
|
---|
294 | throw new IllegalArgumentException("not a GpxLayer: " + from);
|
---|
295 | data.mergeFrom(((GpxLayer) from).data);
|
---|
296 | drawHelper.dataChanged();
|
---|
297 | }
|
---|
298 |
|
---|
299 | @Override
|
---|
300 | public void paint(Graphics2D g, MapView mv, Bounds box) {
|
---|
301 | lastUpdateCount = sumUpdateCount();
|
---|
302 | lastTracks.clear();
|
---|
303 | lastTracks.addAll(data.tracks);
|
---|
304 |
|
---|
305 | List<WayPoint> visibleSegments = listVisibleSegments(box);
|
---|
306 | if (!visibleSegments.isEmpty()) {
|
---|
307 | drawHelper.readPreferences(getName());
|
---|
308 | drawHelper.drawAll(g, mv, visibleSegments);
|
---|
309 | if (Main.getLayerManager().getActiveLayer() == this) {
|
---|
310 | drawHelper.drawColorBar(g, mv);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | private List<WayPoint> listVisibleSegments(Bounds box) {
|
---|
316 | WayPoint last = null;
|
---|
317 | LinkedList<WayPoint> visibleSegments = new LinkedList<>();
|
---|
318 |
|
---|
319 | ensureTrackVisibilityLength();
|
---|
320 | for (Collection<WayPoint> segment : data.getLinesIterable(trackVisibility)) {
|
---|
321 |
|
---|
322 | for (WayPoint pt : segment) {
|
---|
323 | Bounds b = new Bounds(pt.getCoor());
|
---|
324 | if (pt.drawLine && last != null) {
|
---|
325 | b.extend(last.getCoor());
|
---|
326 | }
|
---|
327 | if (b.intersects(box)) {
|
---|
328 | if (last != null && (visibleSegments.isEmpty()
|
---|
329 | || visibleSegments.getLast() != last)) {
|
---|
330 | if (last.drawLine) {
|
---|
331 | WayPoint l = new WayPoint(last);
|
---|
332 | l.drawLine = false;
|
---|
333 | visibleSegments.add(l);
|
---|
334 | } else {
|
---|
335 | visibleSegments.add(last);
|
---|
336 | }
|
---|
337 | }
|
---|
338 | visibleSegments.add(pt);
|
---|
339 | }
|
---|
340 | last = pt;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | return visibleSegments;
|
---|
344 | }
|
---|
345 |
|
---|
346 | @Override
|
---|
347 | public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
348 | v.visit(data.recalculateBounds());
|
---|
349 | }
|
---|
350 |
|
---|
351 | @Override
|
---|
352 | public File getAssociatedFile() {
|
---|
353 | return data.storageFile;
|
---|
354 | }
|
---|
355 |
|
---|
356 | @Override
|
---|
357 | public void setAssociatedFile(File file) {
|
---|
358 | data.storageFile = file;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** ensures the trackVisibility array has the correct length without losing data.
|
---|
362 | * additional entries are initialized to true;
|
---|
363 | */
|
---|
364 | private void ensureTrackVisibilityLength() {
|
---|
365 | final int l = data.tracks.size();
|
---|
366 | if (l == trackVisibility.length)
|
---|
367 | return;
|
---|
368 | final int m = Math.min(l, trackVisibility.length);
|
---|
369 | trackVisibility = Arrays.copyOf(trackVisibility, l);
|
---|
370 | for (int i = m; i < l; i++) {
|
---|
371 | trackVisibility[i] = true;
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | @Override
|
---|
376 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
377 | if (newValue == null) return;
|
---|
378 | data.resetEastNorthCache();
|
---|
379 | }
|
---|
380 |
|
---|
381 | @Override
|
---|
382 | public boolean isSavable() {
|
---|
383 | return true; // With GpxExporter
|
---|
384 | }
|
---|
385 |
|
---|
386 | @Override
|
---|
387 | public boolean checkSaveConditions() {
|
---|
388 | return data != null;
|
---|
389 | }
|
---|
390 |
|
---|
391 | @Override
|
---|
392 | public File createAndOpenSaveFileChooser() {
|
---|
393 | return SaveActionBase.createAndOpenSaveFileChooser(tr("Save GPX file"), GpxImporter.getFileFilter());
|
---|
394 | }
|
---|
395 |
|
---|
396 | @Override
|
---|
397 | public LayerPositionStrategy getDefaultLayerPosition() {
|
---|
398 | return LayerPositionStrategy.AFTER_LAST_DATA_LAYER;
|
---|
399 | }
|
---|
400 |
|
---|
401 | @Override
|
---|
402 | public synchronized void destroy() {
|
---|
403 | super.destroy();
|
---|
404 | SystemOfMeasurement.removeSoMChangeListener(drawHelper);
|
---|
405 | }
|
---|
406 | }
|
---|