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.ILatLon;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
25 | import org.openstreetmap.josm.tools.Logging;
|
---|
26 | import 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")
|
---|
36 | public 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 | }
|
---|