Return x and y coordinates from Lat Long?

Return x and y coordinates from Lat Long?

CodeDing
Advisor Advisor
4,019 Views
8 Replies
Message 1 of 9

Return x and y coordinates from Lat Long?

CodeDing
Advisor
Advisor

Hello!

 

I created a function that retrieves Latitude and Longitude from Drawing Properties. Now, I need them converted to X and Y coordinates, but I cannot think of any efficient way to do this without creating an object then having to delete it. I have assigned a coordinate system to my drawing. Any ideas?! (Running AutoCAD 2016, NOT Civil3D).

 

Any help is appreciated.

 

Best,

~DD

 

(defun c:TEST ( / Lat Lon tmpCnt tmpStr1 tmpStr2 cord)
(vl-load-com)
(setq acadObject (vlax-get-acad-object))
(setq acadDocument (vla-get-ActiveDocument acadObject))
(setq dProps (vla-get-SummaryInfo acadDocument))
(setq tmpCnt 0)
(repeat (vla-numcustominfo dProps)
  	(vla-getcustombyindex dProps tmpCnt 'tmpStr1 'tmpStr2)
	(cond
	  	((eq tmpStr1 "Latitude")
	  	(setq Lat tmpStr2))
		((eq tmpStr1 "Longitude")
	  	(setq Lon tmpStr2))
	);cond
	(setq tmpCnt (1+ tmpCnt))
);repeat
(if (and Lat Lon)
	(setq cord (XYfrom Lat Lon))
  	(prompt "\nLatitude or Longitude Missing."))
(vlax-release-object acadObject)
(vlax-release-object acadDocument)
(vlax-release-object dProps)
(princ)

);defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;vvvvvvvvvvvvvvvv;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun XYfrom (Lat Lon / )
;;Need help here! To convert to X Y coordinates (convert lat) / (convert Lon) does not exist lol
(setq xy (list (convert Lat) (convert Lon)))
(princ xy)
);defun

 

 

0 Likes
Accepted solutions (1)
4,020 Views
8 Replies
Replies (8)
Message 2 of 9

Satish_Rajdev
Advocate
Advocate

You can convert lat long to X and Y coordinates by using this functions, but it's supported by only Map 3D or Civil 3D. This conversion doesn't work in plain AutoCAD.

 

 

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

Message 3 of 9

CodeDing
Advisor
Advisor

@Satish_Rajdev,

 

Am I using these wrong? I can't even get it to work in Civil. I am not sure if I have Map3D or not, but I can assign coord systems in Autocad. I tried using functions provided in civil to test.. Is it built in? there is nowhere to download or that shows code from the link you provided. Have you tested this before?

 

Best,

~DD

 

image.png

 

image.png

0 Likes
Message 4 of 9

Satish_Rajdev
Advocate
Advocate

Yes. You are using it wrongly. These functions works in CIVIL 3d as well, no need to assign coordinate system for that. Yes I've created programs using these functions for coordinate conversion. Yes, it's built in functions for Civil and Map 3D.

 

I'll write a code for you. What datum do you use?

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes
Message 5 of 9

CodeDing
Advisor
Advisor

@Satish_Rajdev,

 

Thank you for your help.

 

image.png

0 Likes
Message 6 of 9

CodeDing
Advisor
Advisor
Accepted solution

@Satish_Rajdev,

 

I have come to the solution that placing a Lat/Long marker (GEOMARKLATLONG), getting x y properties ("Position/X" & "Position/Y"), then deleting marker is most efficient. Until somebody can follow up with something more efficient (for AutoCAD, NOT Civil) then I will use this.

 

Best,

~DD

 

(defun c:TEST ( / Lat Lon tmpCnt tmpStr1 tmpStr2 cord ent)
(vl-load-com)
(setvar 'CMDECHO 0)
(setq acadObject (vlax-get-acad-object))
(setq acadDocument (vla-get-ActiveDocument acadObject))
(setq dProps (vla-get-SummaryInfo acadDocument))
(setq tmpCnt 0)
(repeat (vla-numcustominfo dProps)
  	(vla-getcustombyindex dProps tmpCnt 'tmpStr1 'tmpStr2)
	(cond
	  	((eq tmpStr1 "Latitude") (setq Lat tmpStr2))
		((eq tmpStr1 "Longitude") (setq Lon tmpStr2))
	);cond
	(setq tmpCnt (1+ tmpCnt))
);repeat
(vlax-release-object acadObject)
(vlax-release-object acadDocument)
(vlax-release-object dProps)
(if (and Lat Lon)
  	(progn
	(command "GEOMARKLATLONG" Lat Lon "")
	(setq ent (entlast))
	(setq cord (list (getpropertyvalue ent "Position/X") (getpropertyvalue ent "Position/Y")))
	(entdel ent)
	);progn
	;else
  	(progn
	(setq cord nil)
	(setvar 'CMDECHO 1)
  	(prompt "\nLatitude or Longitude Missing.\n")
	);progn
);if
(setvar 'CMDECHO 1)
(princ cord)
);defun

 

 

Message 7 of 9

Anonymous
Not applicable

Hi,

 

I am getting trouble to run 

(command "GEOMARKLATLONG" Lat Lon "")

it takes to long and only results nil

I am using C3D to run it, should I set something first? What I am doing wrong?

 

Thanks in advance + regards

 

0 Likes
Message 8 of 9

CodeDing
Advisor
Advisor

@Anonymous ,

 

I have learned a lot since then, Satish_Rajdev is more correct with his approach using the "ade_" functions if you are using Civil 3D.

 

The "GEOMARKLATLONG" command will create an entity (a marker) at your specified lat/long, then you would take the marker and get x/y from its properties..

HOWEVER, I am going to highly recommend that you use the "ade_" functions. Here is a snippet of code to help guide you, and I have attached a document referencing how to use these functions. Hope this helps!

(defun c:TEST ( / lat lon newPt)
(if (and (ade_projsetsrc "LL84")
	 (ade_projsetdest "TX83-NCF"))
  (progn
    (initget 1) (setq lat (getreal "\nEnter Latitude: "))
    (initget 1) (setq lon (getreal "\nEnter Longitude: "))
    (setq newPt (ade_projptforward (list lon lat)))
    (prompt "\nNew point location: \n") (princ newPt)
    ;Create circle to show your newPt location
    (entmakex (list '(0 . "CIRCLE") (cons 10 newPt) '(40 . 500.0)))
  );progn
);if
(princ)
);defun

Notes:

- You will leave the "LL84" source since that is where our lat/long starts at

- You need to change "ade_projsetdest" to the string that represents your desired coordinate system (Tip: you can use CGEOCS system variable to getyour currently assigned coord system)

- The lat/long point supplied to "ade_projptforward" is  with long first then lat since long is your "x" value and lat is your "y".

Best,

~DD

Message 9 of 9

Anonymous
Not applicable

@CodeDing just perfect!

 

Thanks a lot!

0 Likes