| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.layer.gpx;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.Component;
|
|---|
| 7 | import java.awt.geom.Path2D;
|
|---|
| 8 | import java.util.Collection;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 | import java.util.stream.Collectors;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.Action;
|
|---|
| 13 | import javax.swing.JMenuItem;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.actions.DownloadAlongAction;
|
|---|
| 16 | import org.openstreetmap.josm.data.gpx.GpxData;
|
|---|
| 17 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
|---|
| 18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 19 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 21 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 22 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Action that issues a series of download requests to the API, following the GPX track.
|
|---|
| 26 | *
|
|---|
| 27 | * @author fred
|
|---|
| 28 | * @since 5715
|
|---|
| 29 | */
|
|---|
| 30 | public class DownloadAlongTrackAction extends DownloadAlongAction implements Layer.LayerAction, Layer.MultiLayerAction {
|
|---|
| 31 |
|
|---|
| 32 | private static final int NEAR_TRACK = 0;
|
|---|
| 33 | private static final int NEAR_WAYPOINTS = 1;
|
|---|
| 34 | private static final int NEAR_BOTH = 2;
|
|---|
| 35 |
|
|---|
| 36 | private static final String PREF_DOWNLOAD_ALONG_TRACK_OSM = "downloadAlongTrack.download.osm";
|
|---|
| 37 | private static final String PREF_DOWNLOAD_ALONG_TRACK_GPS = "downloadAlongTrack.download.gps";
|
|---|
| 38 |
|
|---|
| 39 | private static final String PREF_DOWNLOAD_ALONG_TRACK_DISTANCE = "downloadAlongTrack.distance";
|
|---|
| 40 | private static final String PREF_DOWNLOAD_ALONG_TRACK_AREA = "downloadAlongTrack.area";
|
|---|
| 41 | private static final String PREF_DOWNLOAD_ALONG_TRACK_NEAR = "downloadAlongTrack.near";
|
|---|
| 42 |
|
|---|
| 43 | private final transient Collection<GpxData> data;
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Constructs a new {@code DownloadAlongTrackAction}
|
|---|
| 47 | * @param data The GPX data used to download along
|
|---|
| 48 | */
|
|---|
| 49 | public DownloadAlongTrackAction(Collection<GpxData> data) {
|
|---|
| 50 | super(tr("Download from OSM along this track"), "downloadalongtrack", null, null, false);
|
|---|
| 51 | this.data = data;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | @Override
|
|---|
| 55 | protected PleaseWaitRunnable createTask() {
|
|---|
| 56 | final DownloadAlongPanel panel = new DownloadAlongPanel(
|
|---|
| 57 | PREF_DOWNLOAD_ALONG_TRACK_OSM, PREF_DOWNLOAD_ALONG_TRACK_GPS,
|
|---|
| 58 | PREF_DOWNLOAD_ALONG_TRACK_DISTANCE, PREF_DOWNLOAD_ALONG_TRACK_AREA, PREF_DOWNLOAD_ALONG_TRACK_NEAR);
|
|---|
| 59 |
|
|---|
| 60 | int ret = panel.showInDownloadDialog(tr("Download from OSM along this track"), HelpUtil.ht("/Action/DownloadAlongTrack"));
|
|---|
| 61 | if (0 != ret && 1 != ret) {
|
|---|
| 62 | return null;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | final int near = panel.getNear();
|
|---|
| 66 |
|
|---|
| 67 | // Convert the GPX data into a Path2D.
|
|---|
| 68 | Path2D gpxPath = new Path2D.Double();
|
|---|
| 69 | if (near == NEAR_TRACK || near == NEAR_BOTH) {
|
|---|
| 70 | data.stream().flatMap(GpxData::getTrackSegmentsStream).forEach(segment -> {
|
|---|
| 71 | boolean first = true;
|
|---|
| 72 | for (WayPoint p : segment.getWayPoints()) {
|
|---|
| 73 | if (first) {
|
|---|
| 74 | gpxPath.moveTo(p.lon(), p.lat());
|
|---|
| 75 | first = false;
|
|---|
| 76 | } else {
|
|---|
| 77 | gpxPath.lineTo(p.lon(), p.lat());
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | });
|
|---|
| 81 | }
|
|---|
| 82 | if (near == NEAR_WAYPOINTS || near == NEAR_BOTH) {
|
|---|
| 83 | data.stream().flatMap(d -> d.getWaypoints().stream()).forEach(p -> {
|
|---|
| 84 | gpxPath.moveTo(p.lon(), p.lat());
|
|---|
| 85 | gpxPath.closePath();
|
|---|
| 86 | });
|
|---|
| 87 | }
|
|---|
| 88 | return createCalcTask(gpxPath, panel, tr("Download from OSM along this track"), 1 == ret);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | @Override
|
|---|
| 92 | public Component createMenuComponent() {
|
|---|
| 93 | return new JMenuItem(this);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | @Override
|
|---|
| 97 | public boolean supportLayers(List<Layer> layers) {
|
|---|
| 98 | return !Utils.filteredCollection(layers, GpxLayer.class).isEmpty();
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | @Override
|
|---|
| 102 | public Action getMultiLayerAction(List<Layer> layers) {
|
|---|
| 103 | final List<GpxData> gpxData = Utils.filteredCollection(layers, GpxLayer.class)
|
|---|
| 104 | .stream().map(layer -> layer.data)
|
|---|
| 105 | .collect(Collectors.toList());
|
|---|
| 106 | return new DownloadAlongTrackAction(gpxData);
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|