Debian Codegraph @

src/uk/me/parabola/imgfmt/app/net/NODHeader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
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 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 06-Jul-2008
16 */
17 package uk.me.parabola.imgfmt.app.net;
18
19 import java.util.Arrays;
20
21 import uk.me.parabola.imgfmt.Utils;
22 import uk.me.parabola.imgfmt.app.CommonHeader;
23 import uk.me.parabola.imgfmt.app.ImgFileReader;
24 import uk.me.parabola.imgfmt.app.ImgFileWriter;
25 import uk.me.parabola.imgfmt.app.Section;
26
27 /**
28 * Header information for the NOD file.
29 *
30 * This is a routing network for the map.
31 *
32 * @author Steve Ratcliffe
33 */
34 public class NODHeader extends CommonHeader {
35 public static final int HEADER_LEN = 127;
36
37 static final char DEF_ALIGN = 6;
38 private static final char BOUNDARY_ITEM_SIZE = 9;
39
40 private final Section nodes = new Section();
41 private final Section roads = new Section(nodes);
42 private final Section boundary = new Section(roads, BOUNDARY_ITEM_SIZE);
43 private final Section highClassBoundary = new Section(boundary);
44 private final int[] classBoundaries = new int[5];
45 private final Section nod5 = new Section();
46 private final Section nod6 = new Section();
47
48 private int flags;
49 private int align;
50 private int mult1;
51 private int tableARecordLen;
52 private boolean driveOnLeft;
53
54 private int indexIdSize;
55
56 public NODHeader() {
57 super(HEADER_LEN, "GARMIN NOD");
58 Arrays.fill(classBoundaries, Integer.MAX_VALUE);
59 }
60
61 /**
62 * Read the rest of the header. Specific to the given file. It is guaranteed
63 * that the file position will be set to the correct place before this is
64 * called.
65 *
66 * @param reader The header is read from here.
67 */
68 protected void readFileHeader(ImgFileReader reader) {
69 nodes.readSectionInfo(reader, false);
70 flags = reader.get2u();
71 reader.get2u();
72 align = reader.get1u();
73 mult1 = reader.get1u();
74 tableARecordLen = reader.get2u();
75 roads.readSectionInfo(reader, false, true);
76 boundary.readSectionInfo(reader, true, true);
77 if (getHeaderLength() > 0x3f) {
78 highClassBoundary.readSectionInfo(reader, false);
79
80 classBoundaries[0] = reader.get4();
81 classBoundaries[1] = classBoundaries[0] + reader.get4();
82 classBoundaries[2] = classBoundaries[1] + reader.get4();
83 classBoundaries[3] = classBoundaries[2] + reader.get4();
84 classBoundaries[4] = classBoundaries[3] + reader.get4();
85 }
86 if (getHeaderLength() >= 0x7f) {
87 reader.position(reader.getGMPOffset() + 0x67);
88 nod5.readSectionInfo(reader, false);
89 reader.get2u();
90 nod6.readSectionInfo(reader, true);
91 indexIdSize = Utils.numberToPointerSize(nod6.getNumItems());
92 }
93
94 }
95
96 /**
97 * Write the rest of the header. It is guaranteed that the writer will be set
98 * to the correct position before calling.
99 *
100 * @param writer The header is written here.
101 */
102
103 // multiplier shift for road + arc length values, the smaller the shift the higher the precision and NOD size
104 // as it has an influence on the number of bits needed to encode a length
105 static final int DISTANCE_MULT_SHIFT = 1; // 0..7 1 seems to be a good compromise
106 static final int DISTANCE_MULT = 1 << DISTANCE_MULT_SHIFT;
107 protected void writeFileHeader(ImgFileWriter writer) {
108 nodes.setPosition(HEADER_LEN);
109 nodes.writeSectionInfo(writer);
110
111 // 0x0001 always set, meaning ?
112 // 0x0002 (enable turn restrictions)
113 // 0x001c meaning ?
114 // 0x00E0 distance multiplier, effects predicted travel time
115 int flags = 0x0207;
116 assert Integer.bitCount(DISTANCE_MULT) == 1;
117 assert DISTANCE_MULT_SHIFT < 8;
118 flags |= DISTANCE_MULT_SHIFT << 5;
119 if(driveOnLeft)
120 flags |= 0x0100;
121
122 writer.put4(flags);
123
124 writer.put1u(DEF_ALIGN);
125 writer.put1u(0); // pointer multiplier
126 writer.put2u(5);
127
128 roads.writeSectionInfo(writer);
129 writer.put4(0);
130
131 boundary.writeSectionInfo(writer);
132 // new fields for header length > 0x3f
133 writer.put4(2); // no other value spotted, meaning ?
134 highClassBoundary.writeSectionInfo(writer);
135 writer.put4(classBoundaries[0]);
136 for (int i = 1; i < classBoundaries.length; i++){
137 writer.put4(classBoundaries[i] - classBoundaries[i-1]);
138 }
139 }
140
141 private static final double UNIT_TO_METER = 2.4;
142 public static int metersToRaw(double m) {
143 double d = m / (DISTANCE_MULT * UNIT_TO_METER);
144 return (int) Math.round(d);
145 }
146
147 public int getNodeStart() {
148 return nodes.getPosition();
149 }
150
151 public void setNodeStart(int start) {
152 nodes.setPosition(start);
153 }
154
155 public int getNodeSize() {
156 return nodes.getSize();
157 }
158
159 public void setNodeSize(int size) {
160 nodes.setSize(size);
161 }
162
163 public Section getNodeSection() {
164 return nodes;
165 }
166
167 public void setRoadSize(int size) {
168 roads.setSize(size);
169 }
170
171 public Section getRoadSection() {
172 return roads;
173 }
174
175 public void setBoundarySize(int size) {
176 boundary.setSize(size);
177 }
178
179 public Section getBoundarySection() {
180 return boundary;
181 }
182
183 public void setHighClassBoundarySize(int size) {
184 highClassBoundary.setSize(size);
185 }
186 public Section getHighClassBoundary() {
187 return highClassBoundary;
188 }
189
190 public int[] getClassBoundaries() {
191 return classBoundaries;
192 }
193
194 public void setDriveOnLeft(boolean dol) {
195 driveOnLeft = dol;
196 }
197
198 public int getFlags() {
199 return flags;
200 }
201
202 public int getAlign() {
203 return align;
204 }
205
206 public int getMult1() {
207 return mult1;
208 }
209
210 public int getTableARecordLen() {
211 return tableARecordLen;
212 }
213
214 /**
215 * @return the indexIdSize
216 */
217 public int getIndexIdSize() {
218 return indexIdSize;
219 }
220 }