Convert Geo to Degree

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Convert Geo to Degree

Post by infratec »

Since in 'coding questions' is a thread about distance calculation on earth:
http://www.purebasic.fr/english/viewtop ... 13&t=47853

Only for your convenience:

Code: Select all

Procedure.f ConvertGeoToDeg(Value$)
 
  Protected Result.f = 0.0
  Protected Direction$
  Protected Direction = 1
  Protected Pos
 
  ReplaceString(Value$, ",", ".", #PB_String_InPlace)
 
  If FindString(Value$, "°", 1) = 0
    ProcedureReturn ValF(Value$)
  EndIf
 
  ReplaceString(Value$, "''", #DQUOTE$ + " ", #PB_String_InPlace)
 
  Direction$ = Left(Value$, 1)
  If Direction$ > "A"
    Value$ = Mid(Value$, 2)
  Else
    Direction$ = Right(Value$, 1)
  EndIf
 
  If Direction$ = "S" Or Direction$ = "W"
    Direction = -1
  EndIf
 
  Pos = FindString(Value$, "°", 1)
  If Pos
    Result = Val(Value$)
    Value$ = Mid(Value$, Pos + 1)
    Pos = FindString(Value$, "'", 1)
    If Pos
      Result + (Val(Value$) / 60)
      Value$ = Mid(Value$, Pos + 1)
      Pos = FindString(Value$, #DQUOTE$, 1)
      If Pos
        Result + (ValF(Value$) / 3600)
      EndIf
    EndIf
  EndIf
 
  ProcedureReturn Result * Direction
 
EndProcedure


Debug ConvertGeoToDeg("16°19'28.29''")
Debug ConvertGeoToDeg("16° 19' 28.29'' S")
Debug ConvertGeoToDeg("S 16° 19' 28.29''")
Debug ConvertGeoToDeg("16.234")
Debug ConvertGeoToDeg("16,234")
Bernd
Last edited by infratec on Mon Oct 17, 2011 7:23 am, edited 7 times in total.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2139
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Convert Geo to Degree

Post by Andre »

Thanks a lot, will be of good use for me! :D
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Convert Geo to Degree

Post by infratec »

Hi,

added an if which search for °
if not found I interprete the value as a float value.

Bernd

@Andre
nice, that I can help you a bit. :D
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Convert Geo to Degree

Post by infratec »

Hi,

small addition:
now , and . can be used as delimiter
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Convert Geo to Degree

Post by infratec »

Hi,

again a small addition:
As second you can now use 2 ' or one ".

Bernd
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Convert Geo to Degree

Post by Psychophanta »

The Inverse function, which converts radians or degrees to string Degrees, minutes and seconds:

Code: Select all

Procedure$ ToDegMinSec(i.d,radians.b=0)
  If radians:i=Degree(i):EndIf
  If i<0 And i>-1.0:Protected Deg$="-":EndIf
  Deg$+Str(Int(i))+"°"
  i=Abs((i-Int(i))*60)
  Deg$+Str(Int(i))+"'"
  Deg$+StrD((i-Int(i))*60)+"''"
  ProcedureReturn Deg$
EndProcedure
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply