Apply GPS coordinates to image files

Overview

This script button will apply GPS coordinates copied from Google Maps to the selected image files.

How to use

  • Go to Google Map
  • Right click on the desired location and select "More info about this place"
  • At the bottom of the map, there will display coordinates link like "40.274721, -92.445195". Click on it.
  • The coordinates will display into the search input on the left. Copy them.
  • In DOpus, select image files you want coordinates to apply to. (If you select all files in a tab, the script will automatically run on image files only.)
  • Click on the button provided here and the GPS metadata will be applied.
  • If the clipboard doesn't contain any supported GPS coordinates format, you'll be prompted to paste the coordinates.

How to install button

Download the desired button version and drag & drop it in a Dopus toolbar in customize mode.

Here is the english version:

Here is the french version of the button:

History

v2016.11.06
Fix : "42.018396,-105.234445" format was processed as invalid because the script expected there was a space after the comma such as "42.018396, -105.234445"

v2016.09.16
Fix : If among the selected files there were other file types, the script couldn't apply metadata to the image files
Change: No filter is required anymore. Valid files are automatically handled by the script.

v2016.09.13
Smart behavior: if the clipboard contains valid GPS (like "40.274721, -92.445195"), clicking the button will directly apply them to the selected image files else user will be prompted to enter coordinates.

v2016.09.12
First release: The button prompts user to enter GPS data.

Script Code:

The (English) script code is reproduced here for reference. The same or similar code is contained in the .dcf files above.

' v2016.11.06
' Here is a script button that will apply GPS coordinates copied from Google Maps to the selected image files.
' Source and Update: https://resource.dopus.com/viewtopic.php?f=35&t=27486
Function OnClick(ByRef ClickData)
    ' Clear the log output
'     DOpus.ClearOutput

    ' ============================================================================
    '                         INITIATE VARIABLES & OBJECT
    ' ============================================================================
    Match = 0
    Set cmd = ClickData.Func.command
    Set srce = ClickData.Func.sourcetab
    Dim GpsInput
    ' Dialog default values
    Title = "GPS coordinates"
    Message = "Paste the coordinates you copied from Google Maps."& vbCrLf  & vbCrLf &"Valid format is for ex: 47.338388, 0.990228"
    Buttons = "&OK|&Cancel"
    Icon = "question"

    ' ============================================================================
    '                         FILTER FILES TO BE PROCESSED
    ' ============================================================================
'     DOpus.Output "Selected files BEFORE filter : " & srce.selected_files.count
'     DOpus.Output "Files that will actually be processed BEFORE filter : " & cmd.files.count
    cmd.ClearFiles ' All items are removed from the collection so only image files will be added next
'     DOpus.Output ""
'     DOpus.Output "FILTERING…"
'     DOpus.Output ""
    n = 1
    For Each f In srce.selected_files
        ' If a selected file can handle GPS metadata, it will be added to files to process
        If (f.metadata = "image") then
            cmd.AddFile(f)
'             DOpus.Output n & ". """ & f.name & """ will be processed"
        End if
        n = n+1
    Next
'     DOpus.Output ""
    ' Select files that will actually be processed
    cmd.RunCommand("Select FROMSCRIPT DESELECTNOMATCH MAKEVISIBLE")
    srce.Update ' Synchronize changes made by the previous select command to the sourcetab object. It can be useful if you need to use this object later.
'     DOpus.Output "Selected files AFTER filter : " & srce.selected_files.count
'     DOpus.Output "Files that will actually be processed AFTER filter : " & cmd.files.count

    ' ============================================================================
    '                         RETRIEVE GPS COORDS
    ' ============================================================================
    ' Check if at least 1 file is selected to continue else warn and exit.
    If cmd.files.count < 1 Then
        Set dlg = ClickData.Func.Dlg
        dlg.Request "No file selected or no valid file to process."  & vbCrLf & "Please select at least 1 image file and run the command again.", "OK"
        Set dlg = Nothing ' destroy dialog
        Exit Function
    End If

    ' Check if clipboard contains valid coordinates to apply
    If DOpus.GetClipFormat = "text" Then
        GpsInput = DOpus.GetClip
'         DOpus.Output "GpsInput from CP : """ & GpsInput & """"
        ' Check if the clipboard content is a valid GPS coordinates
        Set re = New RegExp
        re.IgnoreCase = True ' No Case-sensitive matching.
        re.Global = False ' Only first match will be matched
        re.Pattern = "^(-?[1-8]?\d(?:\.\d{1,18})?|90(?:\.0{1,18})?),\s*?(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,18})?|180(?:\.0{1,18})?)$"

        Set matches = re.Execute(GpsInput)
        If matches.Count = 1 Then ' Case if values contains valid coordinates
           Match = 1
'          DOpus.Output "GpsInput from CP is VALID"
'          DOpus.Output "Found " & matches.Count & " matches" & vbCRLF & "Match value : " & matches.Item(0).Value
           Lat = matches.Item(0).SubMatches(0)
           Lon = matches.Item(0).SubMatches(1)
'          DOpus.Output "Latitude = " & Lat & vbCrLf & "Longitude = " & Lon
        Else ' Case if clipboard doesn't contain valid coordinates
           ' Prompt the user to enter values, ask coordinates until they are valid
            Do While Match = 0
                ' Create 1st Dialog object.
                Set dlg = DOpus.Dlg
                ' Initialise the object to display a message with 2 buttons and 1 input box
                dlg.Window = DOpus.Listers(0)
                dlg.title = Title
                dlg.message = Message
                dlg.buttons = Buttons
                dlg.icon = Icon
                dlg.max = 128  ' enable the text field
                ret = dlg.Show ' Show the dialog
            '   DOpus.Output "Dialog.Show returned " & ret
            '   DOpus.Output "The string you entered was " & dlg.input

                If ret = 0 then ' When user cancel action, just quit the script
                    Exit Function
                End If

                GpsInput = dlg.input
        '       DOpus.Output "GpsInput from prompt : """ & GpsInput & """"
                Set dlg = Nothing ' destroy 1st dialog prompt

                ' Check if the input content is a valid GPS coordinates
                Set re = New RegExp
                re.IgnoreCase = True ' No Case-sensitive matching.
                re.Global = False ' Only first match will be matched
                re.Pattern = "^(-?[1-8]?\d(?:\.\d{1,18})?|90(?:\.0{1,18})?),\s+?(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,18})?|180(?:\.0{1,18})?)$"

                Set matches = re.Execute(GpsInput)
                If matches.Count = 1 Then ' Case if values contains valid coordinates
                   Match = 1
        '          DOpus.Output "GpsInput from CP is VALID"
        '          DOpus.Output "Found " & matches.Count & " matches" & vbCRLF & "Match value : " & matches.Item(0).Value
                   Lat = matches.Item(0).SubMatches(0)
                   Lon = matches.Item(0).SubMatches(1)
        '          DOpus.Output "Latitude = " & Lat & vbCrLf & "Longitude = " & Lon
                Else ' If fomat is invalid, warn the user and ask if he wants to retry or cancel.
                    ' Just change default values for the 1st dialog that will pop up again because the loop
                    Title = "Invalid Format"
                    Message = "Coordinates you input are not a supported format"& vbCrLf  & vbCrLf &"Please Try again"
                    Icon = "warning"

                    ' Clear the log output
'                     DOpus.ClearOutput
                End If
            Loop
        End If
    End If

    ' ============================================================================
    '                         APPLY GPS DATA
    ' ============================================================================
'     DOpus.Output "Latitude = " & Lat & vbCrLf & "Longitude = " & Lon
'     DOpus.Output "Number of selected files that the script will apply GPS : " & ClickData.Func.sourcetab.selected.count
    cmd.RunCommand("SetAttr META gpslatitude:"&Lat&" gpslongitude:"&Lon&"")
End Function

Note that you can use the built-in metadata editor to set GPS coordinates:


Use this script instead if you want to click a button and be prompted for the coordinates, instead of using the metadata editor. If you are setting lots of GPS coords from scratch, and not editing other fields, then the script may give you a more streamlined workflow for that task.

Thanks fred! o)

I'm not into coordinates within my pictures yet, but while reading your post and code carefully, I thought it might make little sense to bring up the requester for the coordinates in every case. You could look into the clipboard first, parse, trim and shape the content, if valid go on and only if the clip content cannot be used bring up the requester. I assume this would result in a better experience if you deal with lot of files, since the paste and confirm steps are left out.

Yes I agree. I'm working on it. But I face a "bug" that I don't understand. See details here… if you have a hint. I posted a version there that read the CP instead of prompting. I'll add the automatic choice as you suggest later. I really hope some help on that bug because I don't understand why it won't work in some case while I check until the end that the script should work the same in any case.

Update

v2016.09.13
Smart behavior: if the clipboard contains valid GPS (like "40.274721, -92.445195"), clicking the button will directly apply them to the selected image files else user will be prompted to enter coordinates.

Update

v2016.11.06
Fix : "42.018396,-105.234445" format was processed as invalid because the script expected there was a space after the comma such as "42.018396, -105.234445"

Nice! Makes it easy to go directly from decimal degrees. I've been checking it out working directly from caltopo.com too, which means I can be looking at and working from forest service or other maps too. Also makes it easier to enter coords into Flickr's ORGANIZE.

1 Like