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.HashSet;
|
---|
8 |
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
14 | import org.openstreetmap.josm.tools.Pair;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * One full way, consisting of a list of way nodes.
|
---|
18 | *
|
---|
19 | * @author imi
|
---|
20 | */
|
---|
21 | public final class Way extends OsmPrimitive {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * All way nodes in this way
|
---|
25 | */
|
---|
26 | public final List<Node> nodes = new ArrayList<Node>();
|
---|
27 |
|
---|
28 | public void visitNodes(Visitor v) {
|
---|
29 | for (Node n : this.nodes)
|
---|
30 | v.visit(n);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public ArrayList<Pair<Node,Node>> getNodePairs(boolean sort) {
|
---|
34 | ArrayList<Pair<Node,Node>> chunkSet = new ArrayList<Pair<Node,Node>>();
|
---|
35 | Node lastN = null;
|
---|
36 | for (Node n : this.nodes) {
|
---|
37 | if (lastN == null) {
|
---|
38 | lastN = n;
|
---|
39 | continue;
|
---|
40 | }
|
---|
41 | Pair<Node,Node> np = new Pair<Node,Node>(lastN, n);
|
---|
42 | if (sort) {
|
---|
43 | Pair.sort(np);
|
---|
44 | }
|
---|
45 | chunkSet.add(np);
|
---|
46 | lastN = n;
|
---|
47 | }
|
---|
48 | return chunkSet;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | @Override public void visit(Visitor visitor) {
|
---|
53 | visitor.visit(this);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Create an identical clone of the argument (including the id)
|
---|
58 | */
|
---|
59 | public Way(Way clone) {
|
---|
60 | cloneFrom(clone);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Create an empty way without id. Use this only if you set meaningful
|
---|
65 | * values yourself.
|
---|
66 | */
|
---|
67 | public Way() {
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Create an incomplete Way.
|
---|
72 | */
|
---|
73 | public Way(long id) {
|
---|
74 | this.id = id;
|
---|
75 | incomplete = true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override public void cloneFrom(OsmPrimitive osm) {
|
---|
79 | super.cloneFrom(osm);
|
---|
80 | nodes.clear();
|
---|
81 | nodes.addAll(((Way)osm).nodes);
|
---|
82 | checkDirectionTagged();
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override public String toString() {
|
---|
86 | return "{Way id="+id+" version="+version+" nodes="+Arrays.toString(nodes.toArray())+"}";
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override public boolean realEqual(OsmPrimitive osm, boolean semanticOnly) {
|
---|
90 | return osm instanceof Way ? super.realEqual(osm, semanticOnly) && nodes.equals(((Way)osm).nodes) : false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public int compareTo(OsmPrimitive o) {
|
---|
94 | if(o instanceof Relation)
|
---|
95 | return 1;
|
---|
96 | return o instanceof Way ? Long.valueOf(id).compareTo(o.id) : -1;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public String getName() {
|
---|
100 | String name;
|
---|
101 | if (incomplete) {
|
---|
102 | name = tr("incomplete");
|
---|
103 | } else {
|
---|
104 | name = get("name");
|
---|
105 | if (name == null) name = get("ref");
|
---|
106 | if (name == null) {
|
---|
107 | name =
|
---|
108 | (get("highway") != null) ? tr("highway") :
|
---|
109 | (get("railway") != null) ? tr("railway") :
|
---|
110 | (get("waterway") != null) ? tr("waterway") :
|
---|
111 | (get("landuse") != null) ? tr("landuse") : "";
|
---|
112 | }
|
---|
113 |
|
---|
114 | int nodesNo = new HashSet<Node>(nodes).size();
|
---|
115 | name += trn(" ({0} node)", " ({0} nodes)", nodesNo, nodesNo);
|
---|
116 | }
|
---|
117 | return name;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public Boolean isClosed() {
|
---|
121 | int s = nodes.size();
|
---|
122 | return s >= 3 && nodes.get(0) == nodes.get(s-1);
|
---|
123 | }
|
---|
124 | }
|
---|