Android Maps Spherical Util Distance - android

I have a polyline with point A & B dropped on a map.
At present I am calculating the distance from the line from my location by using the
computeOffset()
and dropping points every five feet on that line and then looping through with the
computeDistanceBetween()
to find the nearest point.
Is there another method or classes out there for this because 5 ft is terribly far away when using an external GPS receiver. I could go to infinitesimally small increments but with the loop and point storage memory would be used up in no time.
Is there anything built into Android at the moment that supports automatic distance calculation from a line?

If you ignore the spherical nature of the surface, which should be o.k. for small distances, your location and the line are forming a triangle. The distance is just the height of this triangle.
So you just need enough information about the triangle, e.g. the length of all sides, to calculate the height.
The length of all sides is just the distance between the 3 points (your location and the two ends of the line). Call the side formed by your line A, and the other sides B and C. The height can be computed then as
H=(2/A)sqrt[s(s-A)(s-B)(s-C)] with s=(1/2)(A+B+C)
(Just looked up this formula. Did not try it.)
EDIT
The solution above gives the distance to the straight line defined as prolongation of your polyline, but without considering the endpoints.
I guess you are looking for the distance to the line limited by its endpoints.
Thus you need to find out in addition, whether the orthogonal projection of your location to the line lies within the endpoints or outside, or with other words, whether the angles between the triangle sides A and B or A and C are greater than 90 degrees.
This can only be the case if B or C are longer than A. If so, take the shorter one (let' say its B) and check whether B*B + A*A is greater than C*C.
(It follows from the law of cosines, that the angle is then greater than 90 degrees.) In this case, B is the distance you are looking for.

Related

convert WSG84 gps coordinate to x y cartesian coordinate on a plane

I'm working on an android application which can track movements. My problem is that I need to convert gps coordinates to 2-d coordinate on a plane.
The coordinates which I obtain now are in WSG84 format. I need those coordinates because a I need to compute the distance between a point and a line in order to understand if I'm getting too far from a pre-defined path. The area in which I'm working on is not big in respect to the whole earth, so I think it's okay to not care about z axis.
I have no map, so I just need to compute these coordinates in background. Thanks!
This:
I need to compute the distance between a point and a line in order to understand if I'm getting too far from a pre-defined path
seems to be the key question you are posing. So that is what I will answer.
In a strict sense, converting from spherical to euclidean coordinates is not possible, which is why people invented to the UTM system among many others. The problem with using UTM for your purpose is that a given lat/lon may be in different regions, so the (x,y) pairs will not correlate usefully.
Assuming you have three points A, B and P, where A & B define the start and end of your line, and P is the point you want to know about, then my best suggestion is to:
calculate the great circle distance (d) from P to B
calculate the bearing from the P to B (theta)
calculate the bearing from A to B (alpha)
If the points are actually very close to each other then the space you are working in is locally euclidean, so the distance from the line from A to B is:
d sin(alpha - theta)
There are numerous online references on calculating great circle distance and bearing in Java for WGS84. eg.
http://openmap.bbn.com/svn/openmap/trunk/src/openmap/com/bbn/openmap/proj/GreatCircle.java
...is just one of many.

how to create triangle from collection of points in efficient way?

I have collection of 100 geo points. I want to create triangles (each triangle is a single zone), as many as possible. Now the only criteria to make a side of triangle is that two points can't be further than 5KM. So I need 3 points ABC where
|AB| <= 5
|BC| <= 5
|CA| <= 5
Now the only thing I could think was that I check every point with the rest of points. Of course I'm breaking loop when I have 3 points. But I think this is not the fastest way.
What I have ? I have List of points, each point has latitude and longitude so using google api I can in easy way get distance between points.
Points are stored in SQLite database where each point has latitude and longitude in seperate columns
any ideas ? what alhorithm should I use ? or maybe the one I thought is the only good ?
1) In the worst case, every point is within 5km of every other point, and you will end up creating (100 choose 3) triangles, and clever data structures won't speed this up. So you might be better off just doing the simple thing you have described.
2) Suppose you find, for each point, how for north of a reference point it is, and how far east of that reference point it is. Then think of a grid with one of its intersections at the reference point and with its lines just over 5km apart. Sort each point into the box in the grid that it lies in. Now iterate over the boxes in the grid, combining the points in them to make up triangles. Because the grid width is 5km you don't need to compare points from different grid boxes unless those grid boxes touch, at least at the corners. If they don't touch, those grid boxes are separated by at least 5km, and so are all of the points in them. (You could use some value slightly larger than 5km if you are worried about the curvature of the earth upsetting the implicit assumptions here)
Here are some notes and examples to explain (2) a bit more:
Examples of some of these ideas:
Pick the first point as the reference point. Suppose it is at 54.37N, 5.56W. By definition this is at (0, 0). Take a point at 54.32N, 5.703W. Ignoring the curvature of the earth, work out the distance from 54.37N, 5.56W to 54.32N, 5.56W to find out how far south the second point is from the first. Similarly find out how far west of the first point it is and give it a grid location of (-X, -Y) based on this. Do that for all points. Now use these grid locations to put the points into boxes, where each box holds all the points in a 5km x 5km grid square. You can do this just by dividing by five and then using floor() to turn this into an integer, which gives you a co-ordinate in a grid of boxes.
Iterate over all boxes. Suppose you are working on the box with its southwest point at (10, 15). You have to compare every point in that box with every other point in that box. You have to compare every point in that box with every point in the box with southwest point (10,20), because those two boxes lie alongside each other, so points on either side of the line joining them could be less than 5km apart. You even have to compare every point in the box with southwest point at (10, 15) with every point in the box with southwest point (15, 20) because you could have point (14, 19) in the first box and point (16, 21) in the second box and these are less than 5km apart. You DON'T have to compare any point in the box with southwest point (10, 15) with any point in the box with southwest point (20, 15) because the difference in north distances for these boxes means that every point in the (10, 15) box is more than 5km south of every point in the (20,15) box (if the way you put points into boxes is consistent even points exactly at grid locations won't go into two boxes this far apart unless they are MORE than 5km apart).
So the idea is that because you have sorted points into boxes and you only have to compare the points in a box with the points in the nine boxes that are its boxes and the boxes that neighbour its box, there are a lot of comparisons of points that you don't have to make. - But in the worst case every point ends up in the same box and you are sunk.

Calculate the distance between the point that the user and a <svg:g > element in Android

I want to render my svg in Android via the AndroidSVG library (but I'm open to use another solution). When receiving a Touchevent I want to calculate the distance between the touch event and a given svg element. Specifically a <svg:g id:"myElement"> element. That element inturn cotnains a bunch of <svg:path ...> children.
At the moment I have no idea how to go about the task. Even when there's no library available that supports this function by default I would appreciate if someone could point me to an algorithm that I could implement.
A group element does not have a shape of its own. So you have to look at its content.
If the group element has some transformation, you might either apply this transformation to each child element, or apply the reverse operation to the current mouse position. In the latter case, you will have to apply some correction to the final result, since the distance will be in transformed coordinates so you have to revert it back to untransformed coordinates.
There are several ways how you could define the shape of a path, and each definition yields a different distance metric. Ordered from fast but crude to slow but exact, the options that I can think of are
Axis-aligned bounding box. Simply take all points, including Bézier control points, and take the minimum and maximum for each coordinate. This defines a bounding rectangle, and you can compute the distance to that.
Convex hull. Take all points, again including control points, and compute the convex hull of these. The path will be contained within that hull. Compute the distance to that.
Point cloud. Take all the points, perhaps again including control points but perhaps not, and find the closest one to our input. Use the distance to that.
Piecewise linear. Ignore control points, but consider your path as a sequence of line segments. Compute the distance to each, then take the minimum.
Flattened path. Use De Casteljau's algorithm to approximate curved paths by piecewise linear paths until some error bound is met, then do as described above.
Mathematically exact. Look at each curved segment, and actually compute the minimal distance. For this you need to find the points where the tangent to the curve is orthogonal to the vector towards your input point. This would be a question in its own right for Math SE but please search before you post.

Get the shortest distance from a point

I have this table in sqlite
Locations
ID
Lat ( latitude)
Lon ( longitude)
Type
Name
City
I have for example 100 records
what I need is to get (using my own coordinates) the nearest point in my table.
What I did is to get the shortest distance between my current point and each one in the table, and return the shortest one, but I am searching for a better solution
Thanks
A possible solution is to use a grid for the whole map your are interested in and pre-assign points to a particular row/column. Then:
Calculate the grid location of your new point - add a column to the database for this.
Calculate the distance of all coordinates in the current grid - if one exists
You still need to calculate all the distances in the next grid out (you are unlikely to be perfectly centered in your current square, you always need to check one grid distance out from the one your best match was in.)
Should cut down a lot on the number of calculations you need to do.
If you expect to always find a location within X distance you could query for x/y coords that would fall within that range your coords +/- x KM (a square), calculate if they then fall within the xKM circle from your point, and then choose the shortest.
UPDATE - Grid option
I am assuming you already are doing the distance between two points calculation and will not describe that.
If you have an atlas handy you can see an example by looking a place up in the index. It will give you a page and a grid location like M5. If you go to that page it will have rows and columns labeled with numbers and letters and if you look in the square where row M and column 5 intersect you will find the city there. To do it for your system you need to:
determine how big your grid should be (how dense are your points - would be no good to have a big grid and all your points land in one square).
For each point calculate which grid it is in. If your polygons are complex there is tons of point in polygon code out there to copy. If (as my example is) you just use squares, you just need to determine which row/column each point is between.
See map for user location and closest points example:
So if the user is the green marker, he would be in C4. You would search all other points in C4 and determine that the closest is #2. Then you would also have to check one grid out all the way around to make sure there wasn't a closer item than the one you found, so this includes squares: B3,B4,B5,C3,C5,D3,D4,D5. When you do you will pick #3 from C3 and you are finished.
If the user had been in square D2 where there are no other points your would have found your first match in say C2. When checking C1,C2,C3,D1,D3,E1,E2,E3. Once found you would then again need to check another radius out, which would have be: B0-4, C0,C4,D0,D4,E0,E4,F0-4. etc. You can see that grid selection will be important to make this as efficient as possible.
Also Note this assumes your grids are equal unlike my hand drawn example.
Option 2:
If you expect a result within X km, and you want something your DB will calculate quickly you can do this:
LatMin = currentLatCoord-radiusValInDegrees
LatMax = currentLatCoord+radiusValInDegrees
LonMin = currentLonCoord-radiusValInDegrees
LonMax = currentLonCoord+radiusValInDegrees
SELECT *
From Locations
WHERE Lat BETWEEN LatMin AND LatMax
AND Lon BETWEEN LonMin AND LonMax
Now this gives you all results in a square. It is important that you then check they are actually in the circle - you need to drop any in the corners as there may actually be closer coordinates than those on the edge of the circle. So for each point check if it is inside the circle first (Equation for testing if a point is inside a circle) then calculate the distance and keep the closest one. If you do not get a result, widen the circle.
Again, selecting a good radius will depend on your data.
Have you check this Site of how to count for the distance between two points on Earth?
But just keep in mind that it give the Distance based on Earth Surface not based on the actual path to reach at that position. So if you want to count distance based on the Actual Path to reach that position then you can get it by using Google MAP API.
Google Maps API gives the distance between two point based on the actual path.
Hope this information surly help you.
Enjoy Coding... :)
Distance between two points: ((x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ 0.5. However, distance between these points are straight lines. Most likely, there are variables like local vs highway, not to mention one-way streets and waterways, where you need to find the nearest bridge. Therefore, I suggest using Google and Bing maps api. The are free for a limited number of searches.
There's a rather clever solution over at Query to get records based on Radius in SQLite?
based on precalculating some trigonometric values for each position when inserting the rows which then lets you calculate the distance in your query only using arithmetic functions.
I've used it very successfully in my own code
Let me make sure this is right: You have point a, and a table of points a[]. Currently, you do something like:
loop over b[]
get distance from b[i] to a
if distance is less than minimumDistance
set minimumDistance = distance
set closestPoint = i
return closestPoint
If so, you're finding it in O(n) time, which can't really be improved upon much. You have to check all the points to see which are the closest.
However, as Matthew points out, you can prune n by assigning points to a grid. This can drastically reduce the amount of points needed to compare. The expense is a bit of time preprocessing, and slightly more complicated logic. If you have a long list of points(or the distance() function takes a long time), you'll definitely want to do something like this.
Depends how much you care about being correct when near the poles
if closest by pythagorean distance is good enough you can use this in the orderby of the sql
eg. SELECT * FROM locations ORDERBY (Lat-myLat)*(Lat-myLat) + (Lon-myLon)*(Lon-myLon) LIMIT 1
Not technically the most correct, but saves getting all locations from the database and looping over them, let sqlite do that for you
You can use my php class hilbert curve # phpclasses.org. It uses a monster curve and a quadkey to find the shortest distance. To search the quadkey you can use any depth.
Though this is not a best option.
Let you are trying to figure out shortest distance within N mile/km radious for fixed no of locations/your location table data are not changing regularly.
Add another column Distance_Index (DI) a self multi reference key ArrayType. Once run a procedure and update the DI with ID in ascending order accoording to distance from this DI.
Now from next time onwords distance is with you. just make a query to the database and use it.
Now, in your problem no of location are less within N, then DI will not be too much long.Just as an opinion.

How do I know if I'm on road between two points in android

I have a list of start point and end point coordinates for zoned roads. When I put the coordinates in to google maps it shows the road correctly. I want to develop an android application that will alert me if I am on a zoned road.
How can I check if my current location is in the zone if I have only the start point and end point? can I use the navigation in some way?
If you want to see if a point is between two other points, the first solution that crossed my mind is as follows:
Imagine you have a triangle (point of interest, and the two end points ). If the hypotenuse is represented by the two end points, you can get the two angles that it makes with your point of interest. If both of the angles are <= 90 degrees, then the point is between them, if otherwise, it is not.
Hope this is clear.

Categories

Resources