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