Shop OBEX P1 Docs P2 Docs Learn Events
Converting Latitude string to Float — Parallax Forums

Converting Latitude string to Float

pgbpsupgbpsu Posts: 460
edited 2008-11-21 00:21 in Propeller 1
I'm trying to convert a Latitude string to a float and I'm having LOTS of trouble. My GPS spits out a rather long string for the position:
ddmm.mmmmmm for latitude and dddmm.mmmmmm for longitude. I'm parsing the gps correctly (using Perry's GPS code). I know that because displaying the lat and lon works just fine:
   uarts.str(debug,gps.latitude)




To get these to floats I thought I'd strip off the integer degrees first, then the integer minutes, and finally the factional minutes. If I had those all as integers I could reassemble the position as a float.

What I'm unable to do is strip out certain parts of the string by position. I want to do something like this:
DD := num_from_str(latitude[noparse][[/noparse]0..1])
intMM := num_from_str(latitude[noparse][[/noparse]2..3])
fracMM:= num_from_str(latitude[noparse][[/noparse]5..10])




I realize I can't do that directly, but that's the idea behind what I want to do. It seems I need to make a buffer where I can temporarily store the substring:

long degrees ' final location of degrees as int
byte  DD(4)  ' make this long enough to hold DD or DDD and string terminator
' had to use () above because webpage wasn't showing brackets




I believe I have a pointer to the string containing the latitude which I need to copy into DD and null terminate:

bytemove(@DD,latitude,2)
DD(2):=0  ' null terminal this string; same problem with () and brackets
degrees := num_from_str(DD,DEC)





This doesn't work, which unfortunately doesn't surprise me. I feel like I've come a long way with the Prop, but strings still baffle me!
Any suggestions???

Thanks,
pgb

shakehead.gif

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-20 17:09
    In the BYTEMOVE, you need the address of the source as well as the destination. If your original latitude string is stored in the array "latitude", you need to do: BYTEMOVE(@DD,@latitude,2)
    followed by: DD[noparse][[/noparse] 2 ] := 0

    You could also do the conversion directly with:
    degrees := (latitude[noparse][[/noparse] 0 ] - "0")*10 + (latitude[noparse][[/noparse] 1 ] - "0")

    and:
    intMinutes := (latitude[noparse][[/noparse] 2 ] - "0")*10 + (latitude[noparse][[/noparse] 3 ] - "0")

    Post Edited (Mike Green) : 11/20/2008 5:17:13 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-20 17:19
    The fractional minutes could also be done by:
    fractMin := 0
    repeat ptr from 5 to 10
       fractMin := fractMin * 10 + (latitude[noparse][[/noparse]ptr] - "0")
    
  • Chuck RiceChuck Rice Posts: 210
    edited 2008-11-20 17:53
    The attached archive may help you. It is based on Perry's code too, but adds some items. Look in the "GPSutilities" object for the convert2Radians method. It converts Lat and Lon strings to FP radians.
  • pgbpsupgbpsu Posts: 460
    edited 2008-11-21 00:21
    Mike and Chuck-

    Thanks for your suggestions. I've gotten it working. My problem was that latitude didn't actually contain the string. It was simply a pointer which I could call from outside the object. I wanted to add this method to the same object. Here's what I ended up with that was a combination of Mike's suggestion and Chuck's example code:

    pub FP_Lat : f_lat 
      dd := (byte[noparse][[/noparse]GPGGAa][noparse][[/noparse]0]-"0")*10 + byte[noparse][[/noparse]GPGGAa]-"0"
      mm := (byte[noparse][[/noparse]GPGGAa]-"0")*10 + byte[noparse][[/noparse]GPGGAa]-"0"
    
      frac_mm := 0
      repeat dptr from 5 to 10
         frac_mm := frac_mm * 10 + (byte[noparse][[/noparse]GPGGAa][noparse][[/noparse]dptr] - "0")
    
      f_lat := FMath.fdiv(FMath.FFloat(frac_mm),1_000_000.0)
      f_lat := FMath.fadd(f_lat,FMath.FFloat(mm))
      f_lat := FMath.fdiv(f_lat,60.0)
      f_lat := FMath.fadd(FMath.FFloat(dd),f_lat) 'Convert to decimal degrees
    
      if strcomp(N_S,string("S"))
        f_lat := FMath.fneg(f_lat)
      return  
    
    
    



    Thanks again to both of you.

    pgb
Sign In or Register to comment.