Calculate shortest distance between a GPS coordinate and a route - google-maps

Is there a simple way to calculate the closest distance between a route (directions between two cities for example) and a GPS coordinate using the Google Maps API?
One appoach I can think of is to translate the route into a set of GPS coordinates and calculate the distance to each coordinate. Is there a better way of doing this?

One appoach I can think of is to translate the route into a set of GPS
coordinates and calculate the distance to each coordinate. Is there a
better way of doing this?
Yes there is a much better way.
The first part is correct: translate to a polyline of lat/lon coordinates.
However the second is to simple. You want the shortest distance to the line segment not the the next corner point, that is the start or end of the line.
In school you used the hessian normal distance. However this formula calculates just the distance to an infinite line. In case of a polyline you have an sequence of line segments.
So you need a formula for "distance to line segment". This one you can find using that search terms.
Having this formula implemented, you interate over all line segments each defined by two points (polyPoint[i], polyPoint[i+1]) and take the minimum of the distance.
Don't forget to transform the current line segement to cartesian (x,y) space, because the usualy formulas, do not work on spehrical coordinates.
This approach is more complex, but gives exact results. Remember a line on a route if often 400-700 meters long. So the simple approach you asked, gives in this case an error of 200-350m if you are in the middle between the two points.

Related

Optimal approach for filtering out outlier map coordinates

I've got a list of map coordinates [lat,lon]. I would like to filter out those that by some metric, are too far away from the rest of the main group, outliers.
A) A plain approach to it would be to get the median for lat,lon and then filter out whatever is further away from that median than said metric ( e.g distance ). This would only work for an absolute distance ( e.g 5km ).
B) An improvement to that approach could be to assume that no more than x% of the coordinate pairs would be outliers (essentially setting a threshold there ). Then I'd sort the coordinates array and remove the first x/2% and the final x/2%. Then find the max distance of that group of markers which would be the distance of the first marker to the last marker in that array. Finally, apply A) with the metric for the distance being the distance we just calculated ( so that the distance metric is not fixed )
This is simply an approach I very briefly came up with so if it has any obvious downsides please let me know. In a more open discussion spirit, how would you go about solving this problem? Thanks for your input
Working separately on the coordinates is not the best approach because it is not rotation invariant.
You can try by "onion peeling", i.e. building the convex hull of the point cloud and removing the hull vertices, repeatedly.
Read the paper "Onion-Peeling Outlier Detection in 2-D data Sets; Archit Harsh, John E. Ball & Pan Wei".

GPS coordinations for each kilometer

I'll find a route between two places, for example using google maps. I'd like to divide the route to kilometers (two following places will be at a distance of 1 km), and get GPS coordinations of these places. This is because then I'll be able to get exacly the coordinations of, for example, 5th kilometer on the route. Could you please advice me how to achieve it?
This is extremely nontrivial. Is say your best bet is to find an algorithm to load the bearing between two points, then one to load a coordinate given a start point, distance, and bearing. This could give you it, but only if the data contained only straight lines. Since I assume the Google Maps API only gives you the turns the user has to make, this approach will be inaccurate when there are bends in roads. You'd need GIS data for roads and what will undoubtedly turn into a complicated algorithm to find something like this. It's definitely doable, but that's l how I'd start. Look into the Census TIGER road data, it should help.
Unless, of course, I'm wrong and the API does actually give enough points to cleanly map it, in which case those functions should be easy to find and implement.
This will only work if you have the polyline as a sequence of lat/lon (or other) coordinates, wherever you get that from.
Then you start at the beginning an iterate through the lines (point[i], point[i+1]).
THis distance you calculate with standard API.
while itersting you sum up the distance.
Once you exceed the 1000m, you know that the splitting point (the 1000m marker) is at line segment [i,i+1].
To calculate the exact position where on the line that is, you take the total summed meters from previous segment, and the value of this segment and do a linear interpolation.
The working code is a bit complexer: there can be multiple markes within one segement.
But first find out where you get the polyline from, whitou that it will not work.

Google Maps/OSM - Finding records within 100 miles of an area (not a point)

I would like to find all points that are within N miles of a given area.
E.g. the area is California: Find all points that are within 50 miles of the border of California (not the middle of California).
When using Google Maps the distance is calculated using 'the middle' of the given location, but I need to calculate the distance using the borders of the given location. The location could be any zip code, city or country.
Could that be done by drawing a polygon using California's coordinates on a map and calculate the distance to location B using the points of the polygon?
Is there a more elegant solution to this? Any ideas?
Thanks!
I'm not sure if I understand your requirements completely, but I will give it a try with different interpretations:
1. You want to filter own map points:
This can be done with any GIS or a own service that offers a call like my_points_in_area(bbox). Bbox means here boundingbox and is the 2x lat/lon pair describing the rectangle around your given centerpoint. If you want to be accurate and really just deliver whats within 100km, you might need to test the distance to the POIs once more, as the rectangle will also include points that are a bit more far away.
2. You want to filter OSM data:
You might use a reverse-geocoding service as Nominatim to get informations about points of interests that are within this distance: http://wiki.openstreetmap.org/wiki/Nominatim
Otherwise import the OSM data using osmosis to a PostGIS DB. AFAIK there is (currently) no DB tool for Oracle: http://wiki.openstreetmap.org/wiki/Oracle
I'm sorry if I missed your question, but then please add more details :)

How to find point along line defined by two points?

There is a function in google maps geometry library
interpolate(from:LatLng, to:LatLng, fraction:number)
which finds a point between two other points:
A--X--B
I need something very similar, find a point further along the line:
A--B--X
Interpolate does not accept fraction>1 so I cannot use it. Is there some simple way to calculate the point?
I want to achieve given distance between A and X points.
EDIT: In my application the distance was really small so I used fromLatLngToPoint conversion and linear interpolation, which get good enough results on small area.
You can calculate bearing, then find destination point with code from this excellent page

Mysql geometry AREA() function returns what exactly when coords are long/lat?

My question is somewhat related to this similar one, which links to a pretty complex solution - but what I want to understand is the result of this:
Using a Mysql Geometry field to store a small polygon I duly ran
select AREA(myPolygon) where id =1
over it, and got an value like 2.345. So can anyone tell me, just what does that number represent seeing as the stored values were long/lat sets describing the polygon?
FYI, the areas I am working on are relatively small (car parks and the like) and the area does not have to be exact - I will not be concerned about the curvature of the earth.
2.345 of what? Thanks, this is bugging me.
The short answer is that the units for your area calculation are basically meaningless ([deg lat diff] * [deg lon diff]). Even though the curvature of the earth wouldn't come into play for the area calculation (since your areas are "small"), it does come into play for the calculation of distance between the lat/lon polygon coordinates.
Since a degree of longitude is different based on the distance from the equator (http://en.wikipedia.org/wiki/Longitude#Degree_length), there really is no direct conversion of your area into m^2 or km^2. It is dependent on the distance north/south of the equator.
If you always have rectangular polygons, you could just store the opposite corner coordinates and calculate area using something like this: PHP Library: Calculate a bounding box for a given lat/lng location
The most "correct" thing to do would be to store your polygons using X-Y (meters) coordinates (perhaps UTM using the WGS-84 ellipsoid), which can be calculated from lat/lon using various libraries like the following for Java: Java, convert lat/lon to UTM. You could then continue to use the MySQL AREA() function.