AutoHotkey Community

It is currently May 27th, 2012, 10:34 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: December 2nd, 2007, 5:53 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
wikipedia wrote:
There are three basic forms of a coordinate.

1. Coordinate containing degrees (integer), minutes (integer), and seconds (integer, or real number) (DMS).
2. Coordinate containing degrees (integer) and minutes (real number) (MinDec).
3. Coordinate containing only degrees (real number) (DegDec).


These functions convert the coordinates between these basic forms.

Valid Formats are:
Code:
DMS)    87° 43' 41.00'' N       87° 43' 41.00'' W
MinDec) 87° 43.68' N            87° 43.68' W
DegDec) 87.7280555556          -87.7280555556


More information on wikipedia

Code:
Convert_DMS2DegDec(DMSLat, DMSLon, ByRef DegDecLat, ByRef DegDecLon){
  RegExMatch(DMSLat, "([snSN]?)(\d+)\D+(\d+)\D+([\d.]*)[^snSN]+([snSN]?)", Lat)
  RegExMatch(DMSLon, "([weoWEO]?)(\d+)\D+(\d+)\D+([\d.]*)[^weoWEO]+([weoWEO]?)", Lon)
  Save_FormatFloat := A_FormatFloat
  SetFormat Float, 0.6
  DegDecLat := (Lat2 + (Lat3 * 60 + Lat4) / 3600) * ((InStr(Lat1,"S") OR InStr(Lat5,"S")) ? -1 : 1)
  DegDecLon := (Lon2 + (Lon3 * 60 + Lon4) / 3600) * ((InStr(Lon1,"W") OR InStr(Lon5,"W")) ? -1 : 1)
  SetFormat Float, %Save_FormatFloat%
}

Convert_DMS2MinDec(DMSLat, DMSLon, ByRef MinDecLat, ByRef MinDecLon){
  RegExMatch(DMSLat, "[snSN]?\d+\D+((\d+)\D+([\d.]*)'*)[^snSN]+[snSN]?", Lat)
  RegExMatch(DMSLon, "[weoWEO]?\d+\D+((\d+)\D+([\d.]*)'*)[^weoWEO]+[weoWEO]?", Lon)
  Save_FormatFloat := A_FormatFloat
  SetFormat Float, 0.2
  MinLat := Lat2 + Lat3 / 60
  MinLon := Lon2 + Lon3 / 60
  MinDecLat := RegExReplace(DMSLat, "([snSN]?\d+\D+)\d+\D+[\d.]*'*([^snSN]+[snSN]?)", "$1" MinLat "'$2")
  MinDecLon := RegExReplace(DMSLon, "([weoWEO]?\d+\D+)\d+\D+[\d.]*'*([^weoWEO]+[weoWEO]?)", "$1" MinLon "'$2")
  SetFormat Float, %Save_FormatFloat%
}
Convert_DegDec2DMS(DegLat, DegLon, ByRef DMS1, ByRef DMS2){
  Dir1 := (DegLat > 0 ? "N" : (DegLat < 0 ? "S" : ""))
  Dir2 := (DegLon > 0 ? "E" : (DegLon < 0 ? "W" : ""))
  Save_FormatFloat := A_FormatFloat
  Loop,2{
    SetFormat Float, 0.99
    DegDec := Abs(A_Index = 1 ? DegLat : DegLon)
    Deg := Floor(DegDec)
    Fract := (DegDec - Deg) * 60
    Min := Floor(Fract)
    SetFormat Float, 0.2
    Sec := (Fract - Min) * 60
    DMS%A_Index% := Deg "° " Min "' " Sec "'' " Dir%A_Index%
  }
  SetFormat Float, %Save_FormatFloat%
}
Convert_DegDec2MinDec(DegLat, DegLon, ByRef MinDec1, ByRef MinDec2){
  Dir1 := (DegLat > 0 ? "N" : (DegLat < 0 ? "S" : ""))
  Dir2 := (DegLon > 0 ? "E" : (DegLon < 0 ? "W" : ""))
  Save_FormatFloat := A_FormatFloat
  Loop,2{
    SetFormat Float, 0.99
    DegDec := Abs(A_Index = 1 ? DegLat : DegLon)
    Deg := Floor(DegDec)
    SetFormat Float, 0.2
    Min := (DegDec - Deg) * 60
    MinDec%A_Index% := Deg "° " Min "' " Dir%A_Index%
  }
  SetFormat Float, %Save_FormatFloat%
}
Convert_MinDec2DMS(MinDecLat, MinDecLon, ByRef DMSLat, ByRef DMSLon){
  RegExMatch(MinDecLat, "[snSN]?\d+\D+([\d.]*)[^snSN]+[snSN]?", Lat)
  RegExMatch(MinDecLon, "[weoWEO]?\d+\D+([\d.]*)[^weoWEO]+[weoWEO]?", Lon)
  MinLat := Floor(Lat1)
  MinLon := Floor(Lon1)
  Save_FormatFloat := A_FormatFloat
  SetFormat Float, 0.2
  SecLat := (Lat1 - MinLat) * 60
  SecLon := (Lon1 - MinLon) * 60
  DMSLat := RegExReplace(MinDecLat, "([snSN]?\d+\D+)[\d.]*([^snSN]+[snSN]?)", "$1" MinLat "' " SecLat "'$2")
  DMSLon := RegExReplace(MinDecLon, "([weoWEO]?\d+\D+)[\d.]*([^weoWEO]+[weoWEO]?)", "$1" MinLat "' " SecLon "'$2")
  SetFormat Float, %Save_FormatFloat%
}
Convert_MinDec2DegDec(MinDecLat, MinDecLon, ByRef DegDecLat, ByRef DegDeclon){
  RegExMatch(MinDecLat, "([snSN]?)(\d+)\D+([\d.]*)[^snSN]+([snSN]?)", Lat)
  RegExMatch(MinDecLon, "([weoWEO]?)(\d+)\D+([\d.]*)[^weoWEO]+([weoWEO]?)", Lon)
  Save_FormatFloat := A_FormatFloat
  SetFormat Float, 0.6
  DegDecLat := (Lat2 + Lat3 / 60) * ((InStr(Lat1,"S") OR InStr(Lat4,"S")) ? -1 : 1)
  DegDecLon := (Lon2 + Lon3 / 60) * ((InStr(Lon1,"W") OR InStr(Lon4,"W")) ? -1 : 1)
  SetFormat Float, %Save_FormatFloat%
}

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 25th, 2008, 5:07 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Cool I didn't know about this before. You make good examples of regex here.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 10 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group