| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package uk.me.parabola.imgfmt.app.trergn; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
import java.io.OutputStream; |
| 18 |
import java.util.ArrayList; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import uk.me.parabola.imgfmt.app.BitWriter; |
| 22 |
import uk.me.parabola.imgfmt.app.Coord; |
| 23 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 24 |
import uk.me.parabola.imgfmt.app.Label; |
| 25 |
import uk.me.parabola.imgfmt.app.net.RoadDef; |
| 26 |
import uk.me.parabola.log.Logger; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class Polyline extends MapObject { |
| 40 |
private static final Logger log = Logger.getLogger(Polyline.class); |
| 41 |
|
| 42 |
|
| 43 |
private static final int FLAG_NETINFO = 0x800000; |
| 44 |
private static final int FLAG_EXTRABIT = 0x400000; |
| 45 |
|
| 46 |
|
| 47 |
private static final int FLAG_DIR = 0x40; |
| 48 |
private static final int FLAG_2BYTE_LEN = 0x80; |
| 49 |
|
| 50 |
|
| 51 |
private RoadDef roaddef; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private boolean lastSegment = true; |
| 57 |
|
| 58 |
|
| 59 |
private boolean direction; |
| 60 |
|
| 61 |
|
| 62 |
private List<Label> refLabels; |
| 63 |
|
| 64 |
|
| 65 |
private final List<Coord> points = new ArrayList<>(); |
| 66 |
|
| 67 |
public Polyline(Subdivision div) { |
| 68 |
setSubdiv(div); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public void write(ImgFileWriter file) { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
LinePreparer w; |
| 82 |
try { |
| 83 |
|
| 84 |
w = new LinePreparer(this); |
| 85 |
} catch (AssertionError ae) { |
| 86 |
log.error("Problem writing line (" + getClass() + ") of type 0x" + Integer.toHexString(getType()) + " containing " + points.size() + " points and starting at " + points.get(0).toOSMURL()); |
| 87 |
log.error(" Subdivision shift is " + getSubdiv().getShift() + |
| 88 |
" and its centre is at " + getSubdiv().getCenter().toOSMURL()); |
| 89 |
log.error(" " + ae.getMessage()); |
| 90 |
if(roaddef != null) |
| 91 |
log.error(" Way is " + roaddef); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
int minPointsRequired = (this instanceof Polygon)? 3 : 2; |
| 96 |
BitWriter bw = w.makeShortestBitStream(minPointsRequired); |
| 97 |
if (bw == null) { |
| 98 |
log.error("Level " + getSubdiv().getZoom().getLevel() + " " + ((this instanceof Polygon)? "polygon" : "polyline") + " has less than " + minPointsRequired + " points, discarding"); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
int b1 = getType(); |
| 104 |
if (direction) |
| 105 |
b1 |= FLAG_DIR; |
| 106 |
|
| 107 |
int blen = bw.getLength() - 1; |
| 108 |
assert blen > 0 : "zero length bitstream"; |
| 109 |
assert blen < 0x10000 : "bitstream too long " + blen; |
| 110 |
if (blen >= 0x100) |
| 111 |
b1 |= FLAG_2BYTE_LEN; |
| 112 |
|
| 113 |
file.put1u(b1); |
| 114 |
|
| 115 |
|
| 116 |
int loff = getLabel().getOffset(); |
| 117 |
if (w.isExtraBit()) |
| 118 |
loff |= FLAG_EXTRABIT; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if (roaddef != null) { |
| 123 |
roaddef.addLabel(getLabel()); |
| 124 |
roaddef.addOffsetTarget(file.position(), |
| 125 |
FLAG_NETINFO | (loff & FLAG_EXTRABIT)); |
| 126 |
|
| 127 |
if(refLabels != null) |
| 128 |
refLabels.forEach(roaddef::addLabel); |
| 129 |
} |
| 130 |
|
| 131 |
file.put3u(loff); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
file.put2s(getDeltaLong()); |
| 136 |
file.put2s(getDeltaLat()); |
| 137 |
if(log.isDebugEnabled()) |
| 138 |
log.debug("out center", getDeltaLat(), getDeltaLong()); |
| 139 |
|
| 140 |
file.putNu(blen < 0x100 ? 1 : 2, blen); |
| 141 |
|
| 142 |
file.put(bw.getBytes(), 0, blen+1); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public void write(OutputStream stream) throws IOException { |
| 151 |
assert hasExtendedType(); |
| 152 |
int type = getType(); |
| 153 |
int labelOff = getLabel().getOffset(); |
| 154 |
byte[] extraBytes = getExtTypeExtraBytes(); |
| 155 |
|
| 156 |
LinePreparer w; |
| 157 |
try { |
| 158 |
|
| 159 |
w = new LinePreparer(this); |
| 160 |
} |
| 161 |
catch (AssertionError ae) { |
| 162 |
log.error("Problem writing line (" + getClass() + ") of type 0x" + Integer.toHexString(getType()) + " containing " + points.size() + " points and starting at " + points.get(0).toOSMURL()); |
| 163 |
log.error(" Subdivision shift is " + getSubdiv().getShift() + |
| 164 |
" and its centre is at " + getSubdiv().getCenter().toOSMURL()); |
| 165 |
log.error(" " + ae.getMessage()); |
| 166 |
if(roaddef != null) |
| 167 |
log.error(" Way is " + roaddef); |
| 168 |
return; |
| 169 |
} |
| 170 |
int minPointsRequired = (this instanceof Polygon)? 3 : 2; |
| 171 |
BitWriter bw = w.makeShortestBitStream(minPointsRequired); |
| 172 |
if(bw == null) { |
| 173 |
log.error("Level " + getSubdiv().getZoom().getLevel() + " " + ((this instanceof Polygon)? "polygon" : "polyline") + " has less than " + minPointsRequired + " points, discarding"); |
| 174 |
return; |
| 175 |
} |
| 176 |
int blen = bw.getLength(); |
| 177 |
assert blen > 1 : "zero length bitstream"; |
| 178 |
assert blen < 0x10000 : "bitstream too long " + blen; |
| 179 |
|
| 180 |
if(labelOff != 0) |
| 181 |
type |= 0x20; |
| 182 |
if (direction) |
| 183 |
type |= FLAG_DIR; |
| 184 |
if(extraBytes != null) |
| 185 |
type |= 0x80; |
| 186 |
stream.write(type >> 8); |
| 187 |
stream.write(type); |
| 188 |
|
| 189 |
int deltaLong = getDeltaLong(); |
| 190 |
int deltaLat = getDeltaLat(); |
| 191 |
stream.write(deltaLong); |
| 192 |
stream.write(deltaLong >> 8); |
| 193 |
stream.write(deltaLat); |
| 194 |
stream.write(deltaLat >> 8); |
| 195 |
|
| 196 |
if (blen >= 0x7f) { |
| 197 |
stream.write((blen << 2) | 2); |
| 198 |
stream.write((blen << 2) >> 8); |
| 199 |
} |
| 200 |
else { |
| 201 |
stream.write((blen << 1) | 1); |
| 202 |
} |
| 203 |
|
| 204 |
stream.write(bw.getBytes(), 0, blen); |
| 205 |
|
| 206 |
if(labelOff != 0) { |
| 207 |
stream.write(labelOff); |
| 208 |
stream.write(labelOff >> 8); |
| 209 |
stream.write(labelOff >> 16); |
| 210 |
} |
| 211 |
|
| 212 |
if(extraBytes != null) |
| 213 |
stream.write(extraBytes); |
| 214 |
} |
| 215 |
|
| 216 |
public void addCoord(Coord co) { |
| 217 |
points.add(co); |
| 218 |
} |
| 219 |
|
| 220 |
public void addCoords(List<Coord> coords) { |
| 221 |
points.addAll(coords); |
| 222 |
} |
| 223 |
|
| 224 |
public List<Coord> getPoints() { |
| 225 |
return points; |
| 226 |
} |
| 227 |
|
| 228 |
public void setDirection(boolean direction) { |
| 229 |
this.direction = direction; |
| 230 |
} |
| 231 |
|
| 232 |
public boolean isRoad() { |
| 233 |
return roaddef != null; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
boolean hasInternalNodes() { |
| 240 |
if (isRoad() && (roaddef.hasHouseNumbers() || !roaddef.skipAddToNOD())) { |
| 241 |
|
| 242 |
for (int i = 1; i < points.size() - 1; i++) { |
| 243 |
Coord co = points.get(i); |
| 244 |
if (co.getId() > 0 || (roaddef.hasHouseNumbers() && co.isNumberNode())) { |
| 245 |
return true; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
public void setLastSegment(boolean last) { |
| 253 |
lastSegment = last; |
| 254 |
} |
| 255 |
|
| 256 |
public boolean isLastSegment() { |
| 257 |
return lastSegment; |
| 258 |
} |
| 259 |
|
| 260 |
public void setRoadDef(RoadDef rd) { |
| 261 |
this.roaddef = rd; |
| 262 |
} |
| 263 |
|
| 264 |
public int getOffsetNet1() { |
| 265 |
if (!isRoad()) |
| 266 |
return 0; |
| 267 |
return roaddef.getOffsetNet1(); |
| 268 |
} |
| 269 |
|
| 270 |
public boolean sharesNodeWith(Polyline other) { |
| 271 |
for (Coord p1 : points) { |
| 272 |
if (p1.getId() != 0) { |
| 273 |
|
| 274 |
|
| 275 |
for (Coord p2 : other.points) |
| 276 |
if (p1.getId() == p2.getId()) |
| 277 |
return true; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
public int getLat() { |
| 285 |
return getSubdiv().getLatitude() + (getDeltaLat() << getSubdiv().getShift()); |
| 286 |
} |
| 287 |
|
| 288 |
public int getLong() { |
| 289 |
return getSubdiv().getLongitude() + (getDeltaLong() << getSubdiv().getShift()); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public int getNodeCount(boolean countAllNodes ) { |
| 298 |
int count = 0; |
| 299 |
|
| 300 |
for (int i = 1; i< points.size() -1; i++) { |
| 301 |
Coord co = points.get(i); |
| 302 |
if (co.getId() > 0 || countAllNodes && co.isNumberNode()) |
| 303 |
count++; |
| 304 |
} |
| 305 |
return count; |
| 306 |
} |
| 307 |
|
| 308 |
public boolean hasHouseNumbers() { |
| 309 |
if (!isRoad()) |
| 310 |
return false; |
| 311 |
return roaddef.hasHouseNumbers(); |
| 312 |
} |
| 313 |
|
| 314 |
public void addRefLabel(Label refLabel) { |
| 315 |
if (refLabels == null) |
| 316 |
refLabels = new ArrayList<>(); |
| 317 |
refLabels.add(refLabel); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
} |