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