Link to home
Start Free TrialLog in
Avatar of baz86
baz86

asked on

Converting X, Y to Lat, Long

Hi All,

I am creating a golf scorer for a windows mobile 5 device and used some of the posts from the following thread to use for the conversion of x,y to lat and long co ordinates:

https://www.experts-exchange.com/questions/24135663/Need-to-Convert-x-y-to-lat-lon-Found-possible-solution-I-think-VB-2008-Express.html?sfQueryTermInfo=1+10+convert+lat+lon+x+y

The piece of code in particular which i am trying to get working for my cropped image is :
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
       
        txtLon.Text = delX
 
        delY = (tlLat - brLat) / yDim
       
        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

For the life of me, I cannot seem to get it working for any of the course layouts (which are all cropped images)

i will post what my version of the code looks like and as you will find, the lat long co ordinates are not miles off but very wide off the mark in terms of accuracy. If anyone can have a look through the coding and find where i am going wrong i would be eternally grateful!!

the picturebox is a 210x806 dimensioned control

the bitmap has been cropped and 'straightened' but the lat and long of the two corners are for the final image so to speak.

the x and y valye of 58 and 95 respectively is the location of the Green and when i get the co-ordinates from the calculations, its quite a way off.

i will be giving the full 500 points for this as i have spent along time trying to look into this and try to recalculate it but to no avail.

thanks in advance

Baz
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 = -1.136182
        brLon = -1.133490
 
        tlLat = 52.668572
        brLat = 52.666803
 
        delX = (tlLon - (brLon)) / XDim
       
        txtLon.Text = delX
 
        delY = (tlLat - brLat) / yDim
        
 
        txtLat.Text = delY
        Dim y As Integer = 95
        myLattitude = tlLat - y * delY
        Dim X As Integer = 58
        myLon = tlLon - X * delX
        txt1.Text = myLattitude
        txt2.Text = myLon

Open in new window

Avatar of baz86
baz86

ASKER

Correction to code snippet above:
xDim= 210
yDim = 806
Avatar of baz86

ASKER

The Lat and Long co-ordinates i should be getting are 52.668575lat and -1.135770long
Avatar of baz86

ASKER

The formula works fine for the first hole:
Xdim=210,
Ydim=908,
TLLong = -1.133241666
TLLat  = 52.6674027
BRLong = -1.134216666
BRLat = 52.6703861
 
But as soon as i enter he details from the database for the second hole (and anyh hole thereafter, the co-ordinates are wrong).
Avatar of baz86

ASKER

Can anyone please help?
Avatar of oobayly
When you say you want to convert x, y to Lat, Lon, you need to chose what projection to use, as mapping from spherical coordinates to planar coordinates isn't as simple as the code you have provided. Basically you're not taking into account the the distance between meridians changes you move away form the equator.

The simplest project to use would be Sinusoidal, which is an equal area projection.
http://en.wikipedia.org/wiki/Sinusoidal_projection
As you're dealing with only a small area, you wouldn't see the kind of deformation you see in the wiki images.

I'll dig out some code I wrote a while ago for generating aviation charts that should do proper mapping for you.
Avatar of baz86

ASKER

oobayly thanks for your reply and apologies for my delayed one!
would you be able to advice how i can alter my code so that i can rotate it and still having the co-ordinates be correct?
I may have been a bit hasty saying that you needed a non-linear transform. You're dealing with such small areas that it shouldn't be an issue using the linear transform you're using.

Where is the x, y origin, Top-left corner (like a System.Drawing.Graphics object), or Bottom-left?
From your code, I assume it in the bottom-left corner.

For the coordinates you gave in the question, there's no way that area can fit in an image 210x806 as the aspect ratios are completely different.

For the 2nd set of coordinates and the image shape of 210x908 the aspect ratios are similar.

I'm using Google Earth to get get an idea of what you're looking at, so your images will be slightly different.

Can you upload a sample image, saying what the lat/lon position of a certain point on the image is.
Avatar of baz86

ASKER

the origin for the picturebox is top left corner at (0,0)
and the picturebox resizes to the dimensions of the bitmap automatically one the form is loaded......
what i found is that when the image is rotated then the formula doesnt give the correct co-ordinates..ie Pic1
but when the image is not rotated and North is true north, the formula gives the corrrect co-ordinates. ie Pic2
the problem i have with this(Pic2) is that im creating an application for a windows mobile device and having Pic1 would only cause me to have a vertical scrollbar on the panel which i can live with, but Pic2 would have the inconvenience of vertical and horzontal....

Untitled-4-copy.bmp
test1unmoved.bmp
Avatar of baz86

ASKER

apologies for the enlarged pictures!!! didn't realise them to be so large
Avatar of baz86

ASKER

an example of this:

for Pic1...point(107,87) on a 210X860 bitmap is the bright white bunker near the green...this results in the lat long of (52.6686080837,-1.1348202380) which places the mark 60 yards to the right.
for Pic 2...point (84,98) on a 413X678 is the bright white bunker right near the green....this results in the lat long of (52.6685142094,-1.1354876101) which is correct.
 
 
ASKER CERTIFIED SOLUTION
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland 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
Was my code any help?