Changeset 29746 in osm for applications
- Timestamp:
- 2013-07-06T21:17:54+02:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/download_along
- Files:
-
- 1 deleted
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/download_along/build.xml
r29745 r29746 4 4 <property name="commit.message" value="Changed the constructor signature of the plugin main class"/> 5 5 <!-- enter the *lowest* JOSM version this plugin is currently compatible with --> 6 <property name="plugin.main.version" value="605 3"/>6 <property name="plugin.main.version" value="6054"/> 7 7 8 8 <!-- Configure these properties (replace "..." accordingly). -
applications/editors/josm/plugins/download_along/src/org/openstreetmap/josm/plugin/download_along/DownloadAlong.java
r29745 r29746 7 7 8 8 public class DownloadAlong extends Plugin { 9 public static final String PREF_DOWNLOAD_ALONG_TRACK_DISTANCE = "downloadAlong.downloadAlongTrack.distance";10 public static final String PREF_DOWNLOAD_ALONG_TRACK_AREA = "downloadAlong.downloadAlongTrack.area";11 12 public static final String PREF_DOWNLOAD_ALONG_OSM = "downloadAlong.download.osm";13 public static final String PREF_DOWNLOAD_ALONG_GPS = "downloadAlong.download.gps";14 9 15 10 public DownloadAlong(PluginInformation info) { 16 11 super(info); 17 MainMenu.add(Main.main.menu.toolsMenu, new DownloadAlong Action());12 MainMenu.add(Main.main.menu.toolsMenu, new DownloadAlongWayAction()); 18 13 } 19 14 } -
applications/editors/josm/plugins/download_along/src/org/openstreetmap/josm/plugin/download_along/DownloadAlongWayAction.java
r29745 r29746 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.awt.GridBagLayout;6 5 import java.awt.event.ActionEvent; 7 6 import java.awt.event.KeyEvent; … … 10 9 import java.util.ArrayList; 11 10 import java.util.Collection; 12 import java.util.List; 13 import java.util.concurrent.Future; 11 import java.util.Set; 14 12 15 import javax.swing.JLabel;16 13 import javax.swing.JOptionPane; 17 import javax.swing.JPanel;18 import javax.swing.event.ChangeEvent;19 import javax.swing.event.ChangeListener;20 14 21 15 import org.openstreetmap.josm.Main; 22 import org.openstreetmap.josm.actions.JosmAction; 23 import org.openstreetmap.josm.actions.downloadtasks.DownloadTaskList; 16 import org.openstreetmap.josm.actions.DownloadAlongAction; 24 17 import org.openstreetmap.josm.data.coor.LatLon; 25 18 import org.openstreetmap.josm.data.osm.Node; 26 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 27 20 import org.openstreetmap.josm.data.osm.Way; 28 import org.openstreetmap.josm.gui.HelpAwareOptionPane;29 import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;30 21 import org.openstreetmap.josm.gui.help.HelpUtil; 31 import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor; 32 import org.openstreetmap.josm.tools.GBC; 33 import org.openstreetmap.josm.tools.ImageProvider; 22 import org.openstreetmap.josm.gui.layer.gpx.DownloadAlongPanel; 23 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 34 24 import org.openstreetmap.josm.tools.Shortcut; 35 25 import org.openstreetmap.josm.tools.Utils; 36 26 37 class DownloadAlong Action extends JosmAction {27 class DownloadAlongWayAction extends DownloadAlongAction { 38 28 39 public DownloadAlongAction() { 29 private static final String PREF_DOWNLOAD_ALONG_WAY_DISTANCE = "downloadAlongWay.distance"; 30 private static final String PREF_DOWNLOAD_ALONG_WAY_AREA = "downloadAlongWay.area"; 31 32 private static final String PREF_DOWNLOAD_ALONG_WAY_OSM = "downloadAlongWay.download.osm"; 33 private static final String PREF_DOWNLOAD_ALONG_WAY_GPS = "downloadAlongWay.download.gps"; 34 35 public DownloadAlongWayAction() { 40 36 super(tr("Download along..."), "download_along", tr("Download OSM data along the selected ways."), 41 37 Shortcut.registerShortcut("tools:download_along", tr("Tool: {0}", tr("Download Along")), … … 44 40 45 41 public void actionPerformed(ActionEvent e) { 46 Collection<OsmPrimitive> selection = Main.main.getCurrentDataSet().getSelected();42 Set<Way> selectedWays = OsmPrimitive.getFilteredSet(Main.main.getCurrentDataSet().getSelected(), Way.class); 47 43 48 int ways = 0; 49 for (OsmPrimitive prim : selection) { 50 if (prim instanceof Way) 51 ways++; 52 } 53 54 if (ways < 1) { 44 if (selectedWays.isEmpty()) { 55 45 JOptionPane.showMessageDialog(Main.parent, tr("Please select 1 or more ways to download along")); 56 46 return; 57 47 } 58 48 59 final DownloadPanel panel = new DownloadPanel(); 60 61 final ButtonSpec[] options = new ButtonSpec[] { 62 new ButtonSpec( 63 tr("Download"), 64 ImageProvider.get("download"), 65 tr("Click to download"), 66 null // no specific help text 67 ), 68 new ButtonSpec( 69 tr("Cancel"), 70 ImageProvider.get("cancel"), 71 tr("Click to cancel"), 72 null // no specific help text 73 ) 74 }; 75 76 panel.addChangeListener(new ChangeListener() { 77 @Override public void stateChanged(ChangeEvent e) { 78 options[0].setEnabled(panel.isDownloadOsmData() || panel.isDownloadGpxData()); 79 } 80 }); 49 final DownloadAlongPanel panel = new DownloadAlongPanel( 50 PREF_DOWNLOAD_ALONG_WAY_OSM, PREF_DOWNLOAD_ALONG_WAY_GPS, 51 PREF_DOWNLOAD_ALONG_WAY_DISTANCE, PREF_DOWNLOAD_ALONG_WAY_AREA, null); 81 52 82 if (0 != HelpAwareOptionPane.showOptionDialog(Main.parent, panel, tr("Download from OSM along this track"), 83 JOptionPane.QUESTION_MESSAGE, null, options, options[0], HelpUtil.ht("/Tools/DownloadAlong"))) { 53 if (0 != panel.showInDownloadDialog(tr("Download from OSM along selected ways"), HelpUtil.ht("/Tools/DownloadAlong"))) { 84 54 return; 85 55 } 86 87 panel.rememberSettings();88 56 89 57 /* … … 94 62 int latcnt = 0; 95 63 96 for (OsmPrimitive prim : selection) { 97 if (prim instanceof Way) { 98 Way way = (Way) prim; 99 for (Node n : way.getNodes()) { 100 latsum += n.getCoor().lat(); 101 latcnt++; 102 } 64 for (Way way : selectedWays) { 65 for (Node n : way.getNodes()) { 66 latsum += n.getCoor().lat(); 67 latcnt++; 103 68 } 104 69 } … … 128 93 */ 129 94 LatLon previous = null; 130 for (OsmPrimitive prim : selection) { 131 if (prim instanceof Way) { 132 Way way = (Way) prim; 133 for (Node p : way.getNodes()) { 134 LatLon c = p.getCoor(); 135 ArrayList<LatLon> intermediateNodes = new ArrayList<LatLon>(); 136 if (previous != null && c.greatCircleDistance(previous) > buffer_dist) { 137 Double d = c.greatCircleDistance(previous) / buffer_dist; 138 int nbNodes = d.intValue(); 139 System.out.println(tr("{0} intermediate nodes to download.", nbNodes)); 140 System.out.println(tr("between {0} {1} and {2} {3}", c.lat(), c.lon(), previous.lat(), 141 previous.lon())); 142 for (int i = 1; i < nbNodes; i++) { 143 intermediateNodes.add(new LatLon(previous.lat() 144 + (i * (c.lat() - previous.lat()) / (nbNodes + 1)), previous.lon() 145 + (i * (c.lon() - previous.lon()) / (nbNodes + 1)))); 146 System.out.println(tr(" adding {0} {1}", previous.lat() 147 + (i * (c.lat() - previous.lat()) / (nbNodes + 1)), previous.lon() 148 + (i * (c.lon() - previous.lon()) / (nbNodes + 1)))); 149 } 95 for (Way way : selectedWays) { 96 for (Node p : way.getNodes()) { 97 LatLon c = p.getCoor(); 98 ArrayList<LatLon> intermediateNodes = new ArrayList<LatLon>(); 99 if (previous != null && c.greatCircleDistance(previous) > buffer_dist) { 100 Double d = c.greatCircleDistance(previous) / buffer_dist; 101 int nbNodes = d.intValue(); 102 System.out.println(tr("{0} intermediate nodes to download.", nbNodes)); 103 System.out.println(tr("between {0} {1} and {2} {3}", c.lat(), c.lon(), previous.lat(), 104 previous.lon())); 105 for (int i = 1; i < nbNodes; i++) { 106 intermediateNodes.add(new LatLon(previous.lat() 107 + (i * (c.lat() - previous.lat()) / (nbNodes + 1)), previous.lon() 108 + (i * (c.lon() - previous.lon()) / (nbNodes + 1)))); 109 System.out.println(tr(" adding {0} {1}", previous.lat() 110 + (i * (c.lat() - previous.lat()) / (nbNodes + 1)), previous.lon() 111 + (i * (c.lon() - previous.lon()) / (nbNodes + 1)))); 150 112 } 151 intermediateNodes.add(c);152 for (LatLon d : intermediateNodes) {153 if (previous == null || d.greatCircleDistance(previous) > buffer_dist) {154 // we add a buffer around the point.155 r.setRect(d.lon() - buffer_x, d.lat() - buffer_y, 2 * buffer_x, 2 * buffer_y);156 a.add(new Area(r));157 previous = d;158 }113 } 114 intermediateNodes.add(c); 115 for (LatLon d : intermediateNodes) { 116 if (previous == null || d.greatCircleDistance(previous) > buffer_dist) { 117 // we add a buffer around the point. 118 r.setRect(d.lon() - buffer_x, d.lat() - buffer_y, 2 * buffer_x, 2 * buffer_y); 119 a.add(new Area(r)); 120 previous = d; 159 121 } 160 previous = c;161 122 } 123 previous = c; 162 124 } 163 125 } 164 165 /* 166 * Area "a" now contains the hull that we would like to download 167 * data for. however we can only download rectangles, so the 168 * following is an attempt at finding a number of rectangles to 169 * download. 170 * 171 * The idea is simply: Start out with the full bounding box. If it 172 * is too large, then split it in half and repeat recursively for 173 * each half until you arrive at something small enough to download. 174 * The algorithm is improved by always using the intersection 175 * between the rectangle and the actual desired area. For example, 176 * if you have a track that goes like this: +----+ | /| | / | | / | 177 * |/ | +----+ then we would first look at downloading the whole 178 * rectangle (assume it's too big), after that we split it in half 179 * (upper and lower half), but we donot request the full upper and 180 * lower rectangle, only the part of the upper/lower rectangle that 181 * actually has something in it. 182 */ 183 184 List<Rectangle2D> toDownload = new ArrayList<Rectangle2D>(); 185 186 addToDownload(a, a.getBounds(), toDownload, max_area); 187 188 JPanel msg = new JPanel(new GridBagLayout()); 189 190 msg.add(new JLabel(tr("<html>This action will require {0} individual<br>" 191 + "download requests. Do you wish<br>to continue?</html>", toDownload.size())), GBC.eol()); 192 193 if (toDownload.size() > 1) { 194 if (JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(Main.parent, msg, tr("Download from OSM along this track"), 195 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) { 196 return; 197 } 198 } 199 final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(tr("Download data")); 200 final Future<?> future = new DownloadTaskList().download(false, toDownload, panel.isDownloadOsmData(), panel.isDownloadGpxData(), monitor); 201 Main.worker.submit(new Runnable() { 202 public void run() { 203 try { 204 future.get(); 205 } catch (Exception e) { 206 e.printStackTrace(); 207 return; 208 } 209 monitor.close(); 210 } 211 }); 212 } 213 214 private static void addToDownload(Area a, Rectangle2D r, Collection<Rectangle2D> results, double max_area) { 215 Area tmp = new Area(r); 216 // intersect with sought-after area 217 tmp.intersect(a); 218 if (tmp.isEmpty()) 219 return; 220 Rectangle2D bounds = tmp.getBounds2D(); 221 if (bounds.getWidth() * bounds.getHeight() > max_area) { 222 // the rectangle gets too large; split it and make recursive 223 // call. 224 Rectangle2D r1; 225 Rectangle2D r2; 226 if (bounds.getWidth() > bounds.getHeight()) { 227 // rectangles that are wider than high are split into a left 228 // and right 229 // half, 230 r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth() / 2, bounds.getHeight()); 231 r2 = new Rectangle2D.Double(bounds.getX() + bounds.getWidth() / 2, bounds.getY(), 232 bounds.getWidth() / 2, bounds.getHeight()); 233 } else { 234 // others into a top and bottom half. 235 r1 = new Rectangle2D.Double(bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() / 2); 236 r2 = new Rectangle2D.Double(bounds.getX(), bounds.getY() + bounds.getHeight() / 2, 237 bounds.getWidth(), bounds.getHeight() / 2); 238 } 239 addToDownload(a, r1, results, max_area); 240 addToDownload(a, r2, results, max_area); 241 } else { 242 results.add(bounds); 243 } 126 127 confirmAndDownloadAreas(a, max_area, panel.isDownloadOsmData(), panel.isDownloadGpxData(), 128 tr("Download from OSM along selected ways"), NullProgressMonitor.INSTANCE); 244 129 } 245 130
Note:
See TracChangeset
for help on using the changeset viewer.