[15412] | 1 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 2 |
|
---|
| 3 | import java.awt.event.ActionEvent;
|
---|
| 4 | import java.awt.event.KeyEvent;
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 | import java.util.Collection;
|
---|
| 7 | import java.util.LinkedList;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.JOptionPane;
|
---|
| 10 |
|
---|
| 11 | import org.openstreetmap.josm.Main;
|
---|
| 12 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
[16590] | 13 | import org.openstreetmap.josm.actions.MergeNodesAction;
|
---|
[15412] | 14 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
| 15 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 19 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 20 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
[16590] | 21 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
[15412] | 22 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
| 23 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Plugin class for the Way Downloader plugin
|
---|
[16290] | 27 | *
|
---|
[15412] | 28 | * @author Harry Wood
|
---|
| 29 | */
|
---|
| 30 | public class WayDownloaderPlugin extends Plugin {
|
---|
| 31 |
|
---|
[16290] | 32 | private Way priorConnectedWay = null;
|
---|
| 33 | private Node selectedNode = null;
|
---|
[15412] | 34 |
|
---|
| 35 |
|
---|
[16290] | 36 | /** Plugin constructor called at JOSM startup */
|
---|
| 37 | public WayDownloaderPlugin() {
|
---|
| 38 | //add WayDownloadAction to tools menu
|
---|
| 39 | MainMenu.add(Main.main.menu.toolsMenu, new WayDownloadAction());
|
---|
| 40 | }
|
---|
[15412] | 41 |
|
---|
[16290] | 42 | private class WayDownloadAction extends JosmAction implements Runnable {
|
---|
[15412] | 43 |
|
---|
[16290] | 44 | /** Set up the action (text appearing on the menu, keyboard shortcut etc */
|
---|
| 45 | public WayDownloadAction() {
|
---|
[15412] | 46 |
|
---|
[16290] | 47 | super( "Way Download" ,
|
---|
| 48 | "way-download",
|
---|
| 49 | "Download map data on the end of selected way",
|
---|
| 50 | Shortcut.registerShortcut("waydownloader:waydownload", "Way Download", KeyEvent.VK_W, Shortcut.GROUP_MENU, Shortcut.SHIFT_DEFAULT),
|
---|
| 51 | true);
|
---|
| 52 | }
|
---|
[15412] | 53 |
|
---|
[16290] | 54 | /** Called when the WayDownloadAction action is triggered (e.g. user clicked the menu option) */
|
---|
| 55 | public void actionPerformed(ActionEvent e) {
|
---|
[15412] | 56 |
|
---|
[16290] | 57 | System.out.println("Way Download");
|
---|
[15412] | 58 |
|
---|
[16290] | 59 | String errMsg = null;
|
---|
[15412] | 60 |
|
---|
[16290] | 61 | selectedNode = null;
|
---|
[16631] | 62 | Collection<OsmPrimitive> selection = Main.main.getCurrentDataSet().getSelectedNodes();
|
---|
[15412] | 63 |
|
---|
[16290] | 64 | if (selection.size()==0) {
|
---|
[16631] | 65 | selection = Main.main.getCurrentDataSet().getSelectedWays();
|
---|
[16290] | 66 | if (!workFromWaySelection(selection)) {
|
---|
| 67 | errMsg = tr("Select a starting node on the end of a way");
|
---|
| 68 | }
|
---|
[16631] | 69 | selection = Main.main.getCurrentDataSet().getSelectedNodes();
|
---|
[16290] | 70 | }
|
---|
[15412] | 71 |
|
---|
[16290] | 72 | if ( selection.size()==0 || selection.size()>1 ) {
|
---|
| 73 | errMsg = tr("Select a starting node on the end of a way");
|
---|
| 74 | } else {
|
---|
| 75 | OsmPrimitive p = selection.iterator().next();
|
---|
[15412] | 76 |
|
---|
| 77 |
|
---|
| 78 |
|
---|
[16290] | 79 | if (!(p instanceof Node)) {
|
---|
| 80 | errMsg = tr("Select a starting node on the end of a way");
|
---|
| 81 | } else {
|
---|
| 82 | selectedNode = (Node) p;
|
---|
[15412] | 83 |
|
---|
| 84 |
|
---|
[16290] | 85 | Main.map.mapView.zoomTo(selectedNode.getEastNorth());
|
---|
[15412] | 86 |
|
---|
[16290] | 87 | //Before downloading. Figure a few things out.
|
---|
| 88 | //Find connected way
|
---|
| 89 | ArrayList<Way> connectedWays = findConnectedWays();
|
---|
[15412] | 90 |
|
---|
[16290] | 91 | if (connectedWays.size()==0) {
|
---|
| 92 | errMsg = tr("Select a starting node on the end of a way");
|
---|
| 93 | } else {
|
---|
[16590] | 94 | priorConnectedWay =connectedWays.get(0);
|
---|
[15412] | 95 |
|
---|
[16290] | 96 | //Download a little rectangle around the selected node
|
---|
| 97 | double latbuffer=0.0003; //TODO make this an option
|
---|
| 98 | double lonbuffer=0.0005;
|
---|
| 99 | DownloadOsmTask downloadTask = new DownloadOsmTask();
|
---|
| 100 | downloadTask.download( null,
|
---|
| 101 | selectedNode.getCoor().lat()-latbuffer,
|
---|
| 102 | selectedNode.getCoor().lon()-lonbuffer,
|
---|
| 103 | selectedNode.getCoor().lat()+latbuffer,
|
---|
[16590] | 104 | selectedNode.getCoor().lon()+lonbuffer,
|
---|
| 105 | new PleaseWaitProgressMonitor());
|
---|
[15412] | 106 |
|
---|
[16290] | 107 | //The download is scheduled to be executed.
|
---|
| 108 | //Now schedule the run() method (below) to be executed once that's completed.
|
---|
| 109 | Main.worker.execute(this);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if(errMsg != null)
|
---|
| 115 | JOptionPane.showMessageDialog(Main.parent, errMsg);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * Logic to excute after the download has happened
|
---|
| 120 | */
|
---|
| 121 | public void run() {
|
---|
| 122 | //Find ways connected to the node after the download
|
---|
| 123 | ArrayList<Way> connectedWays = findConnectedWays();
|
---|
| 124 |
|
---|
| 125 | String errMsg = null;
|
---|
| 126 | if (connectedWays.size()==0) {
|
---|
| 127 | throw new RuntimeException("Way downloader data inconsistency. priorConnectedWay (" +
|
---|
| 128 | priorConnectedWay.toString() + ") wasn't discovered after download");
|
---|
| 129 |
|
---|
| 130 | } else if (connectedWays.size()==1) {
|
---|
| 131 | //Just one way connecting the node still. Presumably the one which was there before
|
---|
| 132 |
|
---|
| 133 | //Check if it's just a duplicate node
|
---|
| 134 | Node dupeNode = duplicateNode();
|
---|
| 135 | if (dupeNode!=null) {
|
---|
| 136 |
|
---|
| 137 | if (JOptionPane.showConfirmDialog(null, "Merge duplicate node?")==JOptionPane.YES_OPTION) {
|
---|
| 138 | LinkedList<Node> dupeNodes = new LinkedList<Node>();
|
---|
| 139 | dupeNodes.add(dupeNode);
|
---|
[17654] | 140 | MergeNodesAction.mergeNodes(Main.main.getEditLayer(),
|
---|
| 141 | dupeNodes, selectedNode);
|
---|
[16290] | 142 |
|
---|
| 143 | connectedWays = findConnectedWays(); //Carry on
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | } else {
|
---|
| 148 | errMsg = tr("Reached the end of the line");
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | if (connectedWays.size()>2) {
|
---|
| 154 | //Three or more ways meeting at this node. Means we have a junction.
|
---|
| 155 | errMsg = tr("Reached a junction");
|
---|
| 156 |
|
---|
| 157 | } else if (connectedWays.size()==2) {
|
---|
| 158 | //Two connected ways (The "normal" way downloading case)
|
---|
| 159 | //Figure out which of the two is new.
|
---|
| 160 | System.out.println("connectedWays.toString()=" + connectedWays.toString());
|
---|
[16590] | 161 | Way wayA = connectedWays.get(0);
|
---|
| 162 | Way wayB = connectedWays.get(1);
|
---|
[16290] | 163 | Way nextWay = wayA;
|
---|
| 164 | if (priorConnectedWay.equals(wayA)) nextWay = wayB;
|
---|
| 165 |
|
---|
| 166 | Node nextNode = findOtherEnd(nextWay, selectedNode);
|
---|
| 167 |
|
---|
| 168 | //Select the next node
|
---|
[16631] | 169 | Main.main.getCurrentDataSet().setSelected(nextNode);
|
---|
[16290] | 170 |
|
---|
| 171 | Main.map.mapView.zoomTo(nextNode.getEastNorth());
|
---|
| 172 | }
|
---|
| 173 | if(errMsg != null)
|
---|
| 174 | JOptionPane.showMessageDialog(Main.parent, errMsg);
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /** See if there's another node at the same coordinates. If so return it. Otherwise null */
|
---|
| 179 | private Node duplicateNode() {
|
---|
[16963] | 180 | for (Node onNode:Main.main.getCurrentDataSet().nodes) {
|
---|
[16290] | 181 | if (!onNode.equals(this.selectedNode)
|
---|
[16963] | 182 | && !onNode.incomplete
|
---|
[16290] | 183 | && onNode.getCoor().lat()==selectedNode.getCoor().lat()
|
---|
| 184 | && onNode.getCoor().lon()==selectedNode.getCoor().lon()) {
|
---|
| 185 | return onNode;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | return null;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /** Given the the node on one end of the way, return the node on the other end */
|
---|
| 192 | private Node findOtherEnd(Way way, Node firstEnd) {
|
---|
[16962] | 193 | Node otherEnd = way.firstNode();
|
---|
| 194 | if (otherEnd.equals(firstEnd)) otherEnd = way.lastNode();
|
---|
[16290] | 195 | return otherEnd;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /** find set of ways which have an end on the selectedNode */
|
---|
| 199 | private ArrayList<Way> findConnectedWays() {
|
---|
| 200 | ArrayList<Way> connectedWays = new ArrayList<Way>();
|
---|
| 201 |
|
---|
| 202 | //loop through every way
|
---|
[16631] | 203 | for (Way onWay:Main.main.getCurrentDataSet().ways) {
|
---|
[16962] | 204 | if (onWay.getNodesCount() >= 2) {
|
---|
| 205 | if (onWay.isFirstLastNode(selectedNode)) {
|
---|
[16290] | 206 | //Found it
|
---|
| 207 | connectedWays.add(onWay);
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | return connectedWays;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * given a selected way, select a node on the end of the way which is not in a downloaded area
|
---|
| 216 | * return true if this worked
|
---|
| 217 | */
|
---|
| 218 | private boolean workFromWaySelection(Collection<OsmPrimitive> selection) {
|
---|
| 219 |
|
---|
[16963] | 220 | if (selection.size() != 1)
|
---|
[16290] | 221 | return false;
|
---|
[16963] | 222 | Way selectedWay = (Way) selection.iterator().next();
|
---|
| 223 | selectedNode = selectedWay.firstNode();
|
---|
[16290] | 224 |
|
---|
[16963] | 225 | if (isDownloaded(selectedNode)) {
|
---|
| 226 | selectedNode = selectedWay.lastNode();
|
---|
[16290] | 227 |
|
---|
[16963] | 228 | if (isDownloaded(selectedNode)) return false;
|
---|
[16290] | 229 | }
|
---|
[16631] | 230 | Main.main.getCurrentDataSet().setSelected(selectedNode);
|
---|
[16290] | 231 | return true;
|
---|
| 232 | }
|
---|
[15412] | 233 |
|
---|
[16290] | 234 | private boolean isDownloaded(Node node) {
|
---|
[16631] | 235 | for (DataSource datasource:Main.main.getCurrentDataSet().dataSources) {
|
---|
[16290] | 236 | Bounds bounds = datasource.bounds;
|
---|
| 237 |
|
---|
| 238 | if (node.getCoor().lat()>bounds.min.lat() &&
|
---|
| 239 | node.getCoor().lat()<bounds.max.lat() &&
|
---|
| 240 | node.getCoor().lon()>bounds.min.lon() &&
|
---|
| 241 | node.getCoor().lon()<bounds.max.lon()) {
|
---|
| 242 | return true;
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | return false;
|
---|
| 246 | }
|
---|
[15412] | 247 | }
|
---|