1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 |
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
11 | import org.openstreetmap.josm.tools.CopyList;
|
---|
12 | import org.openstreetmap.josm.tools.Pair;
|
---|
13 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * One full way, consisting of a list of way nodes.
|
---|
17 | *
|
---|
18 | * @author imi
|
---|
19 | */
|
---|
20 | public final class Way extends OsmPrimitive {
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * All way nodes in this way
|
---|
24 | *
|
---|
25 | */
|
---|
26 | private final List<Node> nodes = new ArrayList<Node>();
|
---|
27 |
|
---|
28 | /**
|
---|
29 | *
|
---|
30 | * You can modify returned list but changes will not be propagated back
|
---|
31 | * to the Way. Use {@link #setNodes(List)} to update this way
|
---|
32 | * @return Nodes composing the way
|
---|
33 | * @since 1862
|
---|
34 | */
|
---|
35 | public List<Node> getNodes() {
|
---|
36 | return new CopyList<Node>(nodes.toArray(new Node[nodes.size()]));
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * @param nodes New way nodes. Can be null, in that case all way nodes are removed
|
---|
41 | * @since 1862
|
---|
42 | */
|
---|
43 | public void setNodes(List<Node> nodes) {
|
---|
44 | this.nodes.clear();
|
---|
45 | if (nodes != null) {
|
---|
46 | this.nodes.addAll(nodes);
|
---|
47 | }
|
---|
48 | clearCached();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Replies the number of nodes in this ways.
|
---|
53 | *
|
---|
54 | * @return the number of nodes in this ways.
|
---|
55 | * @since 1862
|
---|
56 | */
|
---|
57 | public int getNodesCount() {
|
---|
58 | return nodes.size();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Replies the node at position <code>index</code>.
|
---|
63 | *
|
---|
64 | * @param index the position
|
---|
65 | * @return the node at position <code>index</code>
|
---|
66 | * @exception IndexOutOfBoundsException thrown if <code>index</code> < 0
|
---|
67 | * or <code>index</code> >= {@see #getNodesCount()}
|
---|
68 | * @since 1862
|
---|
69 | */
|
---|
70 | public Node getNode(int index) {
|
---|
71 | return nodes.get(index);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Replies true if this way contains the node <code>node</code>, false
|
---|
76 | * otherwise. Replies false if <code>node</code> is null.
|
---|
77 | *
|
---|
78 | * @param node the node. May be null.
|
---|
79 | * @return true if this way contains the node <code>node</code>, false
|
---|
80 | * otherwise
|
---|
81 | * @since 1909
|
---|
82 | */
|
---|
83 | public boolean containsNode(Node node) {
|
---|
84 | if (node == null) return false;
|
---|
85 | return nodes.contains(node);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /* mappaint data */
|
---|
89 | public boolean isMappaintArea = false;
|
---|
90 | public Integer mappaintDrawnAreaCode = 0;
|
---|
91 | /* end of mappaint data */
|
---|
92 | @Override protected void clearCached() {
|
---|
93 | super.clearCached();
|
---|
94 | isMappaintArea = false;
|
---|
95 | mappaintDrawnAreaCode = 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public ArrayList<Pair<Node,Node>> getNodePairs(boolean sort) {
|
---|
99 | ArrayList<Pair<Node,Node>> chunkSet = new ArrayList<Pair<Node,Node>>();
|
---|
100 | if (incomplete) return chunkSet;
|
---|
101 | Node lastN = null;
|
---|
102 | for (Node n : this.nodes) {
|
---|
103 | if (lastN == null) {
|
---|
104 | lastN = n;
|
---|
105 | continue;
|
---|
106 | }
|
---|
107 | Pair<Node,Node> np = new Pair<Node,Node>(lastN, n);
|
---|
108 | if (sort) {
|
---|
109 | Pair.sort(np);
|
---|
110 | }
|
---|
111 | chunkSet.add(np);
|
---|
112 | lastN = n;
|
---|
113 | }
|
---|
114 | return chunkSet;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | @Override public void visit(Visitor visitor) {
|
---|
119 | visitor.visit(this);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Creates a new way with id 0.
|
---|
124 | *
|
---|
125 | */
|
---|
126 | public Way(){
|
---|
127 | super(0);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Create an identical clone of the argument (including the id).
|
---|
132 | *
|
---|
133 | * @param original the original way. Must not be null.
|
---|
134 | */
|
---|
135 | public Way(Way original) {
|
---|
136 | super(original.getId());
|
---|
137 | cloneFrom(original);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Creates a new way for the given id. If the id > 0, the way is marked
|
---|
142 | * as incomplete.
|
---|
143 | *
|
---|
144 | * @param id the id. > 0 required
|
---|
145 | * @throws IllegalArgumentException thrown if id < 0
|
---|
146 | */
|
---|
147 | public Way(long id) throws IllegalArgumentException {
|
---|
148 | super(id);
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override public void cloneFrom(OsmPrimitive osm) {
|
---|
152 | super.cloneFrom(osm);
|
---|
153 | nodes.clear();
|
---|
154 | nodes.addAll(((Way)osm).nodes);
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Override public String toString() {
|
---|
158 | if (incomplete) return "{Way id="+getId()+" version="+getVersion()+" (incomplete)}";
|
---|
159 | return "{Way id="+getId()+" version="+getVersion()+" nodes="+Arrays.toString(nodes.toArray())+"}";
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
164 | if (other == null || ! (other instanceof Way) )
|
---|
165 | return false;
|
---|
166 | if (! super.hasEqualSemanticAttributes(other))
|
---|
167 | return false;
|
---|
168 | Way w = (Way)other;
|
---|
169 | return nodes.equals(w.nodes);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public int compareTo(OsmPrimitive o) {
|
---|
173 | if (o instanceof Relation)
|
---|
174 | return 1;
|
---|
175 | return o instanceof Way ? Long.valueOf(getId()).compareTo(o.getId()) : -1;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public void removeNode(Node n) {
|
---|
179 | if (incomplete) return;
|
---|
180 | boolean closed = (lastNode() == n && firstNode() == n);
|
---|
181 | int i;
|
---|
182 | while ((i = nodes.indexOf(n)) >= 0) {
|
---|
183 | nodes.remove(i);
|
---|
184 | }
|
---|
185 | i = nodes.size();
|
---|
186 | if (closed && i > 2) {
|
---|
187 | addNode(firstNode());
|
---|
188 | } else if (i >= 2 && i <= 3 && nodes.get(0) == nodes.get(i-1)) {
|
---|
189 | nodes.remove(i-1);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void removeNodes(Collection<? extends OsmPrimitive> selection) {
|
---|
194 | if (incomplete) return;
|
---|
195 | for(OsmPrimitive p : selection) {
|
---|
196 | if (p instanceof Node) {
|
---|
197 | removeNode((Node)p);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Adds a node to the end of the list of nodes. Ignored, if n is null.
|
---|
204 | *
|
---|
205 | * @param n the node. Ignored, if null.
|
---|
206 | * @throws IllegalStateException thrown, if this way is marked as incomplete. We can't add a node
|
---|
207 | * to an incomplete way
|
---|
208 | */
|
---|
209 | public void addNode(Node n) throws IllegalStateException {
|
---|
210 | if (n==null) return;
|
---|
211 | if (incomplete)
|
---|
212 | throw new IllegalStateException(tr("Cannot add node {0} to incomplete way {1}.", n.getId(), getId()));
|
---|
213 | if (incomplete) return;
|
---|
214 | clearCached();
|
---|
215 | nodes.add(n);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Adds a node at position offs.
|
---|
220 | *
|
---|
221 | * @param int offs the offset
|
---|
222 | * @param n the node. Ignored, if null.
|
---|
223 | * @throws IllegalStateException thrown, if this way is marked as incomplete. We can't add a node
|
---|
224 | * to an incomplete way
|
---|
225 | * @throws IndexOutOfBoundsException thrown if offs is out of bounds
|
---|
226 | */
|
---|
227 | public void addNode(int offs, Node n) throws IllegalStateException, IndexOutOfBoundsException {
|
---|
228 | if (n==null) return;
|
---|
229 | if (incomplete)
|
---|
230 | throw new IllegalStateException(tr("Cannot add node {0} to incomplete way {1}.", n.getId(), getId()));
|
---|
231 | clearCached();
|
---|
232 | nodes.add(offs, n);
|
---|
233 | }
|
---|
234 |
|
---|
235 | public boolean isClosed() {
|
---|
236 | if (incomplete) return false;
|
---|
237 | return nodes.size() >= 3 && lastNode() == firstNode();
|
---|
238 | }
|
---|
239 |
|
---|
240 | public Node lastNode() {
|
---|
241 | if (incomplete || nodes.size() == 0) return null;
|
---|
242 | return nodes.get(nodes.size()-1);
|
---|
243 | }
|
---|
244 |
|
---|
245 | public Node firstNode() {
|
---|
246 | if (incomplete || nodes.size() == 0) return null;
|
---|
247 | return nodes.get(0);
|
---|
248 | }
|
---|
249 |
|
---|
250 | public boolean isFirstLastNode(Node n) {
|
---|
251 | if (incomplete || nodes.size() == 0) return false;
|
---|
252 | return n == firstNode() || n == lastNode();
|
---|
253 | }
|
---|
254 |
|
---|
255 | @Override
|
---|
256 | public String getDisplayName(NameFormatter formatter) {
|
---|
257 | return formatter.format(this);
|
---|
258 | }
|
---|
259 | }
|
---|