1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
package cgeo.geocaching.location;
import cgeo.geocaching.ICoordinates;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import java.util.Collection;
public final class Viewport {
public final @NonNull Geopoint center;
public final @NonNull Geopoint bottomLeft;
public final @NonNull Geopoint topRight;
public Viewport(final @NonNull ICoordinates point1, final @NonNull ICoordinates point2) {
final Geopoint gp1 = point1.getCoords();
final Geopoint gp2 = point2.getCoords();
this.bottomLeft = new Geopoint(Math.min(gp1.getLatitude(), gp2.getLatitude()),
Math.min(gp1.getLongitude(), gp2.getLongitude()));
this.topRight = new Geopoint(Math.max(gp1.getLatitude(), gp2.getLatitude()),
Math.max(gp1.getLongitude(), gp2.getLongitude()));
this.center = new Geopoint((gp1.getLatitude() + gp2.getLatitude()) / 2,
(gp1.getLongitude() + gp2.getLongitude()) / 2);
}
public Viewport(final @NonNull ICoordinates center, final double latSpan, final double lonSpan) {
this.center = center.getCoords();
final double centerLat = this.center.getLatitude();
final double centerLon = this.center.getLongitude();
final double latHalfSpan = Math.abs(latSpan) / 2;
final double lonHalfSpan = Math.abs(lonSpan) / 2;
bottomLeft = new Geopoint(centerLat - latHalfSpan, centerLon - lonHalfSpan);
topRight = new Geopoint(centerLat + latHalfSpan, centerLon + lonHalfSpan);
}
public double getLatitudeMin() {
return bottomLeft.getLatitude();
}
public double getLatitudeMax() {
return topRight.getLatitude();
}
public double getLongitudeMin() {
return bottomLeft.getLongitude();
}
public double getLongitudeMax() {
return topRight.getLongitude();
}
public @NonNull
Geopoint getCenter() {
return center;
}
public double getLatitudeSpan() {
return getLatitudeMax() - getLatitudeMin();
}
public double getLongitudeSpan() {
return getLongitudeMax() - getLongitudeMin();
}
/**
* Check whether a point is contained in this viewport.
*
* @param point
* the coordinates to check
* @return true if the point is contained in this viewport, false otherwise or if the point contains no coordinates
*/
public boolean contains(final @NonNull ICoordinates point) {
final Geopoint coords = point.getCoords();
return coords != null
&& coords.getLongitudeE6() >= bottomLeft.getLongitudeE6()
&& coords.getLongitudeE6() <= topRight.getLongitudeE6()
&& coords.getLatitudeE6() >= bottomLeft.getLatitudeE6()
&& coords.getLatitudeE6() <= topRight.getLatitudeE6();
}
/**
* Count the number of points present in the viewport.
*
* @param points a collection of (possibly null) points
* @return the number of non-null points in the viewport
*/
public int count(final @NonNull Collection<? extends ICoordinates> points) {
int total = 0;
for (final ICoordinates point: points) {
if (point != null && contains(point)) {
total += 1;
}
}
return total;
}
@Override
public String toString() {
return "(" + bottomLeft.toString() + "," + topRight.toString() + ")";
}
/**
* Check whether another viewport is fully included into the current one.
*
* @param vp
* the other viewport
* @return true if the viewport is fully included into this one, false otherwise
*/
public boolean includes(final @NonNull Viewport vp) {
return contains(vp.bottomLeft) && contains(vp.topRight);
}
/**
* Return the "where" part of the string appropriate for a SQL query.
*
* @param dbTable
* the database table to use as prefix, or null if no prefix is required
* @return the string without the "where" keyword
*/
@NonNull
public StringBuilder sqlWhere(@Nullable final String dbTable) {
final String prefix = dbTable == null ? "" : (dbTable + ".");
return new StringBuilder(prefix).append("latitude >= ").append(getLatitudeMin()).append(" and ")
.append(prefix).append("latitude <= ").append(getLatitudeMax()).append(" and ")
.append(prefix).append("longitude >= ").append(getLongitudeMin()).append(" and ")
.append(prefix).append("longitude <= ").append(getLongitudeMax());
}
/**
* Return a widened or shrunk viewport.
*
* @param factor
* multiplicative factor for the latitude and longitude span (> 1 to widen, < 1 to shrink)
* @return a widened or shrunk viewport
*/
public @NonNull
Viewport resize(final double factor) {
return new Viewport(getCenter(), getLatitudeSpan() * factor, getLongitudeSpan() * factor);
}
/**
* Return the smallest viewport containing all the given points.
*
* @param points
* a set of points. Point with null coordinates (or null themselves) will be ignored
* @return the smallest viewport containing the non-null coordinates, or null if no coordinates are non-null
*/
static public @Nullable
Viewport containing(final Collection<? extends ICoordinates> points) {
boolean valid = false;
double latMin = Double.MAX_VALUE;
double latMax = -Double.MAX_VALUE;
double lonMin = Double.MAX_VALUE;
double lonMax = -Double.MAX_VALUE;
for (final ICoordinates point : points) {
if (point != null) {
final Geopoint coords = point.getCoords();
if (coords != null) {
valid = true;
final double latitude = coords.getLatitude();
final double longitude = coords.getLongitude();
latMin = Math.min(latMin, latitude);
latMax = Math.max(latMax, latitude);
lonMin = Math.min(lonMin, longitude);
lonMax = Math.max(lonMax, longitude);
}
}
}
if (!valid) {
return null;
}
return new Viewport(new Geopoint(latMin, lonMin), new Geopoint(latMax, lonMax));
}
@Override
public boolean equals(final Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Viewport)) {
return false;
}
final Viewport vp = (Viewport) other;
return bottomLeft.equals(vp.bottomLeft) && topRight.equals(vp.topRight);
}
@Override
public int hashCode() {
return bottomLeft.hashCode() ^ topRight.hashCode();
}
}
|