Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Longitude/Latitude to screen coordinates

  1. Dec 25, 2008 #1
    Hello. I have a cylindrical Earth map of ratio 1:2. I also have a bunch of coordinates in longitude/latitude which I would like to plot. The problem is, how do I convert the longitude/latitude to screen coordinates (origin at the top left corner of the screen)?
  2. jcsd
  3. Dec 25, 2008 #2


    User Avatar
    Staff Emeritus
    Science Advisor

    A cylindrical map? Of course, you can't map a sphere onto either a cylinder or a plane without "stretching" and the closer to the poles you get the the worse that problem is with the poles themselves being mapped to the entire upper and lower edges of the cylinder or rectangle. I will assume you are not worrying about that and that latitude on your cylindrical map goes from 90 degrees north to 90 degrees south. Also I will assume that you just want to map it "linearly" onto the screen and that you want the right and left edges to correspond to longitude 180 W= 180 E.

    Now, about that "1:2 ratio". The map width (circumference of the cylinder) is twice the height? Do you want to keep that on the screen? That is, fill the screen horizontally but use only half that distance vertically rather that fill the screen vertically.

    Okay, here's what I'd do. Let MAXI be the width of the screen (i.e. pixels horizontally run from 0 to MAXI and the part of the screen used for the map vertically will be from 0 to MAXI/2. Longitude will run from 180 degrees East (-180) to 180 degrees West (180), Latitude will run from 90 degrees N (90) to 90 degrees S.

    For a linear map I must have i= Ax+ B where i is the pixel horizontally and x is the Longitude. When x= -180 (180 degrees East longitude) i must be 0 so 0= A(-180)+ B and when x= 180 (180 degrees West longitude) i must be MAXI so MAXI= A(180)+ B. We need to solve those two equations for A and B. Adding the two equations cancels the "A" terms and gives MAXI= 2B so B= MAXI/2. Subtracting the two equations cancels the "B" terms and gives MAXI= 360A so A= MAXI/360.

    The horizontal pixel value for longitude x is i= (MAXI/360)x+ MAXI/2= MAXI(x/360+ 1/2).

    Same idea vertically. j= A y+ B. You want 90 degrees North, y= 90, to correspond to j= 0 so 0= A(90)+ B. You want 90 degees S, y= -90 to correspond to j= MAXI/2 so MAXI/2= A(-90)+ B. Again, adding the two equation eliminates A and gives MAXI/2= 2B so B= MAXI/4. Subtracting eliminates B and gives -MAXI= 180A so A= -MAXI/180.
    The Vertical pixel, j, corresponding to latitude y is j= (-MAXI/180)y+ MAXI/4= MAXI(-y/180+ 1/4).

    Putting those together, the (i, j) pixel position corresponding to longitude x and latitude y is given by
    i= MAXI(x/360+ 1/2), j= MAXI(-y/180+ 1/4).

    (The "-" in front of y is because we are reversing orientation. On the map, latitude increases from south to north, on the screen the pixel count increases downward.)
  4. Mar 29, 2011 #3

    I'm a programmer and working on a game that is a planet with users on it.
    the game have 3 levels of view:
    1. 2d view - users grids
    2. 2d view - textures
    3. 3d view - sphere

    i already mapped the planet by longitude (horizontally, (-180)->180, left to right) and latitude (vertically, 90->(-90), top to bottom).

    everything is working fine, mean, when i populate a user grid on some specific coordinate i can see him on the 3 levels, in the exact place it's should be.

    level 3 view is a rectangular bitmap (see earthFor3d.jpg) that by some transformation it's cover the sphere (see earth3d.jpg).

    what i understood is that in level3 image (see earthFor3d.jpg), the only realistic location (with 360 longitudes units) on it is just the line of the equator (from (-180) to 180), but when you're going up or down (mean - changing latitudes) your longitude range start to reduce till you arrived to the poles, in the poles you should be on [90 lat, 0 long] in the top pole, or [-90 lat, 0 long] in the bottom pole.

    since this is the first time in my life i'm dealing with mapping of sphere, i want to ask if i understood it correctly?

    thanks in advanced.


    Attached Files:

  5. Mar 29, 2011 #4


    User Avatar
    Gold Member

    If you travel from North to South in a straight line, your longitude doesn't change but your latitude does.

    If you travel East to West in a straight line, your latitude doesn't change but your longitude does.

    It's easier if you look at the map here: http://www.satsig.net/lat_long.htm

    It shows (and explains) how the values change as you move around. Follow the lines and you'll see how the above works.
  6. Mar 29, 2011 #5
    sure.. this is what i said: "..but when you're going up or down (mean - changing latitudes)"
    my wondering is: is the range of 360 ((-180) to 180) longitudes saved 360 or reduce as much as i go south or north?
  7. Mar 29, 2011 #6


    User Avatar
    Gold Member

    No, it's not what you said. Here is what you put (bolded is section I'm referring to):
    Incorrect. Everywhere up to the precise point of the north pole has all 360 degrees of longitude available. (It's always a circle around the globe).

    See here for a globe with lines marked on it. Remember, longitude is horizontal (x-axis), latitude is vertical (y-axis), but horizontal lines mark latitudes, vertical lines mark longitude.
    Incorrect - as above.
    Only when precisely on the pole at an infintely small point, otherwise you always have a value of longitude - but this is largely semantics.
    No, you always have 360 of longitude wherever you are on the planet (minor note above aside).

    Think of it like this, if the full 360 weren't there, it wouldn't be a complete circle around the planet would it?

    The difference is that travelling one degree of longitude on the equator could mean 100km, but doing so near the poles could only be 1m. They simply get closer together.
  8. Mar 29, 2011 #7
    i don't understand.
    the longitude range (from (-180) to 180) should be longer than the latitude range (from (-90) to 90), in the attached images (1.gif) from the article you sent me seems like the latitude's range is longer.

    Attached Files:

    • 1.gif
      File size:
      22.4 KB
  9. Mar 29, 2011 #8


    User Avatar
    Gold Member

    The longitude range is greater, you are correct.

    Both have lines that go a full 360 degrees around the earth.

    Latitude is referenced to the equator, you can go 90 degrees up from the equator to get to the north pole or 90 degrees down to get to the south pole.
    Now, when you get to the north pole and start "coming down" the other side, the degrees reduce back to 0 on the equator, giving you a maximum of 90 degrees north or south.

    Longitude starts at a line called the Grenwich Meridian (0 degrees) and extends 180 degrees east and west (meeting on the opposite side of the planet).
    So, you can travel 180 degrees west from 0 degrees, but once you get to the other side you start coming down from 180 degrees east. (Exactly the same as with the north example above.)

    Longitude gives you two 180 degree divisions which total 360 degrees, latitude gives you four 90 degree divisions which total 360 degrees.
  10. Mar 29, 2011 #9
    wow.. no it's clearly..

    but now i have another question:
    in my 1st post here, i mentioned that i have different view levels: the 2d textures level and the 3d texture level.
    the 2d is rectangle that devides to coordinates as you can see on the attached (11.jpg), this mean that i have longitudes from (-180) to 180 horizontally and latitudes from (-90) to 90 vertically.
    i marked couple of points on the image (red, green and blue) that i can't imagine where those points will show on the 3d sphere, since this rectangle should wrap the sphere and when you covering ball with rectangle some of the area is disappear.. which area (on the 2d rectangle) will appear on the sphere and which isn't?

    hope you understood me..



    Attached Files:

    • 11.gif
      File size:
      11.6 KB
  11. Mar 29, 2011 #10


    User Avatar
    Gold Member

    I understand there's a language barrier, are disagreeing with me or saying it's now clear?
    This is a classic cartography problem. You can't wrap it around a globe and you can't expand a globe to flat - without distortion.

    If you expand a globe to be flat, Greenland becomes the same size as Africa - so you get some pretty big problems (EDIT: no pun intended).

    Now, I'm not sure how good you are with programming, but what I'd recommend is handle one coordinate at a time.

    Before I continue, do you have a 3d globe animation? Or how are you generating the 3d image?
  12. Mar 29, 2011 #11
    oh.. i forgot the 'w', it's should be "wow.. now it's clearly.."
    BUT BE AWARE - I'm an Hebrew speaker so - as you can see - my English is not so fluent (saying the least). :)

    about your questions:
    about programming, I'm 4+ years programmer but in the games field just in the last year so it's a little bit new. my basis is web sites and apps so the whole geometric aspects are new (except the things i learn on the Academy)

    what do you mean by 'handling one coordinate at a time'?

    i'm using flash to create and wrap the 3d sphere with given JPG/PNG image (like 1.jpg) [using this], but i need to render this image from time to time, because the users making changes on them grids. [this process is corresponding the NASA (for instance) re-picturing of the earth to update them maps]
    my problem is to understand how i can take a 2d images (rectangle that it's height is half of it's width - like 1.jpg) and put the users data on it and do all of this with the distortion you talked about.

    hope you got me..



    Attached Files:

    • 1.jpg
      File size:
      40.4 KB
  13. Mar 29, 2011 #12


    User Avatar
    Gold Member

    Put simply, you can't.

    The nasa images (Google Earth etc) are not the same and don't wrap flat images.

    An image taken from space (or the air) already has the curvature built in - they're taking pictures of the earth with the curvature. So all you do is take a lot of pictures and stick them together.
  14. Mar 29, 2011 #13
    so if i want to create image like 1.jpg but with my own data on it do you meant i can't generate it?
  15. Mar 29, 2011 #14


    User Avatar
    Gold Member

    You can't take a flat map and make it into a globe, not without distortion (badly).
  16. Mar 29, 2011 #15
    sure.. the distortion is required anyway but i'm trying to find a solution to make a regular image and then distort it that it'll be proper to the 3d wrapping.
    as i understood, it's should be distort above and below the equator.. but it's should be with some mathematics function (something like hyperbola etc.)
  17. Mar 29, 2011 #16


    User Avatar
    Science Advisor
    Gold Member

    I think what you asking for is the spherical polar coordinate transformation

    [tex]x = r \cos \theta \, \cos \phi[/tex]
    [tex]y = r \cos \theta \, \sin \phi[/tex]
    [tex]z = r \sin \theta[/tex]​


    [itex]\theta[/itex] is latitude (vertical on the flattened map)
    [itex]\phi[/itex] is longitude (horizontal on the flattened map)
    x is depth (so you plot if x>0 and ignore if x<0)
    y is horizontal screen position, leftwards from centre
    z is vertical screen position, upwards from centre
    r is the Earth radius, in pixels​

    This assumes the Earth is a perfect sphere (really it's slightly ellipsoidal), and that we are viewing from directly above lat 0 long 0, and we are ignoring the tilt of the Earth's axis, and doesn't account for half of the globe being in shadow. If you want to change these assumptions, there's more work to do.
  18. Mar 29, 2011 #17


    User Avatar
    Gold Member

    Just a thought that occured to me, you can get maps which are 2D of the earth that are exploded views of the earth. If you could get one of those, you could wrap it with little to no distortion.

    I can't remember the name now, I'll have a look.

    Here's an example: http://star.pst.qub.ac.uk/idl/images/map_goode.gif

    In 2D it looks ridiculous, but once 'folded' around a sphere it would align nicely.

    Apply something like that with DrGreg's fantastic input above and you should be away.
  19. Mar 30, 2011 #18

    i'm try to understand from what you said how can do the 2d images rendering? i have a data that stored on the DB, the data is the objects that each user have on his grid, i need to run on this data and draw it on some 2d image, after that i need to make the distortion so that it'll wrap the 'naked' 3d sphere i have.
    the part of rendering regular 2d images it's already done (see 2dRegular.jpg), as i understood the data that surround with black circle (on the equator area) should looks the same (without distortion) and data on the red's one should distorted, now- from your explanation, how can i make this distortion to this image?

    did you meant that the first rendering of my images will looks like the images you attached, and after that what? i need to distort it to something like this image?? do i?

    thanks you guys for your answers.. it's helping me a lot..

    Attached Files:

  20. Mar 30, 2011 #19


    User Avatar
    Gold Member

    No, you need to use an image like the one I provided you to wrap around a sphere.

    The image I gave you is designed to be wrapped around a sphere - the pieces missing from it are where the distortion occur during wrapping.

    Once you've wrapped it, you must then place the 'people' on it. You can't place the people on the flat image and then wrap it easily.
  21. Mar 30, 2011 #20
    as i understood.. the image i sent before (earthFor3d.jpg) it's designed to wrap the sphere, this is why it's distorted.
    the image you sent it's just an illustrating of how the sphere looks like when you spread it without the distortion but it's not helping me since i need to work in the both level:
    1. distorted images to wrap the sphere.
    2. sets of 2d textures that i need to know which locations on them are valid against the sphere.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Longitude/Latitude to screen coordinates