# Longitude/Latitude to screen coordinates

1. Dec 25, 2008

### computerex

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. Dec 25, 2008

### HallsofIvy

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.)

3. Mar 29, 2011

### shlomi

hi.

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?

Shlomi.

#### Attached Files:

File size:
47.7 KB
Views:
191
• ###### earth3d.jpg
File size:
20.9 KB
Views:
196
4. Mar 29, 2011

### JaredJames

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.

5. Mar 29, 2011

### shlomi

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?

6. Mar 29, 2011

### JaredJames

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 http://mathforum.org/cgraph/history/greekmaps.html" 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.

Last edited by a moderator: Apr 25, 2017
7. Mar 29, 2011

### shlomi

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:
14.7 KB
Views:
491
8. Mar 29, 2011

### JaredJames

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.

9. Mar 29, 2011

### shlomi

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

thanks.

Shlomi.

#### Attached Files:

• ###### 11.gif
File size:
11.5 KB
Views:
315
10. Mar 29, 2011

### JaredJames

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?

11. Mar 29, 2011

### shlomi

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 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) [http://www.flashandmath.com/advanced/sphere/" [Broken]], 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..

thanks

Shlomi.

#### Attached Files:

• ###### 1.jpg
File size:
40.1 KB
Views:
181
Last edited by a moderator: May 5, 2017
12. Mar 29, 2011

### JaredJames

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.

13. Mar 29, 2011

### shlomi

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?

14. Mar 29, 2011

### JaredJames

You can't take a flat map and make it into a globe, not without distortion (badly).

15. Mar 29, 2011

### shlomi

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.)

16. Mar 29, 2011

### DrGreg

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

$$x = r \cos \theta \, \cos \phi$$
$$y = r \cos \theta \, \sin \phi$$
$$z = r \sin \theta$$​

where

$\theta$ is latitude (vertical on the flattened map)
$\phi$ 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.

17. Mar 29, 2011

### JaredJames

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 [Broken]

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.

Last edited by a moderator: May 5, 2017
18. Mar 30, 2011

### shlomi

DrGreg,

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?

jarednjames,
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 http://celestia.h-schmidt.net/earth-vt/unshaded1024.jpg" [Broken] image?? do i?

#### Attached Files:

• ###### 2dRegular.jpg
File size:
5.5 KB
Views:
171
Last edited by a moderator: May 5, 2017
19. Mar 30, 2011

### JaredJames

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.

Last edited by a moderator: May 5, 2017
20. Mar 30, 2011

### shlomi

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.

21. Mar 30, 2011

### JaredJames

No, that image is not designed to be wrapped around a sphere and it isn't distorted for 2D. The distortion only occurs if you wrap it.
No, the image I provided is designed to wrap around a sphere. If you expand a sphere that is what it looks like. The image I provided has no distortion. That's the whole point of it.
You don't need distorted images, you need an image along the lines of what I provided.
You won't get this. You'll need two seperate codes for mapping to each. It's easy on a flat map (X,Y) but a sphere requires a 3D grid (X,Y,Z).

You will have coordinates for a location, which you will plug into two functions. One function plots them on the 2D map, the other on the 3D globe.

22. Mar 30, 2011

### shlomi

but my client's engine know to take a distorted images and wrap a sphere with it, like you can see http://www.flashandmath.com/advanced/sphere/" [Broken], so the 'weird' picture you send me without distortion can't wrap my sphere. am i right?

Last edited by a moderator: May 5, 2017
23. Mar 30, 2011

### JaredJames

1. The 2D picture of the earth you have is not distorted. It will be if you wrap it.

2. The picture I gave is for you wrapping it yourself without distortion.

3. You could have mentioned you have software to do this for you.

4. The software wraps the image onto a sphere very well, but it distorts the image on doing so. This will make mapping coordinates very difficult - you will have to compensate for the distortion.

5. You can either use this software to wrap your globe and have severe distortion or you can take the image I provided and wrap it yourself and have no distortion.

Both will require some work on your part, coordinate mapping will have to be corrected for depending on which model you use.

Last edited by a moderator: May 5, 2017
24. Mar 30, 2011

### DrGreg

Having re-read this, I don't think you need to distort anything. Your flat 2D image has a square grid, longitude horizontal and latitude vertical, without distortion. Then use the software you've already got to get the 3D image.

(This process is illustrated at www.flashandmath.com/advanced/sphere/ideas.html and is what the equations I gave earlier do, but you don't have to worry about that, it's already been done for you. The "distortion" is already done, you don't have to do anything yourself.)

I think that's all there is to it.

25. Mar 30, 2011

### JaredJames

If used, the 'flat' 2D globe will be distorted severely at the poles.

Looking at the samples given by the OP you can see this clearly.

If the OP is happy to go with that then yes, mapping shouldn't be that difficult. However, it's going to look silly as Antartica (and like places) will be sized all to pot.