1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.data.coor.CachedLatLon;
|
---|
5 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
6 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
7 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * One node data, consisting of one world coordinate waypoint.
|
---|
11 | *
|
---|
12 | * @author imi
|
---|
13 | */
|
---|
14 | public final class Node extends OsmPrimitive {
|
---|
15 |
|
---|
16 | private CachedLatLon coor;
|
---|
17 |
|
---|
18 | public final void setCoor(LatLon coor) {
|
---|
19 | if(coor != null){
|
---|
20 | updateCoor(coor, null);
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | public final void setEastNorth(EastNorth eastNorth) {
|
---|
25 | if(eastNorth != null) {
|
---|
26 | updateCoor(null, eastNorth);
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | private void updateCoor(LatLon coor, EastNorth eastNorth) {
|
---|
31 | if (getDataSet() != null) {
|
---|
32 | boolean locked = writeLock();
|
---|
33 | try {
|
---|
34 | getDataSet().fireNodeMoved(this, coor, eastNorth);
|
---|
35 | } finally {
|
---|
36 | writeUnlock(locked);
|
---|
37 | }
|
---|
38 | } else {
|
---|
39 | setCoorInternal(coor, eastNorth);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public final LatLon getCoor() {
|
---|
44 | return coor;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public final EastNorth getEastNorth() {
|
---|
48 | return coor != null ? coor.getEastNorth() : null;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * To be used only by Dataset.reindexNode
|
---|
53 | */
|
---|
54 | protected void setCoorInternal(LatLon coor, EastNorth eastNorth) {
|
---|
55 | if(this.coor == null) {
|
---|
56 | if (eastNorth == null) {
|
---|
57 | this.coor = new CachedLatLon(coor);
|
---|
58 | } else {
|
---|
59 | this.coor = new CachedLatLon(eastNorth);
|
---|
60 | }
|
---|
61 | } else {
|
---|
62 | if (eastNorth == null) {
|
---|
63 | this.coor.setCoor(coor);
|
---|
64 | } else {
|
---|
65 | this.coor.setEastNorth(eastNorth);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected Node(long id, boolean allowNegative) {
|
---|
71 | super(id, allowNegative);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Create a new local node.
|
---|
76 | *
|
---|
77 | */
|
---|
78 | public Node() {
|
---|
79 | this(0, false);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Create an incomplete Node object
|
---|
84 | */
|
---|
85 | public Node(long id) {
|
---|
86 | super(id, false);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Create new node
|
---|
91 | * @param id
|
---|
92 | * @param version
|
---|
93 | */
|
---|
94 | public Node(long id, int version) {
|
---|
95 | super(id, version, false);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | *
|
---|
100 | * @param clone
|
---|
101 | * @param clearId If true, set version to 0 and id to new unique value
|
---|
102 | */
|
---|
103 | public Node(Node clone, boolean clearId) {
|
---|
104 | super(clone.getUniqueId(), true /* allow negative IDs */);
|
---|
105 | cloneFrom(clone);
|
---|
106 | if (clearId) {
|
---|
107 | clearOsmId();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Create an identical clone of the argument (including the id)
|
---|
113 | */
|
---|
114 | public Node(Node clone) {
|
---|
115 | this(clone, false);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public Node(LatLon latlon) {
|
---|
119 | super(0, false);
|
---|
120 | setCoor(latlon);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public Node(EastNorth eastNorth) {
|
---|
124 | super(0, false);
|
---|
125 | setEastNorth(eastNorth);
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | void setDataset(DataSet dataSet) {
|
---|
130 | super.setDataset(dataSet);
|
---|
131 | if (!isIncomplete() && (getCoor() == null || getEastNorth() == null))
|
---|
132 | throw new DataIntegrityProblemException("Complete node with null coordinates: " + toString() + get3892DebugInfo());
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override public void visit(Visitor visitor) {
|
---|
136 | visitor.visit(this);
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override public void cloneFrom(OsmPrimitive osm) {
|
---|
140 | boolean locked = writeLock();
|
---|
141 | try {
|
---|
142 | super.cloneFrom(osm);
|
---|
143 | setCoor(((Node)osm).coor);
|
---|
144 | } finally {
|
---|
145 | writeUnlock(locked);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Merges the technical and semantical attributes from <code>other</code> onto this.
|
---|
151 | *
|
---|
152 | * Both this and other must be new, or both must be assigned an OSM ID. If both this and <code>other</code>
|
---|
153 | * have an assigend OSM id, the IDs have to be the same.
|
---|
154 | *
|
---|
155 | * @param other the other primitive. Must not be null.
|
---|
156 | * @throws IllegalArgumentException thrown if other is null.
|
---|
157 | * @throws DataIntegrityProblemException thrown if either this is new and other is not, or other is new and this is not
|
---|
158 | * @throws DataIntegrityProblemException thrown if other is new and other.getId() != this.getId()
|
---|
159 | */
|
---|
160 | @Override
|
---|
161 | public void mergeFrom(OsmPrimitive other) {
|
---|
162 | boolean locked = writeLock();
|
---|
163 | try {
|
---|
164 | super.mergeFrom(other);
|
---|
165 | if (!other.isIncomplete()) {
|
---|
166 | setCoor(new LatLon(((Node)other).coor));
|
---|
167 | }
|
---|
168 | } finally {
|
---|
169 | writeUnlock(locked);
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override public void load(PrimitiveData data) {
|
---|
174 | boolean locked = writeLock();
|
---|
175 | try {
|
---|
176 | super.load(data);
|
---|
177 | setCoor(((NodeData)data).getCoor());
|
---|
178 | } finally {
|
---|
179 | writeUnlock(locked);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override public NodeData save() {
|
---|
184 | NodeData data = new NodeData();
|
---|
185 | saveCommonAttributes(data);
|
---|
186 | if (!isIncomplete()) {
|
---|
187 | data.setCoor(getCoor());
|
---|
188 | }
|
---|
189 | return data;
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override public String toString() {
|
---|
193 | String coorDesc = coor == null?"":"lat="+coor.lat()+",lon="+coor.lon();
|
---|
194 | return "{Node id=" + getUniqueId() + " version=" + getVersion() + " " + getFlagsAsString() + " " + coorDesc+"}";
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Override
|
---|
198 | public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
199 | if (other == null || ! (other instanceof Node) )
|
---|
200 | return false;
|
---|
201 | if (! super.hasEqualSemanticAttributes(other))
|
---|
202 | return false;
|
---|
203 | Node n = (Node)other;
|
---|
204 | if (coor == null && n.coor == null)
|
---|
205 | return true;
|
---|
206 | else if (coor != null && n.coor != null)
|
---|
207 | return coor.equalsEpsilon(n.coor);
|
---|
208 | else
|
---|
209 | return false;
|
---|
210 | }
|
---|
211 |
|
---|
212 | public int compareTo(OsmPrimitive o) {
|
---|
213 | return o instanceof Node ? Long.valueOf(getUniqueId()).compareTo(o.getUniqueId()) : 1;
|
---|
214 | }
|
---|
215 |
|
---|
216 | @Override
|
---|
217 | public String getDisplayName(NameFormatter formatter) {
|
---|
218 | return formatter.format(this);
|
---|
219 | }
|
---|
220 |
|
---|
221 | public OsmPrimitiveType getType() {
|
---|
222 | return OsmPrimitiveType.NODE;
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public BBox getBBox() {
|
---|
227 | return new BBox(this);
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | public void updatePosition() {
|
---|
232 | // TODO: replace CachedLatLon with simple doubles and update precalculated EastNorth value here
|
---|
233 | }
|
---|
234 |
|
---|
235 | public boolean isConnectionNode() {
|
---|
236 | return isReferredByWays(2);
|
---|
237 | }
|
---|
238 |
|
---|
239 | public String get3892DebugInfo() {
|
---|
240 | StringBuilder builder = new StringBuilder();
|
---|
241 | builder.append("Unexpected error. Please report it to http://josm.openstreetmap.de/ticket/3892\n");
|
---|
242 | builder.append(toString());
|
---|
243 | builder.append("\n");
|
---|
244 | if (coor == null) {
|
---|
245 | builder.append("Coor is null\n");
|
---|
246 | } else {
|
---|
247 | builder.append(String.format("EastNorth: %s\n", coor.getEastNorth()));
|
---|
248 | builder.append(coor.getProjection());
|
---|
249 | builder.append("\n");
|
---|
250 | }
|
---|
251 |
|
---|
252 | return builder.toString();
|
---|
253 | }
|
---|
254 | }
|
---|