Debian Codegraph @

src/uk/me/parabola/imgfmt/app/dem/DEMSection.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017.
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 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.dem;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import uk.me.parabola.imgfmt.Utils;
19 import uk.me.parabola.imgfmt.app.ImgFileWriter;
20 import uk.me.parabola.log.Logger;
21 import uk.me.parabola.mkgmap.reader.hgt.HGTConverter;
22
23 public class DEMSection {
24 private static final Logger log = Logger.getLogger(DEMSection.class);
25 private static final int STD_DIM = 64;
26 private int unknown1 = 0;
27 private final int zoomLevel;
28 private final boolean lastLevel;
29 private final int pointsPerLat = STD_DIM;
30 private final int pointsPerLon = STD_DIM;
31 private final int nonStdHeight;
32 private final int nonStdWidth;
33 private final short flags1 = 0;
34 private final int tilesLat;
35 private final int tilesLon;
36 private int recordDesc;
37 private int tileDescSize;
38 private int dataOffset;
39 private int dataOffset2;
40 private final int pointsDistanceLat;
41 private final int pointsDistanceLon;
42 private final int top;
43 private final int left;
44 private boolean hasExtra;
45 private int minHeight = Integer.MAX_VALUE;
46 private int maxHeight = Integer.MIN_VALUE;
47 private List<DEMTile> tiles = new ArrayList<>();
48
49 /**
50 * Calculate the DEM data for the given position and resolution.
51 * @param zoomLevel the zoom level
52 * @param areaTop latitude of upper left corner in DEM units
53 * @param areaLeft longitude of upper left corner in DEM units
54 * @param areaHeight height in DEM units
55 * @param areaWidth width in DEM units
56 * @param hgtConverter the hgt converter
57 * @param pointDist distance in DEM units between to height samples
58 * @param lastLevel: set to true to signal that readers are no longer needed for further levels
59 */
60 public DEMSection(int zoomLevel, int areaTop, int areaLeft, int areaHeight, int areaWidth,
61 HGTConverter hgtConverter, int pointDist, boolean lastLevel) {
62 this.zoomLevel = zoomLevel;
63 this.lastLevel = lastLevel;
64
65 this.top = areaTop;
66 this.left = areaLeft;
67
68 // calculate raster that starts at top left corner
69 // last row and right column have non-standard height / row values
70 pointsDistanceLat = pointDist;
71 pointsDistanceLon = pointDist;
72
73 // allow automatic selection of interpolation method for each zoom level
74 hgtConverter.startNewLevel(pointDist);
75
76 int[] latInfo = getTileInfo(areaHeight, pointsDistanceLat);
77 int[] lonInfo = getTileInfo(areaWidth, pointsDistanceLon);
78 // store the values written to the header
79 tilesLat = latInfo[0];
80 tilesLon = lonInfo[0];
81 nonStdHeight = latInfo[1];
82 nonStdWidth = lonInfo[1];
83 log.info("calculating zoom level:",zoomLevel,", dist:",pointDist,tilesLon,"x",tilesLat,"std tiles, nonstd x/y",nonStdWidth,"/",nonStdHeight);
84 calcTiles(hgtConverter);
85 }
86
87 /**
88 * Calculate the number of rows / columns and the non-standard height/width
89 * @param demPoints number of 32 bit points
90 * @param demDist distance between two sample points
91 * @return array with dimension and non standard value normalised to 1 .. 95
92 */
93 private int[] getTileInfo(int demPoints, int demDist) {
94 int resolution = STD_DIM * demDist;
95 demPoints += demDist; // probably not needed, but Garmin seems to prefer large overlaps
96 int nFull = demPoints / resolution;
97 int rest = demPoints - nFull * resolution;
98 int num = nFull;
99 int nonstd = rest / demDist;
100 if (rest % demDist != 0)
101 ++nonstd;
102 // normalise non std value so that it is between 1 .. 95 because Garmin does it also
103 if (nonstd >= STD_DIM / 2) {
104 ++num;
105 } else {
106 if (num > 0)
107 nonstd += STD_DIM;
108 }
109 if (num == 0)
110 num = 1;
111 int[] res = {num, nonstd};
112 return res;
113 }
114
115
116 private void calcTiles(HGTConverter hgtConverter) {
117 int resLon = pointsPerLon * pointsDistanceLon;
118 int resLat = pointsPerLat * pointsDistanceLat;
119 int latOff;
120 int lonOff;
121 int dataLen = 0;
122 int minBaseHeight = Integer.MAX_VALUE;
123 int maxBaseHeight = Integer.MIN_VALUE;
124 int maxDeltaHeight = Integer.MIN_VALUE;
125
126 for (int m = 0; m < tilesLat; m++) {
127 latOff = top - m * resLat;
128
129 int height = pointsPerLat;
130 if (m + 1 == tilesLat) {
131 height = nonStdHeight;
132 }
133 for (int n = 0; n < tilesLon; n++) {
134 lonOff = left + n * resLon;
135 int width = pointsPerLon;
136 if (n + 1 == tilesLon) {
137 width = nonStdWidth;
138 }
139 short[] realHeights = hgtConverter.getHeights(latOff, lonOff, height, width);
140 DEMTile tile;
141 tile = new DEMTile(n, m, width, height, realHeights);
142 tiles.add(tile);
143 if (tile.getEncodingType() != 0)
144 hasExtra = true;
145 if (tile.hasValidHeights()) {
146 if (tile.getBaseHeight() < minBaseHeight)
147 minBaseHeight = tile.getBaseHeight();
148 if (tile.getBaseHeight() > maxBaseHeight)
149 maxBaseHeight = tile.getBaseHeight();
150 if (tile.getMaxHeight() > maxHeight)
151 maxHeight = tile.getMaxHeight();
152 if (tile.getMaxDeltaHeight() > maxDeltaHeight)
153 maxDeltaHeight = tile.getMaxDeltaHeight();
154 }
155 dataLen += tile.getBitStreamLen();
156 }
157 if (lastLevel) {
158 hgtConverter.freeMem();
159 }
160 }
161
162 hgtConverter.printStat();
163
164 if (dataLen > 0) {
165 minHeight = minBaseHeight;
166 } else {
167 minHeight = 0;
168 maxHeight = 0;
169 }
170 int deltaSize = (maxDeltaHeight <= 255) ? 1 : 2;
171 int baseSize = (-128 < minBaseHeight && maxBaseHeight < 128) ? 1 : 2;
172 int offsetSize = Utils.numberToPointerSize(dataLen);
173
174 tileDescSize = offsetSize + baseSize + deltaSize + (hasExtra ? 1:0);
175 recordDesc = offsetSize -1; // 0..3
176 if (baseSize > 1)
177 recordDesc |= (1 << 2);
178 if (deltaSize > 1)
179 recordDesc |= (1 << 3);
180 if (hasExtra)
181 recordDesc |= (1 << 4);
182
183 }
184
185 public void writeHeader(ImgFileWriter writer) {
186 writer.put1u(unknown1); //0x00
187 writer.put1u(zoomLevel); //0x01
188 writer.put4(pointsPerLat); //0x02
189 writer.put4(pointsPerLon); //0x06
190 writer.put4(nonStdHeight - 1); //0x0A
191 writer.put4(nonStdWidth - 1); //0x0E
192 writer.put2u(flags1); //0x12
193 writer.put4(tilesLon - 1); //0x14
194 writer.put4(tilesLat - 1); //0x18
195
196 writer.put2u(recordDesc); //0x1c
197 writer.put2u(tileDescSize); //0x1e
198 writer.put4(dataOffset); //0x20
199 writer.put4(dataOffset2); //0x24
200 writer.put4(left); //0x28
201 writer.put4(top); //0x2c
202 writer.put4(pointsDistanceLat); //0x30
203 writer.put4(pointsDistanceLon); //0x34
204 writer.put2s(minHeight); //0x38
205 writer.put2s(maxHeight); //0x3a
206 }
207
208 public void writeRest(ImgFileWriter writer) {
209 dataOffset = writer.position();
210
211 int off = 0;
212 for (DEMTile tile : tiles) {
213 tile.setOffset(off);
214 tile.writeHeader(writer, recordDesc);
215 off += tile.getBitStreamLen();
216 }
217 dataOffset2 = writer.position();
218 for (DEMTile tile : tiles) {
219 tile.writeBitStreamData(writer);
220 }
221 }
222 }
223