We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# Need to Convert x,y to lat lon ..... Found possible solution I think  ... VB.2008 Express

on
Medium Priority
1,590 Views
I found this by Arthur Wood but .... I don't either understand it or its not complete or applicable to my needs ..
I will put it after I explain what I am wanting.

I have an image of a map area ..... Image size .... 800 x 800

I know what the lat, lon of every corner is of the image .... based on the real world coordinate of that given place in the world ..... anyway ..

I just want to figure out how to convert x400, y916 which is at 81 ST & S Riverside Pkwy, Tulsa Ok which according to this site http://stevemorse.org/jcal/latlon.php?cookie=&hidden=&doextra=&time=1234397493406&addr2latlon=1&address=81+ST+%26+Riverside+Pkwy&city=Tulsa&state=OK&zip=&country=US&latlon2addr=0&latitude=&longitude=

The lat/lon for this x,y coordinate would be 36.0319208 -95.9919026 .....

ANyway, I think you might see what I am aiming for .... Now if I know the 4 corners lat and lon and the x,y coordiantes of each shouldnt I be able by thisdetrmine when I am at 400, 916 or lets say 588, 916  th lat and lon?

Thats my Q?  ...... Below is what I found on here posted by another Expert Arthur Wood .... but when I plugged in numbers I got back an x,y number that wouldnt relate to the x,y coordinates on my image ...

I may have done it wrong .... it seems to me it would also need to take into account the x,y relationship of the image on the Winform
Right?

I have searched high and lo for this .... with a lot o close calls but none show an example or point me to one ..

Thank You

Mark

-------------------------------------------- Lat2, Long2
|                                                     |
|                                                     |
|                                                     |
|                                                     |
|                                                     |
|                                                     |
|                                                     |
|                                                     |
|                                                     |
--------------------------------------------
Lat1, Long1

Lat - Lat1
X =      ---------------
Lat2 - Lat1

Long - Long1
Y =       ---------------
Long2 - Long1
Comment
Watch Question

## View Solution Only

Chief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
See Arthur's comment below the accepted solution:

http:/Q_22013975.html#17669496

What it appears to me Arthur is doing is representing your location as a fraction of the x and y coordinates of the UI.  In the linked additional comment, you will see that the next step is to multiply the fraction by the total number of pixels for x and y.  In your case they are both 800.

So for example, you have this:
X = (Lat - Lat1) / (Lat2 - Lat1) * 800 ==> your x positioning on the bitmap
Y = (Long - Long1) / (Long2 - Long1) * 800 ==> your y positioning on the bitmap
Chief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
I may be mistaken, but I took (Lat1, Long1) and (Lat2, Long2) to be the (x, y) coordinates corresponding to bitmaps placement on the winform.

Commented:
Ok, I am playing around with this and I will let you know how it turns out ...

My image size has changed .... I think though base on the results ... my lat and lon coordinates for the corners may b off a bit ....

When I com back to this later today I will re work this with variables I know will be 100% .. but I think this might be working .... my concern in this first test was that it was off enough that it concerns me a bit but then again that might b my fault as I was in a bit of a hurry when I got the lat and lon's

I wanted to be sure I am also doing this right in vb.2008 express ...

This is my first time with vb math .... I assume ... my variables should be double correct not just integer

forgive my ignorance there

Here is what I have let me know if its on track or if there is a better way to go here ...

``````XDim = 1792
yDim = 1394

mLat = 36.046375149261479
mLon = -95.965082645416274

lat1 = 36.013282878519043
Lon1 = -95.999565124511719

lat2 = 36.108062797830584
Lon2 = -95.845670700073242

x = (mLat - lat1) / (lat2 - lat1) * XDim
y = (mLon - Lon1) / (Lon2 - Lon1) * yDim

lblLat.Text = x
lblLon.Text = y
``````
Chief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Since you have decimals involved, yes either Double or Decimal data types would be more appropriate than Integer.

Commented:
Ok, I have reworked this with the given formula and I am going to show my results

The target x,y coordinate I was shooting for was;

x132, y212 <---- This is E 81st ST S & Riverside Pkwy Tulsa, OK on my map

After running the formula in my code ... it converts my Lat and lon to;
x322,y87 <-------Aprox 2 miles from location needed

Note: To make sure that the Lat lon I was using was not the culprit I used several sources to test their accuracy
For instance on of my sources was of course google ....
Googles result did not giv me 81st & Riverside ... well actually it almost did it put m at
8086 Riverside Pkwy which is just a matter of feet from target desire ... this would work :)
The other sources did about the same all showing my lat and lon's were dead on

So if I am looking at this right then the formula or my us of it is wrong ... you all tell me
Here is the code and test ... and I am going to look at a few others  to see if they might work so far nothing is putting me there

I could make the image available to if needed ...... but note I dont post it or show it because of copy right issues .... I am only using it to test with ... I am making my own map until then I am using what is convenient.
IMage Dimensions are now
640 x 425
``````Dim lat1 As Double, lat2 As Double, Lon1 As Double, _
Lon2 As Double, x As Double, y As Double, mLat As Double, _
mLon As Double, XDim As Integer, yDim As Integer

XDim = 640
yDim = 425

mLat = 36.046323101022757
mLon = -95.965061187744155

lat1 = 36.031470597983571
Lon1 = -95.97630500793457

lat2 = 36.060964648518294
Lon2 = -95.921287536621108

x = (mLat - lat1) / (lat2 - lat1) * XDim
y = (mLon - Lon1) / (Lon2 - Lon1) * yDim

lblLat.Text = x
lblLon.Text = y
``````

Commented:
Can you provide the lat long of the four corners? Is simple linear scaling from X,Y to Lat long not working?

Commented:
Yes, and welcome to the discussion ....

The 4 corners are all right here in the code .... located here
lat1 = 36.031470597983571
Lon1 = -95.97630500793457

lat2 = 36.060964648518294
Lon2 = -95.921287536621108

but, I will put them in order

TopLeft corner = lat 36.060964648518294 lon -95.97630500793457
TopRight corner = lat 36.060964648518294 lon -95.921287536621108
BottomLeft corner lat 36.031470597983571 lon -95.97630500793457
BottomRight corner lat 36.031470597983571 lon -95.921287536621108

OK, there they are and I am looking forward to your response ....thanx for joining in ....
oh and the target x,y as seen in the code above is at 132, 212
the real world lat & lon for that location is
XDim = 640   ' image dimensions
yDim = 425

mLat = 36.046323101022757 ' these represent 81st ST & Riverside, Tulsa OK
mLon = -95.965061187744155

Commented:
I think simple scaling would be fine.... Here is how to do it....

First calculate scaling factor in X and Y axis....

This can be done by using
delX= (TopLeft Longitude - Bottom Right Longitude)/(640)
delX= (-95.97630500793457 - (-95.921287536621108))/640
delX= 0.000085964798927284375 (Ignore the -ve sign)

similarly
delY= (TopLeft Latitude - Bottom Right Latitude)/425
delY=(36.060964648518294-36.031470597983571)/425
delY=0.000069397766

Now that we have delX and delY and Latitudes of Top Left corner so claculating any coordinates is very easy... Simply use the following formula

Latitude = TopLeft Latitude - Y * delY
Longitude= TopLeft Longitude + X * delX
Now as in your example you need to calculate the coordinates of 132,212

So Latitude=  36.060964648518294 - 212 * 0.000069397766 = 36.0462523
and Longitude = -95.97630500793457 + 132 *  0.000085964798927284375 = -95.9649577

and I think these are quite close to your required coordinates....

Commented:
Calculating delX and delY will be a one time job.
delX is basically the no. of degrees/pixel in x direction. i.e if you move 1 pixel in X direction your longitude will change by delX.
Similarly delY is Degrees/pixel in Y direction.

Commented:
Ok, I am convinced this is probably it .... I tested you numbers above in googl and it places me right where I want it to go ... way cool man ... However, when I plug in the variables in my code my results are different not much but enough

My results place me on the right street ... Lat is perfect but the Lon places me across the river .... about 9 blocks away to the west

SO I spent a couple of hours looking very number over and I found what is causing me to go to the west
It is is the Longitude result
Mine differs from yours ... in fact I can use my lat result along with your Long result and place them in google and it places me dead on where I want ....But for the life of me I cant figure out why I don't get the same answer you do  SO, I am posting my code for examination

Take a look at what my delX and delY results as to yours .... minor diffs but diffs ...
If I could get the Long to match yours I would be right on target ...... I know its got to be me but for the life of me I cant se what I am doing wrong ...

Mark

``````Dim XDim As Integer, yDim As Integer, delX As Decimal, _
tlLon As Double, brLon As Double, delY As Decimal, _
tlLat As Double, brLat As Double
Dim myLattitude As Double, myLon As Double

XDim = 640
yDim = 425

tlLon = -95.97630500793457
brLon = -95.921287536621108

tlLat = 36.060964648518294
brLat = 36.031470597983571

delX = (tlLon - (brLon)) / XDim
' delX = -0.0000859647989272849 but yours is 0.000085964798927284375
' mine ends with 849 yours 84375 min has a negative yous don't if that matters
txtLon.Text = delX

delY = (tlLat - brLat) / yDim
' delY = 0.0000693977659640536 NOT 0.000069397766 its like yours rounded

txtLat.Text = delY
Dim y As Integer = 212
myLattitude = tlLat - y * delY
Dim X As Integer = 132
myLon = tlLon + X * delX
txt1.Text = myLattitude
txt2.Text = myLon
``````
Commented:
If you use the -ve delX(like you are doing in your code) then you should change the formula for calculating myLon as
myLon = tlLon - X * delX
i.e change the + to - and you will get correct Longitude....

as far as the difference in delX and delY values is concerned , the difference is insignificant. You can simply ignore the difference....

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Yes, that did, and thus is my accepted solution ....

Even though I am going to accept this solution looking at the code again I was wondering how hard would it be to solve for x & y I mean we now can get the lat lon by knowing the x,y how could we get the x,y by knowing the lat lon?

Oh, ..... not sure how this works do I need to put this in a new question?

Anyway ... your solution worked great ...

Commented:
Great, I really appreciate the simplicity of your explanation ... thank you.

Commented:
X = (tlLon-myLon)/delX

Y=(tlLat-myLattitude)/delY
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile