1 | // License: GPL. Copyright 2011 by Alexei Kasatkin
|
---|
2 | package utilsplugin2;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.LinkedHashSet;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Set;
|
---|
8 | import javax.swing.JOptionPane;
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.tools.Geometry;
|
---|
14 | import org.openstreetmap.josm.tools.Pair;
|
---|
15 |
|
---|
16 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
17 |
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Class with some useful functions that are reused in extend selection actions
|
---|
21 | *
|
---|
22 | */
|
---|
23 | public final class NodeWayUtils {
|
---|
24 |
|
---|
25 | static final int maxLevel = Main.pref.getInteger("selection.maxrecursion", 5);
|
---|
26 | static final int maxWays = Main.pref.getInteger("selection.maxfoundways", 2000);
|
---|
27 | static final int maxWays1 = Main.pref.getInteger("selection.maxfoundwaysrec", 200);
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Find the neighbours of node n on the way w and put them in given collection
|
---|
31 | * @param w way on which the search goes
|
---|
32 | * @param n node to find its neighbours
|
---|
33 | * @param nodes collection to place the nodes we found
|
---|
34 | */
|
---|
35 | static void addNeighbours(Way w, Node n, Collection<Node> nodes) {
|
---|
36 | List<Node> nodeList = w.getNodes();
|
---|
37 |
|
---|
38 | int idx = nodeList.indexOf(n);
|
---|
39 | if (idx == -1) return;
|
---|
40 |
|
---|
41 | // add previous element
|
---|
42 | if (idx > 0) {
|
---|
43 | nodes.add(nodeList.get(idx - 1));
|
---|
44 | }
|
---|
45 | // add next element
|
---|
46 | if (idx < nodeList.size() - 1) {
|
---|
47 | nodes.add(nodeList.get(idx + 1));
|
---|
48 | }
|
---|
49 | if (w.isClosed()) {
|
---|
50 | // cyclic neighbours detection
|
---|
51 | if (idx == 0) {
|
---|
52 | nodes.add(nodeList.get(nodeList.size() - 2));
|
---|
53 | }
|
---|
54 | if (idx == nodeList.size() - 1) {
|
---|
55 | nodes.add(nodeList.get(1));
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Adds all ways attached to way to specified collection
|
---|
62 | * @param w way to find attached ways
|
---|
63 | * @param ways collection to place the ways we found
|
---|
64 | */
|
---|
65 | static int addWaysConnectedToWay(Way w, Set<Way> ways) {
|
---|
66 | int s = ways.size();
|
---|
67 | List<Node> nodes = w.getNodes();
|
---|
68 | for (Node n: nodes) {
|
---|
69 | ways.addAll(OsmPrimitive.getFilteredList(n.getReferrers(), Way.class));
|
---|
70 | }
|
---|
71 | return ways.size() - s;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Adds all ways attached to node to specified collection
|
---|
76 | * @param n Node to find attached ways
|
---|
77 | * @param ways collection to place the ways we found
|
---|
78 | */
|
---|
79 | static int addWaysConnectedToNode(Node n, Set<Way> ways) {
|
---|
80 | int s = ways.size();
|
---|
81 | ways.addAll(OsmPrimitive.getFilteredList(n.getReferrers(), Way.class));
|
---|
82 | return ways.size() - s;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Adds all ways intersecting one way to specified set
|
---|
87 | * @param ways collection of ways to search
|
---|
88 | * @param w way to check intersections
|
---|
89 | * @param newWays set to place the ways we found
|
---|
90 | */
|
---|
91 | static int addWaysIntersectingWay(Collection<Way> ways, Way w, Set<Way> newWays,Set<Way> excludeWays) {
|
---|
92 | List<Pair<Node, Node>> nodePairs = w.getNodePairs(false);
|
---|
93 | int count=0;
|
---|
94 | for (Way anyway: ways) {
|
---|
95 | if (anyway == w) continue;
|
---|
96 | if (newWays.contains(anyway) || excludeWays.contains(anyway) ) continue;
|
---|
97 |
|
---|
98 | List<Pair<Node, Node>> nodePairs2 = anyway.getNodePairs(false);
|
---|
99 | loop: for (Pair<Node,Node> p1 : nodePairs) {
|
---|
100 | for (Pair<Node,Node> p2 : nodePairs2) {
|
---|
101 | if (null!=Geometry.getSegmentSegmentIntersection(
|
---|
102 | p1.a.getEastNorth(),p1.b.getEastNorth(),
|
---|
103 | p2.a.getEastNorth(),p2.b.getEastNorth())) {
|
---|
104 | newWays.add(anyway);
|
---|
105 | count++;
|
---|
106 | break loop;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | return count;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | static int addWaysIntersectingWay(Collection<Way> ways, Way w, Set<Way> newWays) {
|
---|
116 | List<Pair<Node, Node>> nodePairs = w.getNodePairs(false);
|
---|
117 | int count=0;
|
---|
118 | for (Way anyway: ways) {
|
---|
119 | if (anyway == w) continue;
|
---|
120 | if (newWays.contains(anyway)) continue;
|
---|
121 | List<Pair<Node, Node>> nodePairs2 = anyway.getNodePairs(false);
|
---|
122 | loop: for (Pair<Node,Node> p1 : nodePairs) {
|
---|
123 | for (Pair<Node,Node> p2 : nodePairs2) {
|
---|
124 | if (null!=Geometry.getSegmentSegmentIntersection(
|
---|
125 | p1.a.getEastNorth(),p1.b.getEastNorth(),
|
---|
126 | p2.a.getEastNorth(),p2.b.getEastNorth())) {
|
---|
127 | newWays.add(anyway);
|
---|
128 | count++;
|
---|
129 | break loop;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | return count;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Adds all ways from allWays intersecting initWays way to specified set newWays
|
---|
139 | * @param allWays collection of ways to search
|
---|
140 | * @param initWays ways to check intersections
|
---|
141 | * @param newWays set to place the ways we found
|
---|
142 | */
|
---|
143 | static int addWaysIntersectingWays(Collection<Way> allWays, Collection<Way> initWays, Set<Way> newWays) {
|
---|
144 | int count=0;
|
---|
145 | for (Way w : initWays){
|
---|
146 | count+=addWaysIntersectingWay(allWays, w, newWays);
|
---|
147 | }
|
---|
148 | return count;
|
---|
149 | }
|
---|
150 |
|
---|
151 | static int addWaysConnectedToNodes(Set<Node> selectedNodes, Set<Way> newWays) {
|
---|
152 | int s = newWays.size();
|
---|
153 | for (Node node: selectedNodes) {
|
---|
154 | addWaysConnectedToNode(node, newWays);
|
---|
155 | }
|
---|
156 | return newWays.size() - s;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int addNodesConnectedToWays(Set<Way> initWays, Set<Node> newNodes) {
|
---|
160 | int s = newNodes.size();
|
---|
161 | for (Way w: initWays) {
|
---|
162 | newNodes.addAll(w.getNodes());
|
---|
163 | }
|
---|
164 | return newNodes.size()-s;
|
---|
165 | }
|
---|
166 |
|
---|
167 | static void addWaysIntersectingWaysRecursively
|
---|
168 | (Collection<Way> allWays, Collection<Way> initWays, Set<Way> newWays)
|
---|
169 | {
|
---|
170 | Set<Way> foundWays = new LinkedHashSet<Way>();
|
---|
171 | foundWays.addAll(initWays);
|
---|
172 | newWays.addAll(initWays);
|
---|
173 | Set<Way> newFoundWays = new LinkedHashSet<Way>();
|
---|
174 |
|
---|
175 | int level=0,c;
|
---|
176 | do {
|
---|
177 | c=0;
|
---|
178 | newFoundWays = new LinkedHashSet<Way>();
|
---|
179 | for (Way w : foundWays){
|
---|
180 | c+=addWaysIntersectingWay(allWays, w, newFoundWays,newWays);
|
---|
181 | }
|
---|
182 | foundWays = newFoundWays;
|
---|
183 | newWays.addAll(newFoundWays);
|
---|
184 | level++;
|
---|
185 | // System.out.printf("%d: %d ways added to selection intersectiong\n",level,c);
|
---|
186 | if (c>maxWays1) {
|
---|
187 | JOptionPane.showMessageDialog(Main.parent,
|
---|
188 | tr("Too many ways are added: {0}!",c),
|
---|
189 | tr("Warning"),
|
---|
190 | JOptionPane.WARNING_MESSAGE);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 | } while ( c >0 && level < maxLevel );
|
---|
194 | return;
|
---|
195 | }
|
---|
196 |
|
---|
197 | static void addWaysConnectedToWaysRecursively
|
---|
198 | (Collection<Way> initWays, Set<Way> newWays)
|
---|
199 | {
|
---|
200 | int level=0,c;
|
---|
201 | newWays.addAll(initWays);
|
---|
202 | do {
|
---|
203 | c=0;
|
---|
204 | Set<Way> foundWays = new LinkedHashSet<Way>();
|
---|
205 | foundWays.addAll(newWays);
|
---|
206 | for (Way w : foundWays){
|
---|
207 | c+=addWaysConnectedToWay(w, newWays);
|
---|
208 | }
|
---|
209 | level++;
|
---|
210 | // System.out.printf("%d: %d ways added to selection\n",level,c);
|
---|
211 | if (c>maxWays) {
|
---|
212 | JOptionPane.showMessageDialog(Main.parent,
|
---|
213 | tr("Too many ways are added: {0}!",c),
|
---|
214 | tr("Warning"),
|
---|
215 | JOptionPane.WARNING_MESSAGE);
|
---|
216 | return;
|
---|
217 | }
|
---|
218 | } while ( c >0 && level < maxLevel );
|
---|
219 | return;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 |
|
---|
224 | }
|
---|