| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.imgfmt.app.map; |
| 14 |
|
| 15 |
import java.io.Closeable; |
| 16 |
import java.io.FileNotFoundException; |
| 17 |
import java.io.IOException; |
| 18 |
import java.util.ArrayDeque; |
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.Collections; |
| 21 |
import java.util.Deque; |
| 22 |
import java.util.List; |
| 23 |
|
| 24 |
import uk.me.parabola.imgfmt.Utils; |
| 25 |
import uk.me.parabola.imgfmt.app.Area; |
| 26 |
import uk.me.parabola.imgfmt.app.lbl.City; |
| 27 |
import uk.me.parabola.imgfmt.app.lbl.Country; |
| 28 |
import uk.me.parabola.imgfmt.app.lbl.LBLFileReader; |
| 29 |
import uk.me.parabola.imgfmt.app.lbl.Region; |
| 30 |
import uk.me.parabola.imgfmt.app.lbl.Zip; |
| 31 |
import uk.me.parabola.imgfmt.app.net.NETFileReader; |
| 32 |
import uk.me.parabola.imgfmt.app.net.RoadDef; |
| 33 |
import uk.me.parabola.imgfmt.app.trergn.Point; |
| 34 |
import uk.me.parabola.imgfmt.app.trergn.Polygon; |
| 35 |
import uk.me.parabola.imgfmt.app.trergn.Polyline; |
| 36 |
import uk.me.parabola.imgfmt.app.trergn.RGNFileReader; |
| 37 |
import uk.me.parabola.imgfmt.app.trergn.Subdivision; |
| 38 |
import uk.me.parabola.imgfmt.app.trergn.TREFileReader; |
| 39 |
import uk.me.parabola.imgfmt.app.trergn.Zoom; |
| 40 |
import uk.me.parabola.imgfmt.fs.DirectoryEntry; |
| 41 |
import uk.me.parabola.imgfmt.fs.FileSystem; |
| 42 |
import uk.me.parabola.imgfmt.fs.ImgChannel; |
| 43 |
import uk.me.parabola.imgfmt.sys.ImgFS; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class MapReader implements Closeable { |
| 52 |
private final TREFileReader treFile; |
| 53 |
private final RGNFileReader rgnFile; |
| 54 |
private final LBLFileReader lblFile; |
| 55 |
private final NETFileReader netFile; |
| 56 |
|
| 57 |
public static final boolean WITH_EXT_TYPE_DATA = true; |
| 58 |
public static final boolean WITHOUT_EXT_TYPE_DATA = false; |
| 59 |
|
| 60 |
private final Deque<Closeable> toClose = new ArrayDeque<>(); |
| 61 |
|
| 62 |
public MapReader(String filename) throws FileNotFoundException { |
| 63 |
FileSystem fs = ImgFS.openFs(filename); |
| 64 |
saveForClose(fs); |
| 65 |
|
| 66 |
List<DirectoryEntry> entries = fs.list(); |
| 67 |
|
| 68 |
|
| 69 |
String mapname = null; |
| 70 |
for (DirectoryEntry ent : entries) { |
| 71 |
if ("TRE".equals(ent.getExt())) { |
| 72 |
mapname = ent.getName(); |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (mapname == null) |
| 78 |
throw new FileNotFoundException("No TRE entry in img file"); |
| 79 |
|
| 80 |
ImgChannel chan = fs.open(mapname + ".TRE", "r"); |
| 81 |
treFile = new TREFileReader(chan); |
| 82 |
saveForClose(treFile, chan); |
| 83 |
|
| 84 |
chan = fs.open(mapname + ".RGN", "r"); |
| 85 |
rgnFile = new RGNFileReader(chan); |
| 86 |
saveForClose(rgnFile, chan); |
| 87 |
|
| 88 |
chan = fs.open(mapname + ".LBL", "r"); |
| 89 |
lblFile = new LBLFileReader(chan); |
| 90 |
saveForClose(lblFile, chan); |
| 91 |
|
| 92 |
|
| 93 |
NETFileReader nr; |
| 94 |
try { |
| 95 |
chan = fs.open(mapname + ".NET", "r"); |
| 96 |
nr = new NETFileReader(chan); |
| 97 |
nr.setLabels(lblFile); |
| 98 |
nr.setCities(lblFile.getCities()); |
| 99 |
nr.setZips(lblFile.getZips()); |
| 100 |
saveForClose(nr); |
| 101 |
} catch (FileNotFoundException e) { |
| 102 |
nr = null; |
| 103 |
} |
| 104 |
netFile = nr; |
| 105 |
|
| 106 |
rgnFile.setNetFile(netFile); |
| 107 |
rgnFile.setLblFile(lblFile); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public List<Point> pointsForLevel(int level, boolean withExtType) { |
| 115 |
List<Point> points = new ArrayList<>(); |
| 116 |
|
| 117 |
Subdivision[] subdivisions = treFile.subdivForLevel(level); |
| 118 |
for (Subdivision sd : subdivisions) { |
| 119 |
List<Point> subdivPoints = rgnFile.pointsForSubdiv(sd, withExtType); |
| 120 |
points.addAll(subdivPoints); |
| 121 |
} |
| 122 |
|
| 123 |
return points; |
| 124 |
} |
| 125 |
|
| 126 |
public Zoom[] getLevels() { |
| 127 |
return treFile.getMapLevels(); |
| 128 |
} |
| 129 |
|
| 130 |
public String[] getCopyrights(){ |
| 131 |
return treFile.getCopyrights(lblFile); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public List<Polyline> linesForLevel(int level) { |
| 138 |
ArrayList<Polyline> lines = new ArrayList<>(); |
| 139 |
|
| 140 |
Subdivision[] subdivisions = treFile.subdivForLevel(level); |
| 141 |
for (Subdivision div : subdivisions) { |
| 142 |
List<Polyline> subdivLines = rgnFile.linesForSubdiv(div); |
| 143 |
lines.addAll(subdivLines); |
| 144 |
} |
| 145 |
|
| 146 |
return lines; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public List<Polygon> shapesForLevel(int level, boolean witExtTypeData) { |
| 151 |
ArrayList<Polygon> shapes = new ArrayList<>(); |
| 152 |
|
| 153 |
Subdivision[] subdivisions = treFile.subdivForLevel(level); |
| 154 |
for (Subdivision div : subdivisions) { |
| 155 |
List<Polygon> subdivShapes = rgnFile.shapesForSubdiv(div, witExtTypeData); |
| 156 |
shapes.addAll(subdivShapes); |
| 157 |
} |
| 158 |
|
| 159 |
return shapes; |
| 160 |
} |
| 161 |
|
| 162 |
public void close() throws IOException { |
| 163 |
for (Closeable c : toClose) |
| 164 |
Utils.closeFile(c); |
| 165 |
} |
| 166 |
|
| 167 |
private void saveForClose(Closeable c1, Closeable c2) { |
| 168 |
saveForClose(c1); |
| 169 |
saveForClose(c2); |
| 170 |
} |
| 171 |
|
| 172 |
private void saveForClose(Closeable c) { |
| 173 |
toClose.push(c); |
| 174 |
} |
| 175 |
|
| 176 |
public List<City> getCities() { |
| 177 |
return lblFile.getCities(); |
| 178 |
} |
| 179 |
|
| 180 |
public List<Country> getCountries() { |
| 181 |
return lblFile.getCountries(); |
| 182 |
} |
| 183 |
|
| 184 |
public List<Region> getRegions() { |
| 185 |
return lblFile.getRegions(); |
| 186 |
} |
| 187 |
|
| 188 |
public List<Zip> getZips() { |
| 189 |
|
| 190 |
|
| 191 |
return lblFile.getZips(); |
| 192 |
} |
| 193 |
|
| 194 |
public Area getTreBounds() { |
| 195 |
return treFile.getBounds(); |
| 196 |
} |
| 197 |
|
| 198 |
public java.util.Map<Integer, String> getLabels() { |
| 199 |
return lblFile.getLabels(); |
| 200 |
} |
| 201 |
|
| 202 |
public List<RoadDef> getRoads() { |
| 203 |
if (netFile == null) |
| 204 |
return Collections.emptyList(); |
| 205 |
return netFile.getRoads(); |
| 206 |
} |
| 207 |
|
| 208 |
public int getEncodingType(){ |
| 209 |
return lblFile.getEncodingType(); |
| 210 |
} |
| 211 |
} |