<p>I have a number of tracks recorded by a GPS, which more formally can be described as a number of line strings.</p>

<p>Now, some of the recorded tracks might be recordings of the same route, but because of inaccurasies in the GPS system, the fact that the recordings were made on separate occasions and that they might have been recorded travelling at different speeds, they won't match up perfectly, but still look close enough when viewed on a map by a human to determine that it's actually the same route that has been recorded.</p>

<p>I want to find an algorithm that calculates the similarity between two line strings. I have come up with some home grown methods to do this, but would like to know if this is a problem that's already has good algorithms to solve it.</p>

<p>How would you calculate the similarity, given that similar means represents the same path on a map?</p>

<p><b>Edit:</b> For those unsure of what I'm talking about, please look at this link for a definition of what a line string is: <a href="http://msdn.microsoft.com/en-us/library/bb895372.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/bb895372.aspx</a> - I'm <i>not</i> asking about character strings.</p>

[{'answer_id': 62537, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 2, 'selected': False, 'text': '<p>I would add a buffer around the first line based on the estimated probable error, and then determine if the second line fits entirely within the buffer.</p>\n'}, {'answer_id': 62726, 'author': 'Stephen Marney', 'author_id': 7133, 'author_profile': 'https://Stackoverflow.com/users/7133', 'pm_score': 2, 'selected': False, 'text': '<p>To determine "same route," create the minimal set of normalized path vectors, calculate the total power differences and compare the total to a quality measure.</p>\n\n<ol>\n<li>Normalize the GPS waypoints on total path length,</li>\n<li>walk the vectors of the paths together, creating a new set of path vectors for each path based upon the shortest vector at each waypoint,</li>\n<li>calculate the total power differences between endpoints of each vector in the normalized paths weighting for vector length, and</li>\n<li>compare against a quality measure.</li>\n</ol>\n\n<p>Tune the power of the differences (start with, say, squared differences) and the quality measure (say as a percent of the total power differences) visually. This algorithm produces a continuous quality measure of the path match as well as a binary result (Are the paths the same?)</p>\n\n<blockquote>\n  <p>Paul Tomblin said: I would add a buffer\n  around the first line based on the\n  estimated probable error, and then\n  determine if the second line fits\n  entirely within the buffer.</p>\n</blockquote>\n\n<p>You could modify the algorithm as the normalized vector endpoints are compared. You could determine if any endpoint difference was above a certain size (implementing Paul\'s buffer idea) or perhaps, if the endpoints were outside the "buffer," use that fact to ignore that endpoint difference, allowing a comparison <em>ignoring side trips</em>.</p>\n'}, {'answer_id': 65216, 'author': 'eladv', 'author_id': 7314, 'author_profile': 'https://Stackoverflow.com/users/7314', 'pm_score': -1, 'selected': False, 'text': '<p>I actually side with the person (Aaron F) who said that you might be interested in the Levenshtein distance problem (and cited <a href="http://en.wikipedia.org/wiki/Fasta" rel="nofollow noreferrer">this</a>). His answer seems to me to be the best so far.</p>\n\n<p>More specifically, Levenshtein distance (also called edit distance), does not measure strictly the character-by-character distance, but also allows you to perform insertions and deletions. The best algorithm for this distance measure can be computed in quadratic time (pretty slow if your strings are long), but the computational biologists have pretty good heuristics for this, that might be of interest to you on their own. Check out <a href="http://en.wikipedia.org/wiki/BLAST" rel="nofollow noreferrer">BLAST</a> and <a href="http://en.wikipedia.org/wiki/Fasta" rel="nofollow noreferrer">FASTA</a>.</p>\n\n<p>In your problem, it seems that you are dealing with differences between strings of numbers, and you care about the numbers. If you give more information, I might be able to direct you to the right variant of BLAST/FASTA/etc for your purposes. In any case, you might consider adapting BLAST and FASTA for your needs. They\'re quite simple.</p>\n\n<p><a href="http://en.wikipedia.org/wiki/Fasta" rel="nofollow noreferrer">1</a>: <a href="http://en.wikipedia.org/wiki/Levenshtein_distance" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Levenshtein_distance</a>, <a href="http://www.nist.gov/dads/HTML/Levenshtein.html" rel="nofollow noreferrer">http://www.nist.gov/dads/HTML/Levenshtein.html</a></p>\n'}, {'answer_id': 67799, 'author': 'P Kinney', 'author_id': 7576, 'author_profile': 'https://Stackoverflow.com/users/7576', 'pm_score': 1, 'selected': False, 'text': '<p>You could walk along each point (Pa) of LineString A and measure the distance from Pa to the nearest line-segment of LineString B, averaging each of these distances.  </p>\n\n<p>This is not a quick or perfect method, but should be able to give use a useful number and is pretty quick to implement.</p>\n\n<p>Do the line strings start and finish at similar points, or are they of very different extents?</p>\n'}, {'answer_id': 84253, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 5, 'selected': True, 'text': '<p>Compute the <a href="http://cgm.cs.mcgill.ca/~athens/cs507/Projects/2002/StephanePelletier/#what" rel="nofollow noreferrer">Fréchet distance</a> on each pair of tracks. The distance can be used to gauge the similarity of your tracks.</p>\n\n<p><em>Math alert:</em> Fréchet was a pioneer in the field of <a href="http://en.wikipedia.org/wiki/Metric_space" rel="nofollow noreferrer">metric space</a> which is relevant to your problem.</p>\n'}, {'answer_id': 89889, 'author': 'Chuck Wooters', 'author_id': 8879, 'author_profile': 'https://Stackoverflow.com/users/8879', 'pm_score': 1, 'selected': False, 'text': '<p>If you consider a single line string to be a sequence of [x,y] points (or [x,y,z] points), then you could compute the similarity between each pair of line strings using the <a href="http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm" rel="nofollow noreferrer">Needleman-Wunsch</a> algorithm. As described in the referenced Wikipedia article, the Needleman-Wunsch algorithm requires a "similarity matrix" which defines the distance between a pair of points. However, it would be easy to use a function instead of a matrix. In your case you could simply use the 2D <a href="http://en.wikipedia.org/wiki/Euclidean_distance" rel="nofollow noreferrer">Euclidean distance</a> function (or a 3D Euclidean function if your points have elevation) to provide the distance between each pair of points. </p>\n'}]
['https://Stackoverflow.com/questions/62496', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/890/']