| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.net; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.BitSet; |
| 21 |
import java.util.Comparator; |
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Map; |
| 25 |
|
| 26 |
import uk.me.parabola.imgfmt.Utils; |
| 27 |
import uk.me.parabola.imgfmt.app.BufferedImgFileWriter; |
| 28 |
import uk.me.parabola.imgfmt.app.ImgFile; |
| 29 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 30 |
import uk.me.parabola.imgfmt.app.Label; |
| 31 |
import uk.me.parabola.imgfmt.app.lbl.City; |
| 32 |
import uk.me.parabola.imgfmt.app.lbl.Zip; |
| 33 |
import uk.me.parabola.imgfmt.app.srt.DoubleSortKey; |
| 34 |
import uk.me.parabola.imgfmt.app.srt.IntegerSortKey; |
| 35 |
import uk.me.parabola.imgfmt.app.srt.MultiSortKey; |
| 36 |
import uk.me.parabola.imgfmt.app.srt.Sort; |
| 37 |
import uk.me.parabola.imgfmt.app.srt.SortKey; |
| 38 |
import uk.me.parabola.imgfmt.fs.ImgChannel; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class NETFile extends ImgFile { |
| 48 |
private final NETHeader netHeader = new NETHeader(); |
| 49 |
private List<RoadDef> roads; |
| 50 |
private Sort sort; |
| 51 |
|
| 52 |
public NETFile(ImgChannel chan) { |
| 53 |
setHeader(netHeader); |
| 54 |
setWriter(new BufferedImgFileWriter(chan, "NET")); |
| 55 |
position(NETHeader.HEADER_LEN); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public void write(int numCities, int numZips) { |
| 64 |
|
| 65 |
ImgFileWriter writer = netHeader.makeRoadWriter(getWriter()); |
| 66 |
try { |
| 67 |
for (RoadDef rd : roads) |
| 68 |
rd.writeNet1(writer, numCities, numZips); |
| 69 |
|
| 70 |
} finally { |
| 71 |
Utils.closeFile(writer); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public void writePost(ImgFileWriter rgn) { |
| 83 |
for (RoadDef rd : roads) |
| 84 |
rd.writeRgnOffsets(rgn); |
| 85 |
|
| 86 |
ImgFileWriter writer = netHeader.makeSortedRoadWriter(getWriter()); |
| 87 |
try { |
| 88 |
List<LabeledRoadDef> labeledRoadDefs = deDupRoads(); |
| 89 |
sortByName(labeledRoadDefs); |
| 90 |
for (LabeledRoadDef labeledRoadDef : labeledRoadDefs) |
| 91 |
labeledRoadDef.roadDef.putSortedRoadEntry(writer, labeledRoadDef.label); |
| 92 |
} finally { |
| 93 |
Utils.closeFile(writer); |
| 94 |
} |
| 95 |
|
| 96 |
getHeader().writeHeader(getWriter()); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
private List<LabeledRoadDef> deDupRoads() { |
| 117 |
List<SortKey<LabeledRoadDef>> sortKeys = createSortKeysByNameAndCity(); |
| 118 |
sortKeys.sort(null); |
| 119 |
|
| 120 |
List<LabeledRoadDef> out = new ArrayList<>(sortKeys.size()); |
| 121 |
|
| 122 |
List<LabeledRoadDef> dupes = new ArrayList<>(); |
| 123 |
SortKey<LabeledRoadDef> lastKey = null; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
for (SortKey<LabeledRoadDef> key : sortKeys) { |
| 128 |
if (lastKey == null || key.compareTo(lastKey) != 0) { |
| 129 |
analyseRoadsOfCity(dupes, out); |
| 130 |
dupes.clear(); |
| 131 |
lastKey = key; |
| 132 |
} |
| 133 |
dupes.add(key.getObject()); |
| 134 |
} |
| 135 |
|
| 136 |
analyseRoadsOfCity(dupes, out); |
| 137 |
return out; |
| 138 |
} |
| 139 |
|
| 140 |
private List<SortKey<LabeledRoadDef>> createSortKeysByNameAndCity() { |
| 141 |
List<SortKey<LabeledRoadDef>> sortKeys = new ArrayList<>(roads.size()); |
| 142 |
|
| 143 |
for (RoadDef rd : roads) { |
| 144 |
Label[] labels = rd.getLabels(); |
| 145 |
for (int i = 0; i < labels.length && labels[i] != null; ++i) { |
| 146 |
Label label = labels[i]; |
| 147 |
if (label.getLength() == 0) |
| 148 |
continue; |
| 149 |
|
| 150 |
|
| 151 |
LabeledRoadDef lrd = new LabeledRoadDef(label, rd); |
| 152 |
SortKey<LabeledRoadDef> nameKey = new IntegerSortKey<>(lrd, label.getOffset(), 0); |
| 153 |
|
| 154 |
|
| 155 |
City city = (rd.getCities().isEmpty() ? null : rd.getCities().get(0)); |
| 156 |
SortKey<LabeledRoadDef> cityKey; |
| 157 |
if (city != null) { |
| 158 |
int region = city.getRegionNumber(); |
| 159 |
int country = city.getCountryNumber(); |
| 160 |
cityKey = sort.createSortKey(null, city.getLabel(), (region & 0xffff) << 16 | (country & 0xffff)); |
| 161 |
} else { |
| 162 |
cityKey = sort.createSortKey(null, Label.NULL_OUT_LABEL, 0); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
Zip zip = (rd.getZips().isEmpty() ? null : rd.getZips().get(0)); |
| 167 |
Label zipLabel = zip == null ? Label.NULL_OUT_LABEL: zip.getLabel(); |
| 168 |
SortKey<LabeledRoadDef> zipKey = sort.createSortKey(null, zipLabel); |
| 169 |
|
| 170 |
sortKeys.add(new MultiSortKey<>(nameKey, cityKey, zipKey)); |
| 171 |
} |
| 172 |
} |
| 173 |
return sortKeys; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private void sortByName(List<LabeledRoadDef> roads) { |
| 181 |
List<SortKey<LabeledRoadDef>> sortKeys = new ArrayList<>(roads.size()); |
| 182 |
Map<Label, byte[]> cachePartial = new HashMap<>(); |
| 183 |
Map<Label, byte[]> cacheFull = new HashMap<>(); |
| 184 |
|
| 185 |
for (LabeledRoadDef lrd : roads) { |
| 186 |
SortKey<LabeledRoadDef> sk1 = sort.createSortKeyPartial(lrd, lrd.label, 0, cachePartial); |
| 187 |
SortKey<LabeledRoadDef> sk2 = sort.createSortKey(null, lrd.label, 0, cacheFull); |
| 188 |
sortKeys.add(new DoubleSortKey<>(sk1, sk2)); |
| 189 |
} |
| 190 |
sortKeys.sort(null); |
| 191 |
roads.clear(); |
| 192 |
for (SortKey<LabeledRoadDef> key : sortKeys) { |
| 193 |
roads.add(key.getObject()); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private static void analyseRoadsOfCity(List<LabeledRoadDef> in, List<LabeledRoadDef> out) { |
| 205 |
|
| 206 |
|
| 207 |
if (in.size() > 200) { |
| 208 |
analyseRoadsOfCityLarge(in, out); |
| 209 |
} else { |
| 210 |
analyseRoadsOfCitySmall(in, out); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private static void analyseRoadsOfCitySmall(List<LabeledRoadDef> in, List<LabeledRoadDef> out) { |
| 224 |
if (in.size() < 2) { |
| 225 |
out.addAll(in); |
| 226 |
} else { |
| 227 |
|
| 228 |
in.sort((o1, o2) -> Boolean.compare(needed(o2), needed(o1))); |
| 229 |
|
| 230 |
|
| 231 |
int posOther = -1; |
| 232 |
for (int i = 0; i < in.size(); i++) { |
| 233 |
if (needed(in.get(i))) { |
| 234 |
out.add(in.get(i)); |
| 235 |
} else { |
| 236 |
posOther = i; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
if (posOther >= 0) { |
| 241 |
findRoadNetworks(in, posOther, out); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
private static void analyseRoadsOfCityLarge(List<LabeledRoadDef> in, List<LabeledRoadDef> out) { |
| 261 |
in.sort(Comparator.comparingInt(lr -> lr.roadDef.getStartSubdivNumber())); |
| 262 |
|
| 263 |
int lastDiv = 0; |
| 264 |
List<LabeledRoadDef> dupes = new ArrayList<>(); |
| 265 |
for (LabeledRoadDef lrd : in) { |
| 266 |
int sd = lrd.roadDef.getStartSubdivNumber(); |
| 267 |
if (sd != lastDiv) { |
| 268 |
analyseRoadsOfCitySmall(dupes, out); |
| 269 |
dupes.clear(); |
| 270 |
lastDiv = sd; |
| 271 |
} |
| 272 |
dupes.add(lrd); |
| 273 |
} |
| 274 |
|
| 275 |
analyseRoadsOfCitySmall(dupes, out); |
| 276 |
} |
| 277 |
|
| 278 |
private static boolean needed (LabeledRoadDef lr) { |
| 279 |
return lr.roadDef.hasHouseNumbers() || lr.roadDef.getCities().size() > 1; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
private static void findRoadNetworks(List<LabeledRoadDef> in, int posOther, List<LabeledRoadDef> out) { |
| 289 |
|
| 290 |
int inSize = in.size(); |
| 291 |
int[] groups = new int[inSize]; |
| 292 |
for (int i = 0; i < groups.length; i++) |
| 293 |
groups[i] = i; |
| 294 |
|
| 295 |
|
| 296 |
BitSet unconnected = new BitSet(inSize * inSize); |
| 297 |
|
| 298 |
|
| 299 |
boolean done; |
| 300 |
do { |
| 301 |
done = true; |
| 302 |
for (int current = 0; current < groups.length; current++) { |
| 303 |
RoadDef first = in.get(current).roadDef; |
| 304 |
|
| 305 |
for (int i = current + 1; i < groups.length; i++) { |
| 306 |
|
| 307 |
if (groups[current] != groups[i] && !unconnected.get(current * inSize + i)) { |
| 308 |
|
| 309 |
if (first.connectedTo(in.get(i).roadDef)) { |
| 310 |
groups[current] = groups[i] = Math.min(groups[current], groups[i]); |
| 311 |
done = false; |
| 312 |
} else { |
| 313 |
unconnected.set(current * inSize + i); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} while (!done); |
| 319 |
|
| 320 |
|
| 321 |
int last = posOther - 1; |
| 322 |
for (int i = posOther; i < groups.length; i++) { |
| 323 |
if (groups[i] > last) { |
| 324 |
LabeledRoadDef lrd = in.get(i); |
| 325 |
out.add(lrd); |
| 326 |
last = groups[i]; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
public void setNetwork(List<RoadDef> roads) { |
| 332 |
this.roads = roads; |
| 333 |
} |
| 334 |
|
| 335 |
public void setSort(Sort sort) { |
| 336 |
this.sort = sort; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
class LabeledRoadDef { |
| 344 |
private final Label label; |
| 345 |
private final RoadDef roadDef; |
| 346 |
|
| 347 |
LabeledRoadDef(Label label, RoadDef roadDef) { |
| 348 |
this.label = label; |
| 349 |
this.roadDef = roadDef; |
| 350 |
} |
| 351 |
} |
| 352 |
} |