views:

2096

answers:

3

If i have a jpeg map with size sizeX, sizeY

and some calibration points on the map (X, Y, Lon, Lat)

What would be the algorithm for calculating the corresponding XY point in the map with a given Longitude / Latitude pair?

+1  A: 

There is plenty of information on the Internet about calculating the distance between two pairings of latitude and longitude. We're using those calculations on our public website and they are not trivial to understand/discuss (so I won't try to cover them here). That said, they are easy to implement.

Once you have a function that returns distance, you should be able to caculate the width and height of the map in terms of distance between the corners.

Then you can calculate the horizontal and vertical distance of your point from the top-left corner.

Now you find out what ratio of the map's width is represented by the distance between the left side and your point, apply that ratio to the pixel width and you have the number of pixels between the left side and your point. Repeat for the y-axis.

(Pixels from left side) = (total width in pixels) * ((geocode distance between left and your point) / (geocode distance between left side and right side))

(Pixels from top) = (total height in pixels) * ((geocode distance between top and your point) / (geocode distance between top and bottom))

EDIT: As you research this further you will note that some solutions will present more accurate results than others due to the fact that you are approximating distance between two points on a spherical surface and mapping that on a flat surface. The accuracy decreases as the distance increases. Best advice to you is to try it out first and see if it meets your needs.

Mayo
Maps project our 3D on to a 2D surface. Since you can't perfectly wrap a square onto a sphere, there are different projection methods to get an approximately correct result. The distances on a given map are non-linear, so your formula is only an approximation.
Eric J.
Approximations work for most distances. But I should clarify that in the post.
Mayo
Yep, the more zoomed-in your map, the more accurate the result. You should see almost zero difference for a city-level map, but it will be noticeable for a state-level or larger map.
Eric J.
+3  A: 

There are many different map projection schemes. You would have to know which one(s) are used by your maps.

For more information about map projection algorithms and forward/reverse mapping check out this link. It provides the formulas for a number of common projections.

Eric J.
I don need much accuracy so maybe the solution below is enough. If it's not, how would the transformation be? Which are those projectino schemes? So far I've seen:UTMTransverse MercatorMercatorLongitude/Latitude
Jorge
BTW. I've also coded the formulas for converting among Lat/Lon and UTM. Is converting from Lat/Lon to UTM and applying the solution below the right way (at least for UTM projections)?
Jorge
Are your maps using Mercator? You need to know what the maps are coded in before you can pick the right projection. Once you have the geocoded distances right, mmayo's formula gives you the correct pixel on the map.
Eric J.
I have 2 kinds of maps. Ones with Lat/Long Projection and other ones with UTM projection. If im not mistaken, with the first ones i can use mmayo formula and with the second ones i should convert Lat/Lon values to UTM before applying the formula. Am i right?
Jorge
A: 

If using the Equidistant Cylindrical Projection type map, here is what you need to do:

  1. Find the Latitude and longitude of your location tutorial here:
    http://lifehacker.com/267361/how-to-find-latitude-and-longitude
  2. Input that information into the following formulas:
    x = (total width of image in px) * (180 + latitude) / 360
    y = (total height of image in px) * (90 - longitude) / 180

    note: when using negative longitude of latitude make sure to add or subtract the negative number i.e. +(-92) or -(-35) which would actually be -92 and +35

  3. You now have your X and Y to plot on your image

    More information can be found about this formula and the map type here:
    *http://www.progonos.com/furuti/MapProj/Dither/CartHow/HowER_W12/howER_W12.html#DeductionEquirectangular*
Anthony Master