Debian Codegraph @

src/uk/me/parabola/imgfmt/app/lbl/LBLFileReader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2009.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.lbl;
14
15 import static uk.me.parabola.imgfmt.app.Label.NULL_LABEL;
16
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
24 import uk.me.parabola.imgfmt.Utils;
25 import uk.me.parabola.imgfmt.app.BufferedImgFileReader;
26 import uk.me.parabola.imgfmt.app.ImgFile;
27 import uk.me.parabola.imgfmt.app.ImgFileReader;
28 import uk.me.parabola.imgfmt.app.Label;
29 import uk.me.parabola.imgfmt.app.Section;
30 import uk.me.parabola.imgfmt.app.labelenc.CharacterDecoder;
31 import uk.me.parabola.imgfmt.app.labelenc.CodeFunctions;
32 import uk.me.parabola.imgfmt.app.labelenc.DecodedText;
33 import uk.me.parabola.imgfmt.app.trergn.Subdivision;
34 import uk.me.parabola.imgfmt.fs.ImgChannel;
35 import uk.me.parabola.log.Logger;
36
37 /**
38 * The file that holds all the labels for the map.
39 *
40 * There are also a number of sections that hold country,
41 * region, city, etc. records.
42 *
43 * The main focus of mkgmap is creating files, there are plenty of applications
44 * that read and display the data, reading is implemented only to the
45 * extent required to support creating the various auxiliary files etc.
46 *
47 * @author Steve Ratcliffe
48 */
49 public class LBLFileReader extends ImgFile {
50
51 private CharacterDecoder textDecoder = CodeFunctions.getDefaultDecoder();
52
53 private final LBLHeader header = new LBLHeader();
54
55 private final Int2ObjectOpenHashMap<Label> labels = new Int2ObjectOpenHashMap<>();
56 private final Int2ObjectOpenHashMap<POIRecord> pois = new Int2ObjectOpenHashMap<>();
57 private final List<Country> countries = new ArrayList<>();
58 private final List<Region> regions = new ArrayList<>();
59 private final Int2ObjectOpenHashMap<Zip> zips = new Int2ObjectOpenHashMap<>();
60 private final List<City> cities = new ArrayList<>();
61
62 public LBLFileReader(ImgChannel chan) {
63 this(chan, true, 0);
64 }
65
66 public LBLFileReader(ImgChannel chan, boolean fullData, int gmpOffset) {
67 setHeader(header);
68
69 setReader(new BufferedImgFileReader(chan, gmpOffset));
70 header.readHeader(getReader());
71 if (!fullData)
72 return;
73
74 int offsetMultiplier = header.getOffsetMultiplier();
75 CodeFunctions funcs = CodeFunctions.createEncoderForLBL(
76 header.getEncodingType(), header.getCodePage());
77 textDecoder = funcs.getDecoder();
78
79 readLables(offsetMultiplier);
80
81 readCountries();
82 readRegions();
83
84 readCities();
85 readZips();
86 readPoiInfo();
87 }
88
89 /**
90 * Get a label by its offset in the label area.
91 * @param offset The offset in the label section. The offset 0 always
92 * is an empty string.
93 * @return The label including its text.
94 */
95 public Label fetchLabel(int offset) {
96 Label label = labels.get(offset);
97 if (label == null) {
98 assert offset == 0 : "Invalid label offset found " + offset;
99 return NULL_LABEL;
100 }
101
102 return label;
103 }
104
105 /**
106 * Get a list of cites. This is not cached here.
107 * @return A list of City objects.
108 */
109 public List<City> getCities() {
110 return cities;
111 }
112
113 public List<Country> getCountries() {
114 return Collections.unmodifiableList(countries);
115 }
116
117 public List<Region> getRegions() {
118 return Collections.unmodifiableList(regions);
119 }
120
121 public List<Zip> getZips() {
122 return new ArrayList<>(zips.values());
123 }
124
125 /**
126 * Return POI information.
127 * @param offset The offset of the poi information in the header.
128 * @return Returns a poi record at the given offset. Returns null if
129 * there isn't one at that offset (probably a bug if that does happen though...).
130 */
131 public POIRecord fetchPoi(int offset) {
132 return pois.get(offset);
133 }
134
135 /**
136 * Read a cache the countries. These are used when reading cities.
137 */
138 private void readCountries() {
139 ImgFileReader reader = getReader();
140
141 PlacesHeader placeHeader = header.getPlaceHeader();
142
143 countries.add(null); // 1 based indexes
144 Section s = placeHeader.getCountrySection();
145 int start = s.getPosition();
146 int end = s.getEndPos();
147 int recSize = s.getItemSize();
148 int unkSize = recSize - 3; // new maps may have extra data
149 reader.position(start);
150 int index = 1;
151 while (reader.position() < end) {
152 int offset = reader.get3u();
153 Label label = fetchLabel(offset & 0x3fffff);
154 if (unkSize > 0)
155 reader.get(unkSize);
156
157 if (label != null) {
158 Country country = new Country(index, label);
159 countries.add(country);
160 }
161 index++;
162 }
163 }
164
165 /**
166 * Read an cache the regions. These are used when reading cities.
167 */
168 private void readRegions() {
169 ImgFileReader reader = getReader();
170
171 PlacesHeader placeHeader = header.getPlaceHeader();
172 Section s = placeHeader.getRegionSection();
173 int start = s.getPosition();
174 int end = s.getEndPos();
175 int recSize = s.getItemSize();
176 int unkSize = recSize - 5; // new maps may have extra data
177
178 regions.add(null);
179
180 reader.position(start);
181 int index = 1;
182 while (reader.position() < end) {
183 int country = reader.get2u();
184 int offset = reader.get3u();
185 if (unkSize > 0)
186 reader.get(unkSize);
187 Label label = fetchLabel(offset & 0x3fffff);
188 if (label != null) {
189 Region region = new Region(countries.get(country), label);
190 region.setIndex(index);
191
192 regions.add(region);
193 }
194
195 index++;
196 }
197 }
198
199 /**
200 * Read in the city section and cache the results here. They are needed
201 * to read in the POI properties section.
202 */
203 private void readCities() {
204 PlacesHeader placeHeader = header.getPlaceHeader();
205 Section s = placeHeader.getCitySection();
206 int start = s.getPosition();
207 int end = s.getEndPos();
208 int recSize = s.getItemSize();
209 int unkSize = recSize - 5; // new maps may have extra data
210 ImgFileReader reader = getReader();
211
212 // Since cities are indexed starting from 1, we add a null one at index 0
213
214 reader.position(start);
215 int index = 1;
216 while (reader.position() < end) {
217 // First is either a label offset or a point/subdiv combo, we
218 // don't know until we have read further
219 int label = reader.get3u();
220 int info = reader.get2u();
221 if (unkSize > 0)
222 reader.get(unkSize);
223
224 City city;
225 if ((info & 0x4000) == 0) {
226 Region region = regions.get(info & 0x3fff);
227 city = new City(region);
228 } else {
229 Country country = countries.get(info & 0x3fff);
230 city = new City(country);
231 }
232
233 city.setIndex(index);
234 if ((info & 0x8000) == 0) {
235 city.setSubdivision(Subdivision.createEmptySubdivision(1));
236 Label label1 = fetchLabel(label & 0x3fffff);
237 city.setLabel(label1);
238 } else {
239 // Has subdiv/point index
240 int pointIndex = label & 0xff;
241 int subdiv = (label >> 8) & 0xffff;
242 city.setPointIndex(pointIndex);
243 city.setSubdivision(Subdivision.createEmptySubdivision(subdiv));
244 }
245 cities.add(city);
246
247 index++;
248 }
249 }
250
251 /**
252 * Read and cache all the labels.
253 *
254 * Note: It is pretty pointless saving the whole label rather than just
255 * the text, except that other objects take a Label. Perhaps this can
256 * be changed.
257 */
258 private void readLables(int mult) {
259 ImgFileReader reader = getReader();
260
261 labels.put(0, NULL_LABEL);
262
263 int start = header.getLabelStart();
264 int size = header.getLabelSize();
265
266 reader.position((long) start + mult);
267 int labelOffset = mult;
268
269 for (int off = mult; off <= size; off++) {
270 byte b = reader.get();
271 if (textDecoder.addByte(b)) {
272 labelOffset = saveLabel(labelOffset, off, mult);
273
274 // If there is an offset multiplier greater than one then padding will be used to
275 // ensure that the labels are on suitable boundaries. We must skip over any such padding.
276 while ((labelOffset & (mult - 1)) != 0) {
277 textDecoder.reset();
278 if (labelOffset <= off) {
279 // In the 6bit decoder, we may have already read the (first) padding byte and so
280 // we increment the label offset without reading anything more.
281 labelOffset++;
282 } else {
283 reader.get();
284 //noinspection AssignmentToForLoopParameter
285 off++;
286 labelOffset++;
287 }
288 }
289 }
290 }
291 }
292
293 /**
294 * We have a label and we need to save it.
295 *
296 * @param labelOffset The offset of the label we are about to save.
297 * @param currentOffset The current offset that last read from.
298 * @param multiplier The label offset multiplier.
299 * @return The offset of the next label.
300 */
301 private int saveLabel(int labelOffset, int currentOffset, int multiplier) {
302 DecodedText encText = textDecoder.getText();
303 String text = encText.getText();
304
305 Label label = new Label(text);
306 assert (labelOffset & (multiplier - 1)) == 0;
307 int adustedOffset = labelOffset / multiplier;
308 label.setOffset(adustedOffset);
309 labels.put(adustedOffset, label);
310
311 // Calculate the offset of the next label. This is not always
312 // the current offset + 1 because there may be bytes left
313 // inside the decoder.
314 return currentOffset + 1 + encText.getOffsetAdjustment();
315 }
316
317
318 /**
319 * Reads the zips.
320 */
321 private void readZips() {
322 ImgFileReader reader = getReader();
323
324 PlacesHeader placeHeader = header.getPlaceHeader();
325 Section s = placeHeader.getZipSection();
326 int start = s.getPosition();
327 int end = s.getEndPos();
328 int recSize = s.getItemSize();
329 int unkSize = recSize - 3; // new maps may have extra data
330
331 reader.position(start);
332
333 int zipIndex = 1;
334 while (reader.position() < end) {
335 int lblOffset = reader.get3u();
336 if (unkSize > 0)
337 reader.get(unkSize);
338
339 Zip zip = new Zip(fetchLabel(lblOffset & 0x3fffff));
340 zip.setIndex(zipIndex);
341
342 zips.put(zip.getIndex(), zip);
343
344 zipIndex++;
345 }
346 }
347
348 /**
349 * Read all the POI information.
350 * This will create a POIRecord, but we just get the name at the minute.
351 *
352 * TODO: not finished
353 */
354 private void readPoiInfo() {
355 ImgFileReader reader = getReader();
356
357 PlacesHeader placeHeader = header.getPlaceHeader();
358 int poiGlobalFlags = placeHeader.getPOIGlobalFlags();
359
360 int start = placeHeader.getPoiPropertiesStart();
361 int end = placeHeader.getPoiPropertiesEnd();
362
363 reader.position(start);
364
365 PoiMasks localMask = makeLocalMask(placeHeader);
366
367 while (reader.position() < end) {
368 int poiOffset = position() - start;
369 int val = reader.get3u();
370 int labelOffset = val & 0x3fffff;
371
372
373 boolean override = (val & 0x800000) != 0;
374
375 POIRecord poi = new POIRecord();
376 poi.setLabel(fetchLabel(labelOffset));
377
378 // We have what we want, but now have to find the start of the
379 // next record as they are not fixed length.
380 int flags;
381 boolean hasStreet;
382 boolean hasStreetNum;
383 boolean hasCity;
384 boolean hasZip;
385 boolean hasPhone;
386 boolean hasHighwayExit;
387 boolean hasTides;
388
389 if (override) {
390 flags = reader.get1u();
391
392 hasStreetNum = (flags & localMask.streetNumMask) != 0;
393 hasStreet = (flags & localMask.streetMask) != 0;
394 hasCity = (flags & localMask.cityMask) != 0;
395 hasZip = (flags & localMask.zipMask) != 0;
396 hasPhone = (flags & localMask.phoneMask) != 0;
397 hasHighwayExit = (flags & localMask.highwayExitMask) != 0;
398 hasTides = (flags & localMask.tidesMask) != 0;
399 } else {
400 flags = poiGlobalFlags;
401
402 hasStreetNum = (flags & POIRecord.HAS_STREET_NUM) != 0;
403 hasStreet = (flags & POIRecord.HAS_STREET) != 0;
404 hasCity = (flags & POIRecord.HAS_CITY) != 0;
405 hasZip = (flags & POIRecord.HAS_ZIP) != 0;
406 hasPhone = (flags & POIRecord.HAS_PHONE) != 0;
407 hasHighwayExit = (flags & POIRecord.HAS_EXIT) != 0;
408 hasTides = (flags & POIRecord.HAS_TIDE_PREDICTION) != 0;
409 }
410
411 if (hasStreetNum) {
412 byte b = reader.get();
413 if ((b & 0x80) == 0) {
414 int mpoffset = (b << 16) & 0xff0000;
415 mpoffset |= reader.get2u();
416
417 poi.setComplexStreetNumber(fetchLabel(mpoffset));
418 } else {
419 poi.setSimpleStreetNumber(reader.getBase11str(b, '-'));
420 }
421 }
422
423 if (hasStreet) {
424 int streetNameOffset = reader.get3u();// label for street
425 Label label = fetchLabel(streetNameOffset);
426 poi.setStreetName(label);
427 }
428
429 if (hasCity) {
430 int cityIndex = reader.getNu(Utils.numberToPointerSize(placeHeader.getNumCities()));
431 poi.setCity(cities.get(cityIndex-1));
432 }
433
434 if (hasZip) {
435 int zipIndex = reader.getNu(Utils.numberToPointerSize(placeHeader.getNumZips()));
436 poi.setZip(zips.get(zipIndex-1));
437 }
438
439 if (hasPhone) {
440 byte b = reader.get();
441 if ((b & 0x80) == 0) {
442 // Yes this is a bit strange it is a byte followed by a char
443 int mpoffset = (b << 16) & 0xff0000;
444 mpoffset |= reader.get2u();
445
446 poi.setComplexPhoneNumber(fetchLabel(mpoffset));
447 } else {
448 poi.setSimplePhoneNumber(reader.getBase11str(b, '-'));
449 }
450 }
451
452 if (hasHighwayExit) {
453 int lblinfo = reader.get3u();
454 int highwayLabelOffset = lblinfo & 0x3FFFF;
455 boolean indexed = (lblinfo & 0x800000) != 0;
456 boolean overnightParking = (lblinfo & 0x400000) != 0;
457
458 int highwayIndex = reader.getNu(Utils.numberToPointerSize(placeHeader.getNumHighways()));
459 if (indexed) {
460 int eidx = reader.getNu(Utils.numberToPointerSize(placeHeader.getNumExits()));
461 }
462 }
463
464 if (hasTides) {
465 Logger.defaultLogger.warn("Map has tide prediction, please implement!");
466 }
467
468 pois.put(poiOffset, poi);
469 }
470 }
471
472 /**
473 * The meaning of the bits in the local flags depends on which bits
474 * are set in the global flags. Hence we have to calculate the
475 * masks to use. These are held in an instance of PoiMasks
476 * @param placeHeader The label header.
477 * @return The masks as modified by the global flags.
478 */
479 private PoiMasks makeLocalMask(PlacesHeader placeHeader) {
480 int globalPoi = placeHeader.getPOIGlobalFlags();
481
482 int mask= 0x1;
483
484 boolean hasStreetNum = (globalPoi & POIRecord.HAS_STREET_NUM) != 0;
485 boolean hasStreet = (globalPoi & POIRecord.HAS_STREET) != 0;
486 boolean hasCity = (globalPoi & POIRecord.HAS_CITY) != 0;
487 boolean hasZip = (globalPoi & POIRecord.HAS_ZIP) != 0;
488 boolean hasPhone = (globalPoi & POIRecord.HAS_PHONE) != 0;
489 boolean hasHighwayExit = (globalPoi & POIRecord.HAS_EXIT) != 0;
490 boolean hasTides = (globalPoi & POIRecord.HAS_TIDE_PREDICTION) != 0;
491
492 PoiMasks localMask = new PoiMasks();
493
494 if (hasStreetNum) {
495 localMask.streetNumMask = mask;
496 mask <<= 1;
497 }
498
499 if (hasStreet) {
500 localMask.streetMask = mask;
501 mask <<= 1;
502 }
503
504 if (hasCity) {
505 localMask.cityMask = mask;
506 mask <<= 1;
507 }
508
509 if (hasZip) {
510 localMask.zipMask = mask;
511 mask <<= 1;
512 }
513
514 if (hasPhone) {
515 localMask.phoneMask = mask;
516 mask <<= 1;
517 }
518
519 if (hasHighwayExit) {
520 localMask.highwayExitMask = mask;
521 mask <<= 1;
522 }
523
524 if (hasTides) {
525 localMask.tidesMask = mask;
526 mask <<= 1;
527 }
528
529 return localMask;
530 }
531
532 public Map<Integer, String> getLabels() {
533 Map<Integer, String> m = new HashMap<>();
534 for (Map.Entry<Integer, Label> ent : labels.entrySet()) {
535 m.put(ent.getKey(), ent.getValue().getText());
536 }
537 return m;
538 }
539
540 public int getCodePage() {
541 return header.getCodePage();
542 }
543
544 public int getSortOrderId() {
545 return header.getSortOrderId();
546 }
547
548 private class PoiMasks {
549 private int streetNumMask;
550 private int streetMask;
551 private int cityMask;
552 private int zipMask;
553 private int phoneMask;
554 private int highwayExitMask;
555 private int tidesMask;
556 }
557
558 public int getEncodingType() {
559 return header.getEncodingType();
560 }
561 }