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