TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: pedroantonio on August 30, 2017, 04:15:27 AM

Title: Convert coordinate files to x,y
Post by: pedroantonio on August 30, 2017, 04:15:27 AM
hI. I am searching for a lisp to convert coordinate files files to x,y  (*.txt)

1) p,x,y,z --->x,y
2) p,x,y ---->x,y
3) p x y z ---> x,y
4) p x y ----> x,y


i know that i can do it with excel but i need something faster.

Thanks




Title: Re: Convert coordinate files to x,y
Post by: pedroantonio on August 30, 2017, 05:41:52 AM
any ideas ?
Title: Re: Convert coordinate files to x,y
Post by: Pad on August 30, 2017, 07:14:24 AM
you could try this
http://www.lancaster.ac.uk/~steveb/cpaap/pfe/default.htm
and make yourself a macro for each variation.
Title: Re: Convert coordinate files to x,y
Post by: Mark on August 30, 2017, 08:04:44 AM
Do it with AWK! (https://www.theswamp.org/index.php?topic=8476.0)
:)
Title: Re: Convert coordinate files to x,y
Post by: pedroantonio on August 30, 2017, 10:22:22 AM
i can not understand it can you give me an example for    p x y ----> x,y  ?

thanks
Title: Re: Convert coordinate files to x,y
Post by: pedroantonio on August 30, 2017, 11:10:41 AM
i am thinking something like this

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ )
  2.              
  3.   (setq title "select file"
  4.         fname (getfiled title path "txt" 0)
  5.         path  (if (setq tmp (vl-filename-directory fname))
  6.                 (strcat tmp "\\")
  7.                 ""
  8.               )
  9.   (setq f (open (strcat path
  10.                         ""
  11.                         (vl-filename-base fname)
  12.                         ".pl"
  13.                 )
  14.                 "w"
  15.           )
  16.  
  17.     (write-line (strcat (rtos (cadr i) 2 3)    
  18.                         ","
  19.                          (rtos (caddr i) 2 3)    
  20.  
  21.  
  22.                 )
  23.                 f
  24.     )
  25.   )
  26. )
  27.  )
  28.  
  29.  

but is not working. Any ideas?

Thanks
Title: Re: Convert coordinate files to x,y
Post by: mjfarrell on August 30, 2017, 11:16:27 AM
can you post a sample of the  " coordinate files files"  to x,y you are starting with?
And what you want them to look like when done?

It might be possible to use QGIS or similar to do this.
Title: Re: Convert coordinate files to x,y
Post by: Mark on August 30, 2017, 11:24:27 AM
i can not understand it can you give me an example for    p x y ----> x,y  ?
something like this
Code: [Select]
gawk "{print $2 $3 }" input-file.txt > output-file.txt
Title: Re: Convert coordinate files to x,y
Post by: Mark on August 30, 2017, 11:38:13 AM
i can not understand it can you give me an example for    p x y ----> x,y  ?
It would actually be ...

Code: [Select]
gawk "{print $2\",\" $3 }" data.txt > outputfile.txt
Its all in that link i posted earlier.
Title: Re: Convert coordinate files to x,y
Post by: fools on August 30, 2017, 11:39:34 AM
i can not understand it can you give me an example for    p x y ----> x,y  ?

thanks

Code - Auto/Visual Lisp: [Select]
  1. (DEFUN c:test (/ FF FFN TXT TXTS)
  2.   (SETQ ffn  (GETFILED "Select file" "" "txt" 0)
  3.         ff   (OPEN ffn "r")
  4.         txts nil
  5.   )
  6.   (WHILE (SETQ txt (READ-LINE ff)) (SETQ txts (CONS txt txts)))
  7.   (CLOSE ff)
  8.   ;;(VL-FILE-RENAME ffn (STRCAT (SUBSTR ffn 1 (- (STRLEN ffn) 3)) "bak"))
  9.   (SETQ txts (MAPCAR 'CDR (MAPCAR 'F_Str2Lst (MAPCAR 'F_NoExtraSpaces (REVERSE txts)))))
  10.   (SETQ ff (OPEN (STRCAT (SUBSTR ffn 1 (- (STRLEN ffn) 3)) "pl") "w"))
  11.   (FOREACH item txts (WRITE-LINE (STRCAT (CAR item) "," (CADR item)) ff))
  12.   (CLOSE ff)
  13. )
  14.  
  15. (DEFUN F_NoExtraSpaces (str / pos)
  16.   (SETQ str (VL-STRING-TRIM " " str))
  17.   (WHILE (SETQ pos (VL-STRING-SEARCH "  " str pos)) (SETQ str (VL-STRING-SUBST " " "  " str pos)))
  18.   str
  19. )
  20.  
  21. (DEFUN F_Str2Lst (str / pos)
  22.   (IF (SETQ pos (VL-STRING-SEARCH " " str))
  23.     (CONS (SUBSTR str 1 pos) (F_Str2Lst (SUBSTR str (+ (STRLEN " ") pos 1))))
  24.     (LIST str)
  25.   )
  26. )
Title: Re: Convert coordinate files to x,y
Post by: pedroantonio on August 30, 2017, 12:47:38 PM
I attach a *.txt file with the coordinates.I try fools code but the *.pl file is empty
Title: Re: Convert coordinate files to x,y
Post by: pedroantonio on August 30, 2017, 12:58:33 PM
i want the export file to be like this

Quote
318349.74,4231547.73
318445.61,4231568.79
318446.37,4231561.59
318443.36,4231549.82
318438.82,4231539.87
318428.56,4231532.33
318425.25,4231523.57
318428.55,4231513.05
318442.57,4231516.64
318455.87,4231506.94
318473.12,4231495.81
318473.84,4231490.78
318468.81,4231486.11
318468.45,4231473.18
318464.85,4231451.63
318460.90,4231446.96
318447.24,4231446.60
318439.70,4231458.45
318431.79,4231475.34
318424.60,4231475.34
318399.26,4231473.08
318390.21,4231473.39
318382.97,4231477.91
318379.64,4231487.26
318376.02,4231521.35
318372.99,4231537.20
318368.15,4231541.13
318354.88,4231545.65
318349.74,4231547.73



thanks
Title: Re: Convert coordinate files to x,y
Post by: Mark on August 30, 2017, 01:24:44 PM
You're not even trying are you?

Code: [Select]
gawk "{print $2\",\" $3 }" 1.txt
318349.74,4231547.73
318445.61,4231568.79
318446.37,4231561.59
318443.36,4231549.82
318438.82,4231539.87
318428.56,4231532.33
318425.25,4231523.57
318428.55,4231513.05
318442.57,4231516.64
318455.87,4231506.94
318473.12,4231495.81
318473.84,4231490.78
318468.81,4231486.11
Title: Re: Convert coordinate files to x,y
Post by: pedroantonio on August 30, 2017, 01:35:49 PM
Mark i am not using scripts.I use only lisp codes
Title: Re: Convert coordinate files to x,y
Post by: mjfarrell on August 30, 2017, 02:37:32 PM
yes you could read that file into QGIS (its free GIS software)

new layer from delimited file...
have it read your desired columns from that file

then export it as comma delimited file or shp file...or lots of formats.

no list/code/ or scripts required
Title: Re: Convert coordinate files to x,y
Post by: MP on August 30, 2017, 03:08:19 PM
Quick & Dirty concept code.

Code: [Select]
(defun _TSV->List ( tsvfilename / foo i handle s result )

    (defun foo ( s / result )
        (while (setq i (vl-string-position 9 s nil t))
            (setq
                result (cons (substr s (+ 2 i)) result)
                s      (substr s 1 i)
            )
        )
        (cons s result)
    )

    (if (setq handle (open tsvfilename "r"))
        (progn
            (while (setq s (read-line handle))
                (setq result (cons (foo s) result))
            )
            (close handle)
            (vl-remove 'nil (reverse result))
        )
    )
)

Code: [Select]
(defun _List->CSV ( lst tsvfilename / handle )

    (if (setq handle (open tsvfilename "w"))
        (progn
            (foreach r lst
                (princ (car r) handle)
                (foreach x (cdr r) (princ "," handle) (princ x handle))
                (princ "\n" handle)
            )
            (null (close handle))
        )
    )
)

Code: [Select]
;;  read it in
(setq lst (_TSV->List "c:\\docs\\tsv.txt"))
;;  abuse it
(setq alt (mapcar (function (lambda (r) (reverse (mapcar 'set '(x y) (cdr r))))) lst))
;;  write it out
(_List->CSV alt "c:\\docs\\csv_abused.txt")

;;   orig

0    318349.74    4231547.73
1    318445.61    4231568.79
2    318446.37    4231561.59
3    318443.36    4231549.82
4    318438.82    4231539.87
5    318428.56    4231532.33
...

;;   abused

4231547.73,318349.74
4231568.79,318445.61
4231561.59,318446.37
4231549.82,318443.36
4231539.87,318438.82
...


/2¢
Title: Re: Convert coordinate files to x,y
Post by: VovKa on August 30, 2017, 03:30:22 PM
no foo
Code: [Select]
(setq result (cons (read (strcat "(" s ")")) result))
Title: Re: Convert coordinate files to x,y
Post by: MP on August 30, 2017, 05:01:27 PM
If the data hosts non numerical items that technique would (undesirably) yield symbols instead of strings.

Cheers.
Title: Re: Convert coordinate files to x,y
Post by: fools on August 30, 2017, 06:57:20 PM
I attach a *.txt file with the coordinates.I try fools code but the *.pl file is empty

My code is written very rough, just to show the way.
Change space to tab in function F_Str2Lst can convert correctly.

Code - Auto/Visual Lisp: [Select]
  1. (DEFUN c:test (/ FF FFN TXT TXTS)
  2.   (SETQ ffn  (GETFILED "Select file" "" "txt" 0)
  3.         ff   (OPEN ffn "r")
  4.         txts nil
  5.   )
  6.   (WHILE (SETQ txt (READ-LINE ff)) (SETQ txts (CONS txt txts)))
  7.   (CLOSE ff)
  8.   ;;(VL-FILE-RENAME ffn (STRCAT (SUBSTR ffn 1 (- (STRLEN ffn) 3)) "bak"))
  9.   (SETQ txts (MAPCAR 'CDR (MAPCAR 'F_Str2Lst (MAPCAR 'F_NoExtraSpaces (REVERSE txts)))))
  10.   (SETQ ff (OPEN (STRCAT (SUBSTR ffn 1 (- (STRLEN ffn) 3)) "pl") "w"))
  11.   (FOREACH item txts (WRITE-LINE (STRCAT (CAR item) "," (CADR item)) ff))
  12.   (CLOSE ff)
  13. )
  14.  
  15. (DEFUN F_NoExtraSpaces (str / pos)
  16.   (SETQ str (VL-STRING-TRIM " " str))
  17.   (WHILE (SETQ pos (VL-STRING-SEARCH "  " str pos)) (SETQ str (VL-STRING-SUBST " " "  " str pos)))
  18.   str
  19. )
  20.  
  21. (DEFUN F_Str2Lst (str / pos)
  22.   (IF (SETQ pos (VL-STRING-SEARCH "\t" str))
  23.     (CONS (SUBSTR str 1 pos) (F_Str2Lst (SUBSTR str (+ (STRLEN "\t") pos 1))))
  24.     (LIST str)
  25.   )
  26. )
Title: Re: Convert coordinate files to x,y
Post by: pedroantonio on August 30, 2017, 07:08:35 PM
Thank you   :smitten: :smitten:
Title: Re: Convert coordinate files to x,y
Post by: fools on August 30, 2017, 07:10:47 PM
no foo
Code: [Select]
(setq result (cons (read (strcat "(" s ")")) result))

Using read function maybe change data.

Code: [Select]
(SETQ str " 0   318349.74  4231547.73 ")
(read (strcat "(" str ")"))

result:
(0 318350.0 4.23155e+006)
Title: Re: Convert coordinate files to x,y
Post by: kdub_nz on August 30, 2017, 11:53:10 PM
hI. I am searching for a lisp to convert coordinate files files to x,y  (*.txt)

1) p,x,y,z --->x,y
2) p,x,y ---->x,y
3) p x y z ---> x,y
4) p x y ----> x,y


i know that i can do it with excel but i need something faster.

Thanks

to be more in line with the original requirements the translators would need to work successfully on this list.
with the addition of TABS as per the provided file.


0   300000.00   4000000.00 0.0
1   318445.61   4231568.79
2 318446.37,4231561.59,123
3   318443.36,4231549.82
4   318438.82      4231539.87    0.0
5   318428.56,   4231532.33,      0.0
6,318425.25,4231523.57,0.0
7,   318428.55,   4231513.05,   0.0
8   318442.57   4231516.64
9   318455.87   4231506.94
10   318473.12   4231495.81


just saying ...
Title: Re: Convert coordinate files to x,y
Post by: fools on August 31, 2017, 01:20:40 AM
to be more in line with the original requirements the translators would need to work successfully on this list.
with the addition of TABS as per the provided file.

Thanks for remind. I change a function to increase versatility.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ FF FFN TXT TXTS)
  2.   (setq ffn  (getfiled "Select file" "" "txt" 0)
  3.         ff   (open ffn "r")
  4.         txts nil
  5.   )
  6.   (while (setq txt (read-line ff)) (setq txts (cons txt txts)))
  7.   (close ff)
  8.   (setq txts (mapcar '(lambda (x) (STD-STRSPLIT x "\t,; ")) (reverse txts)))
  9.   (setq txts
  10.          (mapcar
  11.            '(lambda (x) (vl-remove-if '(lambda (y) (and (not (equal "0" y)) (zerop (atof y)))) x))
  12.            txts
  13.          )
  14.   )
  15.   (setq ff (open (strcat (substr ffn 1 (- (strlen ffn) 3)) "pl") "w"))
  16.   (foreach item txts (write-line (strcat (cadr item) "," (caddr item)) ff))
  17.   (close ff)
  18.   (princ)
  19. )
  20.  
  21. ;;; The order of chars in delim is not important.
  22. ;;; keeping null tokens, not as with std-strtok.
  23. ;;; Might be renamed to std-string-split
  24. ;;; by Vladimir Nesterowsky
  25. (defun STD-STRSPLIT (s delims / len s1 i c lst)
  26.   (setq delims (vl-string->list delims) ; fixed
  27.         len    (strlen s)
  28.         s1     ""
  29.         i      (1+ len)
  30.   )
  31.   (while (> (setq i (1- i)) 0)
  32.     (setq c (substr s i 1))
  33.     (if (member (ascii c) delims)
  34.       (if (/= i len)                    ; "1,2," -> ("1" "2") and not ("1" "2" "")
  35.         (setq lst (cons s1 lst)
  36.               s1  ""
  37.         )
  38.       )
  39.       (setq s1 (strcat c s1))
  40.     )
  41.   )
  42.   (cons s1 lst)                         ; ",1,2" -> ("" "1" "2")
  43. )


1.txt
0   300000.00   4000000.00 0.0
1   318445.61   4231568.79
2 318446.37,4231561.59,123
3   318443.36,4231549.82
4   318438.82      4231539.87    0.0
5   318428.56,   4231532.33,      0.0
6,318425.25,4231523.57,0.0
7,   318428.55,   4231513.05,   0.0
8   318442.57   4231516.64
9   318455.87   4231506.94
10   318473.12   4231495.81

1.pl
300000.00,4000000.00
318445.61,4231568.79
318446.37,4231561.59
318443.36,4231549.82
318438.82,4231539.87
318428.56,4231532.33
318425.25,4231523.57
318428.55,4231513.05
318442.57,4231516.64
318455.87,4231506.94
318473.12,4231495.81
Title: Re: Convert coordinate files to x,y
Post by: anhquang1989 on August 31, 2017, 02:14:16 AM
This is the norm of my work. It may be right for you. please try
Import txt file, csv with the format: P Y Y Z Code
- different separators like comma, space, tab
- Still works well with other formats like
+ P X Y
+ P X Y Z
+ P Y X
+ P Y X Z
......
Title: Re: Convert coordinate files to x,y
Post by: MP on August 31, 2017, 10:53:24 AM
to be more in line with the original requirements the translators would need to work successfully on this list.
with the addition of TABS as per the provided file.

0   300000.00   4000000.00 0.0
1   318445.61   4231568.79
2 318446.37,4231561.59,123
3   318443.36,4231549.82
4   318438.82      4231539.87    0.0
5   318428.56,   4231532.33,      0.0
6,318425.25,4231523.57,0.0
7,   318428.55,   4231513.05,   0.0
8   318442.57   4231516.64
9   318455.87   4231506.94
10   318473.12   4231495.81


just saying ...

Eschewing the important but I don't have time for "importance of well formed data" discussion I'll ante up this 2 minute quickie:

Code: [Select]
(defun _Normalize ( str )
    (foreach delim '(" " "," "\t")
        (foreach pattern (list (strcat " " delim) (strcat delim " "))
            (while (vl-string-search pattern str)
                (setq str (vl-string-subst delim pattern str))
            )
        )
    )         
    (vl-string-translate " \t" ",," str)
)

Code: [Select]
(progn
    (foreach test
       '(
            "0   300000.00   4000000.00 0.0"
            "1   318445.61   4231568.79"
            "2 318446.37,4231561.59,123"
            "3   318443.36,4231549.82"
            "4   318438.82      4231539.87    0.0"
            "5   318428.56,   4231532.33,      0.0"
            "6,318425.25,4231523.57,0.0"
            "7,   318428.55,   4231513.05,   0.0"
            "8 \t  318442.57   4231516.64"
            "9   318455.87 \t  4231506.94"
            "10   318473.12   4231495.81"   
        )
        (princ (strcat "\n" test " >> " (_Normalize test)))
    )   
    (princ)
)

0   300000.00   4000000.00 0.0        >> 0,300000.00,4000000.00,0.0
1   318445.61   4231568.79            >> 1,318445.61,4231568.79
2 318446.37,4231561.59,123            >> 2,318446.37,4231561.59,123
3   318443.36,4231549.82              >> 3,318443.36,4231549.82
4   318438.82      4231539.87    0.0  >> 4,318438.82,4231539.87,0.0
5   318428.56,   4231532.33,      0.0 >> 5,318428.56,4231532.33,0.0
6,318425.25,4231523.57,0.0            >> 6,318425.25,4231523.57,0.0
7,   318428.55,   4231513.05,   0.0   >> 7,318428.55,4231513.05,0.0
8   318442.57   4231516.64            >> 8,318442.57,4231516.64
9   318455.87    4231506.94           >> 9,318455.87,4231506.94
10   318473.12   4231495.81           >> 10,318473.12,4231495.81


Cheers.
Title: Re: Convert coordinate files to x,y
Post by: Lee Mac on August 31, 2017, 01:36:25 PM
Just to offer an alternative approach:
Code - Auto/Visual Lisp: [Select]
  1. (defun NormaliseList ( src dst lst / rgx rtn )
  2.     (if (setq rgx (vlax-create-object "vbscript.regexp"))
  3.         (progn
  4.             (vl-catch-all-apply
  5.                '(lambda nil
  6.                     (vlax-put-property rgx 'global actrue)
  7.                     (vlax-put-property rgx 'pattern src)
  8.                     (setq rtn (mapcar '(lambda ( x ) (vlax-invoke rgx 'replace x dst)) lst))
  9.                 )
  10.             )
  11.             (vlax-release-object rgx)
  12.             rtn
  13.         )
  14.     )
  15. )
Code - Auto/Visual Lisp: [Select]
  1.     (NormaliseList
  2.         "[^\\s\\,]+[\\s\\,]+([^\\s\\,]+)[\\s\\,]+([^\\s\\,]+).*"
  3.         "$1,$2"
  4.        '(
  5.             "0   300000.00   4000000.00 0.0"
  6.             "1   318445.61   4231568.79"
  7.             "2 318446.37,4231561.59,123"
  8.             "3   318443.36,4231549.82"
  9.             "4   318438.82      4231539.87    0.0"
  10.             "5   318428.56,   4231532.33,      0.0"
  11.             "6,318425.25,4231523.57,0.0"
  12.             "7,   318428.55,   4231513.05,   0.0"
  13.             "8 \t  318442.57   4231516.64"
  14.             "9   318455.87 \t  4231506.94"
  15.             "10   318473.12   4231495.81"  
  16.         )
  17.     )
  18. )

Yields:

"300000.00,4000000.00"
"318445.61,4231568.79"
"318446.37,4231561.59"
"318443.36,4231549.82"
"318438.82,4231539.87"
"318428.56,4231532.33"
"318425.25,4231523.57"
"318428.55,4231513.05"
"318442.57,4231516.64"
"318455.87,4231506.94"
"318473.12,4231495.81"
Title: Re: Convert coordinate files to x,y
Post by: MP on August 31, 2017, 02:39:54 PM
Nicely done Lee, cheers.
Title: Re: Convert coordinate files to x,y
Post by: Lee Mac on August 31, 2017, 05:42:42 PM
Cheers Michael, same to you. :-)
Title: Re: Convert coordinate files to x,y
Post by: fools on September 01, 2017, 09:39:54 PM
Thanks Lee and MP, I learned a lot.
Title: Re: Convert coordinate files to x,y
Post by: DEVITG on September 02, 2017, 12:38:32 AM
Just put , or upload, or whats ever, your sample dwg and your sample xyz value file, as you do it by hand.

Keep it simple sir .. just KISS

Title: Re: Convert coordinate files to x,y
Post by: kdub_nz on September 02, 2017, 02:01:09 AM
While that is generally good advice, and Michaels comments regarding data formatting ;
I can understand the desire to be able to handle various formatting structures.

Having one function to parse the data file irrespective of it's structure is definitely an advantage if we are dealing with data from various sources.

I hadn't imagined the format would be as mixed in ONE file as the example I posted but being able to process ANY of the formats would be handy.

Good generic solution Fools.
Title: Re: Convert coordinate files to x,y
Post by: fools on September 02, 2017, 05:05:22 AM
While that is generally good advice, and Michaels comments regarding data formatting ;
I can understand the desire to be able to handle various formatting structures.

Having one function to parse the data file irrespective of it's structure is definitely an advantage if we are dealing with data from various sources.

I hadn't imagined the format would be as mixed in ONE file as the example I posted but being able to process ANY of the formats would be handy.

Good generic solution Fools.

Thanks.
I tested Lee, MP and my code by benchmark.lsp.
Before testing, I added some content to MP's code. Let three functions has same result.
MP's function is the fastest and mine is the slowest. So, MP's function is recommended, especially when the amount of data is large.
BTW, Lee use regular expression is very wise solution.
Title: Re: Convert coordinate files to x,y
Post by: MP on September 04, 2017, 02:17:14 PM
Thanks for the nod fools. That said, I wouldn't necessarily consider my normalize function as recommended. It was more of an acedemic response to a contrived challenge that doesn't tend to exist much in real life. That is, data is normally consistant (tho it may be ugly). Be it spawned from a spreadsheet, a database, a data collector ... whatever. As such -- and especially if you're concerned with performance -- tailored code will perform far better than my "try to do it all" function. Cheers.