views:

286

answers:

2

Hello,

I am working on a geographic project in Java.

The input are coordinates : 24.4444 N etc Output: a PLAIN map (not round) showing the point of the coordinates.

I don't know the algorithm to transform from coordinates to x,y on a JComponent, can somebody help me?

The map looks like this: http://upload.wikimedia.org/wikipedia/commons/7/74/Mercator-projection.jpg

Thank you

+1  A: 

Given your sparse example, the range of your inputs will be (90.0N - 90.0S) and (180W - 180E). It is easiest - and standard - if you convert South and West to negatives giving you latitudes of (90.0..-90.0) and longitudes of (180.0..-180.0).

Given the size of your canvas - let's say it is 140x120 pixels - you get:

x = (latitude * canvas_height / 180.0) + (canvas_height / 2)
y = (longitude * canvas_width / 360.0) + (canvas_width / 2)

or:

x = (longitude * 120.0 / 180.0) + (120/2)
y = (latitude  * 140.0 / 360.0) + (140/2)

where I have ordered the operations to minimize rounding error. This assumes the canvas has point (0,0) in the upper-left or, if not, that you are Australian.

Added: you just threw in the bit about Mercator projections making my simple answer incorrect (but possibly still usable by you if you don't actually care about projection)

msw
Yes, there is a domain overlap at the anti-meridian, but I don't think the OP will be plotting many points in suburban Tavuki, Fiji, and even Google Earth gets a little wonky there.
msw
This isn't a mercator projection, which is what his map uses.
Nick Johnson
whence the **Added** part above...
msw
I don't exactly need the Mercator. Can you suggest me a flat map which I can use for you algorithm ?
HJ-INCPP
http://commons.wikimedia.org/wiki/File:BlankMap-World-162E-flat.svg Please note that flat projections like this are hardly ever used and so look funny. Also note that it is centered - like it says - on 162E which would have to be taken into account in the calculations.
msw
A: 

MSW provided a good example. Ultimately the algorithm depends on the map projection used. Here are a couple of good resources I used in the past.

The following link is a good reference to a number of different map projections with enough math formulas to choke a horse.

http://www.remotesensing.org/geotiff/proj_list/

Here is a decent reference for doing this in PhP. While not Java, it should be straight forward enough to apply the principles outlined.

http://www.web-max.ca/PHP/article_1.php

Bill