[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);
|
---|
[16631] | 140 | new MergeNodesAction().mergeNodes(dupeNodes, selectedNode);
|
---|
[16290] | 141 |
|
---|
| 142 | connectedWays = findConnectedWays(); //Carry on
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | } else {
|
---|
| 147 | errMsg = tr("Reached the end of the line");
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | if (connectedWays.size()>2) {
|
---|
| 153 | //Three or more ways meeting at this node. Means we have a junction.
|
---|
| 154 | errMsg = tr("Reached a junction");
|
---|
| 155 |
|
---|
| 156 | } else if (connectedWays.size()==2) {
|
---|
| 157 | //Two connected ways (The "normal" way downloading case)
|
---|
| 158 | //Figure out which of the two is new.
|
---|
| 159 | System.out.println("connectedWays.toString()=" + connectedWays.toString());
|
---|
[16590] | 160 | Way wayA = connectedWays.get(0);
|
---|
| 161 | Way wayB = connectedWays.get(1);
|
---|
[16290] | 162 | Way nextWay = wayA;
|
---|
| 163 | if (priorConnectedWay.equals(wayA)) nextWay = wayB;
|
---|
| 164 |
|
---|
| 165 | Node nextNode = findOtherEnd(nextWay, selectedNode);
|
---|
| 166 |
|
---|
| 167 | //Select the next node
|
---|
[16631] | 168 | Main.main.getCurrentDataSet().setSelected(nextNode);
|
---|
[16290] | 169 |
|
---|
| 170 | Main.map.mapView.zoomTo(nextNode.getEastNorth());
|
---|
| 171 | }
|
---|
| 172 | if(errMsg != null)
|
---|
| 173 | JOptionPane.showMessageDialog(Main.parent, errMsg);
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /** See if there's another node at the same coordinates. If so return it. Otherwise null */
|
---|
| 178 | private Node duplicateNode() {
|
---|
[16963] | 179 | for (Node onNode:Main.main.getCurrentDataSet().nodes) {
|
---|
[16290] | 180 | if (!onNode.equals(this.selectedNode)
|
---|
[16963] | 181 | && !onNode.incomplete
|
---|
[16290] | 182 | && onNode.getCoor().lat()==selectedNode.getCoor().lat()
|
---|
| 183 | && onNode.getCoor().lon()==selectedNode.getCoor().lon()) {
|
---|
| 184 | return onNode;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | return null;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /** Given the the node on one end of the way, return the node on the other end */
|
---|
| 191 | private Node findOtherEnd(Way way, Node firstEnd) {
|
---|
[16962] | 192 | Node otherEnd = way.firstNode();
|
---|
| 193 | if (otherEnd.equals(firstEnd)) otherEnd = way.lastNode();
|
---|
[16290] | 194 | return otherEnd;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /** find set of ways which have an end on the selectedNode */
|
---|
| 198 | private ArrayList<Way> findConnectedWays() {
|
---|
| 199 | ArrayList<Way> connectedWays = new ArrayList<Way>();
|
---|
| 200 |
|
---|
| 201 | //loop through every way
|
---|
[16631] | 202 | for (Way onWay:Main.main.getCurrentDataSet().ways) {
|
---|
[16962] | 203 | if (onWay.getNodesCount() >= 2) {
|
---|
| 204 | if (onWay.isFirstLastNode(selectedNode)) {
|
---|
[16290] | 205 | //Found it
|
---|
| 206 | connectedWays.add(onWay);
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | return connectedWays;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /**
|
---|
| 214 | * given a selected way, select a node on the end of the way which is not in a downloaded area
|
---|
| 215 | * return true if this worked
|
---|
| 216 | */
|
---|
| 217 | private boolean workFromWaySelection(Collection<OsmPrimitive> selection) {
|
---|
| 218 |
|
---|
[16963] | 219 | if (selection.size() != 1)
|
---|
[16290] | 220 | return false;
|
---|
[16963] | 221 | Way selectedWay = (Way) selection.iterator().next();
|
---|
| 222 | selectedNode = selectedWay.firstNode();
|
---|
[16290] | 223 |
|
---|
[16963] | 224 | if (isDownloaded(selectedNode)) {
|
---|
| 225 | selectedNode = selectedWay.lastNode();
|
---|
[16290] | 226 |
|
---|
[16963] | 227 | if (isDownloaded(selectedNode)) return false;
|
---|
[16290] | 228 | }
|
---|
[16631] | 229 | Main.main.getCurrentDataSet().setSelected(selectedNode);
|
---|
[16290] | 230 | return true;
|
---|
| 231 | }
|
---|
[15412] | 232 |
|
---|
[16290] | 233 | private boolean isDownloaded(Node node) {
|
---|
[16631] | 234 | for (DataSource datasource:Main.main.getCurrentDataSet().dataSources) {
|
---|
[16290] | 235 | Bounds bounds = datasource.bounds;
|
---|
| 236 |
|
---|
| 237 | if (node.getCoor().lat()>bounds.min.lat() &&
|
---|
| 238 | node.getCoor().lat()<bounds.max.lat() &&
|
---|
| 239 | node.getCoor().lon()>bounds.min.lon() &&
|
---|
| 240 | node.getCoor().lon()<bounds.max.lon()) {
|
---|
| 241 | return true;
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | return false;
|
---|
| 245 | }
|
---|
[15412] | 246 | }
|
---|