1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.geom.Rectangle2D;
|
---|
7 | import java.text.DecimalFormat;
|
---|
8 | import java.text.MessageFormat;
|
---|
9 | import java.util.Objects;
|
---|
10 | import java.util.function.Consumer;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
15 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * This is a simple data class for "rectangular" areas of the world, given in
|
---|
20 | * lat/lon min/max values. The values are rounded to LatLon.OSM_SERVER_PRECISION
|
---|
21 | *
|
---|
22 | * @author imi
|
---|
23 | *
|
---|
24 | * @see BBox to represent invalid areas.
|
---|
25 | */
|
---|
26 | public class Bounds {
|
---|
27 | /**
|
---|
28 | * The minimum and maximum coordinates.
|
---|
29 | */
|
---|
30 | private double minLat, minLon, maxLat, maxLon;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Gets the point that has both the minimal lat and lon coordinate
|
---|
34 | * @return The point
|
---|
35 | */
|
---|
36 | public LatLon getMin() {
|
---|
37 | return new LatLon(minLat, minLon);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Returns min latitude of bounds. Efficient shortcut for {@code getMin().lat()}.
|
---|
42 | *
|
---|
43 | * @return min latitude of bounds.
|
---|
44 | * @since 6203
|
---|
45 | */
|
---|
46 | public double getMinLat() {
|
---|
47 | return minLat;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Returns min longitude of bounds. Efficient shortcut for {@code getMin().lon()}.
|
---|
52 | *
|
---|
53 | * @return min longitude of bounds.
|
---|
54 | * @since 6203
|
---|
55 | */
|
---|
56 | public double getMinLon() {
|
---|
57 | return minLon;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Gets the point that has both the maximum lat and lon coordinate
|
---|
62 | * @return The point
|
---|
63 | */
|
---|
64 | public LatLon getMax() {
|
---|
65 | return new LatLon(maxLat, maxLon);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Returns max latitude of bounds. Efficient shortcut for {@code getMax().lat()}.
|
---|
70 | *
|
---|
71 | * @return max latitude of bounds.
|
---|
72 | * @since 6203
|
---|
73 | */
|
---|
74 | public double getMaxLat() {
|
---|
75 | return maxLat;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Returns max longitude of bounds. Efficient shortcut for {@code getMax().lon()}.
|
---|
80 | *
|
---|
81 | * @return max longitude of bounds.
|
---|
82 | * @since 6203
|
---|
83 | */
|
---|
84 | public double getMaxLon() {
|
---|
85 | return maxLon;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * The method used by the {@link Bounds#Bounds(String, String, ParseMethod)} constructor
|
---|
90 | */
|
---|
91 | public enum ParseMethod {
|
---|
92 | /**
|
---|
93 | * Order: minlat, minlon, maxlat, maxlon
|
---|
94 | */
|
---|
95 | MINLAT_MINLON_MAXLAT_MAXLON,
|
---|
96 | /**
|
---|
97 | * Order: left, bottom, right, top
|
---|
98 | */
|
---|
99 | LEFT_BOTTOM_RIGHT_TOP
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Construct bounds out of two points. Coords will be rounded.
|
---|
104 | * @param min min lat/lon
|
---|
105 | * @param max max lat/lon
|
---|
106 | */
|
---|
107 | public Bounds(LatLon min, LatLon max) {
|
---|
108 | this(min.lat(), min.lon(), max.lat(), max.lon());
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Constructs bounds out of two points.
|
---|
113 | * @param min min lat/lon
|
---|
114 | * @param max max lat/lon
|
---|
115 | * @param roundToOsmPrecision defines if lat/lon will be rounded
|
---|
116 | */
|
---|
117 | public Bounds(LatLon min, LatLon max, boolean roundToOsmPrecision) {
|
---|
118 | this(min.lat(), min.lon(), max.lat(), max.lon(), roundToOsmPrecision);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Constructs bounds out a single point. Coords will be rounded.
|
---|
123 | * @param b lat/lon
|
---|
124 | */
|
---|
125 | public Bounds(LatLon b) {
|
---|
126 | this(b, true);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Single point Bounds defined by lat/lon {@code b}.
|
---|
131 | * Coordinates will be rounded to osm precision if {@code roundToOsmPrecision} is true.
|
---|
132 | *
|
---|
133 | * @param b lat/lon of given point.
|
---|
134 | * @param roundToOsmPrecision defines if lat/lon will be rounded.
|
---|
135 | */
|
---|
136 | public Bounds(LatLon b, boolean roundToOsmPrecision) {
|
---|
137 | this(b.lat(), b.lon(), roundToOsmPrecision);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Single point Bounds defined by point [lat,lon].
|
---|
142 | * Coordinates will be rounded to osm precision if {@code roundToOsmPrecision} is true.
|
---|
143 | *
|
---|
144 | * @param lat latitude of given point.
|
---|
145 | * @param lon longitude of given point.
|
---|
146 | * @param roundToOsmPrecision defines if lat/lon will be rounded.
|
---|
147 | * @since 6203
|
---|
148 | */
|
---|
149 | public Bounds(double lat, double lon, boolean roundToOsmPrecision) {
|
---|
150 | // Do not call this(b, b) to avoid GPX performance issue (see #7028) until roundToOsmPrecision() is improved
|
---|
151 | if (roundToOsmPrecision) {
|
---|
152 | this.minLat = LatLon.roundToOsmPrecision(lat);
|
---|
153 | this.minLon = LatLon.roundToOsmPrecision(lon);
|
---|
154 | } else {
|
---|
155 | this.minLat = lat;
|
---|
156 | this.minLon = lon;
|
---|
157 | }
|
---|
158 | this.maxLat = this.minLat;
|
---|
159 | this.maxLon = this.minLon;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Constructs bounds out of two points. Coords will be rounded.
|
---|
164 | * @param minlat min lat
|
---|
165 | * @param minlon min lon
|
---|
166 | * @param maxlat max lat
|
---|
167 | * @param maxlon max lon
|
---|
168 | */
|
---|
169 | public Bounds(double minlat, double minlon, double maxlat, double maxlon) {
|
---|
170 | this(minlat, minlon, maxlat, maxlon, true);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Constructs bounds out of two points.
|
---|
175 | * @param minlat min lat
|
---|
176 | * @param minlon min lon
|
---|
177 | * @param maxlat max lat
|
---|
178 | * @param maxlon max lon
|
---|
179 | * @param roundToOsmPrecision defines if lat/lon will be rounded
|
---|
180 | */
|
---|
181 | public Bounds(double minlat, double minlon, double maxlat, double maxlon, boolean roundToOsmPrecision) {
|
---|
182 | if (roundToOsmPrecision) {
|
---|
183 | this.minLat = LatLon.roundToOsmPrecision(minlat);
|
---|
184 | this.minLon = LatLon.roundToOsmPrecision(minlon);
|
---|
185 | this.maxLat = LatLon.roundToOsmPrecision(maxlat);
|
---|
186 | this.maxLon = LatLon.roundToOsmPrecision(maxlon);
|
---|
187 | } else {
|
---|
188 | this.minLat = minlat;
|
---|
189 | this.minLon = minlon;
|
---|
190 | this.maxLat = maxlat;
|
---|
191 | this.maxLon = maxlon;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Constructs bounds out of two points. Coords will be rounded.
|
---|
197 | * @param coords exactly 4 values: min lat, min lon, max lat, max lon
|
---|
198 | * @throws IllegalArgumentException if coords does not contain 4 double values
|
---|
199 | */
|
---|
200 | public Bounds(double... coords) {
|
---|
201 | this(coords, true);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Constructs bounds out of two points.
|
---|
206 | * @param coords exactly 4 values: min lat, min lon, max lat, max lon
|
---|
207 | * @param roundToOsmPrecision defines if lat/lon will be rounded
|
---|
208 | * @throws IllegalArgumentException if coords does not contain 4 double values
|
---|
209 | */
|
---|
210 | public Bounds(double[] coords, boolean roundToOsmPrecision) {
|
---|
211 | CheckParameterUtil.ensureParameterNotNull(coords, "coords");
|
---|
212 | if (coords.length != 4)
|
---|
213 | throw new IllegalArgumentException(MessageFormat.format("Expected array of length 4, got {0}", coords.length));
|
---|
214 | if (roundToOsmPrecision) {
|
---|
215 | this.minLat = LatLon.roundToOsmPrecision(coords[0]);
|
---|
216 | this.minLon = LatLon.roundToOsmPrecision(coords[1]);
|
---|
217 | this.maxLat = LatLon.roundToOsmPrecision(coords[2]);
|
---|
218 | this.maxLon = LatLon.roundToOsmPrecision(coords[3]);
|
---|
219 | } else {
|
---|
220 | this.minLat = coords[0];
|
---|
221 | this.minLon = coords[1];
|
---|
222 | this.maxLat = coords[2];
|
---|
223 | this.maxLon = coords[3];
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Parse the bounds in order {@link ParseMethod#MINLAT_MINLON_MAXLAT_MAXLON}
|
---|
229 | * @param asString The string
|
---|
230 | * @param separator The separation regex
|
---|
231 | */
|
---|
232 | public Bounds(String asString, String separator) {
|
---|
233 | this(asString, separator, ParseMethod.MINLAT_MINLON_MAXLAT_MAXLON);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Parse the bounds from a given string and round to OSM precision
|
---|
238 | * @param asString The string
|
---|
239 | * @param separator The separation regex
|
---|
240 | * @param parseMethod The order of the numbers
|
---|
241 | */
|
---|
242 | public Bounds(String asString, String separator, ParseMethod parseMethod) {
|
---|
243 | this(asString, separator, parseMethod, true);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Parse the bounds from a given string
|
---|
248 | * @param asString The string
|
---|
249 | * @param separator The separation regex
|
---|
250 | * @param parseMethod The order of the numbers
|
---|
251 | * @param roundToOsmPrecision Whether to round to OSM precision
|
---|
252 | */
|
---|
253 | public Bounds(String asString, String separator, ParseMethod parseMethod, boolean roundToOsmPrecision) {
|
---|
254 | CheckParameterUtil.ensureParameterNotNull(asString, "asString");
|
---|
255 | String[] components = asString.split(separator);
|
---|
256 | if (components.length != 4)
|
---|
257 | throw new IllegalArgumentException(
|
---|
258 | MessageFormat.format("Exactly four doubles expected in string, got {0}: {1}", components.length, asString));
|
---|
259 | double[] values = new double[4];
|
---|
260 | for (int i = 0; i < 4; i++) {
|
---|
261 | try {
|
---|
262 | values[i] = Double.parseDouble(components[i]);
|
---|
263 | } catch (NumberFormatException e) {
|
---|
264 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", components[i]), e);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | switch (parseMethod) {
|
---|
269 | case LEFT_BOTTOM_RIGHT_TOP:
|
---|
270 | this.minLat = initLat(values[1], roundToOsmPrecision);
|
---|
271 | this.minLon = initLon(values[0], roundToOsmPrecision);
|
---|
272 | this.maxLat = initLat(values[3], roundToOsmPrecision);
|
---|
273 | this.maxLon = initLon(values[2], roundToOsmPrecision);
|
---|
274 | break;
|
---|
275 | case MINLAT_MINLON_MAXLAT_MAXLON:
|
---|
276 | default:
|
---|
277 | this.minLat = initLat(values[0], roundToOsmPrecision);
|
---|
278 | this.minLon = initLon(values[1], roundToOsmPrecision);
|
---|
279 | this.maxLat = initLat(values[2], roundToOsmPrecision);
|
---|
280 | this.maxLon = initLon(values[3], roundToOsmPrecision);
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | protected static double initLat(double value, boolean roundToOsmPrecision) {
|
---|
285 | if (!LatLon.isValidLat(value))
|
---|
286 | throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", value));
|
---|
287 | return roundToOsmPrecision ? LatLon.roundToOsmPrecision(value) : value;
|
---|
288 | }
|
---|
289 |
|
---|
290 | protected static double initLon(double value, boolean roundToOsmPrecision) {
|
---|
291 | if (!LatLon.isValidLon(value))
|
---|
292 | throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", value));
|
---|
293 | return roundToOsmPrecision ? LatLon.roundToOsmPrecision(value) : value;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Creates new {@code Bounds} from an existing one.
|
---|
298 | * @param other The bounds to copy
|
---|
299 | */
|
---|
300 | public Bounds(final Bounds other) {
|
---|
301 | this(other.minLat, other.minLon, other.maxLat, other.maxLon);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Creates new {@code Bounds} from a rectangle.
|
---|
306 | * @param rect The rectangle
|
---|
307 | */
|
---|
308 | public Bounds(Rectangle2D rect) {
|
---|
309 | this(rect.getMinY(), rect.getMinX(), rect.getMaxY(), rect.getMaxX());
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Creates new bounds around a coordinate pair <code>center</code>. The
|
---|
314 | * new bounds shall have an extension in latitude direction of <code>latExtent</code>,
|
---|
315 | * and in longitude direction of <code>lonExtent</code>.
|
---|
316 | *
|
---|
317 | * @param center the center coordinate pair. Must not be null.
|
---|
318 | * @param latExtent the latitude extent. > 0 required.
|
---|
319 | * @param lonExtent the longitude extent. > 0 required.
|
---|
320 | * @throws IllegalArgumentException if center is null
|
---|
321 | * @throws IllegalArgumentException if latExtent <= 0
|
---|
322 | * @throws IllegalArgumentException if lonExtent <= 0
|
---|
323 | */
|
---|
324 | public Bounds(LatLon center, double latExtent, double lonExtent) {
|
---|
325 | CheckParameterUtil.ensureParameterNotNull(center, "center");
|
---|
326 | if (latExtent <= 0.0)
|
---|
327 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0.0 expected, got {1}", "latExtent", latExtent));
|
---|
328 | if (lonExtent <= 0.0)
|
---|
329 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0.0 expected, got {1}", "lonExtent", lonExtent));
|
---|
330 |
|
---|
331 | this.minLat = LatLon.roundToOsmPrecision(LatLon.toIntervalLat(center.lat() - latExtent / 2));
|
---|
332 | this.minLon = LatLon.roundToOsmPrecision(LatLon.toIntervalLon(center.lon() - lonExtent / 2));
|
---|
333 | this.maxLat = LatLon.roundToOsmPrecision(LatLon.toIntervalLat(center.lat() + latExtent / 2));
|
---|
334 | this.maxLon = LatLon.roundToOsmPrecision(LatLon.toIntervalLon(center.lon() + lonExtent / 2));
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Creates BBox with same coordinates.
|
---|
339 | *
|
---|
340 | * @return BBox with same coordinates.
|
---|
341 | * @since 6203
|
---|
342 | */
|
---|
343 | public BBox toBBox() {
|
---|
344 | return new BBox(minLon, minLat, maxLon, maxLat);
|
---|
345 | }
|
---|
346 |
|
---|
347 | @Override
|
---|
348 | public String toString() {
|
---|
349 | return "Bounds["+minLat+','+minLon+','+maxLat+','+maxLon+']';
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Converts this bounds to a human readable short string
|
---|
354 | * @param format The number format to use
|
---|
355 | * @return The string
|
---|
356 | */
|
---|
357 | public String toShortString(DecimalFormat format) {
|
---|
358 | return format.format(minLat) + ' '
|
---|
359 | + format.format(minLon) + " / "
|
---|
360 | + format.format(maxLat) + ' '
|
---|
361 | + format.format(maxLon);
|
---|
362 | }
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * @return Center of the bounding box.
|
---|
366 | */
|
---|
367 | public LatLon getCenter() {
|
---|
368 | if (crosses180thMeridian()) {
|
---|
369 | double lat = (minLat + maxLat) / 2;
|
---|
370 | double lon = (minLon + maxLon - 360.0) / 2;
|
---|
371 | if (lon < -180.0) {
|
---|
372 | lon += 360.0;
|
---|
373 | }
|
---|
374 | return new LatLon(lat, lon);
|
---|
375 | } else {
|
---|
376 | return new LatLon((minLat + maxLat) / 2, (minLon + maxLon) / 2);
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Extend the bounds if necessary to include the given point.
|
---|
382 | * @param ll The point to include into these bounds
|
---|
383 | */
|
---|
384 | public void extend(LatLon ll) {
|
---|
385 | extend(ll.lat(), ll.lon());
|
---|
386 | }
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Extend the bounds if necessary to include the given point [lat,lon].
|
---|
390 | * Good to use if you know coordinates to avoid creation of LatLon object.
|
---|
391 | * @param lat Latitude of point to include into these bounds
|
---|
392 | * @param lon Longitude of point to include into these bounds
|
---|
393 | * @since 6203
|
---|
394 | */
|
---|
395 | public void extend(final double lat, final double lon) {
|
---|
396 | if (lat < minLat) {
|
---|
397 | minLat = LatLon.roundToOsmPrecision(lat);
|
---|
398 | }
|
---|
399 | if (lat > maxLat) {
|
---|
400 | maxLat = LatLon.roundToOsmPrecision(lat);
|
---|
401 | }
|
---|
402 | if (crosses180thMeridian()) {
|
---|
403 | if (lon > maxLon && lon < minLon) {
|
---|
404 | if (Math.abs(lon - minLon) <= Math.abs(lon - maxLon)) {
|
---|
405 | minLon = LatLon.roundToOsmPrecision(lon);
|
---|
406 | } else {
|
---|
407 | maxLon = LatLon.roundToOsmPrecision(lon);
|
---|
408 | }
|
---|
409 | }
|
---|
410 | } else {
|
---|
411 | if (lon < minLon) {
|
---|
412 | minLon = LatLon.roundToOsmPrecision(lon);
|
---|
413 | }
|
---|
414 | if (lon > maxLon) {
|
---|
415 | maxLon = LatLon.roundToOsmPrecision(lon);
|
---|
416 | }
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Extends this bounds to enclose an other bounding box
|
---|
422 | * @param b The other bounds to enclose
|
---|
423 | */
|
---|
424 | public void extend(Bounds b) {
|
---|
425 | extend(b.minLat, b.minLon);
|
---|
426 | extend(b.maxLat, b.maxLon);
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Determines if the given point {@code ll} is within these bounds.
|
---|
431 | * <p>
|
---|
432 | * Points with unknown coordinates are always outside the coordinates.
|
---|
433 | * @param ll The lat/lon to check
|
---|
434 | * @return {@code true} if {@code ll} is within these bounds, {@code false} otherwise
|
---|
435 | */
|
---|
436 | public boolean contains(LatLon ll) {
|
---|
437 | // binary compatibility
|
---|
438 | return contains((ILatLon) ll);
|
---|
439 | }
|
---|
440 |
|
---|
441 | /**
|
---|
442 | * Determines if the given point {@code ll} is within these bounds.
|
---|
443 | * <p>
|
---|
444 | * Points with unknown coordinates are always outside the coordinates.
|
---|
445 | * @param ll The lat/lon to check
|
---|
446 | * @return {@code true} if {@code ll} is within these bounds, {@code false} otherwise
|
---|
447 | * @since 12161
|
---|
448 | */
|
---|
449 | public boolean contains(ILatLon ll) {
|
---|
450 | if (!ll.isLatLonKnown()) {
|
---|
451 | return false;
|
---|
452 | }
|
---|
453 | if (ll.lat() < minLat || ll.lat() > maxLat)
|
---|
454 | return false;
|
---|
455 | if (crosses180thMeridian()) {
|
---|
456 | if (ll.lon() > maxLon && ll.lon() < minLon)
|
---|
457 | return false;
|
---|
458 | } else {
|
---|
459 | if (ll.lon() < minLon || ll.lon() > maxLon)
|
---|
460 | return false;
|
---|
461 | }
|
---|
462 | return true;
|
---|
463 | }
|
---|
464 |
|
---|
465 | private static boolean intersectsLonCrossing(Bounds crossing, Bounds notCrossing) {
|
---|
466 | return notCrossing.minLon <= crossing.maxLon || notCrossing.maxLon >= crossing.minLon;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * The two bounds intersect? Compared to java Shape.intersects, if does not use
|
---|
471 | * the interior but the closure. (">=" instead of ">")
|
---|
472 | * @param b other bounds
|
---|
473 | * @return {@code true} if the two bounds intersect
|
---|
474 | */
|
---|
475 | public boolean intersects(Bounds b) {
|
---|
476 | if (b.maxLat < minLat || b.minLat > maxLat)
|
---|
477 | return false;
|
---|
478 |
|
---|
479 | if (crosses180thMeridian() && !b.crosses180thMeridian()) {
|
---|
480 | return intersectsLonCrossing(this, b);
|
---|
481 | } else if (!crosses180thMeridian() && b.crosses180thMeridian()) {
|
---|
482 | return intersectsLonCrossing(b, this);
|
---|
483 | } else if (crosses180thMeridian() && b.crosses180thMeridian()) {
|
---|
484 | return true;
|
---|
485 | } else {
|
---|
486 | return b.maxLon >= minLon && b.minLon <= maxLon;
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * Determines if this Bounds object crosses the 180th Meridian.
|
---|
492 | * See http://wiki.openstreetmap.org/wiki/180th_meridian
|
---|
493 | * @return true if this Bounds object crosses the 180th Meridian.
|
---|
494 | */
|
---|
495 | public boolean crosses180thMeridian() {
|
---|
496 | return this.minLon > this.maxLon;
|
---|
497 | }
|
---|
498 |
|
---|
499 | /**
|
---|
500 | * Converts the lat/lon bounding box to an object of type Rectangle2D.Double
|
---|
501 | * @return the bounding box to Rectangle2D.Double
|
---|
502 | */
|
---|
503 | public Rectangle2D.Double asRect() {
|
---|
504 | double w = getWidth();
|
---|
505 | return new Rectangle2D.Double(minLon, minLat, w, maxLat-minLat);
|
---|
506 | }
|
---|
507 |
|
---|
508 | private double getWidth() {
|
---|
509 | return maxLon-minLon + (crosses180thMeridian() ? 360.0 : 0.0);
|
---|
510 | }
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Gets the area of this bounds (in lat/lon space)
|
---|
514 | * @return The area
|
---|
515 | */
|
---|
516 | public double getArea() {
|
---|
517 | double w = getWidth();
|
---|
518 | return w * (maxLat - minLat);
|
---|
519 | }
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * Encodes this as a string so that it may be parsed using the {@link ParseMethod#MINLAT_MINLON_MAXLAT_MAXLON} order
|
---|
523 | * @param separator The separator
|
---|
524 | * @return The string encoded bounds
|
---|
525 | */
|
---|
526 | public String encodeAsString(String separator) {
|
---|
527 | StringBuilder sb = new StringBuilder();
|
---|
528 | sb.append(minLat).append(separator).append(minLon)
|
---|
529 | .append(separator).append(maxLat).append(separator)
|
---|
530 | .append(maxLon);
|
---|
531 | return sb.toString();
|
---|
532 | }
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * <p>Replies true, if this bounds are <em>collapsed</em>, i.e. if the min
|
---|
536 | * and the max corner are equal.</p>
|
---|
537 | *
|
---|
538 | * @return true, if this bounds are <em>collapsed</em>
|
---|
539 | */
|
---|
540 | public boolean isCollapsed() {
|
---|
541 | return Double.doubleToLongBits(minLat) == Double.doubleToLongBits(maxLat)
|
---|
542 | && Double.doubleToLongBits(minLon) == Double.doubleToLongBits(maxLon);
|
---|
543 | }
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Determines if these bounds are out of the world.
|
---|
547 | * @return true if lat outside of range [-90,90] or lon outside of range [-180,180]
|
---|
548 | */
|
---|
549 | public boolean isOutOfTheWorld() {
|
---|
550 | return
|
---|
551 | !LatLon.isValidLat(minLat) ||
|
---|
552 | !LatLon.isValidLat(maxLat) ||
|
---|
553 | !LatLon.isValidLon(minLon) ||
|
---|
554 | !LatLon.isValidLon(maxLon);
|
---|
555 | }
|
---|
556 |
|
---|
557 | /**
|
---|
558 | * Clamp the bounds to be inside the world.
|
---|
559 | */
|
---|
560 | public void normalize() {
|
---|
561 | minLat = LatLon.toIntervalLat(minLat);
|
---|
562 | maxLat = LatLon.toIntervalLat(maxLat);
|
---|
563 | minLon = LatLon.toIntervalLon(minLon);
|
---|
564 | maxLon = LatLon.toIntervalLon(maxLon);
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Visit points along the edge of this bounds instance.
|
---|
569 | * @param projection The projection that should be used to determine how often the edge should be split along a given corner.
|
---|
570 | * @param visitor A function to call for the points on the edge.
|
---|
571 | * @since 10806
|
---|
572 | */
|
---|
573 | public void visitEdge(Projection projection, Consumer<LatLon> visitor) {
|
---|
574 | double width = getWidth();
|
---|
575 | double height = maxLat - minLat;
|
---|
576 | //TODO: Use projection to see if there is any need for doing this along each axis.
|
---|
577 | int splitX = Math.max((int) width / 10, 10);
|
---|
578 | int splitY = Math.max((int) height / 10, 10);
|
---|
579 |
|
---|
580 | for (int step = 0; step < splitX; step++) {
|
---|
581 | visitor.accept(new LatLon(minLat, minLon + width * step / splitX));
|
---|
582 | }
|
---|
583 | for (int step = 0; step < splitY; step++) {
|
---|
584 | visitor.accept(new LatLon(minLat + height * step / splitY, maxLon));
|
---|
585 | }
|
---|
586 | for (int step = 0; step < splitX; step++) {
|
---|
587 | visitor.accept(new LatLon(maxLat, maxLon - width * step / splitX));
|
---|
588 | }
|
---|
589 | for (int step = 0; step < splitY; step++) {
|
---|
590 | visitor.accept(new LatLon(maxLat - height * step / splitY, minLon));
|
---|
591 | }
|
---|
592 | }
|
---|
593 |
|
---|
594 | @Override
|
---|
595 | public int hashCode() {
|
---|
596 | return Objects.hash(minLat, minLon, maxLat, maxLon);
|
---|
597 | }
|
---|
598 |
|
---|
599 | @Override
|
---|
600 | public boolean equals(Object obj) {
|
---|
601 | if (this == obj) return true;
|
---|
602 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
603 | Bounds bounds = (Bounds) obj;
|
---|
604 | return Double.compare(bounds.minLat, minLat) == 0 &&
|
---|
605 | Double.compare(bounds.minLon, minLon) == 0 &&
|
---|
606 | Double.compare(bounds.maxLat, maxLat) == 0 &&
|
---|
607 | Double.compare(bounds.maxLon, maxLon) == 0;
|
---|
608 | }
|
---|
609 | }
|
---|