Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2006 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: 03-Dec-2006
16 */
17 package uk.me.parabola.imgfmt.app.trergn;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import uk.me.parabola.imgfmt.app.Area;
24 import uk.me.parabola.imgfmt.app.BufferedImgFileWriter;
25 import uk.me.parabola.imgfmt.app.ImgFile;
26 import uk.me.parabola.imgfmt.app.ImgFileWriter;
27 import uk.me.parabola.imgfmt.app.Label;
28 import uk.me.parabola.imgfmt.app.labelenc.EncodedText;
29 import uk.me.parabola.imgfmt.fs.ImgChannel;
30 import uk.me.parabola.log.Logger;
31 import uk.me.parabola.util.Configurable;
32 import uk.me.parabola.util.EnhancedProperties;
33
34 /**
35 * This is the file that contains the overview of the map. There
36 * can be different zoom levels and each level of zoom has an
37 * associated set of subdivided areas. Each of these areas then points
38 * into the RGN file.
39 *
40 * This is quite a complex file as there are quite a few miscellaneous pieces
41 * of information stored.
42 *
43 * @author Steve Ratcliffe
44 */
45 public class TREFile extends ImgFile implements Configurable {
46 private static final Logger log = Logger.getLogger(TREFile.class);
47
48 // Zoom levels for map
49 private final Zoom[] mapLevels = new Zoom[16];
50
51 private final List<Label> copyrights = new ArrayList<>();
52
53 // Information about polylines. eg roads etc.
54 private final List<PolylineOverview> polylineOverviews = new ArrayList<>();
55
56 private final List<PolygonOverview> polygonOverviews = new ArrayList<>();
57
58 private final List<PointOverview> pointOverviews = new ArrayList<>();
59
60 private int lastRgnPos;
61
62 private final TREHeader header = new TREHeader();
63
64
65 public TREFile(ImgChannel chan) {
66 setHeader(header);
67 setWriter(new BufferedImgFileWriter(chan, "TRE"));
68
69 // Position at the start of the writable area.
70 position(header.getHeaderLength());
71 }
72
73 public Zoom createZoom(int zoom, int bits) {
74 Zoom z = new Zoom(zoom, bits);
75 mapLevels[zoom] = z;
76 return z;
77 }
78
79 /**
80 * Add a string to the 'mapinfo' section. This is a section between the
81 * header and the start of the data. Nothing points to it directly.
82 *
83 * @param enc A string in the EncodedText format.
84 */
85 public void addInfo(EncodedText enc) {
86 byte[] val = enc.getCtext();
87 if (position() != header.getHeaderLength() + header.getMapInfoSize())
88 throw new IllegalStateException("All info must be added before anything else");
89
90 header.setMapInfoSize(header.getMapInfoSize() + enc.getLength() + 1);
91 getWriter().put(val);
92 getWriter().put1u(0);
93 }
94
95 public void addCopyright(Label cr) {
96 copyrights.add(cr);
97 }
98
99 public void addPointOverview(PointOverview ov) {
100 pointOverviews.add(ov);
101 }
102
103 public void addPolylineOverview(PolylineOverview ov) {
104 polylineOverviews.add(ov);
105 }
106
107 public void addPolygonOverview(PolygonOverview ov) {
108 polygonOverviews.add(ov);
109 }
110
111 public void config(EnhancedProperties props) {
112 header.config(props);
113 }
114
115 /**
116 * Write out the body of the TRE file. The act of writing the body sections
117 * out provides us with pointers that are needed for the header. Therefore
118 * the header needs to be written after the body (or obviously we could
119 * make two passes).
120 */
121 private void writeBody(boolean includeExtendedTypeData) {
122 writeMapLevels();
123
124 writeSubdivs();
125
126 writeCopyrights();
127
128 writeOverviews();
129
130 if(includeExtendedTypeData) {
131 writeExtTypeOffsetsRecords();
132 writeExtTypeOverviews();
133 }
134 }
135
136 /**
137 * Write out the subdivisions. This is quite complex as they have to be
138 * numbered and written out keeping their parent/child relationship
139 * intact.
140 */
141 private void writeSubdivs() {
142 header.setSubdivPos(position());
143 int subdivnum = 1; // numbers start at one
144
145 // First prepare to number them all
146 for (int i = 15; i >= 0; i--) {
147 Zoom z = mapLevels[i];
148 if (z == null)
149 continue;
150
151 Iterator<Subdivision> it = z.subdivIterator();
152 while (it.hasNext()) {
153 Subdivision sd = it.next();
154 log.debug("setting number to", subdivnum);
155 sd.setNumber(subdivnum++);
156 }
157 }
158
159 // Now we can write them all out.
160 for (int i = 15; i >= 0; i--) {
161 Zoom z = mapLevels[i];
162 if (z == null)
163 continue;
164
165 Iterator<Subdivision> it = z.subdivIterator();
166 while (it.hasNext()) {
167 Subdivision sd = it.next();
168
169 sd.write(getWriter());
170 if (sd.hasNextLevel())
171 header.setSubdivSize(header.getSubdivSize() + TREHeader.SUBDIV_REC_SIZE2);
172 else
173 header.setSubdivSize(header.getSubdivSize() + TREHeader.SUBDIV_REC_SIZE);
174 }
175 }
176 getWriter().put4(lastRgnPos);
177 header.setSubdivSize(header.getSubdivSize() + 4);
178 }
179
180 private void writeExtTypeOffsetsRecords() {
181 header.setExtTypeOffsetsPos(position());
182 Subdivision sd = null;
183 for (int i = 15; i >= 0; i--) {
184 Zoom z = mapLevels[i];
185 if (z == null)
186 continue;
187
188 Iterator<Subdivision> it = z.subdivIterator();
189 while (it.hasNext()) {
190 sd = it.next();
191 sd.writeExtTypeOffsetsRecord(getWriter());
192 header.incExtTypeOffsetsSize();
193 }
194 }
195 if(sd != null) {
196 sd.writeLastExtTypeOffsetsRecord(getWriter());
197 header.incExtTypeOffsetsSize();
198 }
199 }
200
201 /**
202 * Write out the map levels. This is a mapping between the level number
203 * and the resolution.
204 */
205 private void writeMapLevels() {
206 // Write out the map levels (zoom)
207 header.setMapLevelPos(position());
208 for (int i = 15; i >= 0; i--) {
209 // They need to be written in reverse order I think
210 Zoom z = mapLevels[i];
211 if (z == null)
212 continue;
213 header.setMapLevelsSize(header.getMapLevelsSize() + TREHeader.MAP_LEVEL_REC_SIZE);
214 z.write(getWriter());
215 }
216 }
217
218 /**
219 * Write out the overview section. This is a mapping between the map feature
220 * type and the highest level (lowest detail) that it appears at. There
221 * are separate ones for points, lines and polygons.
222 */
223 private void writeOverviews() {
224 header.setPointPos(position());
225
226 // Point overview section
227 pointOverviews.sort(null);
228 for (Overview ov : pointOverviews) {
229 if(!ov.hasExtType()) {
230 ov.setMaxLevel(decodeLevel(ov.getMinResolution()));
231 ov.write(getWriter());
232 header.incPointSize();
233 }
234 }
235
236 // Line overview section.
237 header.setPolylinePos(position());
238 polylineOverviews.sort(null);
239 for (Overview ov : polylineOverviews) {
240 if(!ov.hasExtType()) {
241 ov.setMaxLevel(decodeLevel(ov.getMinResolution()));
242 ov.write(getWriter());
243 header.incPolylineSize();
244 }
245 }
246
247 // Polygon overview section
248 header.setPolygonPos(position());
249 polygonOverviews.sort(null);
250 for (Overview ov : polygonOverviews) {
251 if(!ov.hasExtType()) {
252 ov.setMaxLevel(decodeLevel(ov.getMinResolution()));
253 ov.write(getWriter());
254 header.incPolygonSize();
255 }
256 }
257 }
258
259 private void writeExtTypeOverviews() {
260
261 header.setExtTypeOverviewsPos(position());
262
263 // assumes overviews are already sorted
264
265 for (Overview ov : polylineOverviews) {
266 if(ov.hasExtType()) {
267 ov.setMaxLevel(decodeLevel(ov.getMinResolution()));
268 ov.write(getWriter());
269 header.incExtTypeOverviewsSize();
270 header.incNumExtTypeLineTypes();
271 }
272 }
273
274 for (Overview ov : polygonOverviews) {
275 if(ov.hasExtType()) {
276 ov.setMaxLevel(decodeLevel(ov.getMinResolution()));
277 ov.write(getWriter());
278 header.incExtTypeOverviewsSize();
279 header.incNumExtTypeAreaTypes();
280 }
281 }
282
283 for (Overview ov : pointOverviews) {
284 if(ov.hasExtType()) {
285 ov.setMaxLevel(decodeLevel(ov.getMinResolution()));
286 ov.write(getWriter());
287 header.incExtTypeOverviewsSize();
288 header.incNumExtTypePointTypes();
289 }
290 }
291 }
292
293 /**
294 * Convert a min resolution to a level. We return the lowest level (most
295 * detailed) that has a resolution less than or equal to the given resolution.
296 *
297 * @param minResolution The minimum resolution.
298 * @return The level corresponding to the resolution.
299 */
300 private int decodeLevel(int minResolution) {
301 Zoom top = null;
302 for (int i = 15; i >= 0; i--) {
303 Zoom z = mapLevels[i];
304 if (z == null)
305 continue;
306
307 if (top == null)
308 top = z;
309
310 if (z.getResolution() >= minResolution)
311 return z.getLevel();
312 }
313
314 // If not found, then allow it only at the top level
315 if (top != null)
316 return top.getLevel();
317 else
318 return 0; // Fail safe, shouldn't really happen
319 }
320
321 /**
322 * Write out the copyrights. This is just a list of pointers to strings
323 * in the label section basically.
324 */
325 private void writeCopyrights() {
326 // Write out the pointers to the labels that hold the copyright strings
327 header.setCopyrightPos(position());
328 ImgFileWriter writer = getWriter();
329 for (Label l : copyrights) {
330 header.incCopyrightSize();
331 writer.put3u(l.getOffset());
332 }
333 }
334
335 public void setLastRgnPos(int lastRgnPos) {
336 TREFile.this.lastRgnPos = lastRgnPos;
337 }
338
339 public void write(boolean includeExtendedTypeData) {
340 // Do anything that is in structures and that needs to be dealt with.
341 writeBody(includeExtendedTypeData);
342 }
343
344 public void writePost() {
345 // Now refresh the header
346 position(0);
347 getHeader().writeHeader(getWriter());
348 }
349
350 public void setMapId(int mapid) {
351 header.setMapId(mapid);
352 }
353
354 public void setBounds(Area area) {
355 header.setBounds(area);
356 }
357
358 public void addPoiDisplayFlags(int b) {
359 header.addPoiDisplayFlags(b);
360 }
361
362 public void setDriveOnLeft(boolean b) {
363 header.setDriveOnLeft(b);
364 }
365 }