Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/MDRFile.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.mdr;
14
15 import java.util.Arrays;
16 import java.util.Set;
17
18 import uk.me.parabola.imgfmt.MapFailedException;
19 import uk.me.parabola.imgfmt.app.BufferedImgFileReader;
20 import uk.me.parabola.imgfmt.app.FileBackedImgFileWriter;
21 import uk.me.parabola.imgfmt.app.ImgFile;
22 import uk.me.parabola.imgfmt.app.ImgFileWriter;
23 import uk.me.parabola.imgfmt.app.Label;
24 import uk.me.parabola.imgfmt.app.lbl.Country;
25 import uk.me.parabola.imgfmt.app.lbl.Region;
26 import uk.me.parabola.imgfmt.app.lbl.Zip;
27 import uk.me.parabola.imgfmt.app.mdr.MdrSection.PointerSizes;
28 import uk.me.parabola.imgfmt.app.net.RoadDef;
29 import uk.me.parabola.imgfmt.app.srt.Sort;
30 import uk.me.parabola.imgfmt.app.trergn.Point;
31 import uk.me.parabola.imgfmt.fs.ImgChannel;
32 import uk.me.parabola.log.Logger;
33
34 /**
35 * The MDR file. This is embedded into a .img file, either its own
36 * separate one, or as one file in the gmapsupp.img.
37 *
38 * @author Steve Ratcliffe
39 */
40 public class MDRFile extends ImgFile {
41 private final MDRHeader mdrHeader;
42
43 // The sections
44 private final Mdr1 mdr1;
45 private final Mdr4 mdr4;
46 private final Mdr5 mdr5;
47 private final Mdr6 mdr6;
48 private final Mdr7 mdr7;
49 private final Mdr8 mdr8; // unused
50 private final Mdr9 mdr9;
51 private final Mdr10 mdr10;
52 private final Mdr11 mdr11;
53 private final Mdr12 mdr12;
54 private final Mdr13 mdr13;
55 private final Mdr14 mdr14;
56 private final Mdr15 mdr15;
57 private final Mdr17 mdr17;
58 private final Mdr18 mdr18;
59 private final Mdr19 mdr19;
60 private final Mdr20 mdr20;
61 private final Mdr21 mdr21;
62 private final Mdr22 mdr22;
63 private final Mdr23 mdr23;
64 private final Mdr24 mdr24;
65 private final Mdr25 mdr25;
66 private final Mdr26 mdr26;
67 private final Mdr27 mdr27;
68 private final Mdr28 mdr28;
69 private final Mdr29 mdr29;
70
71 private int currentMap;
72
73 private final boolean forDevice;
74 private final boolean isMulti;
75
76 private final MdrSection[] sections;
77 private PointerSizes sizes;
78 private Set<String> mdr7Del;
79
80 private Set<Integer> poiExclTypes;
81
82 public MDRFile(ImgChannel chan, MdrConfig config) {
83 Sort sort = config.getSort();
84
85 forDevice = config.isForDevice();
86 isMulti = config.getSort().isMulti();
87 mdr7Del = config.getMdr7Del();
88 poiExclTypes = config.getPoiExclTypes();
89 mdrHeader = new MDRHeader(config.getHeaderLen());
90 mdrHeader.setSort(sort);
91 setHeader(mdrHeader);
92 if (config.isWritable()) {
93 ImgFileWriter fileWriter = new FileBackedImgFileWriter(chan, config.getOutputDir());
94 setWriter(fileWriter);
95
96 // Position at the start of the writable area.
97 position(mdrHeader.getHeaderLength());
98 } else {
99 setReader(new BufferedImgFileReader(chan));
100 mdrHeader.readHeader(getReader());
101 }
102
103 // Initialise the sections
104 mdr1 = new Mdr1(config);
105 mdr4 = new Mdr4(config);
106 mdr5 = new Mdr5(config);
107 mdr6 = new Mdr6(config);
108 mdr7 = new Mdr7(config);
109 mdr8 = new Mdr8(config);
110 mdr9 = new Mdr9(config);
111 mdr10 = new Mdr10(config);
112 mdr11 = new Mdr11(config);
113 mdr12 = new Mdr12(config);
114 mdr13 = new Mdr13(config);
115 mdr14 = new Mdr14(config);
116 mdr15 = new Mdr15(config);
117 mdr17 = new Mdr17(config);
118 mdr18 = new Mdr18(config);
119 mdr19 = new Mdr19(config);
120 mdr20 = new Mdr20(config);
121 mdr21 = new Mdr21(config);
122 mdr22 = new Mdr22(config);
123 mdr23 = new Mdr23(config);
124 mdr24 = new Mdr24(config);
125 mdr25 = new Mdr25(config);
126 mdr26 = new Mdr26(config);
127 mdr27 = new Mdr27(config);
128 mdr28 = new Mdr28(config);
129 mdr29 = new Mdr29(config);
130
131 this.sections = new MdrSection[]{
132 null,
133 mdr1, null, null, mdr4, mdr5, mdr6,
134 mdr7, mdr8, mdr9, mdr10, mdr11, mdr12,
135 mdr13, mdr14, mdr15, null, mdr17, mdr18, mdr19,
136 mdr20, mdr21, mdr22, mdr23, mdr24, mdr25,
137 mdr26, mdr27, mdr28, mdr29,
138 };
139
140 mdr11.setMdr10(mdr10);
141 }
142
143 /**
144 * Add a map to the index. You must add the map, then all of the items
145 * that belong to it, before adding the next map.
146 * @param mapName The numeric name of the map.
147 * @param codePage The code page of the map.
148 */
149 public void addMap(int mapName, int codePage) {
150 currentMap++;
151 mdr1.addMap(mapName, currentMap);
152 Sort sort = mdrHeader.getSort();
153
154 if (sort.getCodepage() != codePage)
155 Logger.defaultLogger.warn("Input files have different code pages");
156 }
157
158 public Mdr14Record addCountry(Country country) {
159 Mdr14Record record = new Mdr14Record();
160
161 String name = country.getLabel().getText();
162 record.setMapIndex(currentMap);
163 record.setCountryIndex(country.getIndex());
164 record.setLblOffset(country.getLabel().getOffset());
165 record.setName(name);
166 record.setStrOff(createString(name));
167
168 mdr14.addCountry(record);
169 return record;
170 }
171
172 public Mdr13Record addRegion(Region region, Mdr14Record country) {
173 Mdr13Record record = new Mdr13Record();
174
175 String name = region.getLabel().getText();
176 record.setMapIndex(currentMap);
177 record.setLblOffset(region.getLabel().getOffset());
178 record.setCountryIndex(region.getCountry().getIndex());
179 record.setRegionIndex(region.getIndex());
180 record.setName(name);
181 record.setStrOffset(createString(name));
182 record.setMdr14(country);
183
184 mdr13.addRegion(record);
185 return record;
186 }
187
188 public void addCity(Mdr5Record city) {
189 int labelOffset = city.getLblOffset();
190 if (labelOffset != 0) {
191 String name = city.getName();
192 assert name != null : "off=" + labelOffset;
193 city.setMapIndex(currentMap);
194 city.setStringOffset(createString(name));
195 mdr5.addCity(city);
196 }
197 }
198
199 public void addZip(Zip zip) {
200 int strOff = createString(zip.getLabel().getText());
201 mdr6.addZip(currentMap, zip, strOff);
202 }
203
204 public void addPoint(Point point, Mdr5Record city, boolean isCity) {
205 assert currentMap > 0;
206
207 int fullType = point.getType();
208 if (!MdrUtils.canBeIndexed(fullType))
209 return;
210 if (!poiExclTypes.isEmpty()) {
211 int t = (fullType < 0xff) ? fullType << 8 : fullType;
212 if (poiExclTypes.contains(t))
213 return;
214 }
215 Label label = point.getLabel();
216 String name = label.getText();
217 int strOff = createString(name);
218
219 Mdr11Record poi = mdr11.addPoi(currentMap, point, name, strOff);
220 poi.setCity(city);
221 poi.setIsCity(isCity);
222 poi.setType(fullType);
223
224 mdr4.addType(fullType);
225 }
226
227 public void addStreet(RoadDef street, Mdr5Record mdrCity) {
228 // Add a separate record for each name
229 for (Label lab : street.getLabels()) {
230 if (lab == null)
231 break;
232
233 if (lab.getOffset() == 0)
234 continue;
235
236 String name = lab.getText();
237 if (!mdr7Del.isEmpty()) {
238 String[] parts = name.split(" ");
239 int pos = parts.length;
240 for (int i = parts.length - 1; i >= 0; i--) {
241 if (!mdr7Del.contains(parts[i])) {
242 break;
243 }
244 pos = i;
245 }
246 if (pos == 0)
247 continue;
248 if (pos < parts.length) {
249 StringBuilder sb = new StringBuilder();
250 for (int i = 0; i + 1 < pos; i++) {
251 sb.append(parts[i]);
252 sb.append(" ");
253 }
254 sb.append(parts[pos - 1]);
255 name = sb.toString(); // XXX maybe add -intern()
256 }
257 }
258
259 int strOff = createString(name);
260
261 // We sort on the dirty name (ie with the Garmin shield codes) although those codes do not
262 // affect the sort order. The string for mdr15 does not include the shield codes.
263 mdr7.addStreet(currentMap, name, lab.getOffset(), strOff, mdrCity);
264 }
265 }
266
267 public void write() {
268 mdr15.release();
269
270 ImgFileWriter writer = getWriter();
271 writeSections(writer);
272
273 // Now refresh the header
274 position(0);
275 getHeader().writeHeader(writer);
276 }
277
278 /**
279 * Write all the sections out.
280 *
281 * The order of all the operations in this method is important. The order
282 * of the sections in the actual output file doesn't matter at all, so
283 * they can be re-ordered to suit.
284 *
285 * Most of the complexity here is arranging the order of things so that the smallest
286 * amount of temporary memory is required.
287 *
288 * @param writer File is written here.
289 */
290 private void writeSections(ImgFileWriter writer) {
291 sizes = new MdrMapSection.PointerSizes(sections);
292
293 mdr7.trim();
294 // Deal with the dependencies between the sections. The order of the following
295 // statements is sometimes important.
296 mdr28.buildFromRegions(mdr13.getRegions());
297 mdr23.sortRegions(mdr13.getRegions());
298 mdr29.buildFromCountries(mdr14.getCountries());
299 mdr24.sortCountries(mdr14.getCountries());
300 mdr26.sortMdr28(mdr28.getIndex());
301
302 writeSection(writer, 4, mdr4);
303
304 mdr1.preWrite();
305 mdr5.preWrite();
306 mdr20.preWrite();
307
308 // We write the following sections that contain per-map data, in the
309 // order of the subsections of the reverse index that they are associated
310 // with.
311 writeSection(writer, 11, mdr11);
312 mdr10.setNumberOfPois(mdr11.getNumberOfPois());
313 mdr12.setIndex(mdr11.getIndex());
314 mdr19.setPois(mdr11.getPois());
315 if (forDevice && !isMulti) {
316 mdr17.addPois(mdr11.getPois());
317 }
318 mdr11.release();
319
320 if (forDevice) {
321 mdr19.preWrite();
322 writeSection(writer, 19, mdr19);
323 mdr18.setPoiTypes(mdr19.getPoiTypes());
324 mdr19.release();
325 writeSection(writer, 18, mdr18);
326 } else {
327 mdr19.release();
328 }
329
330 writeSection(writer, 10, mdr10);
331 mdr9.setGroups(mdr10.getGroupSizes());
332 mdr10.release();
333
334 // mdr7 depends on the size of mdr20, so mdr20 must be built first
335 mdr7.preWrite();
336 mdr20.buildFromStreets(mdr7.getStreets());
337 writeSection(writer, 7, mdr7);
338
339 writeSection(writer, 5, mdr5);
340 mdr25.sortCities(mdr5.getCities());
341 mdr27.sortCities(mdr5.getCities());
342 if (forDevice && !isMulti) {
343 mdr17.addCities(mdr5.getSortedCities());
344 }
345 mdr5.release();
346 writeSection(writer, 6, mdr6);
347
348 writeSection(writer, 20, mdr20);
349 mdr20.release();
350
351 mdr21.buildFromStreets(mdr7.getStreets());
352 writeSection(writer, 21, mdr21);
353 mdr21.release();
354
355 mdr22.buildFromStreets(mdr7.getStreets());
356 if (forDevice && !isMulti) {
357 mdr17.addStreets(mdr7.getSortedStreets());
358 }
359
360 mdr7.release();
361 writeSection(writer, 22, mdr22);
362 if (forDevice && !isMulti) {
363 mdr17.addStreetsByCountry(mdr22.getStreets());
364 }
365 mdr22.release();
366
367 if (forDevice) {
368 writeSection(writer, 17, mdr17);
369 mdr17.release();
370 }
371
372 // The following do not have mdr1 subsections
373 //writeSection(writer, 8, mdr8);
374 writeSection(writer, 9, mdr9);
375 if (!isMulti) {
376 writeSection(writer, 12, mdr12);
377 }
378 writeSection(writer, 13, mdr13);
379 writeSection(writer, 14, mdr14);
380 writeSection(writer, 15, mdr15);
381
382 writeSection(writer, 23, mdr23);
383 writeSection(writer, 24, mdr24);
384 writeSection(writer, 25, mdr25);
385 mdr28.preWrite(); // TODO reorder writes below so this is not needed. Changes the output file though
386 writeSection(writer, 26, mdr26);
387 writeSection(writer, 27, mdr27);
388 writeSection(writer, 28, mdr28);
389 writeSection(writer, 29, mdr29);
390
391 // write the reverse index last.
392 mdr1.writeSubSections(writer);
393 mdrHeader.setPosition(1, writer.position());
394
395 mdr1.writeSectData(writer);
396 mdrHeader.setItemSize(1, mdr1.getItemSize());
397 mdrHeader.setEnd(1, writer.position());
398 mdrHeader.setExtraValue(1, mdr1.getExtraValue());
399 if (writer.position() < 0) {
400 // integer overflow can cause this
401 throw new MapFailedException("MDR sub file is too large");
402 }
403 }
404
405 /**
406 * Write out the given single section.
407 */
408 private void writeSection(ImgFileWriter writer, int sectionNumber, MdrSection section) {
409
410 // Some sections are just not written in the device config
411 if (forDevice && Arrays.asList(13, 14, 15, 21, 23, 26, 27, 28).contains(sectionNumber))
412 return;
413
414 section.setSizes(sizes);
415
416 mdrHeader.setPosition(sectionNumber, writer.position());
417 mdr1.setStartPosition(sectionNumber);
418
419 section.preWrite();
420 if (!forDevice && section instanceof MdrMapSection) {
421 MdrMapSection mapSection = (MdrMapSection) section;
422 mapSection.setMapIndex(mdr1);
423 mapSection.initIndex(sectionNumber);
424 }
425
426 if (section instanceof HasHeaderFlags)
427 mdrHeader.setExtraValue(sectionNumber, ((HasHeaderFlags) section).getExtraValue());
428
429 section.writeSectData(writer);
430
431 int itemSize = section.getItemSize();
432 if (itemSize > 0)
433 mdrHeader.setItemSize(sectionNumber, itemSize);
434
435 mdrHeader.setEnd(sectionNumber, writer.position());
436 mdr1.setEndPosition(sectionNumber);
437 }
438
439 /**
440 * Creates a string in MDR 15 and returns an offset value that can be
441 * used to refer to it in the other sections.
442 * @param str The text of the string.
443 * @return An offset value.
444 */
445 private int createString(String str) {
446 if (forDevice)
447 return -1;
448 return mdr15.createString(str);
449 }
450 }