WHM Posted July 25, 2022 Posted July 25, 2022 Good day everyone! Have you ever been completely dumbfounded by your own ingenuity and stupidity at the same time? Mine involves data types.... I am very well aware that autolisp shortens floating point data to the 4th or 5th decimal (for display purposes), but still retains the full decimal number. So I've been trying to write a program that gets the geojson data from a server via an API call and then goes and draws the data in cad for you (which is now finished and working). After I translate the data to autolisp I use the read function to convert the string into lists.... this is where all the magic happened. While looking at the results after using the read function, I noticed that my Longitude and Latitude was shortened to 4 decimals. So I thought to myself I had to add \" to all the coordinates to force the read function to see it as a string, so while the data is still in string I had to add the \". I don't know if it is just me or if anyone else experienced this, but manipulating string data (that is actually in list shape already) is pretty difficult. So I converted the string into an ASCII list which I could then use to look for a pattern to find the coordinates and add the " to them. Program below.. I was very happy with the result and I decided to compare it with the just the read function and to my horror, the results were exactly the same and my method was far slower. So to any of you wondering - read will store your floating number! Don't be stupid like me! ;Example data ;(_t2 "type LineString coordinates ((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.0840749 -33.31621582) (26.084660183 -33.316272006))") (defun _t2 (datastr / datalist numlist pos prevpos gateop check prevchar gate firstchar gatecl) (defun _insertatpos ( ins pos lst / ctr ) (setq ctr -1) (apply 'append (mapcar '(lambda ( x ) (if (= pos (setq ctr (1+ ctr))) (list ins x) (list x))) lst)) ) (setq datalist (vl-string->list datastr) numlist (list 45 46 48 49 50 51 52 53 54 55 56 57) gateop (list 40) gatecl (list 41) pos 0) (foreach x datalist (if (member x gateop) (setq gate t) (if (and (member x gatecl) gate) (progn (if check (setq datalist (_insertatpos 34 pos datalist) pos (1+ pos))) (setq gate nil firstchar nil check nil) ) ) ) (if gate (progn (if (and (member x numlist) (member prevchar gateop))(setq firstchar t)) (if (and (member x numlist) firstchar) (progn (if (not (= prevpos (- pos 1))) (progn (setq datalist (_insertatpos 34 pos datalist) pos (1+ pos) prevpos pos check t) ) (setq prevpos pos) ) ) (if check (progn (setq datalist (_insertatpos 34 pos datalist) pos (1+ pos) check nil) ) ) ) ) ) (setq prevchar x pos (1+ pos)) ) (vl-list->string datalist) ) 1 Quote
mhupp Posted July 25, 2022 Posted July 25, 2022 Are all you wanting to do is output your cords to string? ;(setq lst '((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.0840749 -33.31621582) (26.084660183 -33.316272006))) (defun _foo (lst / strlst str) (foreach itm lst (setq strlst (cons (rtos (car itm) 2 9) strlst)) (setq strlst (cons (rtos (cadr itm) 2 9) strlst)) ) (setq strlst (reverse strlst)) (setq str "type LineString coordinates (") (while (> (length strlst) 2) (setq str (strcat str "(" (car strlst) " " (cadr strlst) ") ")) (setq strlst (cddr strlst)) ) (setq str (strcat str "(" (car strlst) " " (cadr strlst) "))")) (princ) ) (_foo lst) outputs "type LineString coordinates ((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.084074900 -33.316215820) (26.084660183 -33.316272006))" 1 Quote
WHM Posted July 25, 2022 Author Posted July 25, 2022 I wanted to make sure that the Latitude and Longitude will accurately translated to X and Y points for AutoCAD. When evaluating the variables after using the read function it only shows to the 4th decimal, but in reality it does store the correct information. I just spent all this time for nothing trying to reinvent the wheel! This post is nothing more than a warning or a reminder to other people doing the same 21 minutes ago, mhupp said: Are all you wanting to do is output your cords to string? ;(setq lst '((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.0840749 -33.31621582) (26.084660183 -33.316272006))) (defun _foo (lst / strlst str) (foreach itm lst (setq strlst (cons (rtos (car itm) 2 9) strlst)) (setq strlst (cons (rtos (cadr itm) 2 9) strlst)) ) (setq strlst (reverse strlst)) (setq str "type LineString coordinates (") (while (> (length strlst) 2) (setq str (strcat str "(" (car strlst) " " (cadr strlst) ") ")) (setq strlst (cddr strlst)) ) (setq str (strcat str "(" (car strlst) " " (cadr strlst) "))")) (princ) ) (_foo lst) outputs "type LineString coordinates ((26.083354482 -33.316154796) (26.083742548 -33.316186902) (26.084074900 -33.316215820) (26.084660183 -33.316272006))" 2 Quote
ronjonp Posted July 25, 2022 Posted July 25, 2022 9 hours ago, WHM said: Have you ever been completely dumbfounded by your own ingenuity and stupidity at the same time? Many times! 1 Quote
BIGAL Posted July 25, 2022 Posted July 25, 2022 The amount of times solved a big problem at 20 minutes, then a rewrite now 6 minutes, then a dumfounded why did I not do this, now 50 seconds. We are talking processing thousands of objects. 1 Quote
Dadgad Posted July 26, 2022 Posted July 26, 2022 Ingenuity and Stupidity are flipsides of a single coin...AKA life. The more you know, the more you realize you don't know. Perfection is an unattainable aspiration. A commendable goal? Hell yes! Live and learn. Enjoy the ride. 2 1 Quote
mhupp Posted July 26, 2022 Posted July 26, 2022 I guess my most recent brain fart was trying to make a lisp that would extend an both sides of the arc to the closest quadrant point. was about 80-100 lines of figuring out were the start and end angles where and if they needed to be extended or not. Scrapped it all to 6 lines of code that pull the center point and radius then delete the arc and create a circle in its place. 1 Quote
Recommended Posts
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.