Announcement

Collapse
No announcement yet.

Degree Decimal conversion

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Degree Decimal conversion

    Hello. I wrote a function to convert Latitude and Longitude in degree decimal format to DMS.xxx format. If I have 32.4640245 as a latitude, the function converts it to: 323750.484, which seems to be fine.

    Problem: If I have the value -86.4596966, then my function seems to have an error with INT. Taking the INT of the value above (32.4640245), it returns 32. However, taking INT of -86.4596966 returns -87, not -86 for this to work. I seem to need the -86. What do I need to do to correct this?

    Or, is there a premade tested function that I can use?

    Thank you.

    Robert

  • #2
    You could use an absolute value ABS() to calculate it when it's < 0:
    Code:
        IF x < 0 THEN
           y = -INT(ABS(x))
        END IF

    Comment


    • #3
      Thx

      Problem solved. Thank you. Robert

      Comment


      • #4
        Robert, I thought of this a few minutes later: since you know your number is negative once you're in the IF... THEN block, the ABS() function is really not needed. It would be more efficient to do this:
        Code:
            IF x < 0 THEN
               y = -INT(-x)
            END IF

        Comment


        • #5
          Originally posted by John Gleason View Post
          Robert, I thought of this a few minutes later: since you know your number is negative once you're in the IF... THEN block, the ABS() function is really not needed. It would be more efficient to do this:
          Code:
              IF x < 0 THEN
                 y = -INT(-x)
              END IF
          Don't forget the FIX function, John. It will strip off the decimal portion and leave the integer portion as is.

          Comment


          • #6
            Originally posted by Robert E. Carneal View Post
            Hello. I wrote a function to convert Latitude and Longitude in degree decimal format to DMS.xxx format. If I have 32.4640245 as a latitude, the function converts it to: 323750.484, which seems to be fine.
            Are you sure? At a glance it would seem that 32.46xx should be less than 32°30' (not 32°37' + change).

            Here are some tips for Lat / Long conversions that I think I copied from this forum..
            Code:
            Guide for Converting Lat/Long Coordinates.
             
            Northern Latitudes and Easterly Longitudes are + (Positive)
            Southern Latitudes and Westerly Longitudes are - (Negative)
             
            Fractions of a degrees are broken down into minutes and seconds. Each minute represents 
            1/60th of a degree and each second represents 1/60th of a minute (or 1/3600th of a degree). 
            Below are the steps of converting from an angle in degrees-minutes-seconds to decimal degrees 
            and back to degrees-minutes-seconds.
             
            D       = Degrees               Examples:   DM.m = Degrees, Minutes, Decimal Minutes 45° 22.6333”
            .d      = Decimal Degrees                   D.d  = Degrees, Decimal Degrees 45.3772°
            M       = Minutes                           DMS  = Degrees, Minutes, Seconds 45° 22” 38’
            .m      = Decimal Minutes
            S       = seconds
            .s      = Decimal Seconds 
             
            Convert
            From                    To                  Operation .
            DMS                     DM.m 
            45 22 38                45 22.633           Divide S by 60 to get .m     (38/60=.6333)
                                                        Add .m to M to get M.m       (22+.6333=22.6333)
            DM.m                    D.d 
            45 22.6333              45.3772             Divide M.m by 60 to get .d   (22.6333/60=.3772)
                                                        Add .d to D to get D.d       (45+.3772=45.3772)
            D.d                     DM.m 
            45.3772                 45 22.6333          Multiply .d by 60 to get M.m (.3772*60=22.6333)
            DM.m                    DMS 
            45 22.6333              45 22 38            Multiply .m by 60 to get S   (.6333*60=38)
            Rgds, Dave

            Comment


            • #7
              However, taking INT of -86.4596966 returns -87, not -86 for this to work. I seem to need the -86.
              CEIL()?
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                FIX probably is what's needed here - don't want any rounding to happen during the conversion.. Here are a couple of functions based on the tips posted above:

                Functions to convert DecDeg <-> DMS
                Code:
                Function DecDeg2DMS(ByVal DecDeg As Double, ByVal DecPoints As Long) As Double
                 Dim DMS As Double, ds As Single
                  Dim Sign As Long, D As Long, M As Long, S As Long
                 
                   Sign = 1
                   If DecDeg < 0 Then  DecDeg = DecDeg * -1 : Sign = -1
                   D = FIX(DecDeg)
                   DecDeg = (DecDeg - D) * 60
                   M = Fix(DecDeg)
                   DecDeg = (DecDeg - M) * 60
                   S = Fix(DecDeg)
                   ds = DecDeg - S
                   DMS = (D * 10000) + (M * 100) + S + Round(ds, DecPoints)
                 
                  Function = DMS  * Sign
                 End Function
                '------------------/
                 
                 Function DMS2DecDeg(ByVal DMS As Double, ByVal DecPoints As Long) As Double
                  Dim DecDeg As Double
                  Dim Sign As Long, D As Long, M As Long
                  Dim S As Single, dm As Single
                 
                   Sign = 1
                   If DMS < 0 Then DMS = DMS * -1 : Sign = -1
                   DMS = DMS / 10000
                   D = FIX(DMS)
                   DMS = (DMS - D) * 100
                   M = FIX(DMS)
                   S = (DMS - M) * 100
                   dm = S / 60
                   DecDeg = Round((M + dm) / 60, DecPoints)
                 
                  Function = (D + DecDeg) * Sign
                 End Function
                 '-----------------/
                 
                CLS
                 
                  Dim DecDeg As Double, DMS As Double
                 
                    DecDeg = 32.4640245
                     ? "DMS   : "+str$(DecDeg2DMS(DecDeg, 4)), "DecDeg: "+ str$(DecDeg)
                 
                    DMS = 322750.4882
                     ? "DecDeg: "+str$(DMS2DecDeg(DMS, 7)), "DMS   : "+ str$(DMS)
                END
                Rgds, Dave

                Comment


                • #9
                  Taking the INT of the value above (32.4640245), it returns 32. However, taking INT of -86.4596966 returns -87, not -86 for this to work. I seem to need the -86. What do I need to do to correct this?
                  Originally posted by Michael Mattias View Post
                  CEIL()?
                  Sorry! Try again. CEIL() would work for negative numbers in this application, but would not work for positive numbers.
                  CEIL(-86.4596966 ) = -86 CEIL(32.4640245) = 33

                  Obviously, the correct function to use would be FIX().

                  Comment

                  Working...
                  X