Debian Codegraph @

src/uk/me/parabola/imgfmt/app/trergn/TREHeader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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: Dec 14, 2007
16 */
17 package uk.me.parabola.imgfmt.app.trergn;
18
19 import java.io.IOException;
20
21 import uk.me.parabola.imgfmt.ReadFailedException;
22 import uk.me.parabola.imgfmt.Utils;
23 import uk.me.parabola.imgfmt.app.Area;
24 import uk.me.parabola.imgfmt.app.CommonHeader;
25 import uk.me.parabola.imgfmt.app.ImgFileReader;
26 import uk.me.parabola.imgfmt.app.ImgFileWriter;
27 import uk.me.parabola.imgfmt.app.Section;
28 import uk.me.parabola.log.Logger;
29 import uk.me.parabola.util.EnhancedProperties;
30
31 /**
32 * @author Steve Ratcliffe
33 */
34 public class TREHeader extends CommonHeader {
35 private static final Logger log = Logger.getLogger(TREHeader.class);
36
37 // The tre section comes in different versions with different length
38 // headers. We just refer to them by the header length for lack of any
39 // better description.
40 public static final int TRE_120 = 120;
41 public static final int TRE_184 = 184;
42 private static final int TRE_188 = 188;
43
44 // The header length to use when creating a file.
45 private static final int DEFAULT_HEADER_LEN = TRE_188;
46
47 // A map has a display priority that determines which map is on top
48 // when two maps cover the same area.
49 private static final int DEFAULT_DISPLAY_PRIORITY = 0x19;
50
51 static final int MAP_LEVEL_REC_SIZE = 4;
52 private static final char POLYLINE_REC_LEN = 2;
53 private static final char POLYGON_REC_LEN = 2;
54 private static final char POINT_REC_LEN = 3;
55 private static final char COPYRIGHT_REC_SIZE = 0x3;
56 private static final char EXT_TYPE_OFFSETS_REC_LEN = 13;
57 private static final char EXT_TYPE_OVERVIEWS_REC_LEN = 4;
58 static final int SUBDIV_REC_SIZE = 14;
59 static final int SUBDIV_REC_SIZE2 = 16;
60
61 public static final int POI_FLAG_DETAIL = 0x1;
62 public static final int POI_FLAG_TRANSPARENT = 0x2;
63 public static final int POI_FLAG_STREET_BEFORE_HOUSENUMBER = 0x4;
64 public static final int POI_FLAG_POSTALCODE_BEFORE_CITY = 0x8;
65 public static final int POI_FLAG_DRIVE_ON_LEFT = 0x20;
66
67 // Bounding box. All units are in map units.
68 private Area area = new Area(0,0,0,0);
69
70 private int mapInfoSize;
71
72 private int mapLevelPos;
73 private int mapLevelsSize;
74
75 private int subdivPos;
76 private int subdivSize;
77
78 private int poiDisplayFlags;
79
80 private int displayPriority = DEFAULT_DISPLAY_PRIORITY;
81
82 private final Section copyright = new Section(COPYRIGHT_REC_SIZE);
83 private final Section polyline = new Section(POLYLINE_REC_LEN);
84 private final Section polygon = new Section(POLYGON_REC_LEN);
85 private final Section points = new Section(POINT_REC_LEN);
86 private final Section extTypeOffsets = new Section(points, EXT_TYPE_OFFSETS_REC_LEN);
87 private final Section extTypeOverviews = new Section(extTypeOffsets, EXT_TYPE_OVERVIEWS_REC_LEN);
88
89 private int numExtTypeAreaTypes;
90 private int numExtTypeLineTypes;
91 private int numExtTypePointTypes;
92
93 private int mapId;
94
95 private boolean custom;
96
97 public TREHeader() {
98 super(DEFAULT_HEADER_LEN, "GARMIN TRE");
99 }
100
101 /**
102 * Read the rest of the header. Specific to the given file. It is guaranteed
103 * that the file position will be set to the correct place before this is
104 * called.
105 *
106 * @param reader The header is read from here.
107 */
108 protected void readFileHeader(ImgFileReader reader) throws ReadFailedException {
109 if (reader.position() - reader.getGMPOffset() != COMMON_HEADER_LEN) {
110 throw new ReadFailedException("Reader position not at expected header length", new IOException());
111 }
112 int maxLat = reader.get3s();
113 int maxLon = reader.get3s();
114 int minLat = reader.get3s();
115 int minLon = reader.get3s();
116 // fix problem with value 0x800000 that is interpreted as a negative value
117 if (maxLon < minLon && maxLon == -8388608 )
118 maxLon = 8388608; // its 180 degrees, not -180
119
120 setBounds(new Area(minLat, minLon, maxLat, maxLon));
121 log.info("read area is", getBounds());
122
123 // more to do...
124 mapLevelPos = reader.get4();
125 mapLevelsSize = reader.get4();
126 subdivPos = reader.get4();
127 subdivSize = reader.get4();
128
129 copyright.readSectionInfo(reader, true, true);
130
131 poiDisplayFlags = reader.get1u();
132 displayPriority = reader.get3u();
133 reader.get4();
134 reader.get2u();
135 reader.get();
136
137 polyline.readSectionInfo(reader, true, true);
138 polygon.readSectionInfo(reader, true, true);
139 points.readSectionInfo(reader, true, true);
140
141 int mapInfoOff = mapLevelPos;
142 if (subdivPos < mapInfoOff)
143 mapInfoOff = subdivPos;
144 if (copyright.getPosition() < mapInfoOff)
145 mapInfoOff = copyright.getPosition();
146
147 mapInfoSize = mapInfoOff - getHeaderLength();
148 if (getHeaderLength() > 116) {
149 assert reader.position() == reader.getGMPOffset() + 116;
150 mapId = reader.get4();
151 }
152 if (getHeaderLength() > 120) {
153 reader.get4();
154 assert reader.position() == reader.getGMPOffset() + 124;
155 extTypeOffsets.readSectionInfo(reader, true, true);
156 }
157 }
158
159 /**
160 * Write the rest of the header. It is guaranteed that the writer will be set
161 * to the correct position before calling.
162 *
163 * @param writer The header is written here.
164 */
165 protected void writeFileHeader(ImgFileWriter writer) {
166 writer.put3s(area.getMaxLat());
167 Utils.put3sLongitude(writer, area.getMaxLong());
168 writer.put3s(area.getMinLat());
169 writer.put3s(area.getMinLong()); // minLong cant be +180
170
171 writer.put4(getMapLevelsPos());
172 writer.put4(getMapLevelsSize());
173
174 writer.put4(getSubdivPos());
175 writer.put4(getSubdivSize());
176
177 copyright.writeSectionInfo(writer);
178 writer.put4(0);
179
180 writer.put1u(getPoiDisplayFlags());
181
182 writer.put3u(displayPriority);
183 if (custom)
184 writer.put4(0x170401);
185 else
186 writer.put4(0x110301);
187
188 writer.put2u(1);
189 writer.put1u(0);
190
191 polyline.writeSectionInfo(writer);
192 writer.put4(0);
193 polygon.writeSectionInfo(writer);
194 writer.put4(0);
195 points.writeSectionInfo(writer);
196 writer.put4(0);
197
198 // There are a number of versions of the header with increasing lengths
199 if (getHeaderLength() > 116)
200 writer.put4(getMapId());
201
202 if (getHeaderLength() > 120) {
203 writer.put4(0);
204
205 // The record size must be zero if the section is empty for compatibility
206 // with cpreview.
207 if (extTypeOffsets.getSize() == 0)
208 extTypeOffsets.setItemSize(0);
209 extTypeOffsets.writeSectionInfo(writer, true);
210
211 // the second byte value of 6 appears to mean "extended
212 // type info present" - a value of 4 has been seen in some
213 // maps but those maps contain something else in these two
214 // sections and not extended type info - the 7 in the
215 // bottom byte could possibly be a bitmask to say which
216 // types are present (line, area, point) but this is just
217 // conjecture
218 writer.put4(0x0607);
219
220 extTypeOverviews.writeSectionInfo(writer);
221 writer.put2u(numExtTypeLineTypes);
222 writer.put2u(numExtTypeAreaTypes);
223 writer.put2u(numExtTypePointTypes);
224 }
225
226 if (getHeaderLength() > 154) {
227 MapValues mv = new MapValues(mapId, getHeaderLength());
228 mv.calculate();
229 writer.put4(mv.value(0));
230 writer.put4(mv.value(1));
231 writer.put4(mv.value(2));
232 writer.put4(mv.value(3));
233
234 writer.put4(0);
235 writer.put4(0);
236 writer.put4(0);
237 writer.put2u(0);
238 writer.put4(0);
239 }
240
241 writer.position(getHeaderLength());
242 }
243
244 public void config(EnhancedProperties props) {
245 String key = "draw-priority";
246 if (props.containsKey(key))
247 setDisplayPriority(props.getProperty(key, 0x19));
248
249 if (props.containsKey("transparent"))
250 poiDisplayFlags |= POI_FLAG_TRANSPARENT;
251 custom = props.containsKey("custom");
252
253 }
254
255 /**
256 * Set the bounds based upon the latitude and longitude in degrees.
257 * @param area The area bounded by the map.
258 */
259 public void setBounds(Area area) {
260 this.area = area;
261 }
262
263 public Area getBounds() {
264 return area;
265 }
266
267 public void setMapId(int id) {
268 mapId = id;
269 }
270
271 public void setDriveOnLeft(boolean dol) {
272 if (dol)
273 this.poiDisplayFlags |= POI_FLAG_DRIVE_ON_LEFT;
274 }
275
276 public void addPoiDisplayFlags(int poiDisplayFlags) {
277 this.poiDisplayFlags |= poiDisplayFlags;
278 }
279
280 public int getMapInfoSize() {
281 return mapInfoSize;
282 }
283
284 public void setMapInfoSize(int mapInfoSize) {
285 this.mapInfoSize = mapInfoSize;
286 }
287
288 public int getMapLevelsPos() {
289 return mapLevelPos;
290 }
291
292 public void setMapLevelPos(int mapLevelPos) {
293 this.mapLevelPos = mapLevelPos;
294 }
295
296 public int getMapLevelsSize() {
297 return mapLevelsSize;
298 }
299
300 public void setMapLevelsSize(int mapLevelsSize) {
301 this.mapLevelsSize = mapLevelsSize;
302 }
303
304 public int getSubdivPos() {
305 return subdivPos;
306 }
307
308 public void setSubdivPos(int subdivPos) {
309 this.subdivPos = subdivPos;
310 }
311
312 public int getSubdivSize() {
313 return subdivSize;
314 }
315
316 public void setSubdivSize(int subdivSize) {
317 this.subdivSize = subdivSize;
318 }
319
320 public void setCopyrightPos(int copyrightPos) {
321 copyright.setPosition(copyrightPos);
322 }
323
324 public void incCopyrightSize() {
325 copyright.inc();
326 }
327
328 protected int getPoiDisplayFlags() {
329 return poiDisplayFlags;
330 }
331
332 public void setPolylinePos(int polylinePos) {
333 polyline.setPosition(polylinePos);
334 }
335
336 public void incPolylineSize() {
337 polyline.inc();
338 }
339
340 public void setPolygonPos(int polygonPos) {
341 polygon.setPosition(polygonPos);
342 }
343
344 public void incPolygonSize() {
345 polygon.inc();
346 }
347
348 public void setPointPos(int pointPos) {
349 points.setPosition(pointPos);
350 }
351
352 public void incPointSize() {
353 points.inc();
354 }
355
356 public void setExtTypeOffsetsPos(int pos) {
357 extTypeOffsets.setPosition(pos);
358 }
359
360 public void incExtTypeOffsetsSize() {
361 extTypeOffsets.inc();
362 }
363
364 public void setExtTypeOverviewsPos(int pos) {
365 extTypeOverviews.setPosition(pos);
366 }
367
368 public void incExtTypeOverviewsSize() {
369 extTypeOverviews.inc();
370 }
371
372 public void incNumExtTypeAreaTypes() {
373 ++numExtTypeAreaTypes;
374 }
375
376 public void incNumExtTypeLineTypes() {
377 ++numExtTypeLineTypes;
378 }
379 public void incNumExtTypePointTypes() {
380 ++numExtTypePointTypes;
381 }
382
383 public int getMapId() {
384 return mapId;
385 }
386
387 protected void setDisplayPriority(int displayPriority) {
388 this.displayPriority = displayPriority;
389 }
390
391 public int getDisplayPriority() {
392 return displayPriority;
393 }
394
395 public int getExtTypeOffsetsPos() {
396 return extTypeOffsets.getPosition();
397 }
398
399 public int getExtTypeOffsetsSize() {
400 return extTypeOffsets.getSize();
401 }
402
403 public int getExtTypeSectionSize() {
404 return extTypeOffsets.getItemSize();
405 }
406
407 public int getTre7Magic() {
408 return extTypeOffsets.getExtraValue();
409 }
410
411 public Section getCopyrightSection() {
412 return copyright;
413 }
414 }