You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Is it possible to create a script that interpolates location information?

I have a group of imported photos, some of which have GPS locations and some of which don't. Is it possible to create a script that, when given a starting and ending photo with GPS locations, looks at the time stamps and assigns linearly interpolated locations to all the photos in between? Unfortunately, I haven't written any code this century.

iMac (27-inch, Late 2013), macOS High Sierra (10.13.6)

Posted on Oct 7, 2018 3:36 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 7, 2018 9:39 PM

It's probably as buggy as all ****, but this is what I hacked together out of some other scripts that a much more talented coder (léonie) wrote. It appears to work, but will probably crash in flames on some corner case somewhere.


(* select at least 3 images in Photos *)

tell application "Photos"

activate

set imageSel to (get selection)

if (imageSel is {}) or (the length of imageSel < 3) then

error "Please select at least three images."

else

set withAlti to false

set withLati to false

set withLongi to false

set withTime to false


-- Get start point GPS lat/long and time

try

tell the first item of imageSel

set loc to get the location --retrieve longitude and latitude as list

set alti to get the altitude -- retrieve the altitude

set lati_1 to (the first item of loc) -- as number

set longi_1 to (the second item of loc) -- as number

set first_date to the date of the first item of imageSel as date -- retrieve the date


-- return altiS

set withAlti to (alti is not equal to missing value)

set withLati to (lati_1 is not equal to missing value)

set withLongi to (longi_1 is not equal to missing value)

set withTime to (first_date is not equal to missing value)


if not withLati then

set withLati to false

error "First Photo has no Latitude assigned"

end if


if not withLongi then

error "First Photo has no Latitude assigned"

end if


if (lati_1 > 90.0) or (lati_1 < -90.0) then

error "Latitude out of range " & lati_1

end if


if (longi_1 > 180.0) or (longi_1 < -180.0) then

error "Longitude out of range " & longi_1

end if


if not withTime then

error "First Photo has no date stamp"

end if

end tell


on error errTexttwo number errNumtwo

display dialog "No GPS: " & errNumtwo & return & errTexttwo

return

end try


-- Get end point GPS lat/long and time

try

tell the last item of imageSel

set loc to get the location --retrieve longitude and latitude as list

set alti to get the altitude -- retrieve the altitude

set lati_2 to (the first item of loc) -- as number

set longi_2 to (the second item of loc) -- as number

set last_date to the date of the last item of imageSel as date -- retrieve the date


-- return altiS

set withAlti to (alti is not equal to missing value)

set withLati to (lati_1 is not equal to missing value)

set withLongi to (longi_2 is not equal to missing value)

set withTime to (first_date is not equal to missing value)


if not withLati then

set withLati to false

error "Last photo has no Latitude assigned"

end if


if not withLongi then

error "Last photo has no Latitude assigned"

end if


if (lati_2 > 90.0) or (lati_2 < -90.0) then

error "Latitude out of range " & lati_2

end if


if (longi_2 > 180.0) or (longi_2 < -180.0) then

error "Longitude out of range " & longi_2

end if


if not withTime then

error "last photo has no date stamp"

end if

end tell


on error errTexttwo number errNumtwo

display dialog "No GPS: " & errNumtwo & return & errTexttwo

return

end try


if (not withLongi or not withLati) then


return {loc, alti} --testing

end if


set timeWindow to (last_date - first_date)


repeat with i from 2 to ((count of imageSel) - 1)


set next_image to item i of imageSel


tell next_image

set imageDate to get the date of item i of imageSel

set percentAlongPath to (imageDate - first_date) / timeWindow


set its location to {(1 - percentAlongPath) * lati_1 + percentAlongPath * lati_2, (1 - percentAlongPath) * longi_1 + percentAlongPath * longi_2}

-- if withAlti then -- photo has altitude tag

-- set its altitude to alti

-- end if

end tell

if withAlti then -- photo has altitude tag

tell next_image

-- set its altitude to alti as number -- does not work

end tell

end if



end repeat

end if


return {loc, alti, "Done"} -- for testing

end tell

Similar questions

3 replies
Question marked as Top-ranking reply

Oct 7, 2018 9:39 PM in response to Aardvark_Phlegm

It's probably as buggy as all ****, but this is what I hacked together out of some other scripts that a much more talented coder (léonie) wrote. It appears to work, but will probably crash in flames on some corner case somewhere.


(* select at least 3 images in Photos *)

tell application "Photos"

activate

set imageSel to (get selection)

if (imageSel is {}) or (the length of imageSel < 3) then

error "Please select at least three images."

else

set withAlti to false

set withLati to false

set withLongi to false

set withTime to false


-- Get start point GPS lat/long and time

try

tell the first item of imageSel

set loc to get the location --retrieve longitude and latitude as list

set alti to get the altitude -- retrieve the altitude

set lati_1 to (the first item of loc) -- as number

set longi_1 to (the second item of loc) -- as number

set first_date to the date of the first item of imageSel as date -- retrieve the date


-- return altiS

set withAlti to (alti is not equal to missing value)

set withLati to (lati_1 is not equal to missing value)

set withLongi to (longi_1 is not equal to missing value)

set withTime to (first_date is not equal to missing value)


if not withLati then

set withLati to false

error "First Photo has no Latitude assigned"

end if


if not withLongi then

error "First Photo has no Latitude assigned"

end if


if (lati_1 > 90.0) or (lati_1 < -90.0) then

error "Latitude out of range " & lati_1

end if


if (longi_1 > 180.0) or (longi_1 < -180.0) then

error "Longitude out of range " & longi_1

end if


if not withTime then

error "First Photo has no date stamp"

end if

end tell


on error errTexttwo number errNumtwo

display dialog "No GPS: " & errNumtwo & return & errTexttwo

return

end try


-- Get end point GPS lat/long and time

try

tell the last item of imageSel

set loc to get the location --retrieve longitude and latitude as list

set alti to get the altitude -- retrieve the altitude

set lati_2 to (the first item of loc) -- as number

set longi_2 to (the second item of loc) -- as number

set last_date to the date of the last item of imageSel as date -- retrieve the date


-- return altiS

set withAlti to (alti is not equal to missing value)

set withLati to (lati_1 is not equal to missing value)

set withLongi to (longi_2 is not equal to missing value)

set withTime to (first_date is not equal to missing value)


if not withLati then

set withLati to false

error "Last photo has no Latitude assigned"

end if


if not withLongi then

error "Last photo has no Latitude assigned"

end if


if (lati_2 > 90.0) or (lati_2 < -90.0) then

error "Latitude out of range " & lati_2

end if


if (longi_2 > 180.0) or (longi_2 < -180.0) then

error "Longitude out of range " & longi_2

end if


if not withTime then

error "last photo has no date stamp"

end if

end tell


on error errTexttwo number errNumtwo

display dialog "No GPS: " & errNumtwo & return & errTexttwo

return

end try


if (not withLongi or not withLati) then


return {loc, alti} --testing

end if


set timeWindow to (last_date - first_date)


repeat with i from 2 to ((count of imageSel) - 1)


set next_image to item i of imageSel


tell next_image

set imageDate to get the date of item i of imageSel

set percentAlongPath to (imageDate - first_date) / timeWindow


set its location to {(1 - percentAlongPath) * lati_1 + percentAlongPath * lati_2, (1 - percentAlongPath) * longi_1 + percentAlongPath * longi_2}

-- if withAlti then -- photo has altitude tag

-- set its altitude to alti

-- end if

end tell

if withAlti then -- photo has altitude tag

tell next_image

-- set its altitude to alti as number -- does not work

end tell

end if



end repeat

end if


return {loc, alti, "Done"} -- for testing

end tell

Oct 15, 2018 9:42 PM in response to léonie

From the math point of view, I think any time you cross the International Date Line, the algorithm is going to drop the resultant location halfway around the world from where it should be, but for most vacation snaps, it should work fine. However, I would appreciate it if somebody with any knowledge of AppleScript programming would go through my code and beef up the error handling. When it works, it works OK, but I'm certain it doesn't fail gracefully.

Is it possible to create a script that interpolates location information?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.