Author Topic: Modify a bearing (string)  (Read 2376 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Modify a bearing (string)
« on: October 11, 2004, 03:35:50 PM »
For the following problem I came up with the following solution, is it unsound?

Given a string (N 89°56'3" E) add a zero in front of any single digit. i.e. N 89°56'03" E. The string can contain no more than 13 characters and will always start with either "N<space>,S<space>" and end with either "E<space>, W<space>". It will always represent a cardinal direction.

More examples:
 change;
 N 78°8'4" E --> N 78°08'04" E
 N 5°14'9" W --> N 05°14'09" W

Code: [Select]
;;; FUNCTION
;;; take a bearing as in (N 89°6'33" E) and add a 0 to the minutes(6)
;;; so the output looks like (N 89°06'33" E)
;;;
;;; ARGUMENTS
;;; string in the form of "N 89°56'3\" E"
;;;
;;; USAGE
;;; (fix-bearing "N 89°56'3\" E")
;;; (fix-bearing "N 89°6'33\" E")
;;; (fix-bearing "N 9°26'33\" E")
;;;
;;;
;;; PLATFORMS
;;; 2000+
;;;
;;; AUTHOR
;;; Copyright© 2004 Mark S. Thomas
;;; mark.thomas@theswamp.org
;;;
;;; VERSION
;;; 1.0 Mon Oct 11, 2004 12:43:41


;;; "N 49°26'33\" E" normal string


(defun fix-bearing (str / str_l cntr lst)

  (cond
    ((< (strlen str) 13); a 'normal' string will contain exactly 13 characters
          (setq cntr 0       ; make sure counter starts at 0
                lst '()      ; create a empty list
                str_l (vl-string->list str) ; conv. string to list
                )

          (cond ((= (vl-string-position 176 str) 3)
                 ;; N 9°26'33" E = input string
                 ;; 0123
                 ;; we know that the ° should be in the 4th position
                 ;; if it's not then we need to add a '0' to the degree
                 ;; N 09°26'33" E

                 (while (< cntr (length str_l))

                        ;; creates a list, first item last
                        (setq lst (vl-list* (nth cntr str_l) lst))

                        (if (and
                              (= (car lst) 32); space
                              (< (length lst) 3); less than 3 item in the list
                              )
                          (setq lst (append '(48) lst))
                          )

                        (setq cntr (1+ cntr))

                        ); end of loop

                 (fix-bearing (vl-list->string (reverse lst)))
                 ); 1st cond.

                ((= (vl-string-position 39 str) 6)
                 ;; N 89°6'33" E
                 ;; 0123456
                 ;; we know that the ' should be in the 7th position
                 ;; if it's not then we need to add a 0 to the minutes
                 ;; N 89°06'33" E

                 (while (< cntr (length str_l))

                        ;; creates a list, first item last
                        (setq lst (vl-list* (nth cntr str_l) lst))

                        (if (= (car lst) 176); °
                          (setq lst (append '(48) lst))
                          )

                        (setq cntr (1+ cntr))
                        )
                 (fix-bearing (vl-list->string (reverse lst)))
                 ); 2nd cond.

                ((= (vl-string-position 34 str) 9)  
                 ;; N 89°16'3" E
                 ;; 0123445678
                 ;; we know that the " should be in the 9th position
                 ;; if it's not then we need to add a 0 to the seconds
                 ;; N 89°16'03" E

                 (while (< cntr (length str_l))

                        ;; creates a list, first item last
                        (setq lst (vl-list* (nth cntr str_l) lst))

                        (if (= (car lst) 39); '
                          (setq lst (append '(48) lst))
                          )

                        (setq cntr (1+ cntr))
                        )
                 (fix-bearing (vl-list->string (reverse lst)))
                 ); 3rd cond.

                ); cond
          ); 1st cond.
    (T str)
    ); cond

  )
TheSwamp.org  (serving the CAD community since 2003)

David Bethel

  • Swamp Rat
  • Posts: 656
Modify a bearing (string)
« Reply #1 on: October 11, 2004, 06:30:25 PM »
mark,

I would the degree symbol would be a hard 1 to catch.  Also string length count with the ecsacpe characters

I'd try something a bit simplier

Code: [Select]
 (if (/= "\"" (substr input 4 1))
      (setq input (strcat (substr input 1 4) "0" (substr input 5))))
  (if (/= (chr 248) (substr input 6 1))
      (setq input (strcat (substr input 1 6) "0" (substr input 8))))
  (if (/= "\"" (substr input 8 1))
      (setq input (strcat (substr input 1 8) "0" (substr input 9))))



I didn't check all of positions etc.  Also the order of the test must be first to last.  The string character postions must be constant.  _David
R12 Dos - A2K

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Modify a bearing (string)
« Reply #2 on: October 11, 2004, 07:38:04 PM »
Just for fun ...

Code: [Select]
(defun NormalizeBearing ( bearing / foo )
    (defun foo ( s n / i )
        (strcat
            (substr s 1 (setq i (1+ (vl-string-position n s))))
            "0"
            (substr s (1+ i))
        )    
    )
    (foreach x '(("* #°*" . 32)("*'#\"*" . 39)("*°#'*" . 176))
        (if (wcmatch bearing (car x))
            (setq bearing (foo bearing (cdr x)))
        )
    )
    bearing
)

An example of it in action...

Code: [Select]
(foreach x
   '(  
        "N 89°56'3\" E"
        "N 89°6'33\" E"
        "N 9°26'33\" E"
        "N 9°6'3\" E"
    )
    (princ
        (strcat
            ";;  "            
            (substr (strcat x "      ") 1 13)
            " => "
            (NormalizeBearing x)
            "\n"
        )    
    )
    (princ)
)

Result:

Code: [Select]
;;  N 89°56'3" E  => N 89°56'03" E
;;  N 89°6'33" E  => N 89°06'33" E
;;  N 9°26'33" E  => N 09°26'33" E
;;  N 9°6'3" E    => N 09°06'03" E

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Modify a bearing (string)
« Reply #3 on: October 12, 2004, 07:24:28 AM »
That's good stuff, thanks guys.
TheSwamp.org  (serving the CAD community since 2003)