001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.layer.gpx; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.geom.Path2D; 007 008import org.openstreetmap.josm.actions.DownloadAlongAction; 009import org.openstreetmap.josm.data.gpx.GpxData; 010import org.openstreetmap.josm.data.gpx.IGpxTrack; 011import org.openstreetmap.josm.data.gpx.IGpxTrackSegment; 012import org.openstreetmap.josm.data.gpx.WayPoint; 013import org.openstreetmap.josm.gui.PleaseWaitRunnable; 014import org.openstreetmap.josm.gui.help.HelpUtil; 015 016/** 017 * Action that issues a series of download requests to the API, following the GPX track. 018 * 019 * @author fred 020 * @since 5715 021 */ 022public class DownloadAlongTrackAction extends DownloadAlongAction { 023 024 private static final int NEAR_TRACK = 0; 025 private static final int NEAR_WAYPOINTS = 1; 026 private static final int NEAR_BOTH = 2; 027 028 private static final String PREF_DOWNLOAD_ALONG_TRACK_OSM = "downloadAlongTrack.download.osm"; 029 private static final String PREF_DOWNLOAD_ALONG_TRACK_GPS = "downloadAlongTrack.download.gps"; 030 031 private static final String PREF_DOWNLOAD_ALONG_TRACK_DISTANCE = "downloadAlongTrack.distance"; 032 private static final String PREF_DOWNLOAD_ALONG_TRACK_AREA = "downloadAlongTrack.area"; 033 private static final String PREF_DOWNLOAD_ALONG_TRACK_NEAR = "downloadAlongTrack.near"; 034 035 private final transient GpxData data; 036 037 /** 038 * Constructs a new {@code DownloadAlongTrackAction} 039 * @param data The GPX data used to download along 040 */ 041 public DownloadAlongTrackAction(GpxData data) { 042 super(tr("Download from OSM along this track"), "downloadalongtrack", null, null, false); 043 this.data = data; 044 } 045 046 @Override 047 protected PleaseWaitRunnable createTask() { 048 final DownloadAlongPanel panel = new DownloadAlongPanel( 049 PREF_DOWNLOAD_ALONG_TRACK_OSM, PREF_DOWNLOAD_ALONG_TRACK_GPS, 050 PREF_DOWNLOAD_ALONG_TRACK_DISTANCE, PREF_DOWNLOAD_ALONG_TRACK_AREA, PREF_DOWNLOAD_ALONG_TRACK_NEAR); 051 052 int ret = panel.showInDownloadDialog(tr("Download from OSM along this track"), HelpUtil.ht("/Action/DownloadAlongTrack")); 053 if (0 != ret && 1 != ret) { 054 return null; 055 } 056 057 final int near = panel.getNear(); 058 059 // Convert the GPX data into a Path2D. 060 Path2D gpxPath = new Path2D.Double(); 061 if (near == NEAR_TRACK || near == NEAR_BOTH) { 062 for (IGpxTrack trk : data.tracks) { 063 for (IGpxTrackSegment segment : trk.getSegments()) { 064 boolean first = true; 065 for (WayPoint p : segment.getWayPoints()) { 066 if (first) { 067 gpxPath.moveTo(p.lon(), p.lat()); 068 first = false; 069 } else { 070 gpxPath.lineTo(p.lon(), p.lat()); 071 } 072 } 073 } 074 } 075 } 076 if (near == NEAR_WAYPOINTS || near == NEAR_BOTH) { 077 for (WayPoint p : data.waypoints) { 078 gpxPath.moveTo(p.lon(), p.lat()); 079 gpxPath.closePath(); 080 } 081 } 082 return createCalcTask(gpxPath, panel, tr("Download from OSM along this track"), 1 == ret); 083 } 084}