Debian Codegraph @

src/uk/me/parabola/imgfmt/app/lbl/POIRecord.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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: Jan 1, 2008
16 */
17 package uk.me.parabola.imgfmt.app.lbl;
18
19 import java.util.List;
20
21 import uk.me.parabola.imgfmt.app.Exit;
22 import uk.me.parabola.imgfmt.app.ImgFileWriter;
23 import uk.me.parabola.imgfmt.app.Label;
24
25 /**
26 * @author Steve Ratcliffe
27 */
28 public class POIRecord {
29
30 static final int HAS_STREET_NUM = 0x01;
31 static final int HAS_STREET = 0x02;
32 static final int HAS_CITY = 0x04;
33 static final int HAS_ZIP = 0x08;
34 static final int HAS_PHONE = 0x10;
35 static final int HAS_EXIT = 0x20;
36 static final int HAS_TIDE_PREDICTION = 0x40;
37
38 /* Not used yet
39 private static final AddrAbbr ABBR_HASH = new AddrAbbr(' ', "#");
40 private static final AddrAbbr ABBR_APARTMENT = new AddrAbbr('1', "APT");
41 private static final AddrAbbr ABBR_BUILDING = new AddrAbbr('2', "BLDG");
42 private static final AddrAbbr ABBR_DEPT = new AddrAbbr('3', "DEPT");
43 private static final AddrAbbr ABBR_FLAT = new AddrAbbr('4', "FL");
44 private static final AddrAbbr ABBR_ROOM = new AddrAbbr('5', "RM");
45 private static final AddrAbbr ABBR_STE = new AddrAbbr('6', "STE"); // don't know what this is?
46 private static final AddrAbbr ABBR_UNIT = new AddrAbbr('7', "UNIT");
47 */
48
49 private int offset = -1;
50 private Label poiName;
51
52 private final SimpleStreetPhoneNumber simpleStreetNumber = new SimpleStreetPhoneNumber();
53 private final SimpleStreetPhoneNumber simplePhoneNumber = new SimpleStreetPhoneNumber();
54
55 private Label streetName;
56 private Label streetNumberName; // Used for numbers such as 221b
57 private Label complexPhoneNumber; // Used for numbers such as 221b
58
59 private City city;
60 private Zip zip;
61 private Exit exit;
62
63 //private String phoneNumber;
64
65 public void setLabel(Label label) {
66 this.poiName = label;
67 }
68
69 public void setStreetName(Label label) {
70 this.streetName = label;
71 }
72
73 public boolean setSimpleStreetNumber(String streetNumber)
74 {
75 return simpleStreetNumber.set(streetNumber);
76 }
77
78 public void setComplexStreetNumber(Label label)
79 {
80 streetNumberName = label;
81 }
82
83 public boolean setSimplePhoneNumber(String phone)
84 {
85 return simplePhoneNumber.set(phone);
86 }
87
88 public void setComplexPhoneNumber(Label label)
89 {
90 complexPhoneNumber = label;
91 }
92
93
94 public void setZip(Zip zip) {
95 this.zip = zip;
96 }
97
98 public void setCity(City city)
99 {
100 this.city = city;
101 }
102
103 public void setExit(Exit exit) {
104 this.exit = exit;
105 }
106
107 void write(ImgFileWriter writer, int POIGlobalFlags, int realofs,
108 int cityPtrSize, int zipPtrSize, int highwayPtrSize, int exitFacilityPtrSize) {
109 assert offset == realofs : "offset = " + offset + " realofs = " + realofs;
110 int ptr = poiName.getOffset();
111 if (POIGlobalFlags != getPOIFlags())
112 ptr |= 0x800000;
113 writer.put3u(ptr);
114
115 if (POIGlobalFlags != getPOIFlags())
116 writer.put1u(getWrittenPOIFlags(POIGlobalFlags));
117
118 if (streetNumberName != null)
119 {
120 int labOff = streetNumberName.getOffset();
121 // !!! seems to write as hybrid endian
122 writer.put1u(labOff >> 16);
123 writer.put2u(labOff & 0xFFFF);
124 }
125 else if (simpleStreetNumber.isUsed())
126 simpleStreetNumber.write(writer);
127
128 if (streetName != null)
129 writer.put3u(streetName.getOffset());
130
131 if (city != null)
132 writer.putNu(cityPtrSize, city.getIndex());
133
134 if (zip != null)
135 writer.putNu(zipPtrSize, zip.getIndex());
136
137 if (complexPhoneNumber != null)
138 {
139 int labOff = complexPhoneNumber.getOffset();
140 // !!! seems to write as hybrid endian
141 writer.put1u(labOff >> 16);
142 writer.put2u(labOff & 0xFFFF);
143 }
144 else if (simplePhoneNumber.isUsed())
145 simplePhoneNumber.write(writer);
146
147 if(exit != null) {
148 Label description = exit.getDescription();
149 int val = 0;
150 if(description != null) {
151 val = description.getOffset();
152 assert val < 0x400000 : "Exit description label offset too large";
153 }
154 if(exit.getOvernightParking())
155 val |= 0x400000;
156 List<ExitFacility> facilites = exit.getFacilities();
157 ExitFacility ef = null;
158 if(!facilites.isEmpty())
159 ef = facilites.get(0);
160 if(ef != null)
161 val |= 0x800000; // exit facilities defined
162 writer.put3u(val);
163
164 int highwayIndex = exit.getHighway().getIndex();
165 writer.putNu(highwayPtrSize, highwayIndex);
166
167 if(ef != null) {
168 int exitFacilityIndex = ef.getIndex();
169 writer.putNu(exitFacilityPtrSize, exitFacilityIndex);
170 }
171 }
172 }
173
174 int getPOIFlags() {
175 int b = 0;
176 if (streetName != null)
177 b |= HAS_STREET;
178 if (simpleStreetNumber.isUsed() || streetNumberName != null)
179 b |= HAS_STREET_NUM;
180 if (city != null)
181 b |= HAS_CITY;
182 if (zip != null)
183 b |= HAS_ZIP;
184 if (simplePhoneNumber.isUsed() || complexPhoneNumber != null)
185 b |= HAS_PHONE;
186 if (exit != null)
187 b |= HAS_EXIT;
188 return b;
189 }
190
191 int getWrittenPOIFlags(int POIGlobalFlags)
192 {
193 int flag = 0;
194 int j = 0;
195
196 int usedFields = getPOIFlags();
197
198 /* the local POI flag is really tricky if a bit is not set in the global mask
199 we have to skip this bit in the local mask. In other words the meaning of the local bits
200 change influenced by the global bits */
201
202 for(byte i = 0; i < 6; i++)
203 {
204 int mask = 1 << i;
205
206 if((mask & POIGlobalFlags) == mask)
207 {
208 if((mask & usedFields) == mask)
209 flag |= (1 << j);
210 j++;
211 }
212
213 }
214
215 flag |= 0x80; // gpsmapedit asserts for this bit set
216
217 return flag;
218 }
219
220 /**
221 * Sets the start offset of this POIRecord
222 *
223 * @return Number of bytes needed by this entry
224 */
225 int calcOffset(int ofs, int POIGlobalFlags, int cityPtrSize, int zipPtrSize, int highwayPtrSize, int exitFacilityPtrSize) {
226 offset = ofs;
227 int size = 3;
228 if (exit != null) {
229 size += 3;
230 size += highwayPtrSize;
231 if(!exit.getFacilities().isEmpty())
232 size += exitFacilityPtrSize;
233 }
234 if (POIGlobalFlags != getPOIFlags())
235 size += 1;
236 if (simpleStreetNumber.isUsed())
237 size += simpleStreetNumber.getSize();
238 if (streetNumberName != null)
239 size += 3;
240 if (simplePhoneNumber.isUsed())
241 size += simplePhoneNumber.getSize();
242 if (complexPhoneNumber != null)
243 size += 3;
244 if (streetName != null)
245 size += 3;
246 if (city != null)
247 size += cityPtrSize;
248 if (zip != null)
249 size += zipPtrSize;
250 return size;
251 }
252
253 public int getOffset() {
254 if (offset == -1)
255 throw new IllegalStateException("Offset not known yet.");
256 return offset;
257 }
258
259 public Label getNameLabel() {
260 return poiName;
261 }
262
263 public City getCity() {
264 return city;
265 }
266
267 /**
268 * Street and Phone numbers can be stored in two different ways in the poi record
269 * Simple Number that only contain digits are coded in base 11 coding.
270 * This helper class tries to code the given number. If the number contains other
271 * chars like in 4a the coding fails and the caller has to use a Label instead
272 */
273 class SimpleStreetPhoneNumber {
274
275 private byte[] encodedNumber;
276 private int encodedSize;
277
278 /**
279 * Encode a string as base 11.
280 * @param str The input string.
281 * @return If the string is not all numeric (or A) then false is returned
282 * and this object is invalid.
283 */
284 public boolean set(String str) {
285
286 // remove surrounding whitespace to increase chance for simple encoding
287 String number = str.trim();
288
289 encodedNumber = new byte[(number.length()/2)+2];
290
291 int i = 0;
292 int j = 0;
293 while (i < number.length()) {
294
295 int c1 = decodeChar(number.charAt(i++));
296 int c2 = (i < number.length()) ? decodeChar(number.charAt(i++)) : 10;
297
298 // Only 0-9 and - allowed
299 if (c1 < 0 || c1 > 10 || c2 < 0 || c2 > 10)
300 return false;
301
302 // Encode as base 11
303 int val = c1 * 11 + c2;
304
305 // first byte needs special marking with 0x80
306 // If this is not set would be treated as label pointer
307 if (j == 0)
308 val |= 0x80;
309
310 encodedNumber[j++] = (byte)val;
311 }
312 if (j == 0)
313 return false;
314
315 if (j == 1)
316 encodedNumber[j++] = (byte) 0xf8;
317 else
318 encodedNumber[j-1] |= 0x80;
319 encodedSize = j;
320
321 return true;
322 }
323
324 public void write(ImgFileWriter writer)
325 {
326 writer.put(encodedNumber, 0, encodedSize);
327 }
328
329 public boolean isUsed()
330 {
331 return (encodedSize > 0);
332 }
333
334 public int getSize()
335 {
336 return encodedSize;
337 }
338
339 /**
340 * Convert the characters '0' to '9' and '-' to a number 0-10 (base 11).
341 * @param ch The character to convert.
342 * @return A number between 0 and 10 or -1 if the character is not valid.
343 */
344 private int decodeChar(char ch) {
345 if (ch == '-')
346 return 10;
347 else if (ch >= '0' && ch <= '9')
348 return (ch - '0');
349 else
350 return -1;
351 }
352 }
353 }