Debian Codegraph @

src/uk/me/parabola/imgfmt/app/map/Map.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.map;
18
19 import uk.me.parabola.imgfmt.FileExistsException;
20 import uk.me.parabola.imgfmt.FileNotWritableException;
21 import uk.me.parabola.imgfmt.FileSystemParam;
22 import uk.me.parabola.imgfmt.Utils;
23 import uk.me.parabola.imgfmt.app.Area;
24 import uk.me.parabola.imgfmt.app.Label;
25 import uk.me.parabola.imgfmt.app.dem.DEMFile;
26 import uk.me.parabola.imgfmt.app.labelenc.CodeFunctions;
27 import uk.me.parabola.imgfmt.app.lbl.LBLFile;
28 import uk.me.parabola.imgfmt.app.net.NETFile;
29 import uk.me.parabola.imgfmt.app.net.NODFile;
30 import uk.me.parabola.imgfmt.app.srt.Sort;
31 import uk.me.parabola.imgfmt.app.trergn.InternalFiles;
32 import uk.me.parabola.imgfmt.app.trergn.MapObject;
33 import uk.me.parabola.imgfmt.app.trergn.PointOverview;
34 import uk.me.parabola.imgfmt.app.trergn.PolygonOverview;
35 import uk.me.parabola.imgfmt.app.trergn.PolylineOverview;
36 import uk.me.parabola.imgfmt.app.trergn.RGNFile;
37 import uk.me.parabola.imgfmt.app.trergn.Subdivision;
38 import uk.me.parabola.imgfmt.app.trergn.TREFile;
39 import uk.me.parabola.imgfmt.app.trergn.Zoom;
40 import uk.me.parabola.imgfmt.fs.FileSystem;
41 import uk.me.parabola.imgfmt.sys.ImgFS;
42 import uk.me.parabola.log.Logger;
43 import uk.me.parabola.mkgmap.combiners.OverviewBuilder;
44 import uk.me.parabola.util.Configurable;
45 import uk.me.parabola.util.EnhancedProperties;
46
47 /**
48 * Holder for a complete map. A map is made up of several files which
49 * include at least the TRE, LBL and RGN files.
50 *
51 * It is the interface for all information about the whole map, such as the
52 * point overviews etc. Subdivision will hold the map elements.
53 *
54 * <p>Needless to say, it has nothing to do with java.util.Map and given
55 * how it has turned out, with all reading functionality in MapReader
56 * it would have been better named MapWriter.
57 *
58 * @author Steve Ratcliffe
59 */
60 public class Map implements InternalFiles, Configurable {
61 private static final Logger log = Logger.getLogger(Map.class);
62 private String filename;
63 private String mapName;
64 private int mapId;
65 private FileSystem fileSystem;
66
67 private TREFile treFile;
68 private RGNFile rgnFile;
69 private LBLFile lblFile;
70 private NETFile netFile;
71 private NODFile nodFile;
72 private DEMFile demFile;
73 private boolean isOverviewCombined;
74
75 // Use createMap() or loadMap() instead of creating a map directly.
76 private Map() {
77 }
78
79 /**
80 * Create a complete map. This consists of (at least) three
81 * files that all have the same basename and different extensions.
82 *
83 * @param mapname The name of the map. This is an 8 digit number as a
84 * string.
85 * @param params Parameters that describe the file system that the map
86 * will be created in.
87 * @return A map object that holds together all the files that make it up.
88 * @throws FileExistsException If the file already exists and we do not
89 * want to overwrite it.
90 * @throws FileNotWritableException If the file cannot
91 * be opened for write.
92 */
93 public static Map createMap(String mapname, String outputdir, FileSystemParam params, String mapnumber, Sort sort, boolean overviewCombined)
94 throws FileExistsException, FileNotWritableException
95 {
96 Map m = new Map();
97 m.mapName = mapname;
98 String outFilename = Utils.joinPath(outputdir, mapname, "img");
99
100 FileSystem fs = ImgFS.createFs(outFilename, params);
101 m.filename = outFilename;
102 m.fileSystem = fs;
103
104 m.rgnFile = new RGNFile(m.fileSystem.create(mapnumber + ".RGN"));
105 m.treFile = new TREFile(m.fileSystem.create(mapnumber + ".TRE"));
106 m.lblFile = new LBLFile(m.fileSystem.create(mapnumber + ".LBL"), sort);
107 m.isOverviewCombined = overviewCombined;
108
109 int mapid;
110 try {
111 mapid = Integer.parseInt(mapnumber);
112 } catch (NumberFormatException e) {
113 mapid = 0;
114 }
115 m.mapId = mapid;
116 m.treFile.setMapId(mapid);
117 m.fileSystem = fs;
118
119 return m;
120 }
121
122 public void config(EnhancedProperties props) {
123 boolean isOverviewComponent = OverviewBuilder.isOverviewImg(mapName);
124 // we don't want routing info in the overview map (for now)
125 if (!isOverviewComponent && !isOverviewCombined) {
126 try {
127 if (props.containsKey("route") || props.containsKey("net") || props.containsKey("housenumbers")) {
128 addNet();
129 }
130 if (props.containsKey("route")) {
131 addNod();
132 }
133 } catch (FileExistsException e) {
134 log.warn("Could not add NET and/or NOD sections");
135 }
136 // this sets things like draw-priority/transparent/custom
137 // and not relevant to overview maps
138 treFile.config(props);
139 }
140 if (!isOverviewComponent && (!isOverviewCombined || props.containsKey("overview-dem-dist"))) {
141 // allow dem on detail tiles but not final overview unless has own option. Never in the ovm_
142 if (props.containsKey("dem")) {
143 try {
144 addDem();
145 } catch (FileExistsException e) {
146 log.warn("Could not add DEM section");
147 }
148 }
149 }
150 }
151
152 private void addNet() throws FileExistsException {
153 netFile = new NETFile(fileSystem.create(mapName + ".NET"));
154 }
155
156 private void addNod() throws FileExistsException {
157 nodFile = new NODFile(fileSystem.create(mapName + ".NOD"), true);
158 }
159
160 private void addDem() throws FileExistsException {
161 demFile = new DEMFile(fileSystem.create(mapId + ".DEM"), true);
162 }
163
164
165 /**
166 * Set the area that the map covers.
167 * @param area The outer bounds of the map.
168 */
169 public void setBounds(Area area) {
170 treFile.setBounds(area);
171 }
172
173 /**
174 * Add a copyright message to the map.
175 * @param str the copyright message. The second (last?) one set
176 * gets shown when the device starts (sometimes?).
177 */
178 public void addCopyright(String str) {
179 Label cpy = lblFile.newLabel(str, false);
180 treFile.addCopyright(cpy);
181 }
182
183 /**
184 * There is an area after the TRE header and before its data
185 * starts that is used to save licence info.
186 *
187 * It seems that this must follow the code page of the LBL file. The format6 encoding is not allowed
188 * however.
189 *
190 * @param msg Any string.
191 */
192 public void addInfo(String msg) {
193 int codePage = lblFile.getCodePage();
194 CodeFunctions functions = CodeFunctions.createEncoderForLBL(0, codePage);
195 treFile.addInfo(functions.getEncoder().encodeText(msg));
196 }
197
198 /**
199 * Create a new zoom level. The level 0 is the most detailed and
200 * level 15 is the most general. Most maps would just have 4
201 * different levels or less. We are just having two to start with
202 * but will probably advance to at least 3.
203 *
204 * @param level The zoom level, and integer between 0 and 15. Its
205 * like a logical zoom level.
206 * @param bits The number of bits per coordinate, a measure of
207 * the actual amount of detail that will be in the level. So this
208 * is like a physical zoom level.
209 * @return The zoom object.
210 */
211 public Zoom createZoom(int level, int bits) {
212 return treFile.createZoom(level, bits);
213 }
214
215 /**
216 * Create the top level division. It must be empty afaik and cover
217 * the whole area of the map.
218 *
219 * @param area The whole map area.
220 * @param zoom The zoom level that you want the top level to be
221 * at. Its going to be at least level 1.
222 * @return The top level division.
223 */
224 public Subdivision topLevelSubdivision(Area area, Zoom zoom) {
225 zoom.setInherited(true); // May not always be necessary/desired
226
227 InternalFiles ifiles = this;
228 Subdivision sub = Subdivision.topLevelSubdivision(ifiles, area, zoom);
229 rgnFile.startDivision(sub);
230 return sub;
231 }
232
233 /**
234 * Create a subdivision that is beneath the top level. We have to
235 * pass the parent division.
236 * <p>
237 * Note that you cannot create these all up front. You must
238 * create it, fill it will its map elements and then create the
239 * next one. You must also start at the top level and work down.
240 *
241 * @param parent The parent subdivision.
242 * @param area The area of the new child subdiv.
243 * @param zoom The zoom level of the child.
244 * @return The new division.
245 */
246 public Subdivision createSubdivision(Subdivision parent, Area area, Zoom zoom)
247 {
248 log.debug("creating division");
249 return parent.createSubdivision(this, area, zoom);
250 }
251
252 public void addPointOverview(PointOverview ov) {
253 treFile.addPointOverview(ov);
254 }
255
256 public void addPolylineOverview(PolylineOverview ov) {
257 treFile.addPolylineOverview(ov);
258 }
259
260 public void addPolygonOverview(PolygonOverview ov) {
261 treFile.addPolygonOverview(ov);
262 }
263
264 /**
265 * Adds the bits to the point of interest flags.
266 * @param flags The POI flags.
267 */
268 public void addPoiDisplayFlags(int flags) {
269 treFile.addPoiDisplayFlags(flags);
270 }
271
272 public void addMapObject(MapObject item) {
273 rgnFile.addMapObject(item);
274 }
275
276 public void setSort(Sort sort) {
277 lblFile.setSort(sort);
278 if (netFile != null)
279 netFile.setSort(sort);
280 }
281
282 public void setLabelCharset(String desc, boolean forceUpper) {
283 lblFile.setCharacterType(desc, forceUpper);
284 }
285
286 /**
287 * Close this map by closing all the constituent files.
288 *
289 * Some history:
290 */
291 public void close() {
292 fileSystem.close();
293 }
294
295 public String getFilename() {
296 return filename;
297 }
298
299 public RGNFile getRgnFile() {
300 return rgnFile;
301 }
302
303 public LBLFile getLblFile() {
304 return lblFile;
305 }
306
307 public TREFile getTreFile() {
308 return treFile;
309 }
310
311 public NETFile getNetFile() {
312 return netFile;
313 }
314
315 public NODFile getNodFile() {
316 return nodFile;
317 }
318
319 public DEMFile getDemFile() {
320 return demFile;
321 }
322 }