Plot GPS positions over an image

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Plot GPS positions over an image

Post by Nakia » Thu Apr 18, 2013 12:14 pm

Hi,

I have been asked today if I could put something together to plot a file of GPS Positions over
an image, image is an aerial photo of the location where the GPS positions have occurred.

Few things to note.
The GPS positions are in a customer user grid (not lats and longs)

So,
The way I see this is I can load up the image then ask the user to enter the upper left
And bottom right coordinates of the image. From here I need to be able somehow work out
an invisible grid to lay over the image to plot the positions on or figure out math based upon the rect of
The image to assign a loc to equivalent GPS Position.

Anyone got any good starting points or throughts on how to calculate the math or create the grid?

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Plot GPS positions over an image

Post by dave_probertGA6e24 » Thu Apr 18, 2013 1:19 pm

The Math is fairly simple as long as the image reference points are far enough apart.

Assume you have some coords to plot e.g.:
250,300
400,150
500,270

And the top-right (TR) coord is entered as 100,100, the bottom-left (BL) as 450,450

Gives:
150,200
300,50
400,170

Then you can simply take the TR values away from each of your points to remap them into the rect that starts at TR
Then you know the width and height of your image rect (BL-TR) = 350,350

If any of the coords now have value bigger than the width/height then they can be ignored. Looks like the last pair is out of the image.

Then you can plot the points based on the scaling factor for the image area.

If the reference points are actually at the corners of the image then you simply use the width/height of the image and the calculated width/height of the coord area the make a scale factor (probably in both the x and y directions separately)

eg. Image is 640 x 640, calculated coord (from above) is 350,350
therefore each pixel is 640/350 = 1.8285 coords wide (and the same for height in this example - though it could be different)

eg. To plot point 150,200 (converted values - see above) you would use: (150 * 1.8285), (200 * 1.8285) = 274, 366 (rounded off) onto the image

There is a simple way of doing it - in explanation form. Obviously you will need to code this based on the data you have and the numbers you are working with.

Hope it helps a bit,

Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Plot GPS positions over an image

Post by Dixie » Thu Apr 18, 2013 1:40 pm

What form do these GPS positions take ? From where and how are they derived ?

Dixie

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Plot GPS positions over an image

Post by Nakia » Thu Apr 18, 2013 11:19 pm

Thanks for the reply's.

Dixie, the GPS Positions are in the form of "Easting and Northing" that include a user offset (therefore the Google API or likes wont understand them). This particular customer uses what they call MGA56 (Mine Grid Australia, Zone 56). Mine sites traditionally setup their sites like this so their on site survey and engineering staff can work with numbers that are more friendly.

Below are some examples of the GPS Posiions recorded that need to be plotted.
228444.12,6609382.50,319.85
228481.08,6609572.00,288.59
228357.77,6609232.00,336.00

{Comma is itemdel above}
The first value is the "Easting", second is the "Northing" and the last is the "Elelvation" (Elevation is not overly important for this project)
Note = Easting equates to X (in terms of plotting the events)
Northing equates to Y (in terms of plotting the events)

So the customer has supplied me with the following coordinates for their overlay image.
Eastings Northing
Lower Left Corner 226122.666 6608540.710
Upper Right Corner 229896.637 6611026.766

Using the above details how does everything this will still apply and work ?

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Plot GPS positions over an image

Post by dave_probertGA6e24 » Fri Apr 19, 2013 1:09 am

Basically Yes.

The numbers are big - the difference is big, so the scale factor will be quite small when going from coords to pixels.

229896.637 - 226122.666 = 3773.971 (coords width)
6611026.766 - 6608540.710 = 2486.056 (coords height)

If your image is 768 x 576 (for example) then the scale factors would be:
768 / 3773.971 = 0.203499 for x
576 / 2486.056 = 0.231692 for y

Plotting the first point would be:
Original coords: 228444.12 , 6609382.50
Minus the origin Offset: 2321.454 , 841.79
Plot at: ( 2321.454 * 0.203499) ,( 841.79 * 0.231692)
= 472.413568, 195.036009
= 472, 195 (as pixels - you need to round the values or else LC will complain - usually!)

Best to write a little algorithm to do this whole thing for you and test it out.
Do the scale calcs only once though - not inside a loop ;)

You might find that you need to invert some of the values or plot them based on the opposite corner - experiment a little with a few known locations first until you get the precise values figured out.
It's quite easy really ;)

Let us know how you get on.

Hope it helps,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Plot GPS positions over an image

Post by Nakia » Fri Apr 19, 2013 1:20 am

Thanks for your advice,

I will have a tinker this afternoon and see what I come up with.

The way I see it is I will do the sclae factor calcs when the user enters the extents coords.
The have have another button (Plot Events) that runs the scripts that fetches the events from the DB and Plots them.

One other question I did have however:
what is the best way to place the markers over the image?
Would you use a template Graphic that you could just duplicate and move? (how do you duplicate an object?)

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Plot GPS positions over an image

Post by dave_probertGA6e24 » Fri Apr 19, 2013 3:02 am

I would use a graphic or a grouped graphic (e.g. graphic with a label)

You can either use the 'copy' command, or the 'clone' command. I usually have a separate card or stack with images and graphics on it and then copy from that card/stack to the card I want them on:

Code: Select all

copy group "foo" of card "_resources" to this card
set the name of it to "bar"
set the loc of it to 100,100
set the cSpecialData of it to "abcdef"
If you use behaviour button scripts on objects on the resource card then they also get used on the copy too. Therefore having a setProp handler in a behaviour script can allow you to modify the marker shape,colour or whatever based on it's actual value (eg. elevation!)

Hope that helps,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Plot GPS positions over an image

Post by Nakia » Fri Apr 19, 2013 3:44 am

Thanks,

Will look at the behviour scripts..I have not used them before but it sounds like a great idea!

If I wanted to use image's in place of graphics does anyone see any issues with that approach?
I have found some nice little PNG's that I want to use as the plotting markers (Red Number 3, Yellow Number 2 and Green Number 1)

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Plot GPS positions over an image

Post by Dixie » Fri Apr 19, 2013 11:15 am

Nakia...

The laziness is coming out in me... would it not be easier to convert the northings & eastings to lat/long ? :oops: and then perhaps overlay markers on a google map with a satellite view ?

Dixie

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Plot GPS positions over an image

Post by Nakia » Fri Apr 19, 2013 10:13 pm

Dixie....

Short answer would be Yes if the customer did not apply
a customer user offset to the MGA96 grid (and didn't use a Geoid model)

Converting from pure MGA96 to lats and longs is easy but factoring in
user ellipsoid factors, scale factors, rotation deltas etc is a pain the
But..

Also, the user wants to be able to use the tool offline on a windows tablet
so this is another reason I have tracked the path I have chosen.

FYI,
Made a quick start yesterday and am up to the stage of getting the markers
to plot onto the image which leads me to the next question.
Is there a way on desktop to lock screen whilst the handler is duplicating
and moving all the marker images?

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Plot GPS positions over an image

Post by dave_probertGA6e24 » Sat Apr 20, 2013 9:50 am

use:

Code: Select all

lock screen
and then do your complicated stuff, followed by:

Code: Select all

unlock screen
:) :)

Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Plot GPS positions over an image

Post by Nakia » Sat Apr 20, 2013 3:18 pm

Thanks Dave,

So had a few spare minutes tonight to get back to this.
I have had some success but its not working as well as I suspected it would.

I think there might be a problem with some rounding or my math as I am not getting the events to plot in the correct positions.

I kind of reverse engineered what I was using to plot the markers to build a x,y output fld that would calculate the mouseLoc in eastings and northings based
upon what was entered as the Reference Images Bottom Right and Top Left Coordinates.
This has allowed me to asses the accuaracy of the math that is calculating this and it seems a little off.

For Example, when I hover my mouse over the bottom left hand corner I should be reading the same coordinates as what was entered for the Reference image Bottom right coordinate but It is off by around 400.

So here is what I have in the imageHolder

Note, Northing is not working correctly as I need to figure out how to inverse the results because in GPS the numbers for Northing Rise when you rise in Y axis (But in LC its decreases)

FWIW the ImageArea is set to 900x500 size

Code: Select all


Global GXFactor -- the calculated Factor to apply to the X (Easting) Positions, using method dave suggested
Global GYFactor -- the calculated Factor to apply to the Y (Northing) Positions, using method dave suggested
Local lMouseBreached -- boolean for mouse within bounds 
Local lXIMGOffset -- Offset to calc img area origin as the image area does not sit at 0,0 on the card
Local lYIMGOffset -- offset to calc img area origin as the image area does not sit at 0,0 on the card
Local lXOrigin -- the bottom right origin coord
Local lYOrigin -- the bottom right origin coord

on mouseEnter
   put item 1 of the effective rect of me into lXIMGOffset
   put item 2 of the effective rect of me into lYIMGOffset
   put the text of fld "BottomLeftEast" into lXOrigin
   put the text of fld "BottomLeftNorth" into lYOrigin
   put true into lMouseBreached
end mouseEnter

on mouseLeave
   put false into lMouseBreached
end mouseLeave

on mouseMove
   if lMouseBreached = true then
      put (((item 1 in mouseLoc()) - (lXIMGOffset) *GXFactor) + lXOrigin) into tXLoc
      put (((item 2 in mouseLoc()) - (lYIMGOffset) *GYFactor) + lYOrigin) into tYLoc
      put tXLoc&comma&tYLoc into fld "mouseLoc"
   end if
end mouseMove

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Plot GPS positions over an image

Post by Nakia » Mon Apr 22, 2013 6:59 am

Anyone got any good idea's on how to fault find the issues I am seeing detailed above?

Spent a few hours on it on Sunday and all I managed to achive was more confusion..

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Plot GPS positions over an image

Post by dave_probertGA6e24 » Mon Apr 22, 2013 7:35 am

Hi Nakia,

In theory the reverse of what you do for plotting a position of a marker should be what you use to get the position under the mouse - as you described.

A quick note first:
mouseMove handler actually gives you two parameters mouseX and mouseY - so you don't need to call the mouseLoc function inside it. Check the dictionary for mouseMove.

As your position is off by around 400 - try moving the window and seeing if that offset changes. If it does then there is an element of the screenLoc being used somewhere!

As to reversing the values you might need to do a subtraction of the calculated value from the value of the top edge.

Does your plotting of data work correctly? If so then it's simply something to do with the reverse lookup process. Can you post the function you use to calculate the plotted positions for markers - not the whole program, just the bit that does the math :) Then I can also look at the reverse process. It's likely that the problem is just a negative sign needed somewhere.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Plot GPS positions over an image

Post by Nakia » Mon Apr 22, 2013 8:23 am

EDITED AGAIN:

Okay, so I have edited this post a bit :D :D :D
I need to do a little more testing but the below code now seems to be plotting both X and Y Axis correctly!!!!

Still no closer on the mouseHover thing though :(

Code: Select all


on plotGPSEvents
   -- This handler will plot the Events of fld EventPlotDetail that contain a GPS Position
   ## First lets place the Events into the fld for viewing
   put lValidEvents into fld "EventPlotDetail"
   ## Next clear off any old markers left on the screen
   removePlotMarkers
   ## Turn the screen updating off here so it dont go skitz
   lock screen
   ## Store the origin points for calcs
   put the text of fld "BottomLeftEast" into tXOrig
   put the text of fld "TopRightNorth" into tYOrig
   set the itemdel to comma
   put item 1 of the effective rect of img "imageHolder" into tXOffset
   put item 2 of the effective rect of img "imageHolder" into tYOffset
   ## Now we should be able to plot an Event
   set the itemdel to tab
   repeat with x = 1 to the number of lines in lValidEvents
      ## Calculate the LOC
      put (item 25 of line x of lValidEvents - tXOrig) * GXFactor into tXLoc
      add tXOffset to tXLoc
      put ( tYOrig - item 26 of line x of lValidEvents) * GYFactor into tYLoc
      subtract tYLoc from tYOffset
      ## Calculate which marker to use and copy the appropriate template and rename
      put item 8 of line x of lValidEvents into tLevel
      copy img ("Level"&tLevel) to this card
      set the name of it to "Plot"&x
      set the toolTip of img ("Plot"&x) to (item 25 of line x of lValidEvents&comma&item 26 of line x of lValidEvents)
      ## Store the name so it can be deleted
      put "Plot"&x & return after lPlotMarkerNames
      ## Set its LOC, using the round() function
      set the loc of it to the round of tXLoc, the round of tYLoc
   end repeat
   set the itemdel to comma
   ## Unlock the screen
   unlock screen
end plotGPSEvents

Post Reply