aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgData.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/cgData.java')
-rw-r--r--main/src/cgeo/geocaching/cgData.java146
1 files changed, 92 insertions, 54 deletions
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index b711de6..413caf6 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -28,6 +28,7 @@ import android.database.sqlite.SQLiteStatement;
import java.io.File;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
@@ -1899,35 +1900,7 @@ public class cgData {
// viewport limitation
if (centerLat != null && centerLon != null && spanLat != null && spanLon != null) {
- double latMin = (centerLat / 1e6) - ((spanLat / 1e6) / 2) - ((spanLat / 1e6) / 4);
- double latMax = (centerLat / 1e6) + ((spanLat / 1e6) / 2) + ((spanLat / 1e6) / 4);
- double lonMin = (centerLon / 1e6) - ((spanLon / 1e6) / 2) - ((spanLon / 1e6) / 4);
- double lonMax = (centerLon / 1e6) + ((spanLon / 1e6) / 2) + ((spanLon / 1e6) / 4);
- double llCache;
-
- if (latMin > latMax) {
- llCache = latMax;
- latMax = latMin;
- latMin = llCache;
- }
- if (lonMin > lonMax) {
- llCache = lonMax;
- lonMax = lonMin;
- lonMin = llCache;
- }
-
- if (where.length() > 0) {
- where.append(" and ");
- }
- where.append("(latitude >= ");
- where.append(String.format((Locale) null, "%.6f", latMin));
- where.append(" and latitude <= ");
- where.append(String.format((Locale) null, "%.6f", latMax));
- where.append(" and longitude >= ");
- where.append(String.format((Locale) null, "%.6f", lonMin));
- where.append(" and longitude <= ");
- where.append(String.format((Locale) null, "%.6f", lonMax));
- where.append(')');
+ where.append(buildCoordinateWhere(centerLat, centerLon, spanLat, spanLon));
}
cursor = databaseRO.query(
dbTableCaches,
@@ -2013,6 +1986,50 @@ public class cgData {
}
/**
+ * Builds a where for coordinates
+ *
+ * @param centerLat
+ * @param centerLon
+ * @param spanLat
+ * @param spanLon
+ * @return
+ */
+
+ private String buildCoordinateWhere(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon) {
+ StringBuilder where = new StringBuilder();
+ double latMin = (centerLat / 1e6) - ((spanLat / 1e6) / 2) - ((spanLat / 1e6) / 4);
+ double latMax = (centerLat / 1e6) + ((spanLat / 1e6) / 2) + ((spanLat / 1e6) / 4);
+ double lonMin = (centerLon / 1e6) - ((spanLon / 1e6) / 2) - ((spanLon / 1e6) / 4);
+ double lonMax = (centerLon / 1e6) + ((spanLon / 1e6) / 2) + ((spanLon / 1e6) / 4);
+ double llCache;
+
+ if (latMin > latMax) {
+ llCache = latMax;
+ latMax = latMin;
+ latMin = llCache;
+ }
+ if (lonMin > lonMax) {
+ llCache = lonMax;
+ lonMax = lonMin;
+ lonMin = llCache;
+ }
+
+ if (where.length() > 0) {
+ where.append(" and ");
+ }
+ where.append("(latitude >= ");
+ where.append(String.format((Locale) null, "%.6f", latMin));
+ where.append(" and latitude <= ");
+ where.append(String.format((Locale) null, "%.6f", latMax));
+ where.append(" and longitude >= ");
+ where.append(String.format((Locale) null, "%.6f", lonMin));
+ where.append(" and longitude <= ");
+ where.append(String.format((Locale) null, "%.6f", lonMax));
+ where.append(')');
+ return where.toString();
+ }
+
+ /**
* creates a Cache from the cursor. Doesn't next.
*
* @param cursor
@@ -2738,32 +2755,8 @@ public class cgData {
Set<String> geocodes = new HashSet<String>();
// viewport limitation
- double latMin = (centerLat / 1e6) - ((spanLat / 1e6) / 2) - ((spanLat / 1e6) / 4);
- double latMax = (centerLat / 1e6) + ((spanLat / 1e6) / 2) + ((spanLat / 1e6) / 4);
- double lonMin = (centerLon / 1e6) - ((spanLon / 1e6) / 2) - ((spanLon / 1e6) / 4);
- double lonMax = (centerLon / 1e6) + ((spanLon / 1e6) / 2) + ((spanLon / 1e6) / 4);
- double llCache;
-
- if (latMin > latMax) {
- llCache = latMax;
- latMax = latMin;
- latMin = llCache;
- }
- if (lonMin > lonMax) {
- llCache = lonMax;
- lonMax = lonMin;
- lonMin = llCache;
- }
-
StringBuilder where = new StringBuilder();
- where.append("latitude >= ");
- where.append(String.format((Locale) null, "%.6f", latMin));
- where.append(" and latitude <= ");
- where.append(String.format((Locale) null, "%.6f", latMax));
- where.append(" and longitude >= ");
- where.append(String.format((Locale) null, "%.6f", lonMin));
- where.append(" and longitude <= ");
- where.append(String.format((Locale) null, "%.6f", lonMax));
+ where.append(buildCoordinateWhere(centerLat, centerLon, spanLat, spanLon));
// cacheType limitation
if (cacheType != CacheType.ALL) {
@@ -3473,4 +3466,49 @@ public class cgData {
return where;
}
+ /**
+ * Loads all Waypoints in the coordinate rectangle.
+ *
+ * @param centerLat
+ * @param centerLon
+ * @param spanLat
+ * @param spanLon
+ * @return
+ */
+
+ public Collection<? extends cgWaypoint> loadWaypoints(long centerLat, long centerLon, long spanLat, long spanLon) {
+ StringBuilder where = new StringBuilder();
+ where.append(buildCoordinateWhere(centerLat, centerLon, spanLat, spanLon));
+ init();
+
+ List<cgWaypoint> waypoints = new ArrayList<cgWaypoint>();
+
+ Cursor cursor = databaseRO.query(
+ dbTableWaypoints,
+ new String[] { "_id", "geocode", "updated", "type", "prefix", "lookup", "name", "latlon", "latitude", "longitude", "note", "own" },
+ where.toString(),
+ null,
+ null,
+ null,
+ "_id",
+ "100");
+
+ if (cursor != null && cursor.getCount() > 0) {
+ cursor.moveToFirst();
+
+ do {
+
+ cgWaypoint waypoint = createWaypointFromDatabaseContent(cursor);
+
+ waypoints.add(waypoint);
+ } while (cursor.moveToNext());
+ }
+
+ if (cursor != null) {
+ cursor.close();
+ }
+
+ return waypoints;
+ }
+
}