Link to home
Start Free TrialLog in
Avatar of jloring
jloring

asked on

Convert Latitude/Longitude values to x,y on a map

Hi, desperate for some help.
I need to place red dots on a map of the United States. The dots represent the locations of several companies, universities and cities on a map of the U.S. We're using Macromedia Flash to display the map and plot the dots. Obviously we can do this manually using an external file, but long story short this data is kept in a database and will change frequently and without warning.

I only have the the latitude and longitude of these universities/companies/cities. I need to convert the latitude/longitude to x,y coordinates on the map. Once I have the x,y plotting them on the map is simple.

I have a map I obtained from http://nationalatlas.gov/natlas/ so I trust it's accuracy. I know the longitude/latitude of the 4 corners of this map. I have been playing with various formulas to no avail. I'm close, but I'm not close enough.

http://www.graphiteproductions.com/mapping/flash/map.php

Much thanks.
Jennifer.
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

http://nationalatlas.gov/natlas/ is a privelaged site (I can not view it) the relationship depends on the projection used for the map.
Avatar of jloring
jloring

ASKER

Try this link: http://nationalatlas.gov/natlas/NatlasStart.asp
This should work great. It's an amazing resource for generating maps.
Avatar of jloring

ASKER

I am being told that I'm using a conic projection, but I need to use a spherical projection so that the lines of longitude and latitude are parallel and perpindicular. This makes a lot of sense, but where could I find such a map online?

If I have such a map, what formulas could I use?
The equations for Mercator projection are

http://geography.uoregon.edu/buckley/teaching/geog311/lectures/lecture3%20-%20map%20projections/sld007.htm

is the Map the continental USA?. The problem with no-global maps is that some times local appoximations to the earth in terms of an ellipsoid is used so accurate conversion of long/lat to XY  can be messy. I would find the map you like and then concentrate on working out the conversion.
Avatar of jloring

ASKER

Yes, the map is the continental United States.

Here is the working model I have developed using a conic projection map:
http://www.graphiteproductions.com/mapping/flash/map.php

I think I would better off using a Spherical projection map of the continental U.S. But I don't know where I can find one that also has the lat/long displayed on it.
OK, since you are "desparate" here is a way that will work for you to convert a given lattitude and longitude into the corresponding x, y coordinate values on your map.  The idea is to set up a table that links the lattitude and longitude coords to the x-y coords.

Print your map and sketch in additional lattitude and longitude lines to increase the detail.  You can trace them "parallel" to the official ones.  Then make a list associating each longitude-lattitude pair with the corresponding x-y pair.  

Write your program to do a look-up on this table.  For more precision, do interpolation to get a better x-y for lattitude-longitude that falls between items in the list.

You can increase the precision you get by managing the detail in your table of lattitude-longitude to x-y and by using interpolation to handle in-between values.

Obviously this isn't perfect.  But perfection is not usually the best way to solve an urgent real-world problem.  Please, let me know if you find this useful.
SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

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
I have'nt thoroughly tested it , it is a simple blinear interpolation.
Now got the x coord spot on with

x=1238.3124 - 9.7689*lon

working on the y
This is as good as I can get it  for the map http://www.graphiteproductions.com/mapping/flash/map.php
( i am not sure how accurate your lat data is)

y=632.98168 - 9.07797 * lat - 0.06291* lat*lat
x=1238.3124 - 9.7689*lon
You might want to try y=717.784412711722 - 13.74154357099229 * lat, I am not sure but I think San Francisco Latitude data is very slightly off
Avatar of jloring

ASKER

Gwyn, this is very close to a formula I've been working with myself (as a non-mathematician). I will try your variation right now. I have switched from a conic map projection to a spherical (like a Mercator map). There still seems to be some distortion to the map, but it's definitely better than working with a conic map.
Thanks. Jennifer.
If you exclude the SF y/lat data this pair is within a fraction of a pixel

x=1238.3124 - 9.7689*lon
y = 715-13.7*lat
If you post a larger well distributed (x,y) (lon,lat) data set I can data fit a curve to them for you.
ASKER CERTIFIED SOLUTION
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
Hey I've had the same problem here I posted this question:
https://www.experts-exchange.com/questions/20862516/Dynamic-map.html

This seems to be what I need to get the x and y just where would I place any of these formulas to get the results? In a calculated field in DB or in an asp page or in flash? I'm just lost but it looks like the answer, mine can be a few pixles off I just need a display
Avatar of jloring

ASKER

The map I have (or will have) is going to be a Mercator (Spherical) projection. This way I don't have to deal with the curvature of the Earth crap and introduce trigonometry into my formulas.

Everything suggested by everyone gets me very, very close. For some placements I am dead on, but for others I'm almost 10 pixels off (x and/or y). It's very, very frustrating. Of course, it might just be the quality of the map I'm using too. I wish I could find a 100% qualified map to work with.
I think it is just the offset if u use my comments above i have quoted some offset, jus add it and your work is done. I think that 10 pixels or anything will remain constant throughout the map. For x & y axes both, the value can be same or different.

Cheers!
Avatar of ozo
Avatar of jloring

ASKER

Hey everyone,
I've finally got this working using a very simple set of formulas (in PHP). The biggest battle is getting an accurate Mercator map and knowing your map's endpoints. Here's the map solution we used (you can save the output as an Adobe Illustrator file):
http://www.aquarius.geomar.de/omc/make_map.html

I created a map using the above tool with the following endpoints: N: 53, E: -66.54, S: 23, W: -125.49
This creates a nice Continental US & partial Canadian map. Remember to check the "download" box if you want to save the output as a Illustrator file!

I imported the map into Macromedia Flash MX. The map is 600 x 396 (pixels). Here's the working model on my Web site:
http://www.graphiteproductions.com/mapping/flash/map.php

Flash reads a flat text file containing the following result sets (as example):
totDots=10
&nam1=SF State
&loc1=San Francisco, CA
&posx1=33.18
&posy1=226.97
&link1=http://www.sfsu.edu/

&nam2=Wayne State
&loc2=Detroit, MI
&posx2=432.46
&posy2=162.752
&link2=http://www.wayne.edu

&nam3=University of Texas
&loc3=Austin, TX
&posx3=282.34
&posy3=314.289
&link3=http://www.utexas.edu/
...

You get the idea. You could use PHP to obtain this from a database dynamically rather than use a flat text file, if you choose.
Generating the x,y values from long/lat is, of course, the real trick.

Here's how I derived the y coord:
function findYcoord($myLat) {
      $mapHeight = 396;
      $rfactor = 290;
      $radBtm = deg2rad(23);
      $radPixel = deg2rad($myLat);
      $sinRadBtm = sin($radBtm);
      $sinRadPixel = sin($radPixel);
      $convHtBtm = $rfactor * log((1 + $sinRadPixel)/(1 - $sinRadPixel));
      $convHtPixel = $rfactor * log((1 + $sinRadBtm)/(1 - $sinRadBtm));
      $myTotHt = abs($convHtPixel - $convHtBtm);
      $myYcoord = round($mapHeight - $myTotHt, 3);
      return $myYcoord;
}

NOTES:
You simply pass in the latitude value, which is later converted to a radian.
$mapHeight is the height, in pixels, of my map in Flash.
$rfactor is a constant variable. It corresponds to your map scale. You might have to tweak this value up or down a bit (but it worked for me at 290).
$radBtm = deg2rad(23); the value "23" is the latitude at the bottom of my map.

Getting the x value is much, much easier:
$x =round(((125.49 - $lon)*10.178), 2);

NOTES:
125.59 is the longitude at the top left corner of my map.
$lon is the longitude for the city I want to plot
10.178 is a constant that is based on the width of the map, it's calculated as follows:
longitude West - longitude East / width of map
125.49 - 66.54 = 58.95
600 / 58.95 = 10.178
Let me put it another way: every 10.178 pixels on your map equals one degree of longitude.

Here's how I plotted Detroit:
Detroit, MI<br>
Longitude: 83<br>
Latitude: 42.24<br>
<?php
$lon = 83;
$lat = 42.24;
$x =round(((125.49 - $lon)*10.178), 2);
print "x: ".$x."<br>";
print "y: ".findYcoord($lat);      
?>

Works like a champ, I swear by the results and it's far less complicated or confusing than any other solution I've seen thus far.
Thanks again to everyone.
David and Jennifer.