Debian Codegraph @

src/uk/me/parabola/imgfmt/app/trergn/RGNFileReader.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.trergn;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18
19 import uk.me.parabola.imgfmt.Utils;
20 import uk.me.parabola.imgfmt.app.BitReader;
21 import uk.me.parabola.imgfmt.app.BufferedImgFileReader;
22 import uk.me.parabola.imgfmt.app.Coord;
23 import uk.me.parabola.imgfmt.app.CoordNode;
24 import uk.me.parabola.imgfmt.app.ImgFileReader;
25 import uk.me.parabola.imgfmt.app.ImgReader;
26 import uk.me.parabola.imgfmt.app.Label;
27 import uk.me.parabola.imgfmt.app.lbl.LBLFileReader;
28 import uk.me.parabola.imgfmt.app.lbl.POIRecord;
29 import uk.me.parabola.imgfmt.app.net.NETFileReader;
30 import uk.me.parabola.imgfmt.app.net.RoadDef;
31 import uk.me.parabola.imgfmt.fs.ImgChannel;
32 import uk.me.parabola.log.Logger;
33 import uk.me.parabola.util.EnhancedProperties;
34
35 /**
36 * The region file. Holds actual details of points and lines etc.
37 *
38 * This is the view of the file when it is being read. Use {@link RGNFile}
39 * for writing the file.
40 *
41 * The main focus of mkgmap is creating files, there are plenty of applications
42 * that read and display the data, reading is implemented only to the
43 * extent required to support creating the various auxiliary files etc.
44 *
45 * @author Steve Ratcliffe
46 */
47 public class RGNFileReader extends ImgReader {
48 private static final Logger log = Logger.getLogger(RGNFileReader.class);
49
50 private final RGNHeader rgnHeader;
51 private LBLFileReader lblFile;
52 private NETFileReader netFile;
53 private static final int FLAG_DIR = 0x40;
54
55 public RGNFileReader(ImgChannel chan) {
56 rgnHeader = new RGNHeader();
57 setHeader(rgnHeader);
58
59 setReader(new BufferedImgFileReader(chan));
60 rgnHeader.readHeader(getReader());
61 }
62
63 public void config(EnhancedProperties props) {
64 //config = props;
65 }
66
67 /**
68 * Get a list of all points for the given subdivision. This includes
69 * both the indexed points section and the points section.
70 *
71 * The numbering of the points carries through the sections.
72 * @param sd The subdivision that we are interested in.
73 * @return A list of all points for the subdiv.
74 */
75 public List<Point> pointsForSubdiv(Subdivision sd, boolean withExtType) {
76 ArrayList<Point> list = new ArrayList<>();
77 if (sd.hasIndPoints() || sd.hasPoints()){
78
79 RgnOffsets rgnOffsets = getOffsets(sd);
80
81 // Even though the indexed points are after the points, the numbering
82 // starts with 1 for the first indexed point and carries on into the
83 // points section.
84 fetchPointsCommon(sd, rgnOffsets.getIndPointStart(), rgnOffsets.getIndPointEnd(), list);
85 fetchPointsCommon(sd, rgnOffsets.getPointStart(), rgnOffsets.getPointEnd(), list);
86 }
87 if (withExtType && sd.getExtTypePointsSize() > 0)
88 fetchPointsExtType(sd, list);
89
90 return list;
91 }
92
93 /**
94 * The indexed points and the points sections are both read just the same.
95 */
96 private void fetchPointsCommon(Subdivision sd, long start, long end, List<Point> points) {
97 position(start);
98 ImgFileReader reader = getReader();
99
100 int number = points.size() + 1;
101 while (position() < end) {
102 Point p = new Point(sd);
103
104 int t = reader.get1u();
105 int val = reader.get3u();
106 boolean hasSubtype = (val & 0x800000) != 0;
107 boolean hasPoi = (val & 0x400000) != 0;
108
109 Label l;
110 int labelOffset = val & 0x3fffff;
111 if (hasPoi) {
112 POIRecord record = lblFile.fetchPoi(labelOffset);
113 if (record != null) {
114 l = record.getNameLabel();
115 p.setPOIRecord(record);
116 } else {
117 l = lblFile.fetchLabel(0);
118 }
119 } else {
120 l = lblFile.fetchLabel(labelOffset);
121 }
122 p.setLabel(l);
123
124 p.setDeltaLong(reader.get2s());
125 p.setDeltaLat(reader.get2s());
126
127 t <<= 8;
128 if (hasSubtype) {
129 int st = reader.get1u();
130 t |= st;
131 }
132 p.setType(t);
133
134 p.setNumber(number++);
135 points.add(p);
136 }
137 }
138
139 /**
140 * The points with extended types
141 */
142 private void fetchPointsExtType(Subdivision sd, List<Point> points) {
143 long start = rgnHeader.getExtTypePointsOffset() + sd.getExtTypePointsOffset();
144 long end = start + sd.getExtTypePointsSize();
145 position(start);
146 ImgFileReader reader = getReader();
147
148 int number = points.size() + 1;
149 while (position() < end) {
150 Point p = new Point(sd);
151
152 int type = reader.get1u() << 8;
153 int b = reader.get1u();
154 type |= 0x10000 + (b & 0x1f);
155 p.setType(type);
156 p.setDeltaLong(reader.get2s());
157 p.setDeltaLat(reader.get2s());
158 Label l;
159 if ((b & 0x20) != 0 ){
160 int labelOffset = reader.get3u();
161 boolean hasPoi = (labelOffset & 0x400000) != 0;
162 if (hasPoi) {
163 POIRecord record = lblFile.fetchPoi(labelOffset);
164 if (record != null) {
165 l = record.getNameLabel();
166 p.setPOIRecord(record);
167 } else {
168 l = lblFile.fetchLabel(0);
169 }
170 } else {
171 l = lblFile.fetchLabel(labelOffset);
172 }
173 p.setLabel(l);
174 }
175
176 if ((b & 0x80) != 0){
177 extractExtraBytes(reader, p);
178 }
179 p.setNumber(number++);
180 points.add(p);
181 }
182 }
183
184 /**
185 * Get all the lines for a given subdivision.
186 * @param div The subdivision we want the line from.
187 * @return A list of lines.
188 */
189 public List<Polyline> linesForSubdiv(Subdivision div) {
190 ArrayList<Polyline> list = new ArrayList<>();
191
192 if (div.hasPolylines()){
193 RgnOffsets rgnOffsets = getOffsets(div);
194
195 int start = rgnOffsets.getLineStart();
196 int end = rgnOffsets.getLineEnd();
197
198 position(start);
199 while (position() < end) {
200 Polyline line = new Polyline(div);
201 readLineCommon(getReader(), div, line);
202 list.add(line);
203 }
204 }
205 if (div.getExtTypeLinesSize() > 0){
206 int start = rgnHeader.getExtTypeLinesOffset() + div.getExtTypeLinesOffset();
207 int end = start + div.getExtTypeLinesSize();
208 position(start);
209 while (position() < end) {
210 Polyline line = new Polyline(div);
211 readLineCommonExtType(getReader(), div, line);
212 list.add(line);
213 }
214 }
215 return list;
216 }
217
218 /**
219 * Get all the polygons for a given subdivision.
220 * @param witExtTypeData
221 */
222 public List<Polygon> shapesForSubdiv(Subdivision div, boolean witExtTypeData) {
223 ArrayList<Polygon> list = new ArrayList<>();
224 if (div.hasPolygons()){
225
226 RgnOffsets rgnOffsets = getOffsets(div);
227
228 int start = rgnOffsets.getPolygonStart();
229 int end = rgnOffsets.getPolygonEnd();
230
231 position(start);
232
233 while (position() < end) {
234 Polygon line = new Polygon(div);
235 readLineCommon(getReader(), div, line);
236 list.add(line);
237 line.setNumber(list.size());
238 }
239 }
240 if (witExtTypeData && div.getExtTypeAreasSize() > 0) {
241 int start = rgnHeader.getExtTypeAreasOffset() + div.getExtTypeAreasOffset();
242 int end = start + div.getExtTypeAreasSize();
243 position(start);
244 while (position() < end) {
245 Polygon line = new Polygon(div);
246 readLineCommonExtType(getReader(), div, line);
247 list.add(line);
248 }
249 }
250 return list;
251 }
252
253 /**
254 * Since polygons are pretty much like polylines in the img format the
255 * reading code can be shared.
256 *
257 * @param reader The reader for the img file.
258 * @param div The subdivision.
259 * @param line The line or shape that is to be populated.
260 */
261 private void readLineCommon(ImgFileReader reader, Subdivision div, Polyline line) {
262 int type = reader.get1u();
263 if (line instanceof Polygon)
264 line.setType(type & 0x7f);
265 else {
266 line.setType(type & 0x3f);
267 line.setDirection((type & FLAG_DIR) != 0);
268 }
269 int labelOffset = reader.get3u();
270 // Extra bit (for bit stream)
271 boolean extra = (labelOffset & 0x400000) != 0;
272 Label label;
273 if ((labelOffset & 0x800000) == 0) {
274 label = lblFile.fetchLabel(labelOffset & 0x7fffff);
275 } else {
276 int netoff = labelOffset & 0x3fffff;
277 labelOffset = netFile.getLabelOffset(netoff);
278 label = lblFile.fetchLabel(labelOffset);
279 RoadDef roadDef = new RoadDef(0, netoff, label.getText());
280 line.setRoadDef(roadDef);
281 }
282 line.setLabel(label);
283
284
285 line.setDeltaLong(reader.get2s());
286 line.setDeltaLat(reader.get2s());
287 int len;
288 if ((type & 0x80) == 0)
289 len = reader.get1u();
290 else
291 len = reader.get2u();
292
293 int base = reader.get1u();
294
295 byte[] bitstream = reader.get(len);
296 BitReader br = new BitReader(bitstream);
297 // This reads the bit stream and adds all the points found
298 readBitStream(br, div.getShift(), line, extra, len, base);
299 }
300
301
302 /**
303 * Common code to read extended type lines or polygons for a given sub division.
304 * @param reader The reader for the img file.
305 * @param div The subdivision.
306 * @param line The line or shape that is to be populated.
307 */
308 private void readLineCommonExtType(ImgFileReader reader, Subdivision div, Polyline line) {
309 int type = reader.get1u() << 8;
310 int b1 = reader.get1u();
311 boolean hasExtraBytes = (b1 & 0x80) != 0;
312 boolean hasLabel = (b1 & 0x20) != 0;
313 if (!(line instanceof Polygon)) {
314 line.setDirection((b1 & FLAG_DIR) != 0);
315 }
316 type |= 0x10000 + (b1 & 0x1f);
317 line.setType(type);
318 line.setDeltaLong(reader.get2s());
319 line.setDeltaLat(reader.get2s());
320 b1 = reader.get1u();
321 int len;
322 // one byte or two byte length field?
323 if ((b1 & 0x01) != 0){
324 len = (b1 >> 1) & 0x7f;
325 assert len < 0x7f;
326 }
327 else {
328 int b2 = reader.get1u();
329 len = ((b2 << 8) + b1) >> 2;
330 assert len >= 0x7f;
331 }
332 --len; // the encoded value includes the base field
333 assert len > 0;
334 int base = reader.get1u();
335 byte[] bitstream = reader.get(len);
336 BitReader br = new BitReader(bitstream);
337
338 // This reads the bit stream and adds all the points found,
339 readBitStream(br, div.getShift(), line, false, len, base);
340
341 if (hasLabel){
342 int labelOffset = reader.get3u();
343 Label label = lblFile.fetchLabel(labelOffset & 0x3fffff);
344 line.setLabel(label);
345 }
346 if (hasExtraBytes){
347 extractExtraBytes(reader, line);
348 }
349 }
350
351 /**
352 * Extract extra bytes.
353 * @param reader
354 */
355 void extractExtraBytes(ImgFileReader reader, MapObject o){
356 long pos = reader.position();
357 StringBuilder sb = new StringBuilder();
358 ArrayList<Byte> bytes = new ArrayList<>();
359 byte b1 = reader.get();
360 bytes.add(b1);
361 if ((b1 & 0xe0) != 0){
362 // varying length, search for 0x01 as this seems to be the terminator
363 do{
364 b1 = reader.get();
365 bytes.add(b1);
366 } while (b1 != 0x01);
367 } else if ((b1 & 0xa0) != 0){
368 bytes.add(reader.get());
369 bytes.add(reader.get());
370 }else if ((b1 & 0x80) != 0){
371 bytes.add(reader.get());
372 }
373 for (Byte b: bytes){
374 sb.append(String.format("%x", b));
375 }
376 ExtTypeAttributes eta = new ExtTypeAttributes(Collections.singletonMap("extra-bytes", sb.toString()), "data from img pos " + pos);
377 o.setExtTypeAttributes(eta);
378 }
379 /**
380 * Read the bit stream for a single line in the file.
381 * @param br The bit stream reader.
382 * @param shift delta values are shifted to the left
383 * @param line The line itself.
384 * @param extra True if there is an 'extra' bit in the stream. Used for nodes.
385 * @param len The length of the stream.
386 * @param base The base size of the deltas.
387 */
388 private void readBitStream(BitReader br, final int shift, Polyline line, boolean extra, int len, int base) {
389 int currLat = line.getLat();
390 int currLon = line.getLong();
391
392 log.debug(String.format("Start point %.5f,%.5f",
393 Utils.toDegrees(currLat),
394 Utils.toDegrees(currLon)));
395
396 if (extra)
397 line.addCoord(new CoordNode(currLat, currLon, 0/* XXX */, false, false));
398 else
399 line.addCoord(new Coord(currLat, currLon));
400
401 int xbase = 2;
402 int n = base & 0xf;
403 if (n <= 9)
404 xbase += n;
405 else
406 xbase += (2 * n) - 9;
407
408 n = (base >>> 4) & 0xf;
409 int ybase = 2;
410 if (n <= 9)
411 ybase += n;
412 else
413 ybase += (2 * n) - 9;
414
415 if (len == 0)
416 return;
417
418 boolean xneg = false;
419 boolean xsame = br.get1();
420 if (xsame) {
421 xneg = br.get1();
422 } else {
423 xbase++;
424 }
425
426 boolean ysame = br.get1();
427 boolean yneg = false;
428 if (ysame) {
429 yneg = br.get1();
430 } else {
431 ybase++;
432 }
433
434 if(line.hasExtendedType()) {
435 br.get1();
436 }
437
438 if (extra) {
439 boolean firstextra = br.get1();
440 log.debug("the first extra bit is", firstextra);
441 }
442
443 // All is now prepared, read the actual deltas and decode them into
444 // proper lat/long coords.
445 while (br.getBitPosition() <= 8* len - ((extra ? 1:0) + xbase + ybase)) {
446 br.getBitPosition();
447
448 int dx;
449 if (xsame) {
450 dx = br.get(xbase);
451 if (xneg)
452 dx = -dx;
453 } else {
454 dx = br.sget2(xbase);
455 }
456
457 int dy;
458 if (ysame) {
459 dy = br.get(ybase);
460 if (yneg)
461 dy = -dy;
462 } else {
463 dy = br.sget2(ybase);
464 }
465
466 boolean isnode = false;
467 if (extra)
468 isnode = br.get1();
469
470 // sometimes some 0 bits at the end of the bitstream are interpreted as an additional node
471 // skip it
472 if (!isnode && dx == 0 && dy == 0)
473 continue;
474
475 currLat += dy << shift;
476 currLon += dx << shift;
477 Coord coord;
478 if (isnode)
479 coord = new CoordNode(currLat, currLon, 0/* XXX */, false, false);
480 else
481 coord = new Coord(currLat, currLon);
482
483 line.addCoord(coord);
484 }
485 if (line instanceof Polygon) {
486 // make sure that polygon is closed
487 line.addCoord(line.getPoints().get(0));
488 }
489 }
490
491 /**
492 * Get the offsets to the points, lines etc in RGN for the given subdiv.
493 * @param sd The subdivision is needed to work out the starting points.
494 * @return An Offsets class that allows you to obtain the offsets.
495 */
496 private RgnOffsets getOffsets(Subdivision sd) {
497 int off = sd.getStartRgnPointer();
498 position(rgnHeader.getDataOffset() + off);
499
500 return new RgnOffsets(sd);
501 }
502
503 public void setLblFile(LBLFileReader lblFile) {
504 this.lblFile = lblFile;
505 }
506
507 public void setNetFile(NETFileReader netFile) {
508 this.netFile = netFile;
509 }
510
511 /**
512 * Class to hold the start and end points of point, lines etc within
513 * the area for a given subdivision in the RGN data.
514 */
515 private class RgnOffsets {
516 private final int pointOffset;
517 private int pointEnd;
518
519 private int indPointOffset;
520 private int indPointEnd;
521
522 private int lineOffset;
523 private int lineEnd;
524
525 private int polygonOffset;
526 private int polygonEnd;
527
528 private final int start;
529 private int headerLen;
530
531 /**
532 * Calculate the offsets for the given subdivision.
533 * After this is called the position will be set after any pointers that
534 * exist at the beginning of the area.
535 *
536 * @param sd The subdivision.
537 */
538 private RgnOffsets(Subdivision sd) {
539 ImgFileReader reader = getReader();
540
541 start = (int) position();
542
543 pointOffset = 0;
544
545 if (sd.needsIndPointPtr()) {
546 indPointOffset = reader.get2u();
547 headerLen += 2;
548 }
549
550 if (sd.needsPolylinePtr()) {
551 lineOffset = reader.get2u();
552 headerLen += 2;
553 }
554
555 if (sd.needsPolygonPtr()) {
556 polygonOffset = reader.get2u();
557 headerLen += 2;
558 }
559
560
561 if (sd.hasPoints()) {
562 if (sd.hasIndPoints())
563 pointEnd = indPointOffset;
564 else if (sd.hasPolylines())
565 pointEnd = lineOffset;
566 else if (sd.hasPolygons())
567 pointEnd = polygonOffset;
568 else
569 pointEnd = sd.getEndRgnPointer() - sd.getStartRgnPointer();
570 }
571 if (sd.hasIndPoints()) {
572 if (sd.hasPolylines())
573 indPointEnd = lineOffset;
574 else if (sd.hasPolygons())
575 indPointEnd = polygonOffset;
576 else
577 indPointEnd = sd.getEndRgnPointer() - sd.getStartRgnPointer();
578 }
579 if (sd.hasPolylines()) {
580 if (sd.hasPolygons())
581 lineEnd = polygonOffset;
582 else
583 lineEnd = sd.getEndRgnPointer() - sd.getStartRgnPointer();
584 }
585 if (sd.hasPolygons()) {
586 polygonEnd = sd.getEndRgnPointer() - sd.getStartRgnPointer();
587 }
588 }
589
590 public String toString() {
591 return String.format("rgn div offsets: %x-%x/%x-%x/%x-%x/%x-%x",
592 pointOffset, pointEnd, indPointOffset, indPointEnd,
593 lineOffset, lineEnd, polygonOffset, polygonEnd);
594 }
595
596 public long getPointStart() {
597 return pointOffset == 0 ? start + headerLen : start + pointOffset;
598 }
599
600 public long getPointEnd() {
601 return start + pointEnd;
602 }
603
604 public long getIndPointStart() {
605 return indPointOffset == 0 ? start + headerLen : start + indPointOffset;
606 }
607
608 public long getIndPointEnd() {
609 return start + indPointEnd;
610 }
611
612 public int getLineStart() {
613 return lineOffset == 0? start + headerLen: start + lineOffset;
614 }
615
616 public int getLineEnd() {
617 return start + lineEnd;
618 }
619
620 public int getPolygonStart() {
621 return polygonOffset == 0? start + headerLen: start + polygonOffset;
622 }
623
624 public int getPolygonEnd() {
625 return start + polygonEnd;
626 }
627 }
628 }