Search code examples
algorithmmathtransformgeography

How can I transform latitude and longitude to x,y in Java?


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


Solution

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