Debian Codegraph @

src/uk/me/parabola/imgfmt/app/lbl/PlacesFile.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.ArrayList;
20 import java.util.LinkedHashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Random;
25 import java.util.TreeMap;
26
27 import uk.me.parabola.imgfmt.Utils;
28 import uk.me.parabola.imgfmt.app.Exit;
29 import uk.me.parabola.imgfmt.app.ImgFileWriter;
30 import uk.me.parabola.imgfmt.app.Label;
31 import uk.me.parabola.imgfmt.app.srt.CombinedSortKey;
32 import uk.me.parabola.imgfmt.app.srt.Sort;
33 import uk.me.parabola.imgfmt.app.srt.SortKey;
34 import uk.me.parabola.imgfmt.app.trergn.Subdivision;
35
36 /**
37 * This is really part of the LBLFile. We split out all the parts of the file
38 * that are to do with location to here.
39 */
40 public class PlacesFile {
41 public static final int MIN_INDEXED_POI_TYPE = 0x29;
42 public static final int MAX_INDEXED_POI_TYPE = 0x30;
43 private final Map<String, Country> countries = new LinkedHashMap<>();
44 private final List<Country> countryList = new ArrayList<>();
45
46 private final Map<String, Region> regions = new LinkedHashMap<>();
47 private final List<Region> regionList = new ArrayList<>();
48
49 private final Map<String, City> cities = new LinkedHashMap<>();
50 private final List<City> cityList = new ArrayList<>();
51
52 private final Map<String, Zip> postalCodes = new LinkedHashMap<>();
53 private final List<Zip> zipList = new ArrayList<>();
54
55 private final List<Highway> highways = new ArrayList<>();
56 private final List<ExitFacility> exitFacilities = new ArrayList<>();
57 private final List<POIRecord> pois = new ArrayList<>();
58 private final TreeMap<Integer, List<POIIndex>> poiIndex = new TreeMap<>();
59
60 private LBLFile lblFile;
61 private PlacesHeader placeHeader;
62 private boolean poisClosed;
63
64 private Sort sort;
65
66 private final Random random = new Random();
67
68 /**
69 * We need to have links back to the main LBL file and need to be passed
70 * the part of the header that we manage here.
71 *
72 * @param file The main LBL file, used so that we can create labels.
73 * @param pheader The place header.
74 */
75 void init(LBLFile file, PlacesHeader pheader) {
76 lblFile = file;
77 placeHeader = pheader;
78 }
79
80 void write(ImgFileWriter writer) {
81 countryList.forEach(c -> c.write(writer));
82 placeHeader.endCountries(writer.position());
83
84 regionList.forEach(r -> r.write(writer));
85 placeHeader.endRegions(writer.position());
86
87 cityList.forEach(sc -> sc.write(writer));
88
89 placeHeader.endCity(writer.position());
90
91 for (List<POIIndex> pil : poiIndex.values()) {
92 // sort entries by POI name
93 List<SortKey<POIIndex>> sorted = new ArrayList<>();
94 for (POIIndex index : pil) {
95 SortKey<POIIndex> sortKey = sort.createSortKey(index, index.getName());
96 sorted.add(sortKey);
97 }
98 sorted.sort(null);
99
100 for (SortKey<POIIndex> key : sorted) {
101 key.getObject().write(writer);
102 }
103 }
104 placeHeader.endPOIIndex(writer.position());
105
106 int poistart = writer.position();
107 int poiglobalflags = placeHeader.getPOIGlobalFlags();
108 final int cityPtrSize = Utils.numberToPointerSize(cityList.size());
109 final int zipPtrSize = Utils.numberToPointerSize(postalCodes.size());
110 final int highwayPtrSize = Utils.numberToPointerSize(highways.size());
111 final int exitFacilityPtrSize = Utils.numberToPointerSize(exitFacilities.size());
112 for (POIRecord p : pois)
113 p.write(writer, poiglobalflags,
114 writer.position() - poistart, cityPtrSize, zipPtrSize, highwayPtrSize, exitFacilityPtrSize);
115 placeHeader.endPOI(writer.position());
116
117 int numPoiIndexEntries = 0;
118 for (Entry<Integer, List<POIIndex>> entry : poiIndex.entrySet()) {
119 int i = entry.getKey();
120 writer.put1u(i);
121 writer.put3u(numPoiIndexEntries + 1);
122 numPoiIndexEntries += entry.getValue().size();
123 }
124 placeHeader.endPOITypeIndex(writer.position());
125
126 zipList.forEach(z -> z.write(writer));
127 placeHeader.endZip(writer.position());
128
129 int extraHighwayDataOffset = 0;
130 for (Highway h : highways) {
131 h.setExtraDataOffset(extraHighwayDataOffset);
132 extraHighwayDataOffset += h.getExtraDataSize();
133 h.write(writer, false);
134 }
135 placeHeader.endHighway(writer.position());
136
137 exitFacilities.forEach(ef -> ef.write(writer));
138 placeHeader.endExitFacility(writer.position());
139
140 highways.forEach(h -> h.write(writer, true));
141 placeHeader.endHighwayData(writer.position());
142 }
143
144 Country createCountry(String name, String abbr) {
145 String s = abbr != null ? name + (char)0x1d + abbr : name;
146 return countries.computeIfAbsent(s, k -> new Country(countries.size() + 1, lblFile.newLabel(s)));
147 }
148
149 Region createRegion(Country country, String name, String abbr) {
150 String s = abbr != null ? name + (char) 0x1d + abbr : name;
151 String uniqueRegionName = s.toUpperCase() + "_C" + country.getLabel().getOffset();
152 return regions.computeIfAbsent(uniqueRegionName, k -> new Region(country, lblFile.newLabel(s)));
153 }
154
155 /**
156 * Create city without region
157 * @param country the country
158 * @param name the name of the city
159 * @param unique set to true if you needed a new city object
160 * @return the city object
161 */
162 City createCity(Country country, String name, boolean unique) {
163 String uniqueCityName = name.toUpperCase() + "_C" + country.getLabel().getOffset();
164
165 // if unique is true, make sure that the name really is unique
166 if (unique) {
167 uniqueCityName = createUniqueCityName(uniqueCityName);
168 }
169
170 City c = null;
171 if (!unique)
172 c = cities.get(uniqueCityName);
173
174 if (c == null) {
175 c = new City(country);
176
177 c.setLabel(lblFile.newLabel(name));
178
179 cityList.add(c);
180 cities.put(uniqueCityName, c);
181 assert cityList.size() == cities.size() : " cityList and cities are different lengths after inserting " + name + " and " + uniqueCityName;
182 }
183
184 return c;
185 }
186
187 /**
188 * Create city with region
189 * @param region the region
190 * @param name the name of the city
191 * @param unique set to true if you needed a new city object
192 * @return the city object
193 */
194 City createCity(Region region, String name, boolean unique) {
195
196 String uniqueCityName = name.toUpperCase() + "_R" + region.getLabel().getOffset();
197
198 if (unique) {
199 uniqueCityName = createUniqueCityName(uniqueCityName);
200 }
201
202 City c = null;
203 if(!unique) {
204 c = cities.get(uniqueCityName);
205 }
206
207 if(c == null) {
208 c = new City(region);
209
210 c.setLabel(lblFile.newLabel(name));
211
212 cityList.add(c);
213 cities.put(uniqueCityName, c);
214 assert cityList.size() == cities.size() : " cityList and cities are different lengths after inserting " + name + " and " + uniqueCityName;
215 }
216
217 return c;
218 }
219
220 private String createUniqueCityName(String name) {
221 String uniqueCityName = name;
222 while (cities.get(uniqueCityName) != null) {
223 // add semi-random suffix.
224 uniqueCityName += "_" + random.nextInt(0x10000);
225 }
226 return uniqueCityName;
227 }
228
229 Zip createZip(String code) {
230 return postalCodes.computeIfAbsent(code, k -> new Zip(lblFile.newLabel(code)));
231 }
232
233 Highway createHighway(Region region, String name) {
234 Highway h = new Highway(region, highways.size()+1);
235
236 h.setLabel(lblFile.newLabel(name));
237
238 highways.add(h);
239 return h;
240 }
241
242 public ExitFacility createExitFacility(int type, char direction, int facilities, String description, boolean last) {
243 Label d = lblFile.newLabel(description);
244 ExitFacility ef = new ExitFacility(type, direction, facilities, d, last, exitFacilities.size()+1);
245 exitFacilities.add(ef);
246 return ef;
247 }
248
249 POIRecord createPOI(String name) {
250 assert !poisClosed;
251 POIRecord p = new POIRecord();
252
253 p.setLabel(lblFile.newLabel(name));
254 pois.add(p);
255
256 return p;
257 }
258
259 POIRecord createExitPOI(String name, Exit exit) {
260 assert !poisClosed;
261 POIRecord p = new POIRecord();
262
263 p.setLabel(lblFile.newLabel(name));
264 p.setExit(exit);
265 pois.add(p);
266
267 return p;
268 }
269
270 void createPOIIndex(String name, int index, Subdivision group, int type) {
271 assert index < 0x100 : "Too many POIS in division";
272 int t = type >> 8;
273 if (t < MIN_INDEXED_POI_TYPE || t > MAX_INDEXED_POI_TYPE)
274 return;
275
276 POIIndex pi = new POIIndex(name, index, group, type & 0xff);
277 poiIndex.computeIfAbsent(t, k -> new ArrayList<>()).add(pi);
278 }
279
280 void allPOIsDone() {
281 sortCountries();
282 sortRegions();
283 sortCities();
284 sortZips();
285
286 poisClosed = true;
287
288 int poiFlags = 0;
289 for (POIRecord p : pois) {
290 poiFlags |= p.getPOIFlags();
291 }
292 placeHeader.setPOIGlobalFlags(poiFlags);
293
294 int ofs = 0;
295 final int cityPtrSize = Utils.numberToPointerSize(cityList.size());
296 final int zipPtrSize = Utils.numberToPointerSize(postalCodes.size());
297 final int highwayPtrSize = Utils.numberToPointerSize(highways.size());
298 final int exitFacilityPtrSize = Utils.numberToPointerSize(exitFacilities.size());
299 for (POIRecord p : pois)
300 ofs += p.calcOffset(ofs, poiFlags, cityPtrSize, zipPtrSize, highwayPtrSize, exitFacilityPtrSize);
301 }
302
303 /**
304 * I don't know that you have to sort these (after almost all tiles will
305 * only be in one country or at least a very small number).
306 *
307 * But why not?
308 */
309 private void sortCountries() {
310 List<SortKey<Country>> keys = new ArrayList<>();
311 for (Country c : countries.values()) {
312 SortKey<Country> key = sort.createSortKey(c, c.getLabel());
313 keys.add(key);
314 }
315 keys.sort(null);
316
317 countryList.clear();
318 int index = 1;
319 for (SortKey<Country> key : keys) {
320 Country c = key.getObject();
321 c.setIndex(index++);
322 countryList.add(c);
323 }
324 }
325
326 /**
327 * Sort the regions by the defined sort.
328 */
329 private void sortRegions() {
330 List<SortKey<Region>> keys = new ArrayList<>();
331 for (Region r : regions.values()) {
332 SortKey<Region> key = sort.createSortKey(r, r.getLabel(), r.getCountry().getIndex());
333 keys.add(key);
334 }
335 keys.sort(null);
336
337 regionList.clear();
338 int index = 1;
339 for (SortKey<Region> key : keys) {
340 Region r = key.getObject();
341 r.setIndex(index++);
342 regionList.add(r);
343 }
344 }
345
346 /**
347 * Sort the cities by the defined sort.
348 */
349 private void sortCities() {
350 List<SortKey<City>> keys = new ArrayList<>();
351 for (City c : cityList) {
352 SortKey<City> sortKey = sort.createSortKey(c, c.getLabel());
353 sortKey = new CombinedSortKey<>(sortKey, c.getRegionNumber(), c.getCountryNumber());
354 keys.add(sortKey);
355 }
356 keys.sort(null);
357
358 cityList.clear();
359 int index = 1;
360 for (SortKey<City> sc: keys) {
361 City city = sc.getObject();
362 city.setIndex(index++);
363 cityList.add(city);
364 }
365 }
366
367 private void sortZips() {
368 List<SortKey<Zip>> keys = new ArrayList<>();
369 for (Zip c : postalCodes.values()) {
370 SortKey<Zip> sortKey = sort.createSortKey(c, c.getLabel());
371 keys.add(sortKey);
372 }
373 keys.sort(null);
374
375 zipList.clear();
376 int index = 1;
377 for (SortKey<Zip> sc: keys) {
378 Zip zip = sc.getObject();
379 zip.setIndex(index++);
380 zipList.add(zip);
381 }
382 }
383
384 public int numCities() {
385 return cityList.size();
386 }
387
388 public int numZips() {
389 return postalCodes.size();
390 }
391
392 public int numHighways() {
393 return highways.size();
394 }
395
396 public int numExitFacilities() {
397 return exitFacilities.size();
398 }
399
400 public void setSort(Sort sort) {
401 this.sort = sort;
402 }
403 }