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 | *
|
---|
58 | * @return
|
---|
59 | * @since 1862
|
---|
60 | */
|
---|
61 | public int getNodesCount() {
|
---|
62 | return nodes.size();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | *
|
---|
67 | * @param index
|
---|
68 | * @return
|
---|
69 | * @since 1862
|
---|
70 | */
|
---|
71 | public Node getNode(int index) {
|
---|
72 | return nodes.get(index);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Replies true if this way contains the node <code>node</code>, false
|
---|
77 | * otherwise. Replies false if <code>node</code> is null.
|
---|
78 | *
|
---|
79 | * @param node the node. May be null.
|
---|
80 | * @return true if this way contains the node <code>node</code>, false
|
---|
81 | * otherwise
|
---|
82 | * @since 1909
|
---|
83 | */
|
---|
84 | public boolean containsNode(Node node) {
|
---|
85 | if (node == null) return false;
|
---|
86 | return nodes.contains(node);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /* mappaint data */
|
---|
90 | public boolean isMappaintArea = false;
|
---|
91 | public Integer mappaintDrawnAreaCode = 0;
|
---|
92 | /* end of mappaint data */
|
---|
93 | @Override protected void clearCached() {
|
---|
94 | super.clearCached();
|
---|
95 | isMappaintArea = false;
|
---|
96 | mappaintDrawnAreaCode = 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void visitNodes(Visitor v) {
|
---|
100 | if (incomplete) return;
|
---|
101 | for (Node n : this.nodes) {
|
---|
102 | v.visit(n);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public ArrayList<Pair<Node,Node>> getNodePairs(boolean sort) {
|
---|
107 | ArrayList<Pair<Node,Node>> chunkSet = new ArrayList<Pair<Node,Node>>();
|
---|
108 | if (incomplete) return chunkSet;
|
---|
109 | Node lastN = null;
|
---|
110 | for (Node n : this.nodes) {
|
---|
111 | if (lastN == null) {
|
---|
112 | lastN = n;
|
---|
113 | continue;
|
---|
114 | }
|
---|
115 | Pair<Node,Node> np = new Pair<Node,Node>(lastN, n);
|
---|
116 | if (sort) {
|
---|
117 | Pair.sort(np);
|
---|
118 | }
|
---|
119 | chunkSet.add(np);
|
---|
120 | lastN = n;
|
---|
121 | }
|
---|
122 | return chunkSet;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | @Override public void visit(Visitor visitor) {
|
---|
127 | visitor.visit(this);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Create an identical clone of the argument (including the id)
|
---|
132 | */
|
---|
133 | public Way(Way clone) {
|
---|
134 | cloneFrom(clone);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Create an empty way without id. Use this only if you set meaningful
|
---|
139 | * values yourself.
|
---|
140 | */
|
---|
141 | public Way() {
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Create an incomplete Way.
|
---|
146 | */
|
---|
147 | public Way(long id) {
|
---|
148 | this.id = id;
|
---|
149 | incomplete = true;
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override public void cloneFrom(OsmPrimitive osm) {
|
---|
153 | super.cloneFrom(osm);
|
---|
154 | nodes.clear();
|
---|
155 | nodes.addAll(((Way)osm).nodes);
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override public String toString() {
|
---|
159 | if (incomplete) return "{Way id="+id+" version="+version+" (incomplete)}";
|
---|
160 | return "{Way id="+id+" version="+version+" nodes="+Arrays.toString(nodes.toArray())+"}";
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
165 | if (other == null || ! (other instanceof Way) )
|
---|
166 | return false;
|
---|
167 | if (! super.hasEqualSemanticAttributes(other))
|
---|
168 | return false;
|
---|
169 | Way w = (Way)other;
|
---|
170 | return nodes.equals(w.nodes);
|
---|
171 | }
|
---|
172 |
|
---|
173 | public int compareTo(OsmPrimitive o) {
|
---|
174 | if (o instanceof Relation)
|
---|
175 | return 1;
|
---|
176 | return o instanceof Way ? Long.valueOf(id).compareTo(o.id) : -1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | @Override
|
---|
180 | public String getName() {
|
---|
181 | String name;
|
---|
182 | if (incomplete) {
|
---|
183 | name = tr("incomplete");
|
---|
184 | } else {
|
---|
185 | name = get("name");
|
---|
186 | if (name == null) {
|
---|
187 | name = get("ref");
|
---|
188 | }
|
---|
189 | if (name == null) {
|
---|
190 | name =
|
---|
191 | (get("highway") != null) ? tr("highway") :
|
---|
192 | (get("railway") != null) ? tr("railway") :
|
---|
193 | (get("waterway") != null) ? tr("waterway") :
|
---|
194 | (get("landuse") != null) ? tr("landuse") : "";
|
---|
195 | }
|
---|
196 |
|
---|
197 | int nodesNo = new HashSet<Node>(nodes).size();
|
---|
198 | String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
|
---|
199 | name += (name.length() > 0) ? " ("+nodes+")" : nodes;
|
---|
200 | if(errors != null) {
|
---|
201 | name = "*"+name;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | return name;
|
---|
205 | }
|
---|
206 |
|
---|
207 | public void removeNode(Node n) {
|
---|
208 | if (incomplete) return;
|
---|
209 | boolean closed = (lastNode() == n && firstNode() == n);
|
---|
210 | int i;
|
---|
211 | while ((i = nodes.indexOf(n)) >= 0) {
|
---|
212 | nodes.remove(i);
|
---|
213 | }
|
---|
214 | i = nodes.size();
|
---|
215 | if (closed && i > 2) {
|
---|
216 | addNode(firstNode());
|
---|
217 | } else if (i >= 2 && i <= 3 && nodes.get(0) == nodes.get(i-1)) {
|
---|
218 | nodes.remove(i-1);
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void removeNodes(Collection<? extends OsmPrimitive> selection) {
|
---|
223 | if (incomplete) return;
|
---|
224 | for(OsmPrimitive p : selection) {
|
---|
225 | if (p instanceof Node) {
|
---|
226 | removeNode((Node)p);
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | public void addNode(Node n) {
|
---|
232 | if (incomplete) return;
|
---|
233 | clearCached();
|
---|
234 | nodes.add(n);
|
---|
235 | }
|
---|
236 |
|
---|
237 | public void addNode(int offs, Node n) {
|
---|
238 | if (incomplete) return;
|
---|
239 | clearCached();
|
---|
240 | nodes.add(offs, n);
|
---|
241 | }
|
---|
242 |
|
---|
243 | public boolean isClosed() {
|
---|
244 | if (incomplete) return false;
|
---|
245 | return nodes.size() >= 3 && lastNode() == firstNode();
|
---|
246 | }
|
---|
247 |
|
---|
248 | public Node lastNode() {
|
---|
249 | if (incomplete || nodes.size() == 0) return null;
|
---|
250 | return nodes.get(nodes.size()-1);
|
---|
251 | }
|
---|
252 |
|
---|
253 | public Node firstNode() {
|
---|
254 | if (incomplete || nodes.size() == 0) return null;
|
---|
255 | return nodes.get(0);
|
---|
256 | }
|
---|
257 |
|
---|
258 | public boolean isFirstLastNode(Node n) {
|
---|
259 | if (incomplete || nodes.size() == 0) return false;
|
---|
260 | return n == firstNode() || n == lastNode();
|
---|
261 | }
|
---|
262 | }
|
---|