| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.lbl; |
| 18 |
|
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import uk.me.parabola.imgfmt.app.Exit; |
| 22 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 23 |
import uk.me.parabola.imgfmt.app.Label; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class POIRecord { |
| 29 |
|
| 30 |
static final int HAS_STREET_NUM = 0x01; |
| 31 |
static final int HAS_STREET = 0x02; |
| 32 |
static final int HAS_CITY = 0x04; |
| 33 |
static final int HAS_ZIP = 0x08; |
| 34 |
static final int HAS_PHONE = 0x10; |
| 35 |
static final int HAS_EXIT = 0x20; |
| 36 |
static final int HAS_TIDE_PREDICTION = 0x40; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private int offset = -1; |
| 50 |
private Label poiName; |
| 51 |
|
| 52 |
private final SimpleStreetPhoneNumber simpleStreetNumber = new SimpleStreetPhoneNumber(); |
| 53 |
private final SimpleStreetPhoneNumber simplePhoneNumber = new SimpleStreetPhoneNumber(); |
| 54 |
|
| 55 |
private Label streetName; |
| 56 |
private Label streetNumberName; |
| 57 |
private Label complexPhoneNumber; |
| 58 |
|
| 59 |
private City city; |
| 60 |
private Zip zip; |
| 61 |
private Exit exit; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public void setLabel(Label label) { |
| 66 |
this.poiName = label; |
| 67 |
} |
| 68 |
|
| 69 |
public void setStreetName(Label label) { |
| 70 |
this.streetName = label; |
| 71 |
} |
| 72 |
|
| 73 |
public boolean setSimpleStreetNumber(String streetNumber) |
| 74 |
{ |
| 75 |
return simpleStreetNumber.set(streetNumber); |
| 76 |
} |
| 77 |
|
| 78 |
public void setComplexStreetNumber(Label label) |
| 79 |
{ |
| 80 |
streetNumberName = label; |
| 81 |
} |
| 82 |
|
| 83 |
public boolean setSimplePhoneNumber(String phone) |
| 84 |
{ |
| 85 |
return simplePhoneNumber.set(phone); |
| 86 |
} |
| 87 |
|
| 88 |
public void setComplexPhoneNumber(Label label) |
| 89 |
{ |
| 90 |
complexPhoneNumber = label; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
public void setZip(Zip zip) { |
| 95 |
this.zip = zip; |
| 96 |
} |
| 97 |
|
| 98 |
public void setCity(City city) |
| 99 |
{ |
| 100 |
this.city = city; |
| 101 |
} |
| 102 |
|
| 103 |
public void setExit(Exit exit) { |
| 104 |
this.exit = exit; |
| 105 |
} |
| 106 |
|
| 107 |
void write(ImgFileWriter writer, int POIGlobalFlags, int realofs, |
| 108 |
int cityPtrSize, int zipPtrSize, int highwayPtrSize, int exitFacilityPtrSize) { |
| 109 |
assert offset == realofs : "offset = " + offset + " realofs = " + realofs; |
| 110 |
int ptr = poiName.getOffset(); |
| 111 |
if (POIGlobalFlags != getPOIFlags()) |
| 112 |
ptr |= 0x800000; |
| 113 |
writer.put3u(ptr); |
| 114 |
|
| 115 |
if (POIGlobalFlags != getPOIFlags()) |
| 116 |
writer.put1u(getWrittenPOIFlags(POIGlobalFlags)); |
| 117 |
|
| 118 |
if (streetNumberName != null) |
| 119 |
{ |
| 120 |
int labOff = streetNumberName.getOffset(); |
| 121 |
|
| 122 |
writer.put1u(labOff >> 16); |
| 123 |
writer.put2u(labOff & 0xFFFF); |
| 124 |
} |
| 125 |
else if (simpleStreetNumber.isUsed()) |
| 126 |
simpleStreetNumber.write(writer); |
| 127 |
|
| 128 |
if (streetName != null) |
| 129 |
writer.put3u(streetName.getOffset()); |
| 130 |
|
| 131 |
if (city != null) |
| 132 |
writer.putNu(cityPtrSize, city.getIndex()); |
| 133 |
|
| 134 |
if (zip != null) |
| 135 |
writer.putNu(zipPtrSize, zip.getIndex()); |
| 136 |
|
| 137 |
if (complexPhoneNumber != null) |
| 138 |
{ |
| 139 |
int labOff = complexPhoneNumber.getOffset(); |
| 140 |
|
| 141 |
writer.put1u(labOff >> 16); |
| 142 |
writer.put2u(labOff & 0xFFFF); |
| 143 |
} |
| 144 |
else if (simplePhoneNumber.isUsed()) |
| 145 |
simplePhoneNumber.write(writer); |
| 146 |
|
| 147 |
if(exit != null) { |
| 148 |
Label description = exit.getDescription(); |
| 149 |
int val = 0; |
| 150 |
if(description != null) { |
| 151 |
val = description.getOffset(); |
| 152 |
assert val < 0x400000 : "Exit description label offset too large"; |
| 153 |
} |
| 154 |
if(exit.getOvernightParking()) |
| 155 |
val |= 0x400000; |
| 156 |
List<ExitFacility> facilites = exit.getFacilities(); |
| 157 |
ExitFacility ef = null; |
| 158 |
if(!facilites.isEmpty()) |
| 159 |
ef = facilites.get(0); |
| 160 |
if(ef != null) |
| 161 |
val |= 0x800000; |
| 162 |
writer.put3u(val); |
| 163 |
|
| 164 |
int highwayIndex = exit.getHighway().getIndex(); |
| 165 |
writer.putNu(highwayPtrSize, highwayIndex); |
| 166 |
|
| 167 |
if(ef != null) { |
| 168 |
int exitFacilityIndex = ef.getIndex(); |
| 169 |
writer.putNu(exitFacilityPtrSize, exitFacilityIndex); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
int getPOIFlags() { |
| 175 |
int b = 0; |
| 176 |
if (streetName != null) |
| 177 |
b |= HAS_STREET; |
| 178 |
if (simpleStreetNumber.isUsed() || streetNumberName != null) |
| 179 |
b |= HAS_STREET_NUM; |
| 180 |
if (city != null) |
| 181 |
b |= HAS_CITY; |
| 182 |
if (zip != null) |
| 183 |
b |= HAS_ZIP; |
| 184 |
if (simplePhoneNumber.isUsed() || complexPhoneNumber != null) |
| 185 |
b |= HAS_PHONE; |
| 186 |
if (exit != null) |
| 187 |
b |= HAS_EXIT; |
| 188 |
return b; |
| 189 |
} |
| 190 |
|
| 191 |
int getWrittenPOIFlags(int POIGlobalFlags) |
| 192 |
{ |
| 193 |
int flag = 0; |
| 194 |
int j = 0; |
| 195 |
|
| 196 |
int usedFields = getPOIFlags(); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
for(byte i = 0; i < 6; i++) |
| 203 |
{ |
| 204 |
int mask = 1 << i; |
| 205 |
|
| 206 |
if((mask & POIGlobalFlags) == mask) |
| 207 |
{ |
| 208 |
if((mask & usedFields) == mask) |
| 209 |
flag |= (1 << j); |
| 210 |
j++; |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
flag |= 0x80; |
| 216 |
|
| 217 |
return flag; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
int calcOffset(int ofs, int POIGlobalFlags, int cityPtrSize, int zipPtrSize, int highwayPtrSize, int exitFacilityPtrSize) { |
| 226 |
offset = ofs; |
| 227 |
int size = 3; |
| 228 |
if (exit != null) { |
| 229 |
size += 3; |
| 230 |
size += highwayPtrSize; |
| 231 |
if(!exit.getFacilities().isEmpty()) |
| 232 |
size += exitFacilityPtrSize; |
| 233 |
} |
| 234 |
if (POIGlobalFlags != getPOIFlags()) |
| 235 |
size += 1; |
| 236 |
if (simpleStreetNumber.isUsed()) |
| 237 |
size += simpleStreetNumber.getSize(); |
| 238 |
if (streetNumberName != null) |
| 239 |
size += 3; |
| 240 |
if (simplePhoneNumber.isUsed()) |
| 241 |
size += simplePhoneNumber.getSize(); |
| 242 |
if (complexPhoneNumber != null) |
| 243 |
size += 3; |
| 244 |
if (streetName != null) |
| 245 |
size += 3; |
| 246 |
if (city != null) |
| 247 |
size += cityPtrSize; |
| 248 |
if (zip != null) |
| 249 |
size += zipPtrSize; |
| 250 |
return size; |
| 251 |
} |
| 252 |
|
| 253 |
public int getOffset() { |
| 254 |
if (offset == -1) |
| 255 |
throw new IllegalStateException("Offset not known yet."); |
| 256 |
return offset; |
| 257 |
} |
| 258 |
|
| 259 |
public Label getNameLabel() { |
| 260 |
return poiName; |
| 261 |
} |
| 262 |
|
| 263 |
public City getCity() { |
| 264 |
return city; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
class SimpleStreetPhoneNumber { |
| 274 |
|
| 275 |
private byte[] encodedNumber; |
| 276 |
private int encodedSize; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public boolean set(String str) { |
| 285 |
|
| 286 |
|
| 287 |
String number = str.trim(); |
| 288 |
|
| 289 |
encodedNumber = new byte[(number.length()/2)+2]; |
| 290 |
|
| 291 |
int i = 0; |
| 292 |
int j = 0; |
| 293 |
while (i < number.length()) { |
| 294 |
|
| 295 |
int c1 = decodeChar(number.charAt(i++)); |
| 296 |
int c2 = (i < number.length()) ? decodeChar(number.charAt(i++)) : 10; |
| 297 |
|
| 298 |
|
| 299 |
if (c1 < 0 || c1 > 10 || c2 < 0 || c2 > 10) |
| 300 |
return false; |
| 301 |
|
| 302 |
|
| 303 |
int val = c1 * 11 + c2; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if (j == 0) |
| 308 |
val |= 0x80; |
| 309 |
|
| 310 |
encodedNumber[j++] = (byte)val; |
| 311 |
} |
| 312 |
if (j == 0) |
| 313 |
return false; |
| 314 |
|
| 315 |
if (j == 1) |
| 316 |
encodedNumber[j++] = (byte) 0xf8; |
| 317 |
else |
| 318 |
encodedNumber[j-1] |= 0x80; |
| 319 |
encodedSize = j; |
| 320 |
|
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
public void write(ImgFileWriter writer) |
| 325 |
{ |
| 326 |
writer.put(encodedNumber, 0, encodedSize); |
| 327 |
} |
| 328 |
|
| 329 |
public boolean isUsed() |
| 330 |
{ |
| 331 |
return (encodedSize > 0); |
| 332 |
} |
| 333 |
|
| 334 |
public int getSize() |
| 335 |
{ |
| 336 |
return encodedSize; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
private int decodeChar(char ch) { |
| 345 |
if (ch == '-') |
| 346 |
return 10; |
| 347 |
else if (ch >= '0' && ch <= '9') |
| 348 |
return (ch - '0'); |
| 349 |
else |
| 350 |
return -1; |
| 351 |
} |
| 352 |
} |
| 353 |
} |