| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 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); |
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 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 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 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) |
| 207 |
return areas; |
| 208 |
|
| 209 |
else if (nAreas == 1) |
| 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 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public int getMaxDimension() { |
| 224 |
return Math.max(getWidth(), getHeight()); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 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 |
|
| 244 |
|
| 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 |
|
| 255 |
|
| 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 |
|
| 272 |
|
| 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 |
|
| 284 |
|
| 285 |
|
| 286 |
public final boolean onBoundary(Coord co) { |
| 287 |
return contains(co) && !insideBoundary(co); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 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 |
|
| 310 |
|
| 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 |
|
| 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 |
} |