|
11 | 11 | import java.io.Serializable; |
12 | 12 |
|
13 | 13 | public class BoundingBox implements Serializable { |
14 | | - private static final long serialVersionUID = -7145192134410261076L; |
15 | | - private double minLat; |
16 | | - private double maxLat; |
17 | | - private double minLon; |
18 | | - private double maxLon; |
19 | | - |
20 | | - /** |
21 | | - * create a bounding box defined by two coordinates |
22 | | - */ |
23 | | - public BoundingBox(WGS84Point p1, WGS84Point p2) { |
24 | | - this(p1.getLatitude(), p2.getLatitude(), p1.getLongitude(), p2.getLongitude()); |
25 | | - } |
26 | | - |
27 | | - public BoundingBox(double y1, double y2, double x1, double x2) { |
28 | | - minLon = Math.min(x1, x2); |
29 | | - maxLon = Math.max(x1, x2); |
30 | | - minLat = Math.min(y1, y2); |
31 | | - maxLat = Math.max(y1, y2); |
32 | | - } |
33 | | - |
34 | | - public BoundingBox(BoundingBox that) { |
35 | | - this(that.minLat, that.maxLat, that.minLon, that.maxLon); |
36 | | - } |
37 | | - |
38 | | - public WGS84Point getUpperLeft() { |
39 | | - return new WGS84Point(maxLat, minLon); |
40 | | - } |
41 | | - |
42 | | - public WGS84Point getLowerRight() { |
43 | | - return new WGS84Point(minLat, maxLon); |
44 | | - } |
45 | | - |
46 | | - public double getLatitudeSize() { |
47 | | - return maxLat - minLat; |
48 | | - } |
49 | | - |
50 | | - public double getLongitudeSize() { |
51 | | - return maxLon - minLon; |
52 | | - } |
53 | | - |
54 | | - @Override |
55 | | - public boolean equals(Object obj) { |
56 | | - if (this == obj) |
57 | | - return true; |
58 | | - if (obj instanceof BoundingBox) { |
59 | | - BoundingBox that = (BoundingBox) obj; |
60 | | - return minLat == that.minLat && minLon == that.minLon && maxLat == that.maxLat && maxLon == that.maxLon; |
61 | | - } else { |
62 | | - return false; |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - @Override |
67 | | - public int hashCode() { |
68 | | - int result = 17; |
69 | | - result = 37 * result + hashCode(minLat); |
70 | | - result = 37 * result + hashCode(maxLat); |
71 | | - result = 37 * result + hashCode(minLon); |
72 | | - result = 37 * result + hashCode(maxLon); |
73 | | - return result; |
74 | | - } |
75 | | - |
76 | | - private static int hashCode(double x) { |
77 | | - long f = Double.doubleToLongBits(x); |
78 | | - return (int) (f ^ (f >>> 32)); |
79 | | - } |
80 | | - |
81 | | - public boolean contains(WGS84Point point) { |
82 | | - return (point.getLatitude() >= minLat) && (point.getLongitude() >= minLon) && (point.getLatitude() <= maxLat) |
83 | | - && (point.getLongitude() <= maxLon); |
84 | | - } |
85 | | - |
86 | | - public boolean intersects(BoundingBox other) { |
87 | | - return !(other.minLon > maxLon || other.maxLon < minLon || other.minLat > maxLat || other.maxLat < minLat); |
88 | | - } |
89 | | - |
90 | | - @Override |
91 | | - public String toString() { |
92 | | - return getUpperLeft() + " -> " + getLowerRight(); |
93 | | - } |
94 | | - |
95 | | - public WGS84Point getCenterPoint() { |
96 | | - double centerLatitude = (minLat + maxLat) / 2; |
97 | | - double centerLongitude = (minLon + maxLon) / 2; |
98 | | - return new WGS84Point(centerLatitude, centerLongitude); |
99 | | - } |
100 | | - |
101 | | - public void expandToInclude(BoundingBox other) { |
102 | | - if (other.minLon < minLon) { |
103 | | - minLon = other.minLon; |
104 | | - } |
105 | | - if (other.maxLon > maxLon) { |
106 | | - maxLon = other.maxLon; |
107 | | - } |
108 | | - if (other.minLat < minLat) { |
109 | | - minLat = other.minLat; |
110 | | - } |
111 | | - if (other.maxLat > maxLat) { |
112 | | - maxLat = other.maxLat; |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - public double getMinLon() { |
117 | | - return minLon; |
118 | | - } |
119 | | - |
120 | | - public double getMinLat() { |
121 | | - return minLat; |
122 | | - } |
123 | | - |
124 | | - public double getMaxLat() { |
125 | | - return maxLat; |
126 | | - } |
127 | | - |
128 | | - public double getMaxLon() { |
129 | | - return maxLon; |
130 | | - } |
| 14 | + private static final long serialVersionUID = -7145192134410261076L; |
| 15 | + private double minLat; |
| 16 | + private double maxLat; |
| 17 | + private double minLon; |
| 18 | + private double maxLon; |
| 19 | + |
| 20 | + /** |
| 21 | + * create a bounding box defined by two coordinates |
| 22 | + */ |
| 23 | + public BoundingBox(WGS84Point p1, WGS84Point p2) { |
| 24 | + this(p1.getLatitude(), p2.getLatitude(), p1.getLongitude(), p2.getLongitude()); |
| 25 | + } |
| 26 | + |
| 27 | + public BoundingBox(double y1, double y2, double x1, double x2) { |
| 28 | + minLon = Math.min(x1, x2); |
| 29 | + maxLon = Math.max(x1, x2); |
| 30 | + minLat = Math.min(y1, y2); |
| 31 | + maxLat = Math.max(y1, y2); |
| 32 | + } |
| 33 | + |
| 34 | + public BoundingBox(BoundingBox that) { |
| 35 | + this(that.minLat, that.maxLat, that.minLon, that.maxLon); |
| 36 | + } |
| 37 | + |
| 38 | + public WGS84Point getUpperLeft() { |
| 39 | + return new WGS84Point(maxLat, minLon); |
| 40 | + } |
| 41 | + |
| 42 | + public WGS84Point getLowerRight() { |
| 43 | + return new WGS84Point(minLat, maxLon); |
| 44 | + } |
| 45 | + |
| 46 | + public double getLatitudeSize() { |
| 47 | + return maxLat - minLat; |
| 48 | + } |
| 49 | + |
| 50 | + public double getLongitudeSize() { |
| 51 | + return maxLon - minLon; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean equals(Object obj) { |
| 56 | + if (this == obj) |
| 57 | + return true; |
| 58 | + if (obj instanceof BoundingBox) { |
| 59 | + BoundingBox that = (BoundingBox) obj; |
| 60 | + return minLat == that.minLat && minLon == that.minLon && maxLat == that.maxLat && maxLon == that.maxLon; |
| 61 | + } else { |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public int hashCode() { |
| 68 | + int result = 17; |
| 69 | + result = 37 * result + hashCode(minLat); |
| 70 | + result = 37 * result + hashCode(maxLat); |
| 71 | + result = 37 * result + hashCode(minLon); |
| 72 | + result = 37 * result + hashCode(maxLon); |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + private static int hashCode(double x) { |
| 77 | + long f = Double.doubleToLongBits(x); |
| 78 | + return (int) (f ^ (f >>> 32)); |
| 79 | + } |
| 80 | + |
| 81 | + public boolean contains(WGS84Point point) { |
| 82 | + return (point.getLatitude() >= minLat) && (point.getLongitude() >= minLon) && (point.getLatitude() <= maxLat) |
| 83 | + && (point.getLongitude() <= maxLon); |
| 84 | + } |
| 85 | + |
| 86 | + public boolean intersects(BoundingBox other) { |
| 87 | + return !(other.minLon > maxLon || other.maxLon < minLon || other.minLat > maxLat || other.maxLat < minLat); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String toString() { |
| 92 | + return getUpperLeft() + " -> " + getLowerRight(); |
| 93 | + } |
| 94 | + |
| 95 | + public WGS84Point getCenterPoint() { |
| 96 | + double centerLatitude = (minLat + maxLat) / 2; |
| 97 | + double centerLongitude = (minLon + maxLon) / 2; |
| 98 | + return new WGS84Point(centerLatitude, centerLongitude); |
| 99 | + } |
| 100 | + |
| 101 | + public void expandToInclude(BoundingBox other) { |
| 102 | + if (other.minLon < minLon) { |
| 103 | + minLon = other.minLon; |
| 104 | + } |
| 105 | + if (other.maxLon > maxLon) { |
| 106 | + maxLon = other.maxLon; |
| 107 | + } |
| 108 | + if (other.minLat < minLat) { |
| 109 | + minLat = other.minLat; |
| 110 | + } |
| 111 | + if (other.maxLat > maxLat) { |
| 112 | + maxLat = other.maxLat; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public double getMinLon() { |
| 117 | + return minLon; |
| 118 | + } |
| 119 | + |
| 120 | + public double getMinLat() { |
| 121 | + return minLat; |
| 122 | + } |
| 123 | + |
| 124 | + public double getMaxLat() { |
| 125 | + return maxLat; |
| 126 | + } |
| 127 | + |
| 128 | + public double getMaxLon() { |
| 129 | + return maxLon; |
| 130 | + } |
131 | 131 | } |
0 commit comments