# How To Map A Latitude/longitude To A Distorted Map?

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

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?

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.

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

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.

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.

## Answer

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 readily available.

If the map is distorted truly arbitrarily, there are lots of things you could try, but the simplest would probably be to compute a weighted average 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.

Some pseudocode:

```
estimate-latitude-longitude (x, y)
numerator-latitude := 0
numerator-longitude := 0
denominator := 0
for each point,
deltaX := x - point.x
deltaY := y - point.y
distSq := deltaX * deltaX + deltaY * deltaY
weight := 1 / distSq
numerator-latitude += weight * point.latitude
numerator-longitude += weight * point.longitude
denominator += weight
return (numerator-latitude / denominator, numerator-longitude / denominator)
```

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.

## Related Questions

- → Undefined Result for Variable with Javascript innerHTML function
- → Sine wave animation not occurring at expected origin
- → (Javascript) Take Three Integers From User to Display Sum, Average, Product, Smallest, and Largest Number
- → Convert base 10 and base 255 integer strings in JavaScript?
- → Which javascript function can be used to covert an array of characters into a number?
- → Creating mathematical charts using chart.js jQuery
- → Understand math done for endpoint in canvas arc
- → Matrix scale/translate from point
- → Getting input to round to integer value if result decimal is followed by a series of 9s
- → OctoberCMS - Is it possible to do a simple math in backend?
- → I want to make images appear according to mathematic results using javascript
- → JS, get the closest previous number from Array
- → is it possible to get ieee: float "bits" from javascript number?