<p>I have a bunch of latitude/longitude pairs that map to known x/y coordinates on a (geographically distorted) map.</p>

<p>Then I have one more latitude/longitude pair. I want to plot it on the map as best is possible. How do I go about doing this?</p>

<p>At first I decided to create a system of linear equations for the three nearest lat/long points and compute a transformation from these, but this doesn't work well at all. Since that's a linear system, I can't use more nearby points either.</p>

<p>You can't assume North is up: all you have is the existing lat/long->x/y mappings.</p>

<p>EDIT: it's not a Mercator projection, or anything like that. It's arbitrarily distorted for readability (think subway map). I want to use only the nearest 5 to 10 mappings so that distortion on other parts of the map doesn't affect the mapping I'm trying to compute.</p>

<p>Further, the entire map is in a very small geographical area so there's no need to worry about the globe--flat-earth assumptions are good enough.</p>

[{'answer_id': 1926, 'author': 'nlucaroni', 'author_id': 157, 'author_profile': 'https://Stackoverflow.com/users/157', 'pm_score': 0, 'selected': False, 'text': "<p>the problem is that the sphere can be distorted a number of ways, and having all those points known on the equator, lets say, wont help you map points further away.</p>\n\n<p>You need better 'close' points, then you can assume these three points are on a plane with the fourth and do the interpolation --knowing that the distance of longitudes is a function, not a constant.</p>\n"}, {'answer_id': 1927, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 0, 'selected': False, 'text': '<p>Ummm.  Maybe I am missing something about the question here, but if you have long/lat info, you also have the direction of north?</p>\n\n<p>It seems you need to map geodesic coordinates to a projected coordinates system. For example osgb to wgs84.</p>\n\n<p>The maths involved is non-trivial, but the code comes out a only a few lines. If I had more time I\'d post more but I need a shower so I will be boring and link to the <a href="http://en.wikipedia.org/wiki/Geographic_coordinate_system" rel="nofollow noreferrer">wikipedia</a> entry which is pretty good.</p>\n\n<p>Note: Post shower edited.</p>\n'}, {'answer_id': 1945, 'author': 'fastcall', 'author_id': 328, 'author_profile': 'https://Stackoverflow.com/users/328', 'pm_score': 4, 'selected': True, 'text': '<p>Are there any more specific details on the kind of distortion?  If, for example, your latitudes and longitudes are "distorted" onto your 2D map using a Mercator projection, the conversion math is <a href="http://www.wikipedia.org/wiki/Mercator_projection" rel="noreferrer">readily available</a>.</p>\n\n<p>If the map is distorted truly arbitrarily, there are lots of things you could try, but the simplest would probably be to compute a <a href="http://www.wikipedia.org/wiki/Weighted_mean" rel="noreferrer">weighted average</a> from your existing point mappings.  Your weights could be the squared inverse of the x/y distance from your new point to each of your existing points.</p>\n\n<p>Some pseudocode:</p>\n\n<pre><code>estimate-latitude-longitude (x, y)\n\n    numerator-latitude := 0\n    numerator-longitude := 0\n    denominator := 0\n\n    for each point,\n        deltaX := x - point.x\n        deltaY := y - point.y\n        distSq := deltaX * deltaX + deltaY * deltaY\n        weight := 1 / distSq\n\n        numerator-latitude += weight * point.latitude\n        numerator-longitude += weight * point.longitude\n        denominator += weight\n\n    return (numerator-latitude / denominator, numerator-longitude / denominator)\n</code></pre>\n\n<p>This code will give a relatively simple approximation.  If you can be more precise about the way the projection distorts the geographical coordinates, you can probably do much better.</p>\n'}, {'answer_id': 2492, 'author': 'Louis Brandy', 'author_id': 2089740, 'author_profile': 'https://Stackoverflow.com/users/2089740', 'pm_score': 2, 'selected': False, 'text': '<p>Alright. From a theoretical point of view, given that the distortion is "arbitrary", and any solution requires you to model this arbitrary distortion, you obviously can\'t get an "answer". However, any solution is going to involve imposing (usually implicitly) some model of the distortion that may or may not reflect the reality of the situation.</p>\n\n<p>Since you seem to be most interested in models that presume some sort of local continuity of the distortion mapping, the most obvious choice is the one you\'ve already tried: linear interpolaton between the nearest points. Going beyond that is going to require more sophisticated mathematical and numerical analysis knowledge.</p>\n\n<p>You are incorrect, however, in presuming you cannot expand this to more points. You can by using a least-squared error approach. Find the linear answer that minimizes the error of the other points. This is probably the most straight-forward extension. In other words, take the 5 nearest points and try to come up with a linear approximation that minimizes the error of those points. And use that. I would try this next.</p>\n\n<p>If that doesn\'t work, then the assumption of linearity over the area of N points is broken. At that point you\'ll need to upgrade to either a quadratic or cubic model. The math is going to get hectic at that point.</p>\n'}]
['https://Stackoverflow.com/questions/1908', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/79/']