English

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

Summary

Note:
The information in this article applies to ArcView GIS versions 3.x only. The instructions described apply to the Avenue programming language, which is specific to the ArcView GIS application.

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.
    1. Activate the Project window.
    2. Click the Scripts icon.
    3. Click New.
  2. Copy the following code into the new script window.
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 
  1. Compile the script by clicking the Compile button.
    [O-Image] Script compile button
  2. Make the appropriate View active.
  3. Make the Script window active.
  4. Run the script by clicking the Run button.
    [O-Image] Run compiled script button
  5. Select the theme for modification from the drop-down list, and click OK.
  6. Select the existing longitude field (X) from the drop-down list, and click OK.
  7. Select the existing latitude field (Y) from the drop-down list, and click OK.
  8. 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