source: osm/applications/editors/josm/plugins/Create_grid_of_ways/src/CreateGridOfWaysPlugin/CreateGridOfWaysAction.java

Last change on this file was 35976, checked in by taylor.smock, 2 years ago

see #22104: Remove usages of Node#getCoor where possible

This also accounts for cases where Node has the methods used later,
so a new LatLon is unnecessary.

File size: 5.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package CreateGridOfWaysPlugin;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.Collection;
9import java.util.LinkedList;
10
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.actions.JosmAction;
14import org.openstreetmap.josm.command.AddCommand;
15import org.openstreetmap.josm.command.Command;
16import org.openstreetmap.josm.command.SequenceCommand;
17import org.openstreetmap.josm.data.UndoRedoHandler;
18import org.openstreetmap.josm.data.coor.ILatLon;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.gui.MainApplication;
25import org.openstreetmap.josm.tools.Logging;
26import org.openstreetmap.josm.tools.Shortcut;
27
28/**
29 * Crea una grilla de vías usando como base las dos seleccionadas que tengan un nodo en común
30 * y probablemente sean perpendiculares entre si , deben tener ambas vías un nodo en cada esquina
31 * (Please if you understand this translate to English)
32 *
33 * @author Jorge Luis Chamorro
34 */
35@SuppressWarnings("serial")
36public final class CreateGridOfWaysAction extends JosmAction {
37
38 public CreateGridOfWaysAction() {
39 super(tr("Create grid of ways"), "creategridofways",
40 tr("Forms a grid of ways in base to two existing that have various nodes and one in common"),
41 Shortcut.registerShortcut("tools:CreateGridOfWays", tr("More tools: {0}", tr("Create grid of ways")),
42 KeyEvent.VK_G, Shortcut.SHIFT), true);
43 }
44
45 /**
46 * Dadas 2 vias seleccionadas buscamos el punto en comun entre ambas y luego las recorremos para crear una grilla
47 * de vias paralelas a esas dos creando los nodos que son sus puntos de union y reusando los existentes
48 * (Please if you understand this translate to English)
49 * They given 2 ways selected we seek the point in common between both and then we travel through them to
50 * create a grid of ways parallel to those creating the nodes that are its points of union and reusing the
51 * existing ones (--this is from a translation machine--)
52 */
53 @Override
54 public void actionPerformed(ActionEvent e) {
55 DataSet ds = getLayerManager().getEditDataSet();
56 Collection<OsmPrimitive> sel = ds.getSelected();
57 Collection<Node> nodesWay1 = new LinkedList<>();
58 Collection<Node> nodesWay2 = new LinkedList<>();
59 if ((sel.size() != 2) || !(sel.toArray()[0] instanceof Way) || !(sel.toArray()[1] instanceof Way)) {
60 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), tr("Select two ways with a node in common"));
61 return;
62 }
63 nodesWay1.addAll(((Way)sel.toArray()[0]).getNodes());
64 nodesWay2.addAll(((Way)sel.toArray()[1]).getNodes());
65 Node nodeCommon = null;
66 for (Node n : nodesWay1)
67 for (Node m : nodesWay2)
68 if (n.equals(m)) {
69 if ( nodeCommon != null ) {
70 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), tr("Select two ways with alone a node in common"));
71 return;
72 }
73 nodeCommon = n;
74 }
75 if (nodeCommon == null) {
76 Logging.error("Cannot find common node");
77 return;
78 }
79 Way w2[] = new Way[nodesWay2.size()-1];
80 for (int c=0;c<w2.length;c++)
81 w2[c]=new Way();
82 Way w1[] = new Way[nodesWay1.size()-1];
83 for (int c=0;c<w1.length;c++)
84 w1[c]=new Way();
85 Collection<Command> cmds = new LinkedList<>();
86 int c1=0,c2;
87 double latDif,lonDif;
88 ILatLon llc = nodeCommon;
89 for (Node n1 : nodesWay1) {
90 ILatLon ll1 = n1;
91 if (!ll1.isLatLonKnown() || !llc.isLatLonKnown()) {
92 Logging.warn("Null coordinates: {0} / {1}", n1, nodeCommon);
93 continue;
94 }
95 latDif = ll1.lat()-llc.lat();
96 lonDif = ll1.lon()-llc.lon();
97 c2=0;
98 for (Node n2 : nodesWay2) {
99 if (n1.equals(nodeCommon) && n2.equals(nodeCommon))
100 continue;
101 if (n2.equals(nodeCommon)) {
102 w1[c1].addNode(n1);
103 continue;
104 }
105 if (n1.equals(nodeCommon)) {
106 w2[c2++].addNode(n2);
107 continue;
108 }
109 ILatLon ll2 = n2;
110 Node nodeOfGrid = new Node(new LatLon(ll2.lat()+latDif, ll2.lon()+lonDif));
111 cmds.add(new AddCommand(ds, nodeOfGrid));
112 w1[c1].addNode(nodeOfGrid);
113 w2[c2++].addNode(nodeOfGrid);
114 }
115 if (!n1.equals(nodeCommon))
116 c1++;
117 }
118 for (int c=0;c<w1.length;c++)
119 cmds.add(new AddCommand(ds, w1[c]));
120 for (int c=0;c<w2.length;c++)
121 cmds.add(new AddCommand(ds, w2[c]));
122 UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Create a grid of ways"), cmds));
123 MainApplication.getMap().repaint();
124 }
125}
Note: See TracBrowser for help on using the repository browser.