I know there many solutions available in Stackover flow but looks none have gone to address the basic question I have is
- I have more than 2500 lon/lat data
- I want to store/retrive them from sqlite
- Query nearest locations based on user input.
Looking for optimum solutions
Note: I have gone through
Finding the closest point to a given point
What is this Geohashing all about How can use Geohashing in my this particular problem
Geohashing is an encoding of latitude, longitude pairs such that points in proximity to eaxh other have geohashes with a common prefix. However, this does not work with every coordinate on the planet, i.e. there regions where the goehash changes significantly for points in proximity. Depending on the hashing algorithms the area near to equator may be such an area.
See here for more detail: http://en.wikipedia.org/wiki/Geohash
For a relatively small database of ca. 500 locations I was able to find the nearest location to a given point of reference (the user's location) very fast by searching for points inside an intervall of 0.1 degrees. Here is the code for the query:
/**
* Query the airfields table for airfields near the given position.
* #param dbCon DB connection
* #param ref_lat latitude
* #param ref_lon longitude
* #return Answer the airfield nearest to the given position as array
* of objects: id, designator, latitude, longitude.
* Answer <code>null</code> if their is no airfield near the
* given position plus or minus 0.1 degrees.
*/
private Object[] rangeQuery(final SQLiteDatabase dbCon, final double ref_lat, final double ref_lon) {
if( DEBUG )
Log.d( TAG, "rangeQuery lat=" + ref_lat + ", lon=" + ref_lon);
final SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(AirfieldsTable.TABLE_NAME);
final String[] whereArgs = new String[] {
Double.toString(ref_lat - 0.1d), Double.toString(ref_lat + 0.1d),
Double.toString(ref_lon - 0.1d), Double.toString(ref_lon + 0.1d)
};
final Cursor crsr = qb.query(dbCon, allFields(), AirfieldsTable.RANGE_CLAUSE, whereArgs, null, null, null);
final Object[] val = this.scanForNearest(crsr, ref_lat, ref_lon);
crsr.close();
if( DEBUG )
Log.d( TAG, "scanForNearest returned " + val);
return val;
}
If there is more than one row selected I compare the remaining points directly (thats what scanForNearest() does). Its fast enough to find the airport after the logger (its a logging application) detected a landing.
Related
How can I get the exact Latitude and Longitude? I only get whole numbers. From my understanding, in order to get down to the meter I need to get down to 5 decimal places. I have tried the horizontal and vertical accuracy but they don't match my phones GPS reading.
How can I get an exact GPS reading with Geolocation API, Down to the meter?
here is my code
var my_geo:Geolocation = new Geolocation();
my_geo.setRequestedUpdateInterval(2000);
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
function onGeoUpdate(e:GeolocationEvent):void
{
gpsLat1 = (e.latitude);
gpsLon1 = (e.longitude);
gpsHeading = (e.heading);
gpsHorAcc = (e.horizontalAccuracy);
gpsVerAcc = (e.verticalAccuracy);
gpsCheck = 2;
my_txt.text = "My Latitude is "+ gpsLat1 + " and my Longitude is "+ gpsLon1 + " Bearing is " + gpsHeading+ " HorAcc "+ gpsHorAcc+ " VertAcc "+gpsVerAcc;
}
Make sure your variables (gpsLat1, gpsLon1, gpsHeading, etc.) are of type Number and not a uint or int. Unsigned Integers and Integers only allow for whole numbers, whereas Number is the equivalent of float in most other languages (and allows for incredibly larger values, as well). If you save a decimal to an integer, it is rounded off/floored (I can't remember which), which sounds exactly like the problem you are having.
Alternatively, the API is restricted by the hardware you are testing on. If the hardware only returns a certain value for GPS coordinates, AIR cannot be any more precise. Odds are this is not the issue since a whole lat long point can be miles and miles in distance, meaning any device with that inaccurate of a GPS chip is absolutely useless.
Try this:
function onGeoUpdate(e:GeolocationEvent):void
{
gpsLat1 = (e.latitude.toString());
gpsLon1 = (e.longitude.toString());
gpsHeading = (e.heading.toString());
gpsHorAcc = (e.horizontalAccuracy.toString());
gpsVerAcc = (e.verticalAccuracy.toString());
gpsCheck = 2;
my_txt.text = "My Latitude is "+ gpsLat1 + " and my Longitude is "+ gpsLon1 + " Bearing is " + gpsHeading+ " HorAcc "+ gpsHorAcc+ " VertAcc "+gpsVerAcc;
}
I am in need of assistance. I will be computing a measured variable, then taking the top 100 values of these and averaging them. Please remember, I have been teaching myself only for the past 6 weeks and what is obvious to some, will not necessarily be obvious to me.
In essence, say 'double x' is the variable, that I have many close values for. What I need is a way to compute the sum (then average) of the top 100 of these values.
In my research, the closest thing I can see that would suit what I need is 'nextAfter(double start, double direction); and before this, using 'max' to determine the maximum value, would this be the correct starting point:
double xm = max(x);
static double (xm, x < xm);
My question is how to get the sum of the top 100 values (the maximum and 99 nextAfter's) - averaging would be easy - just dividing by 100.
To compute the average of the largest n values you read from the source, you need to store at least these values. Since at any given point before the end you don't know whether some of the largest n values overall will come later, you need to keep track the largest n values seen so far.
A simple way to do that is to store the largest values in a heap or priority queue, since that allows easy adding of new values and finding (and removing) of the smallest of the stored values. The default PriorityQueue is well-suited for this task, since it uses the natural ordering of the elements, and thus polling removes the smallest of the stored elements. If one wanted to compute the average of the n smallest elements, one would need to use a PriorityQueue with a custom Comparator (or in this special case, simply negating all values and using the natural ordering would work too).
The lazy way (less code) to achieve the desired is to simply add each incoming value to the queue, and if the queue's size exceeds n [then it must be n+1] remove the smallest element from the queue:
// vp is the value provider
while(vp.hasNext()) {
// read the next value and add it to the queue
pq.add(vp.nextValue());
if (pq.size() > topSize) {
pq.poll();
}
A slightly more involved way is to first check whether the new value needs to be added, and only modify the queue when that is the case,
double newValue = vp.nextValue();
// Check if we have to put the new value in the queue
// that is the case when the queue is not yet full, or the smallest
// stored value is smaller than the new
if (pq.size() < topSize || pq.peek() < newValue) {
// remove the smallest value from the queue only if it is full
if (pq.size() == topSize()) {
pq.poll();
}
pq.add(newValue);
}
This way is potentially more efficient, since adding a value to the queue and removing the smallest are both O(log size) operations, while comparing to the smallest stored value is O(1). So if there are many values smaller than the n largest seen before, the second way saves some work.
If performance is critical, be aware that a PriorityQueue cannot store primitive types like double, so the storing (and retrieving for the average computation) involves boxing (wrapping a double value in a Double object) resp. unboxing (pulling the double value from a Double object), and consequently an indirection from the underlying array of the queue to the actual values. Those costs could be avoided by implementing a heap-based priority queue using a raw double[] yourself. (But that should rarely be necessary, usually, the cost of the boxing and indirections would constitute only a minute part of the overall processing.)
A simple-minded complete working example:
import java.util.PriorityQueue;
/**
* Example class to collect the largest values from a stream and compute their
* average.
*/
public class Average {
// number of values we want to save
private int topSize;
// number of values read so far
private long count = 0;
// priority queue to save the largest topSize values
private PriorityQueue<Double> pq;
// source of read values, could be a file reader, a device reader, or whatever
private ValueProvider vp;
/**
* Construct an <code>Average</code> to sample the largest <code>n</code>
* values from the source.
*
* #param tops Number of values to save for averaging.
* #param v Source of the values to sample.
*
* #throws IllegalArgumentException when the specified number of values is less than one.
*/
public Average(int tops, ValueProvider v) throws IllegalArgumentException {
if (tops < 1) {
throw new IllegalArgumentException("Can't get average of fewer than one values.");
}
topSize = tops;
vp = v;
// Initialise queue to needed capacity; topSize + 1, since we first add
// and then poll. Thus no resizing should ever be necessary.
pq = new PriorityQueue<Double>(topSize+1);
}
/**
* Compute the average of the values stored in the <code>PriorityQueue<Double></code>
*
* #param prio The queue to average.
* #return the average of the values stored in the queue.
*/
public static double average(PriorityQueue<Double> prio) throws IllegalArgumentException {
if (prio == null || prio.size() == 0) {
throw new IllegalArgumentException("Priority queue argument is null or empty.");
}
double sum = 0;
for(Double d : prio) {
sum += d;
}
return sum/prio.size();
}
/**
* Reads values from the provider until exhausted, reporting the average
* of the largest <code>topSize</code> values read so far from time to time
* and when the source is exhausted.
*/
public void collectAverage() {
while(vp.hasNext()) {
// read the next value and add it to the queue
pq.add(vp.nextValue());
++count;
// If the queue was already full, we now have
// topSize + 1 values in it, so we remove the smallest.
// That is, conveniently, what the default PriorityQueue<Double>
// gives us. If we wanted for example the smallest, we'd need
// to use a PriorityQueue with a custom Comparator (or negate
// the values).
if (pq.size() > topSize) {
pq.poll();
}
// Occasionally report the running average of the largest topSize
// values read so far. This may not be desired.
if (count % (topSize*25) == 0 || count < 11) {
System.out.printf("Average of top %d values after collecting %d is %f\n",
pq.size(), count, average(pq));
}
}
// Report final average. Returning the average would be a natural choice too.
System.out.printf("Average of top %d values of %d total is %f\n",
pq.size(), count, average(pq));
}
public static void main(String[] args) {
Average a = new Average(100, new SimpleProvider(123456));
a.collectAverage();
}
}
using the interface
/**
* Interface for a source of <code>double</code>s.
*/
public interface ValueProvider {
/**
* Gets the next value from the source.
*
* #return The next value if there is one.
* #throws RuntimeException if the source is exhausted.
*/
public double nextValue() throws RuntimeException;
/**
* Checks whether the source has more values to deliver.
*
* #return whether there is at least one more value to be obtained from the source.
*/
public boolean hasNext();
}
and implementing class
/**
* Simple provider of a stream of <code>double</code>s.
*/
public class SimpleProvider implements ValueProvider {
// State determining which value to return next.
private long state = 0;
// Last allowed state.
private final long end;
/**
* Construct a provider of <code>e</code> values.
*
* #param e the number of values to yield.
*/
public SimpleProvider(long e) {
end = e > 0 ? e : 0;
}
/**
* Default constructor to provide 10000 values.
*/
public SimpleProvider() {
this(10000);
}
public double nextValue() {
++state;
return Math.log(state)*Math.sin(state) + Math.cos(state/2.0);
}
public boolean hasNext() {
return state < end;
}
}
Until now, I always draw "points" in the MapActivities with OverlayItems and with a class Point made by me. With this method I can draw some "points" in the MapActivity. But how I can draw "clickable" points ??
I improvise this class Point, and follow a tutorial about the OverlayItems method, but I can implement any method that yourselves explain me for this "clickable points".
Thanks.
Have a look at this project as example how to implement clickable OverlayItems https://github.com/jgilfelt/android-mapviewballoons
I couldn't find a way to do this so I wrote my own click logic. This is from a project I've put on ice for now and is work in progress (hard coded values for example) but the logic works. Hope it helps:
#Override
public boolean onTap(GeoPoint geoPoint, MapView mapView){
if (!isRoute){ // nothing to do if it's a route
Cursor cursor = (Cursor) mapView.getTag();
Projection projection = mapView.getProjection();
// get pixels for the point clicked
Point clickedPoint = new Point();
projection.toPixels(geoPoint, clickedPoint);
if (cursor.moveToFirst()){
do {
try {
Double lat = cursor.getFloat(Database.LAT_COLUMN) * 1E6;
Double lng = cursor.getFloat(Database.LONG_COLUMN) * 1E6;
GeoPoint thisGeoPoint = new GeoPoint(lat.intValue(),
lng.intValue());
// get pixels for this point
Point overlayPoint = new Point();
projection.toPixels(thisGeoPoint,overlayPoint);
// did the user click within 30 pixels?
if ((Math.abs(clickedPoint.x - overlayPoint.x) < 30) && (Math.abs(clickedPoint.y - overlayPoint.y) < 30)){
// get a cursor to this record
Cursor thisCursor = TestApp.db.rawQuery("SELECT * FROM " + Database.DATABASE_TABLE_PINS + " WHERE CID='" + cursor.getString(Database.ID_COLUMN) + "'", null);
thisCursor.moveToFirst();
// create and show an instance of the PinDetailsDialog
PinDetailsDialog customiseDialog ;
// TODO this is a kludge, why does this throw an exception sometimes?
try{
customiseDialog = new PinDetailsDialog(mapView, context,thisCursor,context.getResources().getConfiguration().orientation);
customiseDialog.show();
} catch (Exception e){
customiseDialog = new PinDetailsDialog(mapView, mapView.getContext(),thisCursor,context.getResources().getConfiguration().orientation);
customiseDialog.show();
}
return true;
}
} catch (Exception e){
e.printStackTrace();
}
} while(cursor.moveToNext());
}
}
return true;
}
The basic idea is to get the point where the user clicked on the map, convert the point into lat long then iterate my data points looking for a match. Note that I am using a hit test of +/- 30 pixels (which I will not hard code when I pick this up again.
I left the TODO in there just in case you stumble into something similar but I do suspect it's entirely down to a problem somewhere in my PinDetailsDialog class implementation.
I use multiple map views, each of which uses data stored in a SQLite table. I store a reference to a cursor to read the data in the tag property of the Mapview, hence the .getTag() call.
i need to create a function for sqlite, because the sin and cos mysql-function doesn't exist, and i need to make a query like:
String q = "SELECT * FROM shops WHERE _id > 0 AND distance(lat, lng, latitudine, longitudine) < 20 AND cancellato=0 ORDER BY _id";
Cursor c = myDataBase.rawQuery(q, null);
return c;
so, i need a function for distance.
and i can't do it via code because it's a list of +8000 shops!
how can i? i didn't found anything on the web.
thanks!
distance(lat, lng, latitudine, longitudine)
I think you have to calculate distance pragmatically from your javacode then you can compare
http://www.sqlite.org/lang_aggfunc.html
http://oreilly.com/catalog/sqlnut/chapter/ch04.html
This is maybe a noob question but im not 100% sure about it.
How can i make a Location Object using Geo points? I want to use it to get the distance between two points.
I already found a thread where it says
Location loc1 = new Location("Location");
loc.setLatitude(geoPoint.getLatitudeE6);
loc.setLongitude(geoPoint.getLongitudeE6);
Location loc2 = new Location("Location2");
loc2.setLatitude(geoPoint.getLatitudeE6);
loc2.setLongitude(geoPoint.getLongitudeE6);
and then i would use the distanceTo() to get the distance between the two points.
My Questions
What is the Providername for? ...new Location("What is this here???")
So do i have to define a Provider before or something?
I want to use this code in a for() to calaculate between more GeoPoints.
And btw - i have to convert the E6 Values back to normal?
Not exactly
loc.setLatitude() takes a double latitude. So the correct code is:
loc.setLatitude( geoPoint.getLatitudeE6() / 1E6);
Location() constructor take the name of the GPS provider used. It can be LocationManager.GPS_PROVIDER or NETWORK_PROVIDER among other values
To get the distance between two point you can use the Location class and more precisely the distanceBetween static method.
The doc is quite clear on what it does but here a quick code sample:
float[] results = new float[3];
Location.distanceBetween(destLatitude, destLongitude, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), results);
// result in meters, convert it in km
String distance = String.valueOf(Math.round(results[0] / 1000)) + " km");
To convert from minute/second to degree you can use the convert method of the Location class.
Log.i("MapView", "Map distance to mark (in meters): " + myLocation.distanceTo(GeoToLocation(point)) + "m");
then
public Location GeoToLocation(GeoPoint gp) {
Location location = new Location("dummyProvider");
location.setLatitude(gp.getLatitudeE6()/1E6);
location.setLongitude(gp.getLongitudeE6()/1E6);
return location;
}