Jump to content

Insert separator lines of coordinates in a txt file


teknomatika

Recommended Posts

  • Replies 26
  • Created
  • Last Reply

Top Posters In This Topic

  • MSasu

    10

  • teknomatika

    10

  • pBe

    6

  • eldon

    1

Top Posters In This Topic

Posted Images

This will convert a file from one format to another - you should adjust the width of the columns if case:

;;; Convert for Teknomatika's Data File (11-VII-2012)
(defun c:CTDF( / widthColumns fileOld fileNew streamOld streamNew stringOld stringNew indexColumn )
;"5001100926.946000096.8278000221.264000NL"
(vl-load-com)
(setq widthColumns '(8 10 10 10))   [color=red]<-- Validate the width of the data columns !!![/color]

(if (setq fileOld (getfiled "Select file to convert" "C:\\" "*" 0))
 (progn
  (setq fileNew (strcat (vl-filename-directory fileOld)
                        (vl-filename-base      fileOld)
                        "_fixed"
                        (vl-filename-extension      fileOld)))
  (setq streamOld (open fileOld "r")
        streamNew (open fileNew "w"))

  (while (setq stringOld (read-line streamOld))
   (setq indexColumn 1
         stringNew "")

   (foreach widthTemp widthColumns
    (setq stringNew (strcat stringNew
                            ","                            
                            (substr stringOld indexColumn widthTemp))
          indexColumn (+ indexColumn widthTemp))
   )
   (setq stringNew (strcat (substr stringNew 2)
                           ","
                           (substr stringOld indexColumn)))

   (write-line stringNew streamNew)
  )

  (setq streamOld (close streamOld)
        streamNew (close streamNew))

  (startapp "NOTEPAD" fileNew)
 )
)

(princ)
)

(Sorry, but I cannot test it right now.)

Link to comment
Share on other sites

This will convert a file from one format to another - you should adjust the width of the columns if case:

;;; Convert for Teknomatika's Data File (11-VII-2012)
(defun c:CTDF( / widthColumns fileOld fileNew streamOld streamNew stringOld stringNew indexColumn )
;"5001100926.946000096.8278000221.264000NL"
(vl-load-com)
(setq widthColumns '(8 10 10 10))   [color=red]<-- Validate the width of the data columns !!![/color]

(if (setq fileOld (getfiled "Select file to convert" "C:\\" "*" 0))
 (progn
  (setq fileNew (strcat (vl-filename-directory fileOld)
                        (vl-filename-base      fileOld)
                        "_fixed"
                        (vl-filename-extension      fileOld)))
  (setq streamOld (open fileOld "r")
        streamNew (open fileNew "w"))

  (while (setq stringOld (read-line streamOld))
   (setq indexColumn 1
         stringNew "")

   (foreach widthTemp widthColumns
    (setq stringNew (strcat stringNew
                            ","                            
                            (substr stringOld indexColumn widthTemp))
          indexColumn (+ indexColumn widthTemp))
   )
   (setq stringNew (strcat (substr stringNew 2)
                           ","
                           (substr stringOld indexColumn)))

   (write-line stringNew streamNew)
  )

  (setq streamOld (close streamOld)
        streamNew (close streamNew))

  (startapp "NOTEPAD" fileNew)
 )
)

(princ)
)

(Sorry, but I cannot test it right now.)

 

Mircea,

Excellent.

It works well.

I note also that I can control the position or the introduction of the comma separator editing the variable widthColumns

 

Fantastic.:D

Thanks for the help.

Link to comment
Share on other sites

FWIW:

If length is indeed constant

 

(defun c:demo (/ data)
(setq data '("09F15004128012.1640000103.357600113.245600OD"
            "09F1500412818.69700000100.978600142.721400RD"
            "09F1500412824.80900000101.967000112.053400RD"
            "09F1500412834.72000000101.520800112.992800RD"
            "09F1500412843.94700000102.08440067.2318000RD"
            "09F1500412851.67800000103.435000351.147600RD"
            "09F1500412861.76000000103.541200226.526200RD"
            "09F1500412878.81200000100.714400187.285600RD"))
(foreach str Data
(print      
(strcat (substr str 1 
(apply 'strcat
       (mapcar (function
                     (lambda (j)
                           (strcat ","
                                   (substr str
                                           (car j)
                                           (cadr j)))))
               '((9 4) (13 10) (23 10) (33 10) (43 2))))))
)
(princ)
     )

 

Command: demo

"09F15004,1280,12.1640000,103.357600,113.245600,OD"

"09F15004,1281,8.69700000,100.978600,142.721400,RD"

"09F15004,1282,4.80900000,101.967000,112.053400,RD"

"09F15004,1283,4.72000000,101.520800,112.992800,RD"

"09F15004,1284,3.94700000,102.084400,67.2318000,RD"

"09F15004,1285,1.67800000,103.435000,351.147600,RD"

"09F15004,1286,1.76000000,103.541200,226.526200,RD"

"09F15004,1287,8.81200000,100.714400,187.285600,RD"

 

Dmo2

(defun c:demo2 ()
(defun _split (str)
     (mapcar (function (lambda (j)
        (substr str (car j) (cadr j))))
           '((9 4) (13 10) (23 10) (33 10) (43 2))))
(setq data '("09F15004128012.1640000103.357600113.245600OD"
            "09F1500412818.69700000100.978600142.721400RD"
            "09F1500412824.80900000101.967000112.053400RD"
            "09F1500412834.72000000101.520800112.992800RD"
            "09F1500412843.94700000102.08440067.2318000RD"
            "09F1500412851.67800000103.435000351.147600RD"
            "09F1500412861.76000000103.541200226.526200RD"
            "09F1500412878.81200000100.714400187.285600RD"
            )
    codelist nil )
(foreach stringOld Data      
(if (setq code (assoc (setq StationCode (substr stringOld 1 ) codelist))
      (setq codelist (subst (cons (car code)(list
                                     (append (cadr code) (list (_split  stringold)))))
                               code codelist))
         (setq codelist (cons (list  StationCode
                            (list (_split  stringOld))) codelist))
   )
)      
(foreach df codelist 
     (print (car df))
     (foreach itm (cadr df)
            (print itm)(princ))      
     )
     )

 

Command: demo2

"09F15004"

("1280" "12.1640000" "103.357600" "113.245600" "OD")

("1281" "8.69700000" "100.978600" "142.721400" "RD")

("1282" "4.80900000" "101.967000" "112.053400" "RD")

("1283" "4.72000000" "101.520800" "112.992800" "RD")

("1284" "3.94700000" "102.084400" "67.2318000" "RD")

("1285" "1.67800000" "103.435000" "351.147600" "RD")

("1286" "1.76000000" "103.541200" "226.526200" "RD")

("1287" "8.81200000" "100.714400" "187.285600" "RD")

 

HTH

Edited by pBe
Link to comment
Share on other sites

Pbe,

Being different from the solution of Mircea, works equally well.

Thanks for the help. It was very important.

Link to comment
Share on other sites

Pbe,

Being different from the solution of Mircea, works equally well.

Thanks for the help. It was very important.

 

Good for you, Both codes are not complete. it just shows you how to parse the string after "reading" the file.

The frist demo rouitne will add the "comma" separator and you can overwrite or create a new file similar to MSasu's code.

 

The second Demo rouitne demonstrates splitting the string to a list

(Station code (( "point number" "X" "Y" "Z" "Code Point")( "point number" "X" "Y" "Z" "Code Point")....) ready for whatever purposes. its a direct way of reading and using the data without re-writing the source data

 

Glad you find it useful

 

Cheers

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...