Link to home
Start Free TrialLog in
Avatar of stanis
stanis

asked on

Convert between long,lat and x,y using Miller map projection

Hi!

I am working on a small application which will display two cities on a map given the cities' longitude and latitude. As recommended by other people, I first chose the acceptable map projection (Miller Cylindrical) and then found its formula:
(the Miller projection formula is at http://mathworld.wolfram.com/MillerCylindricalProjection.html)

x = long - long0;
y = 1.25 * ln[tan(0.25 * Pi + 2/5 * lat)];

For calculations, I will use the PHP scripting language. X doesn't have to be converted, so here's the PHP code for Y:
$y = 1.25 * log( tan( (0.25 * pi()) + (0.4 * $lat) ) ); // $lat is latitude

However, when calculating y for lat=60.2 (or any number in degrees), I get an error in PHP scripting language (-1.#IND), which I guess has something to do with me supplying a bad formula or out-of-range data. Am I doing everything correctly here? I am not very strong in maths (forgot lots of it over the years), so I will appreciate any pointers on what to do in this situation. I think that I may be thinking in different units here (maybe I should use latitude in radians instead of degrees), but I am really confused.

Thank you very much for all your help!!!

Best regards,
Stanislav
ASKER CERTIFIED SOLUTION
Avatar of SethHoyt
SethHoyt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SethHoyt
SethHoyt

Also, keep in mind the value of lat must be between -5/8 * Pi and +5/8 * Pi (exclusive) or the logarithm may fail, since its argument must be positive.

Those angles correspond to +/- 112.5 degrees, so just be sure lat stays within those values. This should not present a problem, since latitude is always between -90 and +90 degrees anyway.

-Seth
Avatar of stanis

ASKER

Thanks for the answer, Seth!

This does eliminate the error in my code, but for latitude = 60.2 degrees, I get Y = 1.20205840109. What units is this in? Can you give me a few hints?

Thanks a lot!

Stanislav
This is just a y coordinate for plotting. It is only relative to the limits of the output. The best thing for you to do is to plug in +/- 90 degrees and observe the y values (around +/- 2.3). These are the y coordinate limits, and you may want to divide your output by 2.3034... so the outputs stay within +/- 1. Then you can scale those by whatever factor you wish.

-Seth
Avatar of stanis

ASKER

Thanks, Seth!

It's quite clear now. I now need to make sure that the formula works. I will test its correctness over the weekend. I have also being studying the source code of an open-source GIS project, so this will be a backup plan if the formula fails.

Best regards,
Stanislav
You're welcome, Stanislav.

One other thing, when I said you can re-scale the coordinates, I should tell you that you need to scale both x and y by the same factor. The formula is scaled so that near the equator, the value returned is approximately equal to the angle input (in radians). You'll notice that if you input an angle of 0.01 radians, the output is very close to that.

The reason for this is so that there is no distortion at the equator. The distortion will increase the further one goes from the equator. This also means that when you specify the longitude, it should be in radians as well. Then you can think of the outputs x and y as having units of radians. Any scaling you apply to one should be applied to the other.

This also tells you what the final aspect ratio will be like. Since x can range from -Pi to Pi, and y ranges between +/- 2.3034.., the full world map will be about Pi/2.3 = 1.36 times wider than it is high. That ratio is fixed based on the formula, and can't change without distorting the map or modifying the formula.

Keep in mind that this projection is only really useful for displaying the entire globe, or regions near the equator. If you wanted to display only the U.S. for example, there are better projections to minimize the distortion in just that region.

-Seth
Avatar of stanis

ASKER

Hi again, Seth!

Thanks for your last key comment. I made two mistakes which misled me to believe that the formula I had gotten my hands on was wrong. First, I tried to find the multiply factor by using coordinates in a map where the origin of the file (from which all coordinates are measured) was not at the intersection of the equator and the prime meridian. So, even when I tried to apply a specific factor to a different city for longitude which is linear on cylindrical projections, I ended up with weird results all the time. And second, I used two different factors for Y and X, instead of using one in both cases (X in this case). The minute I fixed both things (thanks to your final comment), everything started to work. Thanks a lot!!!

Best regards,
Stanislav