Author Topic: Point to LAT LON  (Read 7825 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7529
Point to LAT LON
« on: December 19, 2014, 03:31:35 PM »
I've done a little bit of searching for this, but have not found what I've needed. I have a list of points from a GPS unit and would like to convert the LAT & LON to place in a KML file. I used this utility to do the conversion, but would like to know if anyone has come up with a lisp solution.


Thanks.  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #1 on: December 19, 2014, 03:53:55 PM »
ronjomp,

What format does your GPS gives you ?

They are usually in Lat long.  KML requires Long Lat in decimal degree.

However if you are getting UTM out of your GPS you could use
the projlib library from Prof. David Allison  and transform back to
Lat. Long.

The projlib library in autolisp is part of the following Zip : http://www.usouthal.edu/geography/allison/mappro/mappro.zip

ymg

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #2 on: December 19, 2014, 04:03:16 PM »
YMG,
<Disclaimer>I'm a newbie to this so handholding may be necessary ;)</Disclaimer>

The GPS points were given to me by someone else. The format is as follows:

to Latitude   to Longitude   Latitude   Longitude
33.67   111.0916667   334012   1110530


Thanks for your help.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #3 on: December 19, 2014, 05:10:54 PM »
Ronjomp,

To use them in a kml all you need to do is
to use them in reverve order that is long first
as 111.0916667 33.67

For example outputting the following
is sufficient to create a placemark at your point:

Code: [Select]
<Placemark>
<name>YourPoint</name>
<Point>
<coordinates>111.01966667,33.67,0</coordinates>
</Point>
</Placemark>


note: no space after the comma.

ymg

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #4 on: December 19, 2014, 05:22:24 PM »
The points that were supplied to me are these ones: 334012   1110530
The other points were the converted points from the website above.


I was wondering how to do that?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #5 on: December 19, 2014, 05:25:54 PM »
ronjomp,

Very likely to mean 111.0530,33.4012

Long would normally be on the interval -180 to 180
While Lat are on -90 to 90

If you paste the text of the placemark code I gave you
into the Myplaces folder in the side window of GE
your placemark should appear.

Double-click on the placemark and GE will zoom to it.

That would be a place in China, nearest city Shaanxi

ymg
« Last Edit: December 19, 2014, 05:49:54 PM by ymg »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #6 on: December 19, 2014, 05:39:53 PM »
Maybe a picture will help :) .. How do you calculate the value in yellow?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Point to LAT LON
« Reply #7 on: December 19, 2014, 05:44:40 PM »
use QGIS

Import them

Export them to KML,or KMZ
or many other formats
Be your Best


Michael Farrell
http://primeservicesglobal.com/

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #8 on: December 19, 2014, 05:54:43 PM »
Figured it out ( I think )  :)

Code - Auto/Visual Lisp: [Select]
  1. ;;1110530
  2. ;; (D 111)(M 05)(S 30)
  3. (alert (strcat "111." (vl-princ-to-string (+ (/ 5 60.) (/ (/ 30 60.) 60.)))))
« Last Edit: December 19, 2014, 06:18:48 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #9 on: December 19, 2014, 05:57:59 PM »
use QGIS

Import them

Export them to KML,or KMZ
or many other formats


Thanks for the reply Michael, I'll give QGIS a look.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #10 on: December 19, 2014, 06:41:12 PM »
rojomp,

It is simply transforming degree minutes second to decimal degree;
Code - Auto/Visual Lisp: [Select]
  1. (defun todec (int / str len d m s)
  2.    (setq str (itoa int)
  3.          len (strlen str)
  4.            s (atof (substr str (- len 2)))
  5.            m (atof (substr str (- len 3) 2))
  6.            d (atof (substr str 1 (- len 4)))
  7.    )     
  8.    (+ d (/ (+ m  (/ s 60)) 60))
  9. )
  10.  
« Last Edit: December 19, 2014, 07:05:15 PM by ymg »

divtiply

  • Guest
Re: Point to LAT LON
« Reply #11 on: December 20, 2014, 07:19:46 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun todec (int / str len d m s)
  2.    (setq str (itoa int)
  3.          len (strlen str)
  4.            s (atof (substr str (- len 2)))
  5.            m (atof (substr str (- len 3) 2))
  6.            d (atof (substr str 1 (- len 4)))
  7.    )     
  8.    (+ d (/ (+ m  (/ s 60)) 60))
  9. )
  10.  

Why not angtof/angtos?

ymg

  • Guest
Re: Point to LAT LON
« Reply #12 on: December 20, 2014, 10:55:20 AM »
divtply,

Your suggestion works also, but this is a carry over from the day of hand calculator.

Angtof returns the result in radians, and the OP needs results in decimal degree.

ymg

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #13 on: December 22, 2014, 10:29:45 AM »
Thanks for the replies YMG. This is what I ended up cobbling together that seems to work.

Code - Auto/Visual Lisp: [Select]
  1. (defun _kmlpoint (n / d i m s)
  2.   (setq d (* n 0.0001))
  3.   (setq n (rtos (float n) 2 8))
  4.   (setq i (1+ (strlen (itoa (fix d)))))
  5.   (setq m (atof (substr n i 2)))
  6.   (setq s (atof (substr n (+ i 2))))
  7.   (if (minusp d)
  8.     (- (+ (abs (fix d)) (/ m 60.) (/ (/ s 60.) 60.)))
  9.     (+ (fix d) (/ m 60.) (/ (/ s 60.) 60.))
  10.   )
  11. )
  12. ;; (rtos (_kmlpoint -1111007.5) 2 8)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #14 on: December 23, 2014, 11:14:55 AM »
ronjomp,

You may have a problem with 03030 as input.

So maybe with a string input intead of an integer:

Code - Auto/Visual Lisp: [Select]
  1. (defun todec (str / d m s n sig)
  2.    (setq n (strlen str)
  3.          s (atof (substr str (- n 2)))
  4.          m (atof (substr str (- n 3) 2))
  5.          d (abs (atoi (substr str 1 (- n 4))))
  6.        sig (if (wcmatch str "-*") -1 1)  
  7.    )     
  8.    (* sig (+ d (/ (+ m  (/ s 60)) 60)))
  9. )
  10.  

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #15 on: December 23, 2014, 11:24:38 AM »
Thanks YMG .. I ended up putting this into my function so it can accept a string\real\int
Code - Auto/Visual Lisp: [Select]
  1.   (and (= (type n) 'str) (setq n (atof n)))
  2.   (setq d (* n 0.0001))


On a side note, your function and my function return different results? My numbers match the conversion on this site.
Code - Auto/Visual Lisp: [Select]
  1. ;; (rtos (_kmlpoint "-1111007.5") 2 7)
  2. ;; "-111.1687500"
  3. ;; (todec "-111.1687500")
  4. ;; -112.389

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #16 on: December 23, 2014, 12:13:45 PM »
ronjomp,

See this picture:


ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #17 on: December 23, 2014, 12:34:06 PM »
The conversion I need is for points formatted like so ... see below .


Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #18 on: December 23, 2014, 02:02:52 PM »
ronjomp,

Just modify the n value with vl-string-position:

Code - Auto/Visual Lisp: [Select]
  1. (defun todec (str / d m s n sig)
  2.    (setq n (if (setq n (vl-string-position 46 str)) n (strlen str))
  3.          s (atof (substr str (- n 2)))
  4.          m (atof (substr str (- n 3) 2))
  5.          d (abs (atoi (substr str 1 (- n 4))))
  6.        sig (if (wcmatch str "-*") -1 1)  
  7.    )     
  8.    (* sig (+ d (/ (+ m  (/ s 60)) 60)))
  9. )
  10.  


ymg

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #19 on: December 23, 2014, 02:27:28 PM »
ymg,


Thanks for your help with this  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #20 on: December 23, 2014, 02:43:00 PM »
ronjomp,


I got the Season's blues, so trying to keep busy.

ymg  :-)

enderprime

  • Guest
Re: Point to LAT LON
« Reply #21 on: December 24, 2014, 10:58:28 AM »
The way LAT LONG values are determined depends on what survey coordinate system you are in. Because the earth is not a perfect shape there are many models used to represent the surface of the earth, and each system has a different way to represent a virtual "flat" grid that survey crews use while shooting points in the field. These points will have x,y values based on a rather arbitrary reference point for the system, but the system will also have a way to translate those field coordinates into real world LAT LONG values for use in GPS, etc. There are some complex formulas involved which software usually handles for you, and if you start doing these conversions manually you will need to research the methods for each system in order to do it right.

Common coordinate systems include State Plane, UTM, WGS84, etc. As a working example of this, Google Earth uses the WGS84 model by default, and even though this may not matter to the common user the software is assuming coordinates are using that geodetic model in order to provide LAT LONG values. However, if you were to go the opposite direction, and convert the LAT LONG back into system coordinates, those coordinate values will end up being different depending on what system you project into, even though they represent the same location on earth.

I know I'm not being very detailed here, but it is a complex subject and I just wanted to throw out a casual warning about doing the tranforms yourself. Other than ArcGIS and Civil 3D, we usually rely on sites like http://www.earthpoint.us/Convert.aspx to handle quick conversions, mainly because asking a surveyor how these calculations are done leads to a 2 hour discussion.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #22 on: December 24, 2014, 11:09:34 AM »
The way LAT LONG values are determined depends on what survey coordinate system you are in. Because the earth is not a perfect shape there are many models used to represent the surface of the earth, and each system has a different way to represent a virtual "flat" grid that survey crews use while shooting points in the field. These points will have x,y values based on a rather arbitrary reference point for the system, but the system will also have a way to translate those field coordinates into real world LAT LONG values for use in GPS, etc. There are some complex formulas involved which software usually handles for you, and if you start doing these conversions manually you will need to research the methods for each system in order to do it right.

Common coordinate systems include State Plane, UTM, WGS84, etc. As a working example of this, Google Earth uses the WGS84 model by default, and even though this may not matter to the common user the software is assuming coordinates are using that geodetic model in order to provide LAT LONG values. However, if you were to go the opposite direction, and convert the LAT LONG back into system coordinates, those coordinate values will end up being different depending on what system you project into, even though they represent the same location on earth.

I know I'm not being very detailed here, but it is a complex subject and I just wanted to throw out a casual warning about doing the tranforms yourself. Other than ArcGIS and Civil 3D, we usually rely on sites like http://www.earthpoint.us/Convert.aspx to handle quick conversions, mainly because asking a surveyor how these calculations are done leads to a 2 hour discussion.
ender.prime,

Thanks for the explanation. That site you recommended is what I used for the initial point conversion, but the curious side of me wanted to know how they did the conversion. I did a little bit of reading on converting to State Plane, UTM .. yada yada and my head nearly exploded :) . I'll definitely use software to do those transformations.

Happy Holidays.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

enderprime

  • Guest
Re: Point to LAT LON
« Reply #23 on: December 24, 2014, 11:27:23 AM »
Ha! I asked one of our surveyors at work one day, and that was a mistake. A few hours later I was like man you could have just said "it's complicated and you don't want to program it yourself". If I wanted a degree in surveying I would go back to school eh. It did make me appreciate why ArcGIS was so expensive though.  /shrug

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #24 on: December 24, 2014, 11:42:33 AM »
I wonder how QGIS that MJ recommended fares against ArcGIS ? The price is definitely right  8) .

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point to LAT LON
« Reply #25 on: December 24, 2014, 11:59:53 AM »
ronjomp,

Most everything you do in ArcGIS can be done in QGIS.

And yes, the price is right.

For your coordinates conversion look at at Osgeo4w,
more specifically cs2cs.exe

Converting Lat long to UTM is not that complicated.

What is a mess is the multitudes of projection, ellipsoid
and coordinates system.


ymg

RAYAKMAL

  • Guest
Re: Point to LAT LON
« Reply #26 on: January 05, 2015, 05:31:45 AM »
Just wanna add something to the discussion.
My solution to export UTM Coordinates to KML Format (CAD to GPS Receiver and Google Earth)


Code: [Select]

;; Zoning and Datum are hard coded.  (My region is Zone 48, North Hemisphere)
;; Line Description on KML File is based on Layer Name

;;  Needs to deal with:
;; - Closed Polyline
;; - Automatic LINE COLOR

(defun C:VRTX2KML  ( / olderr )

  (ShowTM "Vertices to File/GPS/Google Earth")
  (NewErrTrap)

  (getmode '("cmdecho" "blipmode" "osmode"))
  (mapcar 'setvar
          '("cmdecho" "blipmode" "osmode")
  '(0 1 0)
  )

  (princ "\nSelect Polyline: ")
  (setq sset (ssget '((0 . "LWPOLYLINE,POLYLINE"))))
  (if sset
     (progn
        ;;
        ;; PREFIX FOR GPS RECEIVER
        ;;
        (setq prefx (getstring "\n. Prefix for Polyline: "))
        (setq flnm  (getstring "\n. Output Filename: "))

        (initget 1)
        (setq wdth  (getint      "\n. POLYLINE Width [1-10]: "))
        ;;
        ;; MANUAL LINE COLOR
        ;;
        (initget "Blue B Red R Green G Yellow Y")
        (setq answr     (getkword    "\n. POLYLINE Color <Red>/Green/Blue/Yellow: "))

        ;;Color and opacity (alpha) values are expressed in hexadecimal notation.
        ;;The range of values for any one color is 0 to 255 (00 to ff). For alpha, 00 is fully transparent and ff is fully opaque.
        ;;The order of expression is aabbggrr, where aa=alpha (00 to ff); bb=blue (00 to ff); gg=green (00 to ff); rr=red (00 to ff).
        ;;For example, if you want to apply a blue color with 50 percent opacity to an overlay, you would specify the following:
        ;;<color>7fff0000</color>,
        ;;where alpha=0x7f, blue=0xff, green=0x00, and red=0x00.

        (cond ((or (= answr "Red") (= answr "R") (= answr nil))
               (setq clr "7F0000FF")
              )
              ((or (= answr "Green") (= answr "G"))
               (setq clr "7F00FF00")
              )
              ((or (= answr "Blue") (= answr "B"))
               (setq clr "7FFF0000")
              )
              ((or (= answr "Yellow") (= answr "Y"))
               (setq clr "7F00FFFF")
              )
        );end cond

        (setq dwgprefx (getvar "DWGPREFIX"))

        (setq flnmCartesian (strcat dwgprefx flnm "-XY.CSV"))
        (setq flnmLatlong   (strcat dwgprefx flnm "-LongLat.CSV"))
        (setq flnmMap       (strcat dwgprefx flnm ".KML"))

        (setq flCartesian (open flnmCartesian "w"))
        (setq flLatLong   (open flnmLatlong "w"))
        (setq flMap       (open flnmMap "w"))

        (vrtx2GPSstakeout sset)

        (close flCartesian)
        (close flLatLong)
        (close flMap)

        (princ (strcat "\n. Data saved to: " flnmCartesian "!"))
        (princ (strcat "\n. Data saved to: " flnmLatlong "!"))
        (princ (strcat "\n. Data saved to: " flnmMap "!"))

     );end progn
  );end if

  (setmode *mod2)
  (OldErrTrap)
  (princ)

)



(defun vrtx2GPSstakeout ( sset / cntr n)

   (WRITESTARTKML clr wdth)

   (setq jumlhpoly   (sslength sset)     ;; Jumlah Polyline
         n            0
   )
   ;; Konter
   (setq idxpoly 1)

   (repeat jumlhpoly
      (setq enm (ssname sset n))
      (setq etype   (dxf-code 0 enm))
      ;;
      ;; Cek Polyline is Close (Google Earth)
      ;;
      (if (= (logand (dxf-code 70 enm) 1) 1)
         (setq Pclose T)
         (setq Pclose nil)
      );end if LOGAND
      (setq desc  (dxf-code 8 enm))  ;; Layer AS DESCRIPTION
      (setq ptlst (getcoords enm))

      (WRITESTARTPLINE desc)
      ;; Konter
      (setq idxvertex 1)
      (setq idx     0)

      (setq ptcartesian '())
      (setq ptlatlong   '())


      (cond ((= etype "LWPOLYLINE")
             (repeat (/ (length ptlst) 2)
                (setq x (nth idx ptlst))
                (setq y (nth (+ idx 1) ptlst))

                (vrtx2GPSWrite)

                (setq idxvertex (+ idxvertex 1))
                (setq idx (+ idx 2))
             );end repeat
             (setq idxpoly (+ idxpoly 1))   
            )
            ((= etype "POLYLINE")
             (repeat (/ (length ptlst) 3)
                (setq x (nth idx ptlst))
                (setq y (nth (+ idx 1) ptlst))

                (vrtx2GPSWrite)

                (setq idxvertex (+ idxvertex 1))
                (setq idx (+ idx 3))
             );end repeat
             (setq idxpoly (+ idxpoly 1))
            )
      );end cond
      (WRITEENDPLINE)
      (setq n (+ n 1))
   );end repeat
   (WRITEENDKML)
   (princ)
);end defun



(defun vrtx2GPSWrite ()

                (setq ptxy (list x y))
                (setq ptcartesian (append ptcartesian (list (list ptxy (strcat prefx (itoa idxpoly) "-" (itoa idxvertex))))))

                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                ;; Tulis ke FILE X - Y - DESKRIPSI
                (write-line (strcat
                                    (rtos x 2 3)
                                    ","
                                    (rtos y 2 3)
                                    (strcat "," prefx (itoa idxpoly) "-" (itoa idxvertex)) 
                            )
                            flCartesian
                )
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

                ;; Latitude Longitude
                (setq ptgeo (UTM2LatLong x y 48 F))
                (setq ptlatlong (append ptlatlong     (list (list ptgeo (strcat prefx (itoa idxpoly) "-" (itoa idxvertex))))))

                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                ;; FILE (LONG-LAT-ELEVATION)
                (write-line (strcat "\t" "\t"
                                    (rtos (Y-OF ptgeo) 2 9)
                                    ","
                                    (rtos (X-OF ptgeo) 2 9)
                                    ",0"                         ;; 2D 
                            )
                            flMap
                )
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                ;; GPS FORMAT (LONG-LAT-DESCRIPTION)
                (write-line (strcat "\t" "\t"
                                    (rtos (Y-OF ptgeo) 2 9)
                                    ","
                                    (rtos (X-OF ptgeo) 2 9)
                                    (strcat "," prefx (itoa idxpoly) "-" (itoa idxvertex)) 
                            )
                            flLatLong
                )
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

   (princ)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun WRITESTARTKML ( clr wdth )

   (setq AWALKML (strcat
   "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
    <kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\">
    <Document>
     <!-- Begin Style Definitions -->
     <Style id=\"line1\">
       <LineStyle>
         <color>"
          clr
          "</color>
         <width>"
           (itoa wdth)         ;; WIDTH
         "</width>
       </LineStyle>
     </Style>
     <Folder>
       <name>Line Features</name>
       <description>Line Features</description>"
                  )

    )

   (write-line AWALKML flMap)
   (princ)
);END DEFUN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun WRITESTARTPLINE ( desc )

   (setq AWALPline (strcat
   "       <Placemark>
         <description>"

             desc               ;; DESCRIPTION

   "      </description>
         <styleUrl>#line1</styleUrl>
         <LineString>
          <coordinates>"
                   )
   )
   (write-line AWALPLINE flMap)
   (princ)
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun WRITEENDPLINE ( )
   (setq AKHIRPLINE (strcat
    "          </coordinates>
         </LineString>
       </Placemark>"
                       )
   )
   (write-line AKHIRPLINE flMap)
   (princ)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun WRITEENDKML ( )

   (setq AKHIRKML (strcat
   "    </Folder>
   </Document>
   </kml>"
                     )
   )
   (write-line AKHIRKML flMap)
   (princ)
)

;;
;;This function will return a list of coordinates. If the polyline is a LWPOLYLINE entity,
;; the list will be in the form of (x y x y x y x y), and the number of vertices can be determined by ;; dividing the length of the list by
;;If the polyline is a POLYLINE entity (2D or 3D), then the list will be in the form of (x y z x y z x y z),
;; and the number of vertices can be determined by dividing the length of the list by 3.
;;

;;
(defun getcoords (ent)
  (vlax-safearray->list
    (vlax-variant-value
      (vlax-get-property
(vlax-ename->vla-object ent)
"Coordinates"
      )
    )
  )
)

;;
;;(UTM2LatLong 781496 2900000 48 T)


(defun  UTM2LatLong   ( X Y Zone NORTHFLAG  )   ;; NORTHFLAG = TRUE/FALSE

   (if NORTHFLAG
      (setq latd Y)
      (setq latd (- 10000000.00 Y))
   )

   (setq    lond (- 500000.0 X))

   ;; Zone Central Longitude
   (if (> Zone 0)
       (setq ZCL (- (* 6.0 Zone) 183.0))
       (setq ZCL 3.0)
   )


   ;;
   ;;Constant
   ;;
   (setq b    6356752.3142         ) ;; Polar Axis
   (setq a    6378137              ) ;; Equator Radius
   (setq ec   0.0818191909289069   ) ;; Ecc
   (setq e1sq 0.006739496756587    ) ;; E1sq
   (setq K0   0.9996               ) ;; K0



   ;;
   ;; FootPrint Latitude
   ;;
   (setq arc (/ latd K0)           ) ;; Arch Length
   (setq mu  (/ arc (* a (- 1.0  (/ (* 1.0 (expt ec 2)) 4.0)
                                 (/ (* 3.0 (expt ec 4)) 64.0)
                                 (/ (* 5.0 (expt ec 6)) 256.0)
                         )
                    )
             )
   )                                                           ;; =arc/(a*(1-ec^2/4-3*ec^4/64-5*ec^6/256))
   (setq e0  (sqrt (- 1.0 (expt ec 2))))                        ;; =(1-ec^2)^0.5
   (setq e1  (/ (- 1.0 e0) (+ 1.0 e0)))                        ;; =(1-e0)/(1+e0)
   (setq C1  (- (* 3.0 e1 0.5) (/ (* 27.0 (expt e1 3)) 32.0))) ;; =3*ei/2-27*ei^3/32
   (setq C2  (- (/ (* 21.0 e1 e1) 16.0) (/ (* 55.0 e1 e1 e1 e1) 32.0))) ;; =21*ei^2/16-55*ei^4/32
   (setq C3  (/ (* 151.0 e1 e1 e1) 96.0))                      ;; =151*ei^3/96
   (setq C4  (/ (* 1097.0 e1 e1 e1 e1) 512.0))                 ;; =1097*ei^4/512
   (setq phi1 (+ mu (* c1 (sin (* 2.0 mu)))
                    (* c2 (sin (* 4.0 mu)))
                    (* c3 (sin (* 6.0 mu)))
                    (* c4 (sin (* 8.0 mu)))
              )
   )                                                           ;; =mu+c1*SIN(2*mu)+cb*SIN(4*mu)+ccc*SIN(6*mu)+cd*SIN(8*mu)
   ;;
   ;; FINALE FOOTPRINT LATITUDE
   ;;
   (setq lat    (rad2deg phi1))                                ;;=phi1*180/PI()
   (setq latdeg (fix lat))
   (setq latmin (fix (* (- lat latdeg) 60.0)))
   (setq latsec (* 3600.0 (- lat latdeg (/ latmin 60.0))))

   ;;
   (setq Q0 (* e1sq (cos phi1) (cos phi1)))                    ;;=eisq*COS(phi1)^2
   (setq T0 (*      (Tan phi1) (Tan phi1)))                    ;;=TAN(phi1)^2


   (setq N0 (/ a
               (sqrt (- 1.0 (expt (* ec (sin phi1)) 2.0)))
            )
   ) ;;=a/(1-(ec*SIN(phi1))^2)^(1/2)


   (setq R0 (/ (* a (- 1.0 (* ec ec)))
               (expt
                     (- 1.0 (expt (* ec (sin phi1)) 2.0))
                     (/ 3.0 2.0)
               )
            )
   )      ;;=a*(1-ec*ec)/(1-(ec*SIN(phi1))^2)^(3/2)
   (setq D0 (/ lond N0 K0))                                    ;;=lond/(n0*k0)

   ;;
   ;;Coefficients for Calculating Latitude
   ;;

   (setq Fact1  (* N0 (/ (Tan Phi1) R0))) ;;=n0*TAN(phi1)/r0
   (setq Fact2  (* D0 D0 0.5))            ;;=dd0*dd0/2
   (setq Fact3  (/ (* (+ 5.0 (* 3.0 T0)
                             (* 10.0 Q0)
                             (* -4.0 Q0 Q0)
                             (* -9.0 e1sq)   
                      )
                      D0 D0 D0 D0
                   )
                   24.0
                 )     
   )                        ;;=(5+3*t0+10*Q0-4*Q0*Q0-9*eisq)*dd0^4/24
   (setq Fact4   (/ (* (+ 61.0 (* 90.0 T0)
                               (* 298.0 Q0)
                               (* 45.0 T0 T0)
                               (* -252.0 e1sq)
                               (* -3.0 Q0 Q0)
                       )
                       D0 D0 D0 D0 D0 D0
                    )
                    720.0
                  )
                                         
   )                        ;;=(61+90*t0+298*Q0+45*t0*t0-252*eisq-3*Q0*Q0)*dd0^6/720



   ;;
   ;;Coefficients for Calculating Longitude
   ;;

   (setq LoFact1  D0)
   (setq LoFact2  (/ (* (+ 1.0 (*  2.0 T0) Q0)
                        D0 D0 D0
                     )
                     6.0
                  )
   )                        ;; =(1+2*t0+Q0)*dd0^3/6
   (setq LoFact3  (/ (* (+ 5.0 (* -2.0 Q0)
                               (* 28.0 T0)
                               (* -3.0 Q0 Q0)
                               (*  8.0 e1sq)
                               (* 24.0 T0 T0)
                         )
                         D0 D0 D0 D0 D0
                      )
                      120.0
                   )

    )                       ;;=(5-2*Q0+28*t0-3*Q0^2+8*e1sq+24*t0^2)*dd0^5/120

   (setq DeltaLongRad (/ (+ LoFact1 (* -1.0 LoFact2) Lofact3)
                         (Cos Phi1)
                      )
   )                  ;; =(lof1-lof2+lof3)/COS(phi1)
   (setq DeltaLongDec (rad2deg DeltaLongRad))

   (setq LatRad      (- phi1 (* Fact1 (+ Fact2 Fact3 Fact4))))  ;;=(phi1-fact1*(fact2+fact3+fact4))


   (setq LatitudeIs  (rad2deg LatRad))
   (setq LongitudeIs (- ZCL DeltaLongDec))


;   (princ (rtos LatitudeIs  2 8))
;   (princ "\t")
;   (princ (rtos LongitudeIs 2 8))
    (list (* -1.0 LatitudeIs) LongitudeIs) ;;
)



(defun showTM ( txt ) (graphscr)
  (princ (strcat "\n. " txt))
  (princ ". (Copyright of ARY HIMAWAN S. 1990)")
)
(defun NewErrTrap () (setq olderr *error* *error* @hs_err) (princ))
(defun OldErrTrap () (setq *error* olderr olderr nil) (princ))

;;o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o
;; Save Restore System variable setting                         
;; Example : (getmode '("cmdecho" "blipmode" "osmode" etc...etc....))
;; Example : (setmode *mod2)         
;;o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o

(defun getmode (mod1)
  (setq *mod2 '()) ; create global variable to store setting
  (repeat (length mod1)
    (setq *mod2
   (append *mod2 (list (list (car mod1) (getvar (car mod1)))))
    )
    (setq mod1 (cdr mod1)) ; go to next element in list
  )
)

(defun setmode (mod1)
  (repeat (length mod1)
    (setvar (caar mod1) (cadar mod1)) ; extract setting and reset
    (setq mod1 (cdr mod1))
  )
)

;;o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o
;; FIND DXF GROUP
;;o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o

(defun dxf-code (code enm /) (cdr (assoc code (entget enm))))


« Last Edit: January 05, 2015, 06:39:47 PM by RAYAKMAL »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Point to LAT LON
« Reply #27 on: January 05, 2015, 11:39:21 AM »
RAYAKMAL,

Thanks for the reply. It appears the code you posted is missing a few functions needed.

getmode
setmode
dxf-code
utm2latlong

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

RAYAKMAL

  • Guest
Re: Point to LAT LON
« Reply #28 on: January 05, 2015, 06:41:44 PM »
RAYAKMAL,

Thanks for the reply. It appears the code you posted is missing a few functions needed.

getmode
setmode
dxf-code
utm2latlong

Checked and Edit my previous post.
You can find the complete code on the the attachment.

sanju2323

  • Newt
  • Posts: 68
Re: Point to LAT LON
« Reply #29 on: January 06, 2015, 01:48:58 AM »
This Lisp File to Convert Kml

(defun c:dwg2kml (/ coords line_from_kml fn)
  (setq old_osmode (getvar "osmode"))
  (setvar "osmode" 17408)
  (setq   idfilename         ;get the file name to convert
    (getfiled "Choose KML File Name"
         ""
         "kml"
         33
    )
  )
  (setq fn (open idfilename "r"))
  (setq line_from_kml (read-line fn))
  (while (/= line_from_kml nil)
    (while (and   (/= line_from_kml nil)
      (not (vl-string-search "<Polygon>" line_from_kml))
      )
      (setq line_from_kml (read-line fn))
    )
    (setq line_from_kml (read-line fn))
    (while (and   (/= line_from_kml nil)
      (not (vl-string-search "<coordinates>" line_from_kml))
      )
      (setq line_from_kml (read-line fn))
    )
    (setq coords "")
    (while (and   (/= line_from_kml nil)
      (not (vl-string-search "</coordinates>" line_from_kml))
      )
      (setq line_from_kml (read-line fn))
      (if (not (vl-string-search "</coordinates>" line_from_kml))
   (setq coords (strcat coords " " line_from_kml))
      )
    )
    (setq coords (string2list (vl-string-translate "," " " coords) 32))
    (setq coords (xyList->ListofPoints3D coords))
    (if   (/= coords nil)
      (progn (command "_pline")
        (foreach pt coords
          (command pt)
        )
        (command "")
      )
    )
    (spin)
    )
  (setvar "osmode" old_osmode)
  (princ)
)
(defun xyList->ListOfPoints3D (coordList / ptlist)
;;;3D
  (while coordList
    (setq ptlist    (append ptlist
             (list (list   (distof (car coordList) 2)
               (distof (cadr coordList) 2)
               ;(distof (caddr coordList) 2) drop to 2D
              )
             )
          ) ;_ end of append
     coordList (cdddr coordList)   ; input list is still 3D
    ) ;_ end of setq
  ) ;_ end of while
  ptlist
)

;;;converts string to a list using deliminators             
;;;Takes two arguments, first the string and then the integer
;;;representing the ascii code for the delimiter.           
;;;eg (string2list "Hello World" 32) = ("Hello" "World")   
;;;32 for " ", 44 for "," 59 for ";" etc.                   
(defun string2list (f_string delim / f_cur f_len f_loop f_ls f_str)
  (setq   f_ls nil
   f_str ""
  )
  (setq f_loop 0)
  (repeat (strlen f_string)
    (progn
      (setq f_loop (1+ f_loop))
      (setq f_cur (substr f_string f_loop 1))
      (if (= (ascii f_cur) delim)
   (if (/= f_str "")
     (progn (setq f_ls (append f_ls (list f_str)))
       (setq f_str "")
     )
   )
   (setq f_str (strcat f_str f_cur))
      )
    )
  )
  (if (/= f_str "")
    (setq f_ls (append f_ls (list f_str)))
  )
  f_ls
)
(defun SPIN ()
  (setq   a_s (if   a_s
         a_s
         4
       )
  )
  (princ
    (strcat
      "\r"
      (cadr
   (member
     (rem (setq a_s (1+ a_s)) 4)
     '(0 "|" 1 "/" 2 "-" 3 "\\")
   )
      )
    )
  )
)