Debian Codegraph @

src/uk/me/parabola/imgfmt/app/dem/DEMFile.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
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 * The DEM file. This consists of information about elevation. It is used for
32 * hill shading and to calculation the "ele" values in gpx tracks. Based on work
33 * of Frank Stinner.
34 *
35 * @author Gerd Petermann
36 */
37 public class DEMFile extends ImgFile {
38 private static final Logger log = Logger.getLogger(DEMFile.class);
39
40 // allowed increase of DEM area
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 * Calculate the DEM data for a tile.
60 *
61 * @param area
62 * the bounding box of the tile
63 * @param demPolygonMapUnits
64 * a bounding polygon which might be smaller than the area
65 * @param pathsToHGT
66 * string with comma separated list of directories or zip files
67 * @param pointDistances
68 * list of distances which determine the resolution
69 * @param outsidePolygonHeight
70 * the height value that should be used for points outside of the
71 * bounding polygon
72 * @return a new bounding box that should be used for the TRE file
73 */
74 public Area calc(Area area, java.awt.geom.Area demPolygonMapUnits, String pathsToHGT, List<Integer> pointDistances,
75 short outsidePolygonHeight, InterpolationMethod interpolationMethod) {
76 // HGT area is extended by EXTRA degrees in each direction
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 // last 4 bits of distance should be 0
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 // align DEM to distance raster, if distance not bigger than
109 // widening of HGT area
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 // increase file size limit to 256MB, no idea what the limit is
175 ((BufferedImgFileWriter) w).setMaxSize(0xfffffff);
176 }
177 getHeader().writeHeader(getWriter());
178 }
179 }