Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/MdrConfig.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.io.File;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Set;
20 import java.util.TreeSet;
21
22 import uk.me.parabola.imgfmt.ExitException;
23 import uk.me.parabola.imgfmt.app.srt.Sort;
24 import uk.me.parabola.mkgmap.CommandArgs;
25 import uk.me.parabola.mkgmap.reader.osm.FeatureKind;
26 import uk.me.parabola.mkgmap.reader.osm.GType;
27 import uk.me.parabola.mkgmap.scan.SyntaxException;
28
29 /**
30 * Configuration for the MDR file.
31 * Mostly used when creating a file as there are a number of different options
32 * in the way that it is done.
33 *
34 * @author Steve Ratcliffe
35 */
36 public class MdrConfig {
37 private static final int DEFAULT_HEADER_LEN = 568;
38
39 private boolean writable;
40 private boolean forDevice;
41 private int headerLen = DEFAULT_HEADER_LEN;
42 private Sort sort;
43 private File outputDir;
44 private boolean splitName;
45 private Set<String> mdr7Excl = Collections.emptySet();
46 private Set<String> mdr7Del = Collections.emptySet();
47 private Set<Integer> poiExclTypes = Collections.emptySet();
48
49 public MdrConfig() {
50
51 }
52
53 /**
54 * Constructor which copies the values which configure the index details.
55 * @param base
56 */
57 public MdrConfig(MdrConfig base) {
58 splitName = base.isSplitName();
59 mdr7Del = base.getMdr7Del();
60 mdr7Excl = base.getMdr7Excl();
61 poiExclTypes = base.getPoiExclTypes();
62 }
63
64 /**
65 * True if we are creating the file, rather than reading it.
66 */
67 public boolean isWritable() {
68 return writable;
69 }
70
71 public void setWritable(boolean writable) {
72 this.writable = writable;
73 }
74
75 /**
76 * The format that is used by the GPS devices is different to that used
77 * by Map Source. This parameter says which to do.
78 * @return True if we are creating the the more compact format required
79 * for a device.
80 */
81 public boolean isForDevice() {
82 return forDevice;
83 }
84
85 public void setForDevice(boolean forDevice) {
86 this.forDevice = forDevice;
87 }
88
89 /**
90 * There are a number of different header lengths in existence. This
91 * controls what sections can exist (and perhaps what must exist).
92 * @return The header length.
93 */
94 public int getHeaderLen() {
95 return headerLen;
96 }
97
98 public void setHeaderLen(int headerLen) {
99 this.headerLen = headerLen;
100 }
101
102 public Sort getSort() {
103 return sort;
104 }
105
106 public void setSort(Sort sort) {
107 this.sort = sort;
108 }
109
110 public File getOutputDir() {
111 return outputDir;
112 }
113
114 public void setOutputDir(String outputDir) {
115 if (outputDir != null)
116 this.outputDir = new File(outputDir);
117 }
118
119 public void setSplitName(boolean splitName) {
120 this.splitName = splitName;
121 }
122
123 public boolean isSplitName() {
124 return splitName;
125 }
126
127 public Set<String> getMdr7Excl() {
128 return Collections.unmodifiableSet(mdr7Excl);
129 }
130
131 public Set<String> getMdr7Del() {
132 return Collections.unmodifiableSet(mdr7Del);
133 }
134
135 /**
136 * Parse option --poi-excl-index .
137 * @param opt the option string given. Expected is a comma separated list,eg
138 * "0x2800" or "0x2800, 0x6400-0x661f"
139 */
140 public void setPoiExcl (List<String> opts) {
141 if (opts.isEmpty())
142 poiExclTypes = Collections.emptyNavigableSet();
143 else {
144 poiExclTypes = new TreeSet<>();
145 for (String range : opts) {
146 if (range.contains("-")) {
147 String[] ranges = range.split("-");
148 if (ranges.length != 2)
149 throw new IllegalArgumentException("invalid range in option " + range);
150 genTypesForRange(poiExclTypes, ranges[0], ranges[1]);
151 } else {
152 genTypesForRange(poiExclTypes, range,range);
153 }
154 }
155 }
156 }
157
158 /**
159 * Create all POI types for a given range.
160 * @param set set of integers. Generated types are added to it.
161 * @param start first type
162 * @param stop last type (included)
163 */
164 private static void genTypesForRange(Set<Integer> set, String start, String stop) {
165 GType[] types = new GType[2];
166 String[] ranges = {start, stop};
167 boolean ok = true;
168 for (int i = 0; i < 2; i++) {
169 try {
170 types[i] = new GType(FeatureKind.POINT, ranges[i]);
171 } catch (ExitException e) {
172 ok = false;
173 }
174 if (!ok || !GType.checkType(types[i].getFeatureKind(), types[i].getType())){
175 throw new SyntaxException("invalid type " + ranges[i] + " for " + FeatureKind.POINT + " in option " + Arrays.toString(ranges));
176 }
177 }
178
179 if (types[0].getType() > types[1].getType()) {
180 GType gt = types[0];
181 types[0] = types[1];
182 types[1] = gt;
183 }
184 for (int i = types[0].getType(); i <= types[1].getType(); i++) {
185 if ((i & 0xff) > 0x1f)
186 i = ((i >> 8) + 1) << 8;
187 set.add(i);
188 }
189
190 }
191
192 public Set<Integer> getPoiExclTypes() {
193 return Collections.unmodifiableSet(poiExclTypes);
194 }
195
196 public void setIndexOptions(CommandArgs args) {
197 setSplitName(args.get("split-name-index", false));
198 mdr7Excl = args.argToSet("mdr7-excl", null);
199 mdr7Del = args.argToSet("mdr7-del", null);
200 setPoiExcl(args.argToList("poi-excl-index", null));
201 }
202 }