source: josm/trunk/src/org/openstreetmap/josm/actions/UpdateDataAction.java

Last change on this file was 16770, checked in by GerdP, 4 years ago

fix #19451: Update data: Do not download incomplete ways

  • if no download areas exist download again all objects with are neither new nor incomplete
  • don't stop post processing of downloaded data if one or more download areas are empty
  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.awt.geom.Area;
10import java.util.List;
11import java.util.concurrent.Future;
12import java.util.stream.Collectors;
13
14import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList;
15import org.openstreetmap.josm.gui.layer.OsmDataLayer;
16import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
17import org.openstreetmap.josm.io.NetworkManager;
18import org.openstreetmap.josm.io.OnlineResource;
19import org.openstreetmap.josm.spi.preferences.Config;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 * This action synchronizes the dataset with the current state on the server.
24 *
25 * It does so by re-downloading all areas and thereby merging all compatible
26 * changes from the current server version.
27 */
28public class UpdateDataAction extends JosmAction {
29
30 /**
31 * Constructs a new {@code UpdateDataAction}.
32 */
33 public UpdateDataAction() {
34 super(tr("Update data"),
35 "updatedata",
36 tr("Updates the objects in the active data layer from the server."),
37 Shortcut.registerShortcut("file:updatedata",
38 tr("File: {0}", tr("Update data")),
39 KeyEvent.VK_U, Shortcut.CTRL),
40 true);
41 setHelpId(ht("/Action/UpdateData"));
42 }
43
44 @Override
45 protected boolean listenToSelectionChange() {
46 return false;
47 }
48
49 /**
50 * Refreshes the enabled state
51 */
52 @Override
53 protected void updateEnabledState() {
54 OsmDataLayer editLayer = getLayerManager().getEditLayer();
55 setEnabled(editLayer != null && editLayer.isDownloadable() && !NetworkManager.isOffline(OnlineResource.OSM_API));
56 }
57
58 @Override
59 public void actionPerformed(ActionEvent e) {
60 OsmDataLayer editLayer = getLayerManager().getEditLayer();
61 if (!isEnabled() || editLayer == null || !editLayer.isDownloadable())
62 return;
63
64 List<Area> areas = editLayer.data.getDataSources().stream()
65 .map(ds -> new Area(ds.bounds.asRect()))
66 .collect(Collectors.toList());
67
68 // The next two blocks removes every intersection from every DataSource Area
69 // This prevents downloading the same data numerous times at intersections
70 // and also skips smaller bounding boxes that are contained within larger ones entirely.
71 for (int i = 0; i < areas.size(); i++) {
72 for (int j = i+1; j < areas.size(); j++) {
73 areas.get(i).subtract(areas.get(j));
74 }
75 }
76
77 for (int i = areas.size()-1; i > 0; i--) {
78 for (int j = i-1; j > 0; j--) {
79 areas.get(i).subtract(areas.get(j));
80 }
81 }
82
83 List<Area> areasToDownload = areas.stream()
84 .filter(a -> !a.isEmpty())
85 .collect(Collectors.toList());
86
87 if (areasToDownload.isEmpty()) {
88 // no bounds defined in the dataset? We update all but the incomplete primitives in the data set
89 UpdateSelectionAction.updatePrimitives(editLayer.data.allPrimitives().stream()
90 .filter(p -> !p.isNew() && !p.isIncomplete()).collect(Collectors.toList()));
91 } else {
92 // bounds defined? => use the bbox downloader
93 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data"));
94 final Future<?> future = new DownloadTaskList(Config.getPref().getBoolean("update.data.zoom-after-download"))
95 .download(false /* no new layer */, areasToDownload, true, false, monitor);
96 waitFuture(future, monitor);
97 }
98 }
99}
Note: See TracBrowser for help on using the repository browser.