-
Notifications
You must be signed in to change notification settings - Fork 114
/
OsmNodeP.java
299 lines (254 loc) · 7.93 KB
/
OsmNodeP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Container for an osm node (pre-pocessor version)
*
* @author ab
*/
package btools.mapcreator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import btools.codec.MicroCache;
import btools.codec.MicroCache2;
public class OsmNodeP extends OsmLinkP {
/**
* The latitude
*/
public int ilat;
/**
* The longitude
*/
public int ilon;
/**
* The elevation
*/
public short selev;
public final static int NO_BRIDGE_BIT = 1;
public final static int NO_TUNNEL_BIT = 2;
public final static int BORDER_BIT = 4;
public final static int TRAFFIC_BIT = 8;
public final static int ANY_WAY_BIT = 16;
public final static int MULTI_WAY_BIT = 32;
public final static int DP_SURVIVOR_BIT = 64;
public byte bits = 0;
// interface OsmPos
public int getILat() {
return ilat;
}
public int getILon() {
return ilon;
}
public short getSElev() {
// if all bridge or all tunnel, elevation=no-data
return (bits & NO_BRIDGE_BIT) == 0 || (bits & NO_TUNNEL_BIT) == 0 ? Short.MIN_VALUE : selev;
}
public double getElev() {
return selev / 4.;
}
// populate and return the inherited link, if available,
// else create a new one
public OsmLinkP createLink(OsmNodeP source) {
if (sourceNode == null && targetNode == null) {
// inherited instance is available, use this
sourceNode = source;
targetNode = this;
source.addLink(this);
return this;
}
OsmLinkP link = new OsmLinkP(source, this);
addLink(link);
source.addLink(link);
return link;
}
// memory-squeezing-hack: OsmLinkP's "previous" also used as firstlink..
public void addLink(OsmLinkP link) {
link.setNext(previous, this);
previous = link;
}
public OsmLinkP getFirstLink() {
return sourceNode == null && targetNode == null ? previous : this;
}
public byte[] getNodeDecsription() {
return null;
}
public RestrictionData getFirstRestriction() {
return null;
}
public void writeNodeData(MicroCache mc) throws IOException {
boolean valid = true;
if (mc instanceof MicroCache2) {
valid = writeNodeData2((MicroCache2) mc);
} else
throw new IllegalArgumentException("unknown cache version: " + mc.getClass());
if (valid) {
mc.finishNode(getIdFromPos());
} else {
mc.discardNode();
}
}
public void checkDuplicateTargets() {
Map<OsmNodeP, OsmLinkP> targets = new HashMap<>();
for (OsmLinkP link0 = getFirstLink(); link0 != null; link0 = link0.getNext(this)) {
OsmLinkP link = link0;
OsmNodeP origin = this;
OsmNodeP target = null;
// first pass just to see if that link is consistent
while (link != null) {
target = link.getTarget(origin);
if (!target.isTransferNode()) {
break;
}
// next link is the one (of two), does does'nt point back
for (link = target.getFirstLink(); link != null; link = link.getNext(target)) {
if (link.getTarget(target) != origin)
break;
}
origin = target;
}
if (link == null) continue;
OsmLinkP oldLink = targets.put(target, link0);
if (oldLink != null) {
unifyLink(oldLink);
unifyLink(link0);
}
}
}
private void unifyLink(OsmLinkP link) {
if (link.isReverse(this)) return;
OsmNodeP target = link.getTarget(this);
if (target.isTransferNode()) {
target.incWayCount();
}
}
public boolean writeNodeData2(MicroCache2 mc) throws IOException {
boolean hasLinks = false;
// write turn restrictions
RestrictionData r = getFirstRestriction();
while (r != null) {
if (r.isValid() && r.fromLon != 0 && r.toLon != 0) {
mc.writeBoolean(true); // restriction follows
mc.writeShort(r.exceptions);
mc.writeBoolean(r.isPositive());
mc.writeInt(r.fromLon);
mc.writeInt(r.fromLat);
mc.writeInt(r.toLon);
mc.writeInt(r.toLat);
}
r = r.next;
}
mc.writeBoolean(false); // end restritions
mc.writeShort(getSElev());
mc.writeVarBytes(getNodeDecsription());
// buffer internal reverse links
List<OsmNodeP> internalReverse = new ArrayList<>();
for (OsmLinkP link0 = getFirstLink(); link0 != null; link0 = link0.getNext(this)) {
OsmLinkP link = link0;
OsmNodeP origin = this;
OsmNodeP target = null;
List<OsmNodeP> linkNodes = new ArrayList<>();
linkNodes.add(this);
// first pass just to see if that link is consistent
while (link != null) {
target = link.getTarget(origin);
linkNodes.add(target);
if (!target.isTransferNode()) {
break;
}
// next link is the one (of two), does does'nt point back
for (link = target.getFirstLink(); link != null; link = link.getNext(target)) {
if (link.getTarget(target) != origin)
break;
}
if (link != null && link.descriptionBitmap != link0.descriptionBitmap) {
throw new IllegalArgumentException("assertion failed: description change along transfer nodes");
}
origin = target;
}
if (link == null)
continue; // dead end
if (target == this)
continue; // self-ref
hasLinks = true;
// internal reverse links later
boolean isReverse = link0.isReverse(this);
if (isReverse) {
if (mc.isInternal(target.ilon, target.ilat)) {
internalReverse.add(target);
continue;
}
}
byte[] description = link0.descriptionBitmap;
// write link data
int sizeoffset = mc.writeSizePlaceHolder();
mc.writeVarLengthSigned(target.ilon - ilon);
mc.writeVarLengthSigned(target.ilat - ilat);
mc.writeModeAndDesc(isReverse, description);
if (!isReverse && linkNodes.size() > 2) { // write geometry for forward links only
DPFilter.doDPFilter(linkNodes);
origin = this;
for (int i = 1; i < linkNodes.size() - 1; i++) {
OsmNodeP tranferNode = linkNodes.get(i);
if ((tranferNode.bits & DP_SURVIVOR_BIT) != 0) {
mc.writeVarLengthSigned(tranferNode.ilon - origin.ilon);
mc.writeVarLengthSigned(tranferNode.ilat - origin.ilat);
mc.writeVarLengthSigned(tranferNode.getSElev() - origin.getSElev());
origin = tranferNode;
}
}
}
mc.injectSize(sizeoffset);
}
while (internalReverse.size() > 0) {
int nextIdx = 0;
if (internalReverse.size() > 1) {
int max32 = Integer.MIN_VALUE;
for (int i = 0; i < internalReverse.size(); i++) {
int id32 = mc.shrinkId(internalReverse.get(i).getIdFromPos());
if (id32 > max32) {
max32 = id32;
nextIdx = i;
}
}
}
OsmNodeP target = internalReverse.remove(nextIdx);
int sizeoffset = mc.writeSizePlaceHolder();
mc.writeVarLengthSigned(target.ilon - ilon);
mc.writeVarLengthSigned(target.ilat - ilat);
mc.writeModeAndDesc(true, null);
mc.injectSize(sizeoffset);
}
return hasLinks;
}
public String toString2() {
return (ilon - 180000000) + "_" + (ilat - 90000000) + "_" + (selev / 4);
}
public long getIdFromPos() {
return ((long) ilon) << 32 | ilat;
}
public boolean isBorderNode() {
return (bits & BORDER_BIT) != 0;
}
public boolean hasTraffic() {
return (bits & TRAFFIC_BIT) != 0;
}
/**
* Not really count the ways, just detect if more than one
*/
public void incWayCount() {
if ((bits & ANY_WAY_BIT) != 0) {
bits |= MULTI_WAY_BIT;
}
bits |= ANY_WAY_BIT;
}
public boolean isTransferNode() {
return (bits & BORDER_BIT) == 0 && (bits & MULTI_WAY_BIT) == 0 && _linkCnt() == 2;
}
private int _linkCnt() {
int cnt = 0;
for (OsmLinkP link = getFirstLink(); link != null; link = link.getNext(this)) {
cnt++;
}
return cnt;
}
}