Liberty BASIC Community Forum
« KML to pixel to LAT and LON / How to »

Welcome Guest. Please Login or Register.
Apr 20th, 2014, 8:59pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: KML to pixel to LAT and LON / How to  (Read 417 times)
ssergiu
New Member
Image


member is offline

Avatar




PM


Posts: 10
xx KML to pixel to LAT and LON / How to
« Thread started on: Feb 7th, 2012, 7:38pm »

What mathematical formulas I should use to a loaded KML file saved from Google Maps (as JPG) in to a graphic window, then by measuring pixels (by click a point in picture with mouse) to transform that WP(x,y) back to Lat and Lon ? Basically, what I need is to get Lat and Lon of any point from picture by LMB click.
Any help will be appreciated.
Thank you,
Sergiu
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 4146
xx Re: KML to pixel to LAT and LON / How to
« Reply #1 on: Feb 8th, 2012, 02:55am »

From a quick browse of the KML file spec I see that they define the view with a LatLonBox.

<LatLonBox>
<north>48.25475939255556</north>
<south>48.25207367852141</south>
<east>-90.86591508839973</east>
<west>-90.8714285289695</west>
<rotation>39.37878630116985</rotation>
</LatLonBox>

So that should define the limits of the view. Then you would need to interpolate between pixels and these limits. Basically the total degrees east-west are divided by the number of x pixels to give you the number of degrees per pixel. So too for the north-south.

Providing a real example, the image and the LatLonBox data, might prompt someone to code a working example.
User IP Logged

Jim Hiley
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 130
xx Re: KML to pixel to LAT and LON / How to
« Reply #2 on: Feb 8th, 2012, 03:05am »

The problem is, not all KML files have the limits of the display as in Rod's example.
One that I looked at has the centre of the display and a 'range' value.
It doesn't give enough to be sure of the image corner values.

An example of the KML file you are starting with will help a lot!

You could also start with the KML reference:

http://code.google.com/apis/kml/documentation/kmlreference.html

One other problem will be if the image covers a large area - the lat/long will not be linear.
As the area covered gets larger the curvature of the earth will make the calculations difficult.

If Latitude and longitude are parallel and equally spaced, the maths is easy.
Jim
User IP Logged

ssergiu
New Member
Image


member is offline

Avatar




PM


Posts: 10
xx Re: KML to pixel to LAT and LON / How to
« Reply #3 on: Feb 8th, 2012, 09:08am »

First of all, thank you both for reply.
I was working for a while with multi-copters ( see my blog http://diydrones.com/profiles/blogs/xmega-ap-and-esc)
My only programing experience under Windows is with LB . What was left to be completed for my Ground Control Station was to load in GCS one jpg picture saved from Google Maps then with mouse to click few way-points and send those wp's coordinates to navigation MCU.
This is the jpg image which once loaded in Google - even I am offline ,Google "knows" any point coordinate under the mouse cursor. So where in this jpg file are located information's about lat/lon?

Oops... how I should do to attach the jpg file(by using Insert image I get only [img][/img])?

Regards,
Sergiu
User IP Logged

Alyce Watson
Administrator
ImageImageImageImageImage


member is offline

Avatar

The help file is your best friend.


Homepage PM

Gender: Female
Posts: 14392
xx Re: KML to pixel to LAT and LON / How to
« Reply #4 on: Feb 8th, 2012, 09:53am »

on Feb 8th, 2012, 09:08am, ssergiu wrote:
Oops... how I should do to attach the jpg file(by using Insert image I get only [img][/img])?

Regards,
Sergiu


The forum software doesn't allow attachments. Upload your photo to your own site, or to a photo-hosting site like ImageShack.com or PhotoBucket.com and place the link within the [ img ] tags. Use the full URL, including http://

User IP Logged

Alyce
Liberty BASIC Workshop - a complete IDE for Liberty BASIC


Alyce's Restaurant
for Liberty BASIC code, tools and references
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 4146
xx Re: KML to pixel to LAT and LON / How to
« Reply #5 on: Feb 8th, 2012, 1:19pm »

Awesome technology on the blog, you are very talented. I built a tricopter with three standard gyros and managed to get it to hover (almost).

If you give us a little more detail about the image and a better definition of the size of view you are talking about then there are a lot of smart folks reading this forum that will be happy to contribute.

As Jim says the curvature of the earth plays a part but if we are talking about a drone that flies about in a kilometer square perhaps we can get it to be click and fly. I assume heading and height will become part of the solution. Your drone is probably sending back that info, it is probably also telling you where it is. We need to map that to the image.

A real image and the real KML file will get you the help you desire.

« Last Edit: Feb 8th, 2012, 1:26pm by Rod » User IP Logged

Jim Hiley
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 130
xx Re: KML to pixel to LAT and LON / How to
« Reply #6 on: Feb 8th, 2012, 3:54pm »

It looks like your maps wont be too big.
Is it one map or will you want to get a map for 'anywhere' .
For a limited number of maps, I would go for a calibrate routine.
Click on the map to get the pixel coordinates, enter the lat/long. With 3 points around the edges of the jpg, you can get a reasonable calibration.
have a look at the help file for Nomad's Almanac, page 9 has the calibrate your own map description.
http://www.c-com.com.au/nomad.htm
If it is of use to you, I can extract that part of the program.

If you are using lots of different maps, use the KML file the same as Rod's first example. Then you should have the corner coordinates without any extra work.
You will need to check because I am not sure that Google returns the exact area you request. There may be some overshoot to make life difficult.

Jim.
User IP Logged

Stefan Pendl
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Computers are like babies, you must teach them what you like them to do ...


Homepage PM

Gender: Male
Posts: 5072
xx Re: KML to pixel to LAT and LON / How to
« Reply #7 on: Feb 8th, 2012, 4:08pm »

on Feb 8th, 2012, 09:08am, ssergiu wrote:
This is the jpg image which once loaded in Google - even I am offline ,Google "knows" any point coordinate under the mouse cursor. So where in this jpg file are located information's about lat/lon?

The image is an image map or has underlying hot-spots, this is why the lat/long are known.

Imagine the image as each pixel being a button.

In addition there is a Java application behind it all, so it is not magic wink
User IP Logged

Stefan - Homepage

Please give credit if you use code I post, no need to ask for permission.

Liberty BASIC Pro 4.04, Windows 7 Home Premium x64 SP1, AMD Turion X2 RM-70 2GHz, 4GB RAM
ssergiu
New Member
Image


member is offline

Avatar




PM


Posts: 10
xx Re: KML to pixel to LAT and LON / How to
« Reply #8 on: Feb 8th, 2012, 4:46pm »

From GOOGLE EARTH I can save one kml file where, using wordpad to open it, the only useful information is Lat and Lon,altitude,heading and range (?) of the "Starting Location" I added, nothing else! Also I can save one jpg file with the picture of the place containing this "Starting Location" point.
Distances involved are less than 2-3Km so Lat and Lon should be linear. The question is how I can link the info from kml file with the jpg picture? I don't want necessarily to use Google Earth but any free WEB software from where I can save one picture of the place I want to fly over, picture which should contain additional info so I can link pixels to Lat/Lon.
Of course, I can add directly in to Google Earth as many points as I need and save the KML file, then in LB I can filter out all Lat and Lon in the text contained in KML file and finally save the way-points to autopilot.
But I need to have in GCS the picture of the area I intend to fly (more realistic) and also to see in real time position of UAV represented by a moving dot in the map. I had successfully already logged in GCS Lat, Lon, distance to next WP, roll,pitch,yaw,ground speed,etc.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 4146
xx Re: KML to pixel to LAT and LON / How to
« Reply #9 on: Feb 9th, 2012, 04:28am »

Perhaps the trick is to set the eye height correctly. I have played about with an online tool. If I set the eye height (zoom) to 1km I basically get a 1km square in view and can save that as a .jpg.

Sure when I save the kml I only get the Lat and Lon coordinates but that is the centre of the image and I know it is a 1km square image. That might be enough to work with.

I would still like to see the actual file and image you are working with before embarking on any coding.
User IP Logged

ssergiu
New Member
Image


member is offline

Avatar




PM


Posts: 10
xx Re: KML to pixel to LAT and LON / How to
« Reply #10 on: Feb 9th, 2012, 06:06am »

On the link below I have one KML file and corresponding jpg.
The jpg resolution is 1524x965.

[http://dl.transfer.ro/KML-transfer_RO-09feb-53e9d3.rar].

Thank you for your help.
Sergiu
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 4146
xx Re: KML to pixel to LAT and LON / How to
« Reply #11 on: Feb 10th, 2012, 06:14am »

Ok, your link is broken and the file can't be found. I cobbled together a quick and dirty demo of what is possible just from handling the .jpg.

It may be that there are slicker ways to do this job, perhaps with a browser in an ATL control. You may need to go that route if you want a truly dynamic system.

This solution works with a pre prepared .jpg.

This is a link to a .bmp. BMP A bmp preserves the fine detail, your .jpg corrupts a lot of pixels but it depends on the resolution you require. Here is a .jpg, you will find it easier to load the .bmp to Liberty.

User Image

When I copied the image I noted two specific points, bottom left, the church symbol and top right, the top runway marker. Knowing these two points and assuming the image is a flat plane with the camera directly overhead (which it isn't) I simply interpolate between pixels and degrees minutes seconds.


Code:
'nomainwin
    'the .bmp is 1040 x 818
    'taken at 3.14km eye height North - South orientation
    'it shows roughly 3.14km x 2.69km of land
    'the church door at bottom left is 55'57" 22.85 x 4'00" 51.44
    'the top right runway marker is 55'58" 32.54 x 3'58" 09.51


    nomainwin
    WindowWidth = 1050
    WindowHeight = 900
    graphicbox  #1.gb 0,0,1041,819
    textbox #1.lat  10,830,200,25
    textbox #1.lon  250,830,200,25
    open "Map" for window as #1
    #1, "trapclose [quit]"

    'get the decimal equivelent of our position
    Lat1=sexdec(55,57,22.85)
    Lon1=sexdec(4,0,51.44)
    Lat2=sexdec(55,58,32.54)
    Lon2=sexdec(3,58,09.51)

    'workout the interpolation x and y between the two know points
    PixelX1=127
    PixelY1=740
    PixelX2=960
    PixelY2=94
    StepX=abs(Lon1-Lon2)/(abs(PixelX1-PixelX2)+1)
    StepY=abs(Lat1-Lat2)/(abs(PixelY1-PixelY2)+1)



    'show the bmp and wait for mouse click
    loadbmp "map","c:\atemp\rodsmap.bmp"
    #1.gb "down ; drawbmp map 0 0"
    #1.gb "when leftButtonUp [click]"
    wait

    [click]
    x=MouseX
    y=MouseY
    if x>PixelX1 then
        Lon=Lon1-(x-PixelX1)*StepX
    else
        Lon=Lon1+(PixelX1-x)*StepX
    end if
    if y>PixelY1 then
        Lat=Lat1-(y-PixelY1)*StepY
    else
        Lat=Lat1+(PixelY1-y)*StepY
    end if
    #1.lat decsex$(Lat)
    #1.lon decsex$(Lon)
    wait



    [quit]
    close #1
    end


    function sexdec(d,m,s)
        sexdec=abs(d)+(m*60+s)/3600
        if d<0 then sexdec=sexdec*-1
    end function

    function decsex$(d)
        decsex$=str$(int(d))
        d=d-int(d)
        s=3600*d
        m=int(s/60)
        s=s-m*60
        decsex$=decsex$+"'"+str$(m)+chr$(34)+using("##.##",s)
    end function

 


I say again quick and dirty, not debugged.
User IP Logged

ssergiu
New Member
Image


member is offline

Avatar




PM


Posts: 10
xx Re: KML to pixel to LAT and LON / How to
« Reply #12 on: Feb 10th, 2012, 08:19am »

Rod,

Thank you for the example code. I will test it and report back here.

Sergiu
User IP Logged

ssergiu
New Member
Image


member is offline

Avatar




PM


Posts: 10
xx Re: KML to pixel to LAT and LON / How to
« Reply #13 on: Feb 23rd, 2012, 07:38am »

Rod,

Sorry for such a late reply. I was at sea and did not had internet access. The example you gave me is pretty accurate !!
Two questions:
How did you got this pixel values?
PixelX1=127
PixelY1=740
PixelX2=960
PixelY2=94
The picture saved from Google Earth is pretty big at 1000x800. I intend to use one GCS with one SBC (single board computer with XP embedded ) so display will be around 10-12 inch. That means I have to load the BMP in smaller graphic window to make space for other parameters. How can I "compress" the BMP in LB to fit in the smaller graphic box?

Thank you,
Sergiu

User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 4146
xx Re: KML to pixel to LAT and LON / How to
« Reply #14 on: Feb 23rd, 2012, 10:04am »

Well this is what I meant by preprepared v dynamic. If it can be preprepared then you port the image into Paint and, resize it to whatever size you want and then note the pixel positions of your two noted points simply by moving the cursor over them. Save that away as a bmp and record the pixel point info.

So you can have an smaller image either resized or cropped from the larger Google image.

If you want to dynamically grab an image from Google and process it on the fly then we need an entirely different process. It may be possible to do something dynamically but you would need to be much more specific about the image sizes, the process and user interactions you would expect. Describe it as a story rather than think of code at this stage.

But perhaps preprepared bmps are enough, you could store an array of images and data to recall. All depends whether you are going to wander round the country or go to specific locations.
User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls