Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr11.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
14 package uk.me.parabola.imgfmt.app.mdr;
15
16 import java.text.Collator;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Map;
20
21 import uk.me.parabola.imgfmt.app.ImgFileWriter;
22 import uk.me.parabola.imgfmt.app.srt.Sort;
23 import uk.me.parabola.imgfmt.app.srt.Sort.SrtCollator;
24 import uk.me.parabola.imgfmt.app.srt.SortKey;
25 import uk.me.parabola.imgfmt.app.trergn.Point;
26
27 /**
28 * Holds all the POIs, including cities. Arranged alphabetically by
29 * the name.
30 *
31 * @author Steve Ratcliffe
32 */
33 public class Mdr11 extends MdrMapSection {
34 private ArrayList<Mdr11Record> pois = new ArrayList<>();
35 private Mdr10 mdr10;
36
37 public Mdr11(MdrConfig config) {
38 setConfig(config);
39 }
40
41 public Mdr11Record addPoi(int mapIndex, Point point, String name, int strOff) {
42 Mdr11Record poi = new Mdr11Record();
43 poi.setMapIndex(mapIndex);
44 poi.setPointIndex(point.getNumber());
45 poi.setSubdiv(point.getSubdiv().getNumber());
46 poi.setLblOffset(point.getLabel().getOffset());
47 poi.setName(name);
48 poi.setStrOffset(strOff);
49
50 pois.add(poi);
51 return poi;
52 }
53
54 /**
55 * Sort and fill in the mdr10 information.
56 *
57 * The POI index contains individual references to POI by subdiv and index, so they are not
58 * de-duplicated in the index in the same way that streets and cities are.
59 */
60 @Override
61 protected void preWriteImpl() {
62 pois.trimToSize();
63 Sort sort = getConfig().getSort();
64
65 LargeListSorter<Mdr11Record> sorter = new LargeListSorter<Mdr11Record>(sort) {
66
67 @Override
68 protected SortKey<Mdr11Record> makeKey(Mdr11Record r, Sort sort, Map<String, byte[]> cache) {
69 return sort.createSortKey(r, r.getName(), r.getMapIndex(), cache);
70 }
71 };
72 sorter.sort(pois);
73 for (Mdr11Record poi : pois) {
74 mdr10.addPoiType(poi);
75 }
76 }
77
78 public void writeSectData(ImgFileWriter writer) {
79 int count = 1;
80 boolean hasStrings = hasFlag(2);
81 for (Mdr11Record poi : pois) {
82 addIndexPointer(poi.getMapIndex(), count);
83 poi.setRecordNumber(count++);
84
85 putMapIndex(writer, poi.getMapIndex());
86 writer.put1u(poi.getPointIndex());
87 writer.put2u(poi.getSubdiv());
88 writer.put3u(poi.getLblOffset());
89 if (poi.isCity())
90 putRegionIndex(writer, poi.getRegionIndex());
91 else
92 putCityIndex(writer, poi.getCityIndex());
93 if (hasStrings)
94 putStringOffset(writer, poi.getStrOffset());
95 }
96 }
97
98 public int getItemSize() {
99 PointerSizes sizes = getSizes();
100 int size = sizes.getMapSize() + 6 + sizes.getCitySizeFlagged();
101 if (hasFlag(0x2))
102 size += sizes.getStrOffSize();
103 return size;
104 }
105
106 protected int numberOfItems() {
107 return pois.size();
108 }
109
110 public int getNumberOfPois() {
111 return getNumberOfItems();
112 }
113
114 public int getExtraValue() {
115 int mdr11flags = 0x11;
116 PointerSizes sizes = getSizes();
117
118 // two bit field for city bytes. minimum size of 2
119 int citySize = sizes.getCitySizeFlagged();
120 if (citySize > 2)
121 mdr11flags |= (citySize-2) << 2;
122
123 if (isForDevice()) {
124 if (!getConfig().getSort().isMulti())
125 mdr11flags |= 0x80; // mdr17 sub section present (not with unicode)
126 }
127 else
128 mdr11flags |= 0x2;
129
130 return mdr11flags;
131 }
132
133 public List<Mdr8Record> getIndex() {
134 List<Mdr8Record> list = new ArrayList<>();
135 Sort.SrtCollator collator = (SrtCollator) getConfig().getSort().getCollator();
136 for (int number = 1; number <= pois.size(); number += 10240) {
137 char[] prefix = getPrefixForRecord(number);
138
139 // need to step back to find the first...
140 int rec = number;
141 while (rec > 1) {
142 char[] p = getPrefixForRecord(rec);
143 int cmp = collator.compareOneStrengthWithLength(prefix, p, Collator.PRIMARY,
144 MdrUtils.POI_INDEX_PREFIX_LEN);
145 if (cmp != 0) {
146 rec++;
147 break;
148 }
149 rec--;
150 }
151
152 list.add(new Mdr12Record(prefix, rec));
153 }
154 return list;
155 }
156
157 /**
158 * Get the prefix of the name at the given record.
159 * @param number The record number.
160 * @return The first 4 (or whatever value is set) characters of the POI name.
161 */
162 private char[] getPrefixForRecord(int number) {
163 Mdr11Record record = pois.get(number-1);
164 String name = record.getName();
165 return getConfig().getSort().getPrefix(name, MdrUtils.POI_INDEX_PREFIX_LEN);
166 }
167
168 public void setMdr10(Mdr10 mdr10) {
169 this.mdr10 = mdr10;
170 }
171
172 @Override
173 public void releaseMemory() {
174 pois = null;
175 mdr10 = null;
176 }
177
178 public List<Mdr11Record> getPois() {
179 return new ArrayList<>(pois);
180 }
181 }