English

How To: Convert from Decimal Degrees (DD) to Degrees, Minutes, Seconds (DMS)

Summary

This document provides a sample script to convert coordinates in a table from Decimal Degrees to Degrees, Minutes, Seconds.

Procedure

  1. Open a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Copy the following code into the new script window.

    Code:
    theView = av.GetActiveDoc
    theThemeList = theView.GetThemes
    theTheme = MsgBox.ChoiceAsString(theThemeList, "Choose theme with DD values", "")
    'theTheme = theThemeList.Get(0)

    theFTab = theTheme.GetFTab
    theFields = theFTab.GetFields

    lonFieldx = MsgBox.ChoiceAsString(theFields, "Select longitude (X Coord) field", "")
    latFieldy = MsgBox.ChoiceAsString(theFields, "Select latitude (Y Coord) field",
    "")

    if (lonFieldx = nil) then
    MsgBox.Error("Longitude field is missing", "Terminating")
    exit
    elseif (latFieldy = nil) then
    MsgBox.Error("Latitude field is missing", "Terminating")
    end

    'lonFieldx = theFTab.FindField("X-Coord")
    'latFieldy = theFTab.FindField("Y-Coord")

    theDescList = {" 37 15 30N 117 45 20W",
    "N37 15 30 W117 45 20",
    " 37 15 30 -117 45 20"}
    flagDMS = MsgBox.ChoiceAsString
    (theDescList, " Latitude(Y) Longitude(X)", "Select output format (truncated to integer)")

    if (flagDMS = nil) then
    MsgBox.Error("No output format selected", "Terminating")
    end

    if (flagDMS.Contains("-117")) then
    flagDMS = "Signed"
    elseif (flagDMS.Contains("30N")) then
    flagDMS = "Right"
    else
    flagDMS = "Left"
    end

    theFTab.SetEditable(true)

    '-- Add two String fields to the FTab to contain the output
    fldLonDMS = Field.Make("Long(X)", #FIELD_CHAR, 10, 0)
    fldLatDMS = Field.Make("Lat(Y)", #FIELD_CHAR, 10, 0)
    theFTab.AddFields({fldLonDMS, fldLatDMS})

    dirLookUp = {"N", "S", "E", "W"} 'For flag values: "N" = 0, "S" = 1, "E" = 2, "W" = 3

    for each r in theFTab
    lonDD = theFTab.ReturnValue(lonFieldx, r)
    if (lonDD.Abs > 180) then
    MsgBox.Info("Longitude absolute value "+lonDD.Abs.AsString+" greater than 180, using 0", "")
    end
    latDD = theFTab.ReturnValue(latFieldy, r)
    if (latDD.Abs > 900) then
    MsgBox.Info("Latitude absolute value "+latDD.Abs.AsString+" greater than 90, using 0", "")
    end

    integer_lonDegrees = lonDD.Truncate
    work_lonMinutes = ((lonDD - integer_lonDegrees) * 60).Abs
    integer_lonMinutes = work_lonMinutes.Truncate
    work_lonSeconds = ((work_lonMinutes - integer_lonMinutes) * 60).Abs
    integer_lonSeconds = work_lonSeconds.Truncate

    integer_latDegrees = latDD.Truncate
    work_latMinutes = ((latDD - integer_latDegrees) * 60).Abs
    integer_latMinutes = work_latMinutes.Truncate
    work_latSeconds = ((work_latMinutes - integer_latMinutes) * 60).Abs
    integer_latSeconds = work_latSeconds.Truncate
    if (integer_lonDegrees < 0) then
    lonFlag = 3 'West Longitude
    else
    lonFlag = 2 'East Longitude
    end

    if (integer_latDegrees < 0) then
    latFlag = 1 'South Latitude
    else
    latFlag = 0 'North Latitude
    end

    '-- Format the fields based on flagDMS
    if (flagDMS = "Signed") then '-- Use the numbers
    lonDeg = integer_lonDegrees.AsString
    lonMin = integer_lonMinutes.AsString
    lonSec = integer_lonSeconds.AsString
    latDeg = integer_latDegrees.AsString
    latMin = integer_latMinutes.AsString
    latSec = integer_latSeconds.AsString
    elseif (flagDMS = "Right") then '-- Place direction flag to right of seconds
    lonDeg = integer_lonDegrees.Abs.AsString
    lonMin = integer_lonMinutes.AsString
    lonSec = integer_lonSeconds.AsString + dirLookUp.Get(lonFlag)
    latDeg = integer_latDegrees.Abs.AsString
    latMin = integer_latMinutes.AsString
    latSec = integer_latSeconds.AsString + dirLookUp.Get(latFlag)
    else '-- Place direction flag to left of degrees
    lonDeg = dirLookUp.Get(lonFlag) + integer_lonDegrees.Abs.AsString
    lonMin = integer_lonMinutes.AsString
    lonSec = integer_lonSeconds.AsString
    latDeg = dirLookUp.Get(latFlag) + integer_latDegrees.Abs.AsString
    latMin = integer_latMinutes.AsString
    latSec = integer_latSeconds.AsString
    end

    '-- Create the output strings
    lonDMS_String = lonDeg ++ lonMin ++ lonSec
    latDMS_String = latDeg ++ latMin ++ latSec

    theFTab.SetValue(fldLonDMS, r, lonDMS_String)
    theFTab.SetValue(fldLatDMS, r, latDMS_String)
    end

    theFTab.SetEditable(false)
    theFTab.Flush


  3. Compile the script by clicking the compile button.
    [O-Image] Script compile button
  4. Make the appropriate View active.
  5. Make the Script window active.
  6. Run the script by clicking the run button.
    [O-Image] Run compiled script button
  7. Select the theme for modification from the drop down list, and click OK.
  8. Select the existing longitude field (X) from the drop down list, and click OK.
  9. Select the existing latitude field (Y) from the drop down list, and click OK.
  10. Select the desired output format, and click OK.

    Note:
    This script creates two output fields. One is the longitude in Degrees, Minutes, Seconds , and the other is latitude in Degrees, Minutes, Seconds .

Related Information