Debian Codegraph @

src/uk/me/parabola/imgfmt/app/Area.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2006 Steve Ratcliffe
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 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 07-Dec-2006
16 */
17 package uk.me.parabola.imgfmt.app;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Locale;
22
23 import uk.me.parabola.imgfmt.MapFailedException;
24 import uk.me.parabola.imgfmt.Utils;
25 import uk.me.parabola.log.Logger;
26
27 /**
28 * A map area in map units. There is a constructor available for creating
29 * in lat/long form.
30 *
31 * @author Steve Ratcliffe
32 */
33 public class Area {
34 private static final Logger log = Logger.getLogger(Area.class);
35 public static final Area PLANET = new Area(-90.0, -180.0, 90.0, 180.0);
36
37 private final int minLat;
38 private final int minLong;
39 private final int maxLat;
40 private final int maxLong;
41
42 /**
43 * Create an area from the given coordinates. We ensure that no dimension
44 * is zero.
45 *
46 * @param minLat The western latitude.
47 * @param minLong The southern longitude.
48 * @param maxLat The eastern lat.
49 * @param maxLong The northern long.
50 */
51 public Area(int minLat, int minLong, int maxLat, int maxLong) {
52 this.minLat = minLat;
53 if (maxLat == minLat)
54 this.maxLat = minLat+1;
55 else
56 this.maxLat = maxLat;
57
58 this.minLong = minLong;
59 if (minLong == maxLong)
60 this.maxLong = maxLong+1;
61 else
62 this.maxLong = maxLong;
63 }
64
65 public Area(double minLat, double minLong, double maxLat,
66 double maxLong)
67 {
68 this(Utils.toMapUnit(minLat), Utils.toMapUnit(minLong)
69 , Utils.toMapUnit(maxLat), Utils.toMapUnit(maxLong));
70 }
71
72 public static Area getBBox(List<Coord> points) {
73 int tmpMinLat = Integer.MAX_VALUE;
74 int tmpMaxLat = Integer.MIN_VALUE;
75 int tmpMinLong = Integer.MAX_VALUE;
76 int tmpMaxLong = Integer.MIN_VALUE;
77
78 for (Coord co : points) {
79 int lat = co.getLatitude();
80 if (lat < tmpMinLat)
81 tmpMinLat = lat;
82 if (lat > tmpMaxLat)
83 tmpMaxLat = lat;
84
85 int lon = co.getLongitude();
86 if (lon < tmpMinLong)
87 tmpMinLong = lon;
88 if (lon > tmpMaxLong)
89 tmpMaxLong = lon;
90 }
91 return new Area(tmpMinLat, tmpMinLong, tmpMaxLat, tmpMaxLong);
92 }
93
94 public int getMinLat() {
95 return minLat;
96 }
97
98 public int getMinLong() {
99 return minLong;
100 }
101
102 public int getMaxLat() {
103 return maxLat;
104 }
105
106 public int getMaxLong() {
107 return maxLong;
108 }
109
110 public int getWidth() {
111 return maxLong - minLong;
112 }
113
114 public int getHeight() {
115 return maxLat - minLat;
116 }
117
118 public Coord getCenter() {
119 return new Coord((minLat + maxLat)/2, (minLong + maxLong)/2);// high prec not needed
120 }
121
122 public String debugString() {
123 return String.format(Locale.ROOT, "(%d, %d) to (%d, %d) (%.6f, %.6f) to (%.6f, %.6f)",
124 minLat, minLong,
125 maxLat, maxLong,
126 Utils.toDegrees(minLat), Utils.toDegrees(minLong),
127 Utils.toDegrees(maxLat), Utils.toDegrees(maxLong));
128 }
129
130 public String toString() {
131 return "("
132 + Utils.toDegrees(minLat) + ','
133 + Utils.toDegrees(minLong) + ") to ("
134 + Utils.toDegrees(maxLat) + ','
135 + Utils.toDegrees(maxLong) + ')'
136 ;
137 }
138
139 public String toHexString() {
140 return "(0x"
141 + Integer.toHexString(minLat) + ",0x"
142 + Integer.toHexString(minLong) + ") to (0x"
143 + Integer.toHexString(maxLat) + ",0x"
144 + Integer.toHexString(maxLong) + ')';
145 }
146
147 /**
148 * Round integer to nearest power of 2.
149 *
150 * @param val The number of be rounded.
151 * @param shift The power of 2.
152 * @return The rounded number (binary half rounds up).
153 */
154 private static int roundPof2(int val, int shift) {
155 if (shift <= 0)
156 return val;
157 return (((val >> (shift-1)) + 1) >> 1) << shift;
158 }
159
160 /**
161 * Split this area up into a number of smaller areas.
162 *
163 * @param xsplit The number of pieces to split this area into in the x
164 * direction.
165 * @param ysplit The number of pieces to split this area into in the y
166 * direction.
167 * @param resolutionShift round to this power of 2.
168 * @return An array containing xsplit*ysplit areas or null if can't split in half.
169 * @throws MapFailedException if more complex split operation couldn't be honoured.
170 */
171 public Area[] split(int xsplit, int ysplit, int resolutionShift) {
172 Area[] areas = new Area[xsplit * ysplit];
173
174 int xstart;
175 int xend;
176 int ystart;
177 int yend;
178 int nAreas = 0;
179
180 xstart = minLong;
181 for (int x = 0; x < xsplit; x++) {
182 if (x == xsplit - 1)
183 xend = maxLong;
184 else
185 xend = roundPof2(xstart + (maxLong - xstart) / (xsplit - x), resolutionShift);
186 ystart = minLat;
187 for (int y = 0; y < ysplit; y++) {
188 if (y == ysplit - 1)
189 yend = maxLat;
190 else
191 yend = roundPof2(ystart + (maxLat - ystart) / (ysplit - y), resolutionShift);
192 if (xstart < xend && ystart < yend) {
193 Area a = new Area(ystart, xstart, yend, xend);
194 log.debug("Area.split", minLat, minLong, maxLat, maxLong, "res", resolutionShift, "to", ystart,
195 xstart, yend, xend);
196 areas[nAreas++] = a;
197 } else {
198 log.warn("Area.split", minLat, minLong, maxLat, maxLong, "res", resolutionShift, "can't", xsplit,
199 ysplit);
200 }
201 ystart = yend;
202 }
203 xstart = xend;
204 }
205
206 if (nAreas == areas.length) // no problem
207 return areas;
208 // beware - MapSplitter.splitMaxSize requests split of 1/1 if the original area wasn't too big
209 else if (nAreas == 1) // failed to split in half
210 return null;
211 else if (areas.length == 1 && areas[0] == null)
212 return null;
213 else
214 throw new MapFailedException("Area split shift align problems");
215 }
216
217 /**
218 * Get the largest dimension. So either the width or height, depending
219 * on which is larger.
220 *
221 * @return The largest dimension in map units.
222 */
223 public int getMaxDimension() {
224 return Math.max(getWidth(), getHeight());
225 }
226
227 /**
228 *
229 * @param co a coord
230 * @return true if co is inside the Area (it may touch the boundary)
231 */
232 public final boolean contains(Coord co) {
233 int latHp = co.getHighPrecLat();
234 int lonHp = co.getHighPrecLon();
235 return latHp >= (minLat << Coord.DELTA_SHIFT)
236 && latHp <= (maxLat << Coord.DELTA_SHIFT)
237 && lonHp >= (minLong << Coord.DELTA_SHIFT)
238 && lonHp <= (maxLong << Coord.DELTA_SHIFT);
239 }
240
241 /**
242 *
243 * @param other an area
244 * @return true if the other area is inside the Area (it may touch the boundary)
245 */
246 public final boolean contains(Area other) {
247 return other.getMinLat() >= minLat
248 && other.getMaxLat() <= maxLat
249 && other.getMinLong() >= minLong
250 && other.getMaxLong() <= maxLong;
251 }
252
253 /**
254 * @param co a coord
255 * @return true if co is inside the Area and doesn't touch the boundary
256 */
257 public final boolean insideBoundary(Coord co) {
258 int latHp = co.getHighPrecLat();
259 int lonHp = co.getHighPrecLon();
260
261 return latHp > (minLat << Coord.DELTA_SHIFT)
262 && latHp < (maxLat << Coord.DELTA_SHIFT)
263 && lonHp > (minLong << Coord.DELTA_SHIFT)
264 && lonHp < (maxLong << Coord.DELTA_SHIFT);
265 }
266
267
268
269 /**
270 *
271 * @param other an area
272 * @return true if the other area is inside the Area and doesn't touch the boundary
273 */
274 public final boolean insideBoundary(Area other) {
275 return other.getMinLat() > minLat
276 && other.getMaxLat() < maxLat
277 && other.getMinLong() > minLong
278 && other.getMaxLong() < maxLong;
279 }
280
281
282 /**
283 * @param co
284 * @return true if co is on the boundary
285 */
286 public final boolean onBoundary(Coord co) {
287 return contains(co) && !insideBoundary(co);
288 }
289
290 /**
291 * Checks if this area intersects the given bounding box at least
292 * in one point.
293 *
294 * @param bbox an area
295 * @return <code>true</code> if this area intersects the bbox;
296 * <code>false</code> else
297 */
298 public final boolean intersects(Area bbox) {
299 return minLat <= bbox.getMaxLat() && maxLat >= bbox.getMinLat() &&
300 minLong <= bbox.getMaxLong() && maxLong >= bbox.getMinLong();
301 }
302
303 public boolean isEmpty() {
304 return minLat >= maxLat || minLong >= maxLong;
305 }
306
307 /**
308 *
309 * @param coords a list of coord instances
310 * @return false if any of the coords lies on or outside of this area
311 */
312 public boolean allInsideBoundary(List<Coord> coords) {
313 for (Coord co : coords) {
314 if (!insideBoundary(co))
315 return false;
316 }
317 return true;
318 }
319
320 public boolean equals(Object o) {
321 if (this == o)
322 return true;
323 if (o == null || getClass() != o.getClass())
324 return false;
325
326 Area area = (Area) o;
327 return maxLat == area.maxLat && maxLong == area.maxLong && minLat == area.minLat && minLong == area.minLong;
328 }
329
330 public int hashCode() {
331 int result = minLat;
332 result = 31 * result + minLong;
333 result = 31 * result + maxLat;
334 result = 31 * result + maxLong;
335 return result;
336 }
337
338 /**
339 * @return list of coords that form the rectangle
340 */
341 public List<Coord> toCoords(){
342 List<Coord> coords = new ArrayList<>(5);
343 coords.add(new Coord(minLat, minLong));
344 coords.add(new Coord(minLat, maxLong));
345 coords.add(new Coord(maxLat, maxLong));
346 coords.add(new Coord(maxLat, minLong));
347 coords.add(coords.get(0));
348 return coords;
349 }
350
351 public Area intersect(Area other) {
352 return new Area(Math.max(minLat, other.minLat), Math.max(minLong, other.minLong),
353 Math.min(maxLat, other.maxLat), Math.min(maxLong, other.maxLong));
354 }
355 }