001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GraphicsEnvironment;
007import java.awt.GridBagLayout;
008import java.awt.event.ActionEvent;
009import java.awt.geom.Area;
010import java.awt.geom.Path2D;
011import java.awt.geom.PathIterator;
012import java.awt.geom.Rectangle2D;
013import java.util.ArrayList;
014import java.util.Collection;
015import java.util.List;
016import java.util.concurrent.Future;
017
018import javax.swing.JLabel;
019import javax.swing.JOptionPane;
020import javax.swing.JPanel;
021
022import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
023import org.openstreetmap.josm.data.coor.LatLon;
024import org.openstreetmap.josm.gui.MainApplication;
025import org.openstreetmap.josm.gui.PleaseWaitRunnable;
026import org.openstreetmap.josm.gui.layer.gpx.DownloadAlongPanel;
027import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
028import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
029import org.openstreetmap.josm.spi.preferences.Config;
030import org.openstreetmap.josm.tools.GBC;
031import org.openstreetmap.josm.tools.Logging;
032import org.openstreetmap.josm.tools.Shortcut;
033import org.openstreetmap.josm.tools.Utils;
034
035/**
036 * Abstract superclass of DownloadAlongTrackAction and DownloadAlongWayAction
037 * @since 6054
038 */
039public abstract class DownloadAlongAction extends JosmAction {
040
041    /**
042     * Sub classes must override this method.
043     * @return the task to start or null if nothing to do
044     */
045    protected abstract PleaseWaitRunnable createTask();
046
047    /**
048     * Constructs a new {@code DownloadAlongAction}
049     * @param name the action's text as displayed in the menu
050     * @param iconName the filename of the icon to use
051     * @param tooltip  a longer description of the action that will be displayed in the tooltip. Please note
052     *           that html is not supported for menu actions on some platforms.
053     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
054     *            do want a shortcut, remember you can always register it with group=none, so you
055     *            won't be assigned a shortcut unless the user configures one. If you pass null here,
056     *            the user CANNOT configure a shortcut for your action.
057     * @param registerInToolbar register this action for the toolbar preferences?
058     */
059    public DownloadAlongAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
060        super(name, iconName, tooltip, shortcut, registerInToolbar);
061    }
062
063    protected static void addToDownload(Area a, Rectangle2D r, Collection<Rectangle2D> results, double maxArea) {
064        Area tmp = new Area(r);
065        // intersect with sought-after area
066        tmp.intersect(a);
067        if (tmp.isEmpty()) {
068            return;
069        }
070        Rectangle2D bounds = tmp.getBounds2D();
071        if (bounds.getWidth() * bounds.getHeight() > maxArea) {
072            // the rectangle gets too large; split it and make recursive call.
073            Rectangle2D r1;
074            Rectangle2D r2;
075            if (bounds.getWidth() > bounds.getHeight()) {
076                // rectangles that are wider than high are split into a left and right half,
077                r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth() / 2, bounds.getHeight());
078                r2 = new Rectangle2D.Double(bounds.getX() + bounds.getWidth() / 2, bounds.getY(),
079                        bounds.getWidth() / 2, bounds.getHeight());
080            } else {
081                // others into a top and bottom half.
082                r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() / 2);
083                r2 = new Rectangle2D.Double(bounds.getX(), bounds.getY() + bounds.getHeight() / 2, bounds.getWidth(),
084                        bounds.getHeight() / 2);
085            }
086            addToDownload(tmp, r1, results, maxArea);
087            addToDownload(tmp, r2, results, maxArea);
088        } else {
089            results.add(bounds);
090        }
091    }
092
093    /**
094     * Area "a" contains the hull that we would like to download data for. however we
095     * can only download rectangles, so the following is an attempt at finding a number of
096     * rectangles to download.
097     *
098     * The idea is simply: Start out with the full bounding box. If it is too large, then
099     * split it in half and repeat recursively for each half until you arrive at something
100     * small enough to download. The algorithm is improved by always using the intersection
101     * between the rectangle and the actual desired area. For example, if you have a track
102     * that goes like this: +----+ | /| | / | | / | |/ | +----+ then we would first look at
103     * downloading the whole rectangle (assume it's too big), after that we split it in half
104     * (upper and lower half), but we donot request the full upper and lower rectangle, only
105     * the part of the upper/lower rectangle that actually has something in it.
106     *
107     * This functions calculates the rectangles, asks the user to continue and downloads
108     * the areas if applicable.
109     *
110     * @param a download area hull
111     * @param maxArea maximum area size for a single download
112     * @param osmDownload Set to true if OSM data should be downloaded
113     * @param gpxDownload Set to true if GPX data should be downloaded
114     * @param title the title string for the confirmation dialog
115     */
116    protected static void confirmAndDownloadAreas(Area a, double maxArea, boolean osmDownload, boolean gpxDownload, String title) {
117        List<Rectangle2D> toDownload = new ArrayList<>();
118        addToDownload(a, a.getBounds(), toDownload, maxArea);
119        if (toDownload.isEmpty()) {
120            return;
121        }
122        JPanel msg = new JPanel(new GridBagLayout());
123        msg.add(new JLabel(
124                tr("<html>This action will require {0} individual<br>" + "download requests. Do you wish<br>to continue?</html>",
125                        toDownload.size())), GBC.eol());
126        if (!GraphicsEnvironment.isHeadless() && JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(
127                MainApplication.getMainFrame(), msg, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
128            return;
129        }
130        final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
131        final Future<?> future = new DownloadTaskList(Config.getPref().getBoolean("download.along.zoom-after-download"))
132                .download(false, toDownload, osmDownload, gpxDownload, monitor);
133        waitFuture(future, monitor);
134    }
135
136    /**
137     * Calculate list of points between two given points so that the distance between two consecutive points is below a limit.
138     * @param p1 first point or null
139     * @param p2 second point (must not be null)
140     * @param bufferDist the maximum distance
141     * @return a list of points with at least one point (p2) and maybe more.
142     */
143    protected static Collection<LatLon> calcBetweenPoints(LatLon p1, LatLon p2, double bufferDist) {
144        ArrayList<LatLon> intermediateNodes = new ArrayList<>();
145        intermediateNodes.add(p2);
146        if (p1 != null && p2.greatCircleDistance(p1) > bufferDist) {
147            Double d = p2.greatCircleDistance(p1) / bufferDist;
148            int nbNodes = d.intValue();
149            if (Logging.isDebugEnabled()) {
150                Logging.debug(tr("{0} intermediate nodes to download.", nbNodes));
151                Logging.debug(tr("between {0} {1} and {2} {3}", p2.lat(), p2.lon(), p1.lat(), p1.lon()));
152            }
153            double latStep = (p2.lat() - p1.lat()) / (nbNodes + 1);
154            double lonStep = (p2.lon() - p1.lon()) / (nbNodes + 1);
155            for (int i = 1; i <= nbNodes; i++) {
156                LatLon intermediate = new LatLon(p1.lat() + i * latStep, p1.lon() + i * lonStep);
157                intermediateNodes.add(intermediate);
158                if (Logging.isTraceEnabled()) {
159                    Logging.trace(tr("  adding {0} {1}", intermediate.lat(), intermediate.lon()));
160                }
161            }
162        }
163        return intermediateNodes;
164    }
165
166    /**
167     * Create task that downloads areas along the given path using the values specified in the panel.
168     * @param alongPath the path along which the areas are to be downloaded
169     * @param panel the panel that was displayed to the user and now contains his selections
170     * @param confirmTitle the title to display in the confirmation panel
171     * @return the task or null if canceled by user
172     */
173    protected PleaseWaitRunnable createCalcTask(Path2D alongPath, DownloadAlongPanel panel, String confirmTitle) {
174        /*
175         * Find the average latitude for the data we're contemplating, so we can know how many
176         * metres per degree of longitude we have.
177         */
178        double latsum = 0;
179        int latcnt = 0;
180        final PathIterator pit = alongPath.getPathIterator(null);
181        final double[] res = new double[6];
182        while (!pit.isDone()) {
183            int type = pit.currentSegment(res);
184            if (type == PathIterator.SEG_LINETO || type == PathIterator.SEG_MOVETO) {
185                latsum += res[1];
186                latcnt++;
187            }
188            pit.next();
189        }
190        if (latcnt == 0) {
191            return null;
192        }
193        final double avglat = latsum / latcnt;
194        final double scale = Math.cos(Utils.toRadians(avglat));
195
196        /*
197         * Compute buffer zone extents and maximum bounding box size. Note that the maximum we
198         * ever offer is a bbox area of 0.002, while the API theoretically supports 0.25, but as
199         * soon as you touch any built-up area, that kind of bounding box will download forever
200         * and then stop because it has more than 50k nodes.
201         */
202        final double bufferDist = panel.getDistance();
203        final double maxArea = panel.getArea() / 10000.0 / scale;
204        final double bufferY = bufferDist / 100000.0;
205        final double bufferX = bufferY / scale;
206        final int totalTicks = latcnt;
207        // guess if a progress bar might be useful.
208        final boolean displayProgress = totalTicks > 20_000 && bufferY < 0.01;
209
210        class CalculateDownloadArea extends PleaseWaitRunnable {
211
212            private final Path2D downloadPath = new Path2D.Double();
213            private boolean cancel;
214            private int ticks;
215            private final Rectangle2D r = new Rectangle2D.Double();
216
217            CalculateDownloadArea() {
218                super(tr("Calculating Download Area"), displayProgress ? null : NullProgressMonitor.INSTANCE, false);
219            }
220
221            @Override
222            protected void cancel() {
223                cancel = true;
224            }
225
226            @Override
227            protected void finish() {
228                // Do nothing
229            }
230
231            @Override
232            protected void afterFinish() {
233                if (cancel) {
234                    return;
235                }
236                confirmAndDownloadAreas(new Area(downloadPath), maxArea, panel.isDownloadOsmData(), panel.isDownloadGpxData(),
237                        confirmTitle);
238            }
239
240            /**
241             * increase tick count by one, report progress every 100 ticks
242             */
243            private void tick() {
244                ticks++;
245                if (ticks % 100 == 0) {
246                    progressMonitor.worked(100);
247                }
248            }
249
250            /**
251             * calculate area enclosing a single point
252             */
253            private void calcAreaForWayPoint(LatLon c) {
254                r.setRect(c.lon() - bufferX, c.lat() - bufferY, 2 * bufferX, 2 * bufferY);
255                downloadPath.append(r, false);
256            }
257
258            @Override
259            protected void realRun() {
260                progressMonitor.setTicksCount(totalTicks);
261                PathIterator pit = alongPath.getPathIterator(null);
262                double[] res = new double[6];
263                LatLon previous = null;
264                while (!pit.isDone()) {
265                    int type = pit.currentSegment(res);
266                    LatLon c = new LatLon(res[1], res[0]);
267                    if (type == PathIterator.SEG_LINETO) {
268                        tick();
269                        for (LatLon d : calcBetweenPoints(previous, c, bufferDist)) {
270                            calcAreaForWayPoint(d);
271                        }
272                        previous = c;
273                    } else if (type == PathIterator.SEG_MOVETO) {
274                        previous = c;
275                        tick();
276                        calcAreaForWayPoint(c);
277                    }
278                    pit.next();
279                }
280            }
281        }
282
283        return new CalculateDownloadArea();
284    }
285
286    @Override
287    public void actionPerformed(ActionEvent e) {
288        PleaseWaitRunnable task = createTask();
289        if (task != null) {
290            MainApplication.worker.submit(task);
291        }
292    }
293}