source: josm/trunk/src/org/openstreetmap/josm/data/osm/Way.java@ 1499

Last change on this file since 1499 was 1499, checked in by stoecker, 15 years ago

close #2302 - patch by jttt - optimizations and encapsulation

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.util.HashSet;
8
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.List;
13
14import org.openstreetmap.josm.data.osm.visitor.Visitor;
15import org.openstreetmap.josm.tools.Pair;
16
17/**
18 * One full way, consisting of a list of way nodes.
19 *
20 * @author imi
21 */
22public final class Way extends OsmPrimitive {
23
24 /**
25 * All way nodes in this way
26 */
27 public final List<Node> nodes = new ArrayList<Node>();
28
29 /* mappaint data */
30 public boolean isMappaintArea = false;
31 public Integer mappaintDrawnAreaCode = 0;
32 /* end of mappaint data */
33 @Override protected void clearCached()
34 {
35 super.clearCached();
36 isMappaintArea = false;
37 mappaintDrawnAreaCode = 0;
38 }
39
40 public void visitNodes(Visitor v) {
41 for (Node n : this.nodes)
42 v.visit(n);
43 }
44
45 public ArrayList<Pair<Node,Node>> getNodePairs(boolean sort) {
46 ArrayList<Pair<Node,Node>> chunkSet = new ArrayList<Pair<Node,Node>>();
47 Node lastN = null;
48 for (Node n : this.nodes) {
49 if (lastN == null) {
50 lastN = n;
51 continue;
52 }
53 Pair<Node,Node> np = new Pair<Node,Node>(lastN, n);
54 if (sort) {
55 Pair.sort(np);
56 }
57 chunkSet.add(np);
58 lastN = n;
59 }
60 return chunkSet;
61 }
62
63
64 @Override public void visit(Visitor visitor) {
65 visitor.visit(this);
66 }
67
68 /**
69 * Create an identical clone of the argument (including the id)
70 */
71 public Way(Way clone) {
72 cloneFrom(clone);
73 }
74
75 /**
76 * Create an empty way without id. Use this only if you set meaningful
77 * values yourself.
78 */
79 public Way() {
80 }
81
82 /**
83 * Create an incomplete Way.
84 */
85 public Way(long id) {
86 this.id = id;
87 incomplete = true;
88 }
89
90 @Override public void cloneFrom(OsmPrimitive osm) {
91 super.cloneFrom(osm);
92 nodes.clear();
93 nodes.addAll(((Way)osm).nodes);
94 }
95
96 @Override public String toString() {
97 return "{Way id="+id+" version="+version+" nodes="+Arrays.toString(nodes.toArray())+"}";
98 }
99
100 @Override public boolean realEqual(OsmPrimitive osm, boolean semanticOnly) {
101 return osm instanceof Way ? super.realEqual(osm, semanticOnly) && nodes.equals(((Way)osm).nodes) : false;
102 }
103
104 public int compareTo(OsmPrimitive o) {
105 if(o instanceof Relation)
106 return 1;
107 return o instanceof Way ? Long.valueOf(id).compareTo(o.id) : -1;
108 }
109
110 public String getName() {
111 String name;
112 if (incomplete) {
113 name = tr("incomplete");
114 } else {
115 name = get("name");
116 if (name == null) name = get("ref");
117 if (name == null) {
118 name =
119 (get("highway") != null) ? tr("highway") :
120 (get("railway") != null) ? tr("railway") :
121 (get("waterway") != null) ? tr("waterway") :
122 (get("landuse") != null) ? tr("landuse") : "";
123 }
124
125 int nodesNo = new HashSet<Node>(nodes).size();
126 String nodes = trn("{0} node", "{0} nodes", nodesNo, nodesNo);
127 name += (name.length() > 0) ? " ("+nodes+")" : nodes;
128 if(errors != null)
129 name = "*"+name;
130 }
131 return name;
132 }
133
134 public void removeNode(Node n)
135 {
136 Boolean closed = (lastNode() == n && firstNode() == n);
137 int i;
138 while((i = nodes.indexOf(n)) >= 0)
139 nodes.remove(i);
140 i = nodes.size();
141 if(closed && i > 2) // close again
142 addNode(firstNode());
143 // prevent closed ways with less than 3 different nodes
144 else if(i >= 2 && i <= 3 && nodes.get(0) == nodes.get(i-1))
145 nodes.remove(i-1);
146 }
147
148 public void removeNodes(Collection<? extends OsmPrimitive> selection)
149 {
150 for(OsmPrimitive p : selection)
151 {
152 if(p instanceof Node)
153 {
154 removeNode((Node)p);
155 }
156 }
157 }
158
159 public void addNode(Node n)
160 {
161 clearCached();
162 nodes.add(n);
163 }
164
165 public void addNode(int offs, Node n)
166 {
167 clearCached();
168 nodes.add(offs, n);
169 }
170
171 public Boolean isClosed() {
172 return nodes.size() >= 3 && lastNode() == firstNode();
173 }
174
175 public Node lastNode() {
176 return nodes.get(nodes.size()-1);
177 }
178
179 public Node firstNode() {
180 return nodes.get(0);
181 }
182
183 public boolean isFirstLastNode(Node n) {
184 return n == firstNode() || n == lastNode();
185 }
186}
Note: See TracBrowser for help on using the repository browser.