' Name:  Theme.ConvertDMStoDD2
' 
' Title:  Converts DegreesMinutesSeconds values to DecimalDegrees
'
' Author: ESRI
' Edited: Kiana Zimmerman, University of Wyoming
'
' Topics:  Geodata
'
' Description:  Converts DegreesMinutesSeconds values in a table
'               to DecimalDegrees. A new field for the converted
'               values is added to the table.
'
'            Values greater than 180 or less than -180 may not be
'            converted correctly.
'
' Requires:  The table must be editable (dBASE or INFO) and you must
'            have write access to it. The table must have a field
'            in which the DMS values are stored in the format DDD MM SS.
'
' Self:  
'
' Returns:  
'
' Filename: dms2dd2.ave


' Get the list of tables in the project and let the user pick one
tabList = {}
for each i in av.GetProject.GetDocs
  if (i.Is(Table)) then
    tabList.Add(i)
  end 
end

' Have the user pick the table to convert
tb = MsgBox.ChoiceAsString(tabList,
  "Choose the table which contains the values to convert:",
  "Convert DMS values to DD")
if (tb = Nil) then
  return ""
end
tbv = tb.GetVTab

' Have the user pick the field with the DMS longitude values
lon = MsgBox.ChoiceAsString(tbv.GetFields,
  "Choose the field with the longitude to convert from DMS to DD",
  "Select field")
if (lon = Nil) then
  return ""
end

' Have the user pick the field with the DMS latitude values
lat = MsgBox.ChoiceAsString(tbv.GetFields,
  "Choose the field with the latitude to convert from DMS to DD",
  "Select field")
if (lat = Nil) then
  return ""
end

newFieldName1 = MsgBox.Input("Enter a name for the new longitude field (up to 8 characters):",
  "Convert DMS values to DD","DD-X")
if (newFieldName1 = Nil) then
  return ""
end

newFieldName2 = MsgBox.Input("Enter a name for the new latitude field (up to 8 characters):",
  "Convert DMS values to DD","DD-Y")
if (newFieldName2 = Nil) then
  return ""
end


' Make sure you can write to the table, and that the field doesn't already exist
if (tbv.CanEdit.Not) then
  MsgBox.Info("Cannot edit the table.","Convert DMS values to DD")
  return ""
end

if (tbv.FindField(newFieldName1).Is(Field)) then
  MsgBox.Info("A field with name"++newFieldName1.asString++
    "already exists.","Convert DMS values to DD")
  return ""
end
if (tbv.FindField(newFieldName2).Is(Field)) then
  MsgBox.Info("A field with name"++newFieldName2.asString++
    "already exists.","Convert DMS values to DD")
  return ""
end


' If we can get this far, we can add the new field
tbv.SetEditable(true)
newf1 = Field.Make(newFieldName1,#FIELD_DECIMAL,12,6)
newf2 = Field.Make(newFieldName2,#FIELD_DECIMAL,12,6)
tbv.AddFields({newf1})
tbv.AddFields({newf2})

' Convert DMS to DD
' Get the values stored in the longitude and latitude fields
for each r in tbv    'for each row in the table
 lonval = tbv.ReturnValueNumber(lon,r)  'get longitude
 latval = tbv.ReturnValueNumber(lat,r)  'get latitude

 ' Do the conversion (multiply the longitude by -1 if in US)
 ddlon = (((lonval mod 10000000)/10000).truncate) + ((((lonval mod 10000)/100).truncate)/60) + (((lonval mod 100)/3600))
 ddlon.SetFormat("dd.dddddd") 
 tbv.SetValueNumber(newf1,r,ddlon)  

 ddlat = (((latval mod 1000000)/10000).truncate) + ((((latval mod 10000)/100).truncate)/60) + (((latval mod 100)/3600))
 ddlat.SetFormat("dd.dddddd") 
 tbv.SetValueNumber(newf2,r,ddlat)  

end

' Stop editing the table
tbv.SetEditable(false)

