source: josm/trunk/src/org/openstreetmap/josm/data/osm/Node.java@ 2437

Last change on this file since 2437 was 2437, checked in by jttt, 14 years ago

Cache bbox for Way

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm;
3
4import org.openstreetmap.josm.data.coor.CachedLatLon;
5import org.openstreetmap.josm.data.coor.EastNorth;
6import org.openstreetmap.josm.data.coor.LatLon;
7import org.openstreetmap.josm.data.osm.QuadBuckets.BBox;
8import org.openstreetmap.josm.data.osm.visitor.Visitor;
9
10/**
11 * One node data, consisting of one world coordinate waypoint.
12 *
13 * @author imi
14 */
15public final class Node extends OsmPrimitive {
16
17 private CachedLatLon coor;
18
19 public final void setCoor(LatLon coor) {
20 if(coor != null){
21 if(this.coor == null) {
22 this.coor = new CachedLatLon(coor);
23 } else {
24 this.coor.setCoor(coor);
25 }
26 if (getDataSet() != null) {
27 getDataSet().fireNodeMoved(this);
28 }
29 }
30 }
31
32 public final LatLon getCoor() {
33 return coor;
34 }
35
36 public final void setEastNorth(EastNorth eastNorth) {
37 if(eastNorth != null)
38 {
39 if(coor != null) {
40 coor.setEastNorth(eastNorth);
41 } else {
42 coor = new CachedLatLon(eastNorth);
43 }
44 if (getDataSet() != null) {
45 getDataSet().fireNodeMoved(this);
46 }
47 }
48 }
49
50 public final EastNorth getEastNorth() {
51 return coor != null ? coor.getEastNorth() : null;
52 }
53
54
55 protected Node(long id, boolean allowNegative) {
56 super(id, allowNegative);
57 }
58
59
60 /**
61 * Create a new local node.
62 *
63 */
64 public Node() {
65 this(0, false);
66 }
67
68
69 /**
70 * Create an incomplete Node object
71 */
72 public Node(long id) {
73 super(id, false);
74 }
75
76 /**
77 *
78 * @param clone
79 * @param clearId If true, set version to 0 and id to new unique value
80 */
81 public Node(Node clone, boolean clearId) {
82 super(clone.getUniqueId(), true);
83 cloneFrom(clone);
84 if (clearId) {
85 clearOsmId();
86 }
87 }
88
89 /**
90 * Create an identical clone of the argument (including the id)
91 */
92 public Node(Node clone) {
93 this(clone, false);
94 }
95
96 public Node(LatLon latlon) {
97 super(0, false);
98 setCoor(latlon);
99 }
100
101 public Node(EastNorth eastNorth) {
102 super(0, false);
103 setEastNorth(eastNorth);
104 }
105
106 @Override public void visit(Visitor visitor) {
107 visitor.visit(this);
108 }
109
110 @Override public void cloneFrom(OsmPrimitive osm) {
111 super.cloneFrom(osm);
112 setCoor(((Node)osm).coor);
113 }
114
115 /**
116 * Merges the technical and semantical attributes from <code>other</code> onto this.
117 *
118 * Both this and other must be new, or both must be assigned an OSM ID. If both this and <code>other</code>
119 * have an assigend OSM id, the IDs have to be the same.
120 *
121 * @param other the other primitive. Must not be null.
122 * @throws IllegalArgumentException thrown if other is null.
123 * @throws DataIntegrityProblemException thrown if either this is new and other is not, or other is new and this is not
124 * @throws DataIntegrityProblemException thrown if other is new and other.getId() != this.getId()
125 */
126 @Override
127 public void mergeFrom(OsmPrimitive other) {
128 super.mergeFrom(other);
129 if (!other.incomplete) {
130 setCoor(new LatLon(((Node)other).coor));
131 }
132 }
133
134 @Override public void load(PrimitiveData data) {
135 super.load(data);
136 setCoor(((NodeData)data).getCoor());
137 }
138
139 @Override public NodeData save() {
140 NodeData data = new NodeData();
141 saveCommonAttributes(data);
142 data.setCoor(getCoor());
143 return data;
144 }
145
146 @Override public String toString() {
147 String coorDesc = coor == null?"":"lat="+coor.lat()+",lon="+coor.lon();
148 return "{Node id=" + getUniqueId() + " version=" + getVersion() + " " + getFlagsAsString() + " " + coorDesc+"}";
149 }
150
151 @Override
152 public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
153 if (other == null || ! (other instanceof Node) )
154 return false;
155 if (! super.hasEqualSemanticAttributes(other))
156 return false;
157 Node n = (Node)other;
158 if (coor == null && n.coor == null)
159 return true;
160 else if (coor != null && n.coor != null)
161 return coor.equals(n.coor);
162 else
163 return false;
164 }
165
166 public int compareTo(OsmPrimitive o) {
167 return o instanceof Node ? Long.valueOf(getId()).compareTo(o.getId()) : 1;
168 }
169
170 @Override
171 public String getDisplayName(NameFormatter formatter) {
172 return formatter.format(this);
173 }
174
175 public OsmPrimitiveType getType() {
176 return OsmPrimitiveType.NODE;
177 }
178
179 @Override
180 public BBox getBBox() {
181 if (coor == null)
182 return new BBox(0, 0, 0, 0);
183 else
184 return new BBox(coor, coor);
185 }
186
187 @Override
188 public void updatePosition() {
189 // Do nothing for now, but in future replace CachedLatLon with simple doubles and update precalculated EastNorth value here
190 }
191}
Note: See TracBrowser for help on using the repository browser.