1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.geom.Area;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Set;
|
---|
14 | import java.util.stream.Collectors;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
|
---|
17 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
18 | import org.openstreetmap.josm.tools.Geometry;
|
---|
19 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
20 | import org.openstreetmap.josm.tools.Pair;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Helper class to build multipolygons from multiple ways.
|
---|
24 | * @author viesturs
|
---|
25 | * @since 7392 (rename)
|
---|
26 | * @since 3704
|
---|
27 | */
|
---|
28 | public class MultipolygonBuilder {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Represents one polygon that consists of multiple ways.
|
---|
32 | */
|
---|
33 | public static class JoinedPolygon {
|
---|
34 | /** list of ways building this polygon */
|
---|
35 | public final List<Way> ways;
|
---|
36 | /** list of flags that indicate if the nodes of the way in the same position where reversed */
|
---|
37 | public final List<Boolean> reversed;
|
---|
38 | /** the nodes of the polygon, first node is not duplicated as last node. */
|
---|
39 | public final List<Node> nodes;
|
---|
40 | /** the area in east/north space */
|
---|
41 | public final Area area;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code JoinedPolygon} from given list of ways.
|
---|
45 | * @param ways The ways used to build joined polygon
|
---|
46 | * @param reversed list of reversed states
|
---|
47 | */
|
---|
48 | public JoinedPolygon(List<Way> ways, List<Boolean> reversed) {
|
---|
49 | this.ways = ways;
|
---|
50 | this.reversed = reversed;
|
---|
51 | this.nodes = this.getNodes();
|
---|
52 | this.area = Geometry.getArea(nodes);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Creates a polygon from single way.
|
---|
57 | * @param way the way to form the polygon
|
---|
58 | */
|
---|
59 | public JoinedPolygon(Way way) {
|
---|
60 | this(Collections.singletonList(way), Collections.singletonList(Boolean.FALSE));
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Builds a list of nodes for this polygon. First node is not duplicated as last node.
|
---|
65 | * @return list of nodes
|
---|
66 | */
|
---|
67 | public List<Node> getNodes() {
|
---|
68 | List<Node> ringNodes = new ArrayList<>();
|
---|
69 |
|
---|
70 | for (int waypos = 0; waypos < this.ways.size(); waypos++) {
|
---|
71 | Way way = this.ways.get(waypos);
|
---|
72 |
|
---|
73 | if (Boolean.FALSE.equals(this.reversed.get(waypos))) {
|
---|
74 | for (int pos = 0; pos < way.getNodesCount() - 1; pos++) {
|
---|
75 | ringNodes.add(way.getNode(pos));
|
---|
76 | }
|
---|
77 | } else {
|
---|
78 | for (int pos = way.getNodesCount() - 1; pos > 0; pos--) {
|
---|
79 | ringNodes.add(way.getNode(pos));
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | return ringNodes;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** List of outer ways **/
|
---|
89 | public final List<JoinedPolygon> outerWays;
|
---|
90 | /** List of inner ways **/
|
---|
91 | public final List<JoinedPolygon> innerWays;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Constructs a new {@code MultipolygonBuilder} initialized with given ways.
|
---|
95 | * @param outerWays The outer ways
|
---|
96 | * @param innerWays The inner ways
|
---|
97 | */
|
---|
98 | public MultipolygonBuilder(List<JoinedPolygon> outerWays, List<JoinedPolygon> innerWays) {
|
---|
99 | this.outerWays = outerWays;
|
---|
100 | this.innerWays = innerWays;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Constructs a new empty {@code MultipolygonBuilder}.
|
---|
105 | */
|
---|
106 | public MultipolygonBuilder() {
|
---|
107 | this.outerWays = new ArrayList<>(0);
|
---|
108 | this.innerWays = new ArrayList<>(0);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Splits ways into inner and outer JoinedWays. Sets {@link #innerWays} and {@link #outerWays} to the result.
|
---|
113 | * Calculation is done in {@link MultipolygonTest#makeFromWays(Collection)} to ensure that the result is a valid multipolygon.
|
---|
114 | * @param ways ways to analyze
|
---|
115 | * @return error description if the ways cannot be split, {@code null} if all fine.
|
---|
116 | */
|
---|
117 | public String makeFromWays(Collection<Way> ways) {
|
---|
118 | MultipolygonTest mpTest = new MultipolygonTest();
|
---|
119 | Relation calculated = mpTest.makeFromWays(ways);
|
---|
120 | try {
|
---|
121 | if (!mpTest.getErrors().isEmpty()) {
|
---|
122 | return mpTest.getErrors().iterator().next().getMessage();
|
---|
123 | }
|
---|
124 | Pair<List<JoinedPolygon>, List<JoinedPolygon>> outerInner = joinWays(calculated);
|
---|
125 | this.outerWays.clear();
|
---|
126 | this.innerWays.clear();
|
---|
127 | this.outerWays.addAll(outerInner.a);
|
---|
128 | this.innerWays.addAll(outerInner.b);
|
---|
129 | return null;
|
---|
130 | } finally {
|
---|
131 | calculated.setMembers(null); // see #19885
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * An exception indicating an error while joining ways to multipolygon rings.
|
---|
137 | */
|
---|
138 | public static class JoinedPolygonCreationException extends RuntimeException {
|
---|
139 | /**
|
---|
140 | * Constructs a new {@code JoinedPolygonCreationException}.
|
---|
141 | * @param message the detail message. The detail message is saved for
|
---|
142 | * later retrieval by the {@link #getMessage()} method
|
---|
143 | */
|
---|
144 | public JoinedPolygonCreationException(String message) {
|
---|
145 | super(message);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Joins the given {@code multipolygon} to a pair of outer and inner multipolygon rings.
|
---|
151 | *
|
---|
152 | * @param multipolygon the multipolygon to join.
|
---|
153 | * @return a pair of outer and inner multipolygon rings.
|
---|
154 | * @throws JoinedPolygonCreationException if the creation fails.
|
---|
155 | */
|
---|
156 | public static Pair<List<JoinedPolygon>, List<JoinedPolygon>> joinWays(Relation multipolygon) {
|
---|
157 | return joinWays(null, multipolygon);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Joins the given {@code multipolygon} to a pair of outer and inner multipolygon rings.
|
---|
162 | *
|
---|
163 | * @param multipolygon the multipolygon to join.
|
---|
164 | * @return a pair of outer and inner multipolygon rings.
|
---|
165 | * @throws JoinedPolygonCreationException if the creation fails.
|
---|
166 | * @since xxx
|
---|
167 | */
|
---|
168 | public static Pair<List<JoinedPolygon>, List<JoinedPolygon>> joinWays(
|
---|
169 | Map<IRelation<?>, Pair<List<JoinedPolygon>, List<JoinedPolygon>>> cache, Relation multipolygon) {
|
---|
170 | if (cache != null) {
|
---|
171 | return cache.computeIfAbsent(multipolygon, MultipolygonBuilder::joinWaysActual);
|
---|
172 | }
|
---|
173 | return joinWaysActual(multipolygon);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Perform the actual join ways calculation
|
---|
178 | *
|
---|
179 | * @param multipolygon the multipolygon to join.
|
---|
180 | * @return a pair of outer and inner multipolygon rings.
|
---|
181 | * @throws JoinedPolygonCreationException if the creation fails.
|
---|
182 | */
|
---|
183 | private static Pair<List<JoinedPolygon>, List<JoinedPolygon>> joinWaysActual(IRelation<?> multipolygon) {
|
---|
184 | CheckParameterUtil.ensureThat(multipolygon.isMultipolygon(), "multipolygon.isMultipolygon");
|
---|
185 | CheckParameterUtil.ensureThat(multipolygon instanceof Relation,
|
---|
186 | "This method currently only supports Relation objects due to potential breakage");
|
---|
187 | final Map<String, Set<Way>> members = ((Relation) multipolygon).getMembers().stream()
|
---|
188 | .filter(IRelationMember::isWay)
|
---|
189 | .collect(Collectors.groupingBy(IRelationMember::getRole, Collectors.mapping(RelationMember::getWay, Collectors.toSet())));
|
---|
190 | final List<JoinedPolygon> outerRings = joinWays(members.getOrDefault("outer", Collections.emptySet()));
|
---|
191 | final List<JoinedPolygon> innerRings = joinWays(members.getOrDefault("inner", Collections.emptySet()));
|
---|
192 | return Pair.create(outerRings, innerRings);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Joins the given {@code ways} to multipolygon rings.
|
---|
197 | * @param ways the ways to join.
|
---|
198 | * @return a list of multipolygon rings.
|
---|
199 | * @throws JoinedPolygonCreationException if the creation fails.
|
---|
200 | */
|
---|
201 | public static List<JoinedPolygon> joinWays(Collection<Way> ways) {
|
---|
202 | List<JoinedPolygon> joinedWays = new ArrayList<>();
|
---|
203 |
|
---|
204 | //collect ways connecting to each node.
|
---|
205 | MultiMap<Node, Way> nodesWithConnectedWays = new MultiMap<>();
|
---|
206 | Set<Way> usedWays = new HashSet<>();
|
---|
207 |
|
---|
208 | for (Way w: ways) {
|
---|
209 | if (w.getNodesCount() < 2) {
|
---|
210 | throw new JoinedPolygonCreationException(tr("Cannot add a way with only {0} nodes.", w.getNodesCount()));
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (w.isClosed()) {
|
---|
214 | //closed way, add as is.
|
---|
215 | JoinedPolygon jw = new JoinedPolygon(w);
|
---|
216 | joinedWays.add(jw);
|
---|
217 | usedWays.add(w);
|
---|
218 | } else {
|
---|
219 | nodesWithConnectedWays.put(w.lastNode(), w);
|
---|
220 | nodesWithConnectedWays.put(w.firstNode(), w);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | //process unclosed ways
|
---|
225 | for (Way startWay: ways) {
|
---|
226 | if (usedWays.contains(startWay)) {
|
---|
227 | continue;
|
---|
228 | }
|
---|
229 |
|
---|
230 | Node startNode = startWay.firstNode();
|
---|
231 | List<Way> collectedWays = new ArrayList<>();
|
---|
232 | List<Boolean> collectedWaysReverse = new ArrayList<>();
|
---|
233 | Way curWay = startWay;
|
---|
234 | Node prevNode = startNode;
|
---|
235 |
|
---|
236 | //find polygon ways
|
---|
237 | while (true) {
|
---|
238 | boolean curWayReverse = prevNode == curWay.lastNode();
|
---|
239 | Node nextNode = curWayReverse ? curWay.firstNode() : curWay.lastNode();
|
---|
240 |
|
---|
241 | //add cur way to the list
|
---|
242 | collectedWays.add(curWay);
|
---|
243 | collectedWaysReverse.add(curWayReverse);
|
---|
244 |
|
---|
245 | if (nextNode == startNode) {
|
---|
246 | //way finished
|
---|
247 | break;
|
---|
248 | }
|
---|
249 |
|
---|
250 | //find next way
|
---|
251 | Collection<Way> adjacentWays = nodesWithConnectedWays.get(nextNode);
|
---|
252 |
|
---|
253 | if (adjacentWays.size() != 2) {
|
---|
254 | throw new JoinedPolygonCreationException(tr("Each node must connect exactly 2 ways"));
|
---|
255 | }
|
---|
256 |
|
---|
257 | Way nextWay = null;
|
---|
258 | for (Way way: adjacentWays) {
|
---|
259 | if (way != curWay) {
|
---|
260 | nextWay = way;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | //move to the next way
|
---|
265 | curWay = nextWay;
|
---|
266 | prevNode = nextNode;
|
---|
267 | }
|
---|
268 |
|
---|
269 | usedWays.addAll(collectedWays);
|
---|
270 | joinedWays.add(new JoinedPolygon(collectedWays, collectedWaysReverse));
|
---|
271 | }
|
---|
272 |
|
---|
273 | return joinedWays;
|
---|
274 | }
|
---|
275 | }
|
---|