| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package uk.me.parabola.imgfmt.app.dem; |
| 15 |
|
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import uk.me.parabola.imgfmt.ExitException; |
| 19 |
import uk.me.parabola.imgfmt.Utils; |
| 20 |
import uk.me.parabola.imgfmt.app.Area; |
| 21 |
import uk.me.parabola.imgfmt.app.BufferedImgFileReader; |
| 22 |
import uk.me.parabola.imgfmt.app.BufferedImgFileWriter; |
| 23 |
import uk.me.parabola.imgfmt.app.ImgFile; |
| 24 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 25 |
import uk.me.parabola.imgfmt.fs.ImgChannel; |
| 26 |
import uk.me.parabola.log.Logger; |
| 27 |
import uk.me.parabola.mkgmap.reader.hgt.HGTConverter; |
| 28 |
import uk.me.parabola.mkgmap.reader.hgt.HGTConverter.InterpolationMethod; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class DEMFile extends ImgFile { |
| 38 |
private static final Logger log = Logger.getLogger(DEMFile.class); |
| 39 |
|
| 40 |
|
| 41 |
public final static double EXTRA = 0.1d; |
| 42 |
protected final static double FACTOR = 45.0d / (1 << 29); |
| 43 |
|
| 44 |
private final DEMHeader demHeader = new DEMHeader(); |
| 45 |
|
| 46 |
public DEMFile(ImgChannel chan, boolean write) { |
| 47 |
setHeader(demHeader); |
| 48 |
|
| 49 |
if (write) { |
| 50 |
setWriter(new BufferedImgFileWriter(chan, "DEM")); |
| 51 |
position(DEMHeader.HEADER_LEN); |
| 52 |
} else { |
| 53 |
setReader(new BufferedImgFileReader(chan)); |
| 54 |
demHeader.readHeader(getReader()); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Area calc(Area area, java.awt.geom.Area demPolygonMapUnits, String pathsToHGT, List<Integer> pointDistances, |
| 75 |
short outsidePolygonHeight, InterpolationMethod interpolationMethod) { |
| 76 |
|
| 77 |
HGTConverter hgtConverter = new HGTConverter(pathsToHGT, area, demPolygonMapUnits, EXTRA); |
| 78 |
hgtConverter.setInterpolationMethod(interpolationMethod); |
| 79 |
hgtConverter.setOutsidePolygonHeight(outsidePolygonHeight); |
| 80 |
|
| 81 |
log.info("orig bounds",area); |
| 82 |
|
| 83 |
int alignment = 4; |
| 84 |
Area treArea = calcTREBounds (area, alignment); |
| 85 |
log.info("TRE bounds",treArea); |
| 86 |
|
| 87 |
int top = treArea.getMaxLat() * 256; |
| 88 |
int bottom = treArea.getMinLat() * 256; |
| 89 |
int left = treArea.getMinLong() * 256; |
| 90 |
int right = treArea.getMaxLong() * 256; |
| 91 |
|
| 92 |
int zoom = 0; |
| 93 |
int lastDist = pointDistances.get(pointDistances.size() - 1); |
| 94 |
for (int pointDist : pointDistances) { |
| 95 |
int distance = pointDist; |
| 96 |
if (distance == -1) { |
| 97 |
int res = (hgtConverter.getHighestRes() > 0) ? hgtConverter.getHighestRes() : 1200; |
| 98 |
distance = (int) Math.round((1 << 29) / (res * 45.0D)); |
| 99 |
} |
| 100 |
|
| 101 |
distance = ((distance + 8) / 16) * 16; |
| 102 |
if (distance == 0) |
| 103 |
throw new ExitException("DEM distance is zero"); |
| 104 |
|
| 105 |
int xTop = top; |
| 106 |
int xLeft = left; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if (distance < (int) Math.floor((EXTRA / 45.0D * (1 << 29)))) { |
| 111 |
xTop = moveUp(top, distance); |
| 112 |
xLeft = moveLeft(left, distance); |
| 113 |
} |
| 114 |
|
| 115 |
DEMSection section = new DEMSection(zoom++, xTop, xLeft, xTop - bottom, right - xLeft, hgtConverter, |
| 116 |
distance, pointDist == lastDist); |
| 117 |
demHeader.addSection(section); |
| 118 |
} |
| 119 |
return treArea; |
| 120 |
} |
| 121 |
|
| 122 |
private Area calcTREBounds(Area area, int alignment) { |
| 123 |
int treTop = area.getMaxLat() + 1; |
| 124 |
int treLeft = area.getMinLong() - 1; |
| 125 |
|
| 126 |
int treBottom = area.getMinLat() + 1; |
| 127 |
int treRight = area.getMaxLong() - 1; |
| 128 |
while (treTop % alignment != 0) |
| 129 |
++treTop; |
| 130 |
while (treLeft % alignment != 0) |
| 131 |
--treLeft; |
| 132 |
while (treBottom % alignment != 0) |
| 133 |
++treBottom; |
| 134 |
while (treRight % alignment != 0) |
| 135 |
--treRight; |
| 136 |
treBottom -= alignment; |
| 137 |
treRight += alignment; |
| 138 |
|
| 139 |
treRight = Math.min(treRight, Utils.MAX_LON_MAP_UNITS); |
| 140 |
treLeft = Math.max(treLeft, Utils.MIN_LON_MAP_UNITS); |
| 141 |
treTop = Math.min(treTop, Utils.MAX_LAT_MAP_UNITS); |
| 142 |
treBottom = Math.max(treBottom, Utils.MIN_LAT_MAP_UNITS); |
| 143 |
Area treArea = new Area(treBottom, treLeft, treTop, treRight); |
| 144 |
return treArea; |
| 145 |
} |
| 146 |
|
| 147 |
int moveUp(int origLat32, int distance) { |
| 148 |
int moved = origLat32; |
| 149 |
if (moved >= 0) { |
| 150 |
moved -= moved % distance; |
| 151 |
if (moved < 0x3FFFFFFF - distance) |
| 152 |
moved += distance; |
| 153 |
} else { |
| 154 |
moved -= moved % distance; |
| 155 |
} |
| 156 |
return moved; |
| 157 |
} |
| 158 |
|
| 159 |
int moveLeft(int origLon32, int distance) { |
| 160 |
int moved = origLon32; |
| 161 |
if (moved >= 0) { |
| 162 |
moved -= moved % distance; |
| 163 |
} else { |
| 164 |
moved -= moved % distance; |
| 165 |
if (moved > Integer.MIN_VALUE + distance) |
| 166 |
moved -= distance; |
| 167 |
} |
| 168 |
return moved; |
| 169 |
} |
| 170 |
|
| 171 |
public void write() { |
| 172 |
ImgFileWriter w = getWriter(); |
| 173 |
if (w instanceof BufferedImgFileWriter) { |
| 174 |
|
| 175 |
((BufferedImgFileWriter) w).setMaxSize(0xfffffff); |
| 176 |
} |
| 177 |
getHeader().writeHeader(getWriter()); |
| 178 |
} |
| 179 |
} |