Sunday, September 11, 2011

Nav2KML File

Okay, I've come to understand that the navigation world does not like GoogleEarth.  After scouring all over the web I couldn't find anything that would suit what I wanted it to do - create a kml file from a navigation file.  Multiple lines with different linenames.  And, yes, I have more than 3 points in my path.  Let's say 800!  I sat down a few days ago and figured out GoogleEarth scripting and came up with this really cheesy vbs file.

Here it is:
infilename = inputbox("Enter the input file name")
outfilename = inputbox("Enter the output file name")
fname = inputbox("Enter the survey name:  ")

Const ForReading = 1
Dim strFileIn, objFSO, objTextRead

strFileIn = infilename
'strFileIn = "C:\Users\Cremer\Documents\SEG-Y\San Francisco Bay\Navigation\" & infilename ' use this if you want absolute pathname
Set objFSO = CreateObject("Scripting.FileSystemObject") ' File system object
Set objTextRead = objFSO.OpenTextFile(strFileIn, ForReading)  ' Open text file

strFileOut = outfilename
'strFileOut = "C:\Users\Cremer\Documents\SEG-Y\San Francisco Bay\Navigation\" & outfilename ' use this if you want absolute pathname
Set filestreamOUT = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFileOut,2,true)

Dim arrKML()
n = 0
Do Until objTextRead.AtEndOfStream
 Dim strSearchString
 Dim currentID
 strSearchString = objTextRead.ReadLine

 ReDim Preserve arrKML(3,n) ' Initialize array 4 characters, at n entry
 arrKML(0,n) = n ' add number
 arrKML(1,n) = CDbl(trim(mid(strSearchString,1,11))) ' add long
 arrKML(2,n) = CDbl(trim(mid(strSearchString,12,11))) ' add lat
 arrKML(3,n) = (trim(mid(strSearchString,23,11))) ' add line number
 n = n + 1 ' next iteration
Loop
objTextRead.Close
' ***********************************************************************

' msgbox "Number of elements in array: " & Ubound(arrKML,2)
dim currentLineName
currentLineName = arrKML(3,0)
filestreamOUT.WriteLine("<?xml version=" & chr(&H22) & "1.0" & chr(&H22) & " encoding=" & chr(&H22) & "UTF-8" & chr(&H22) & "?>")
filestreamOUT.WriteLine("<kml xmlns=" & chr(&H22) & "
http://www.opengis.net/kml/2.2" & chr(&H22) & " xmlns:gx=" & chr(&H22) & "http://www.google.com/kml/ext/2.2" & chr(&H22) & " xmlns:kml=" & chr(&H22) & "http://www.opengis.net/kml/2.2" & chr(&H22) & " xmlns:atom=" & chr(&H22) & "http://www.w3.org/2005/Atom" & chr(&H22) & ">")
filestreamOUT.WriteLine("<Document id=" & chr(&H22) & "Phil Cremer's Cheesy nav2kml script" & chr(&H22) & ">")
filestreamOUT.WriteLine(vbtab & "<name>" & fname & "</name>")
filestreamOUT.WriteLine(vbtab & vbtab & "<Placemark id=" & chr(&H22) & currentLineName & chr(&H22) & ">")
filestreamOUT.WriteLine(vbtab & vbtab & vbtab & "<name>" & currentLineName & "</name>")
filestreamOUT.WriteLine(vbtab & vbtab & vbtab & "<LineString id=" & chr(&H22) & currentLineName & chr(&H22) & ">")
filestreamOUT.WriteLine(vbtab & vbtab & vbtab & vbtab & "<coordinates>")

for i = 0 to Ubound(arrKML,2)
 dim lineName
 lineName = arrKML(3,i)
 if lineName <> currentLineName then ' if it's different put a marker in it.
  filestreamOUT.WriteLine(vbtab & vbtab & vbtab & vbtab & "</coordinates>")
  filestreamOUT.WriteLine(vbtab & vbtab & vbtab & "</LineString>")
  filestreamOUT.WriteLine(vbtab & vbtab & vbtab & "</Placemark>")
  filestreamOUT.WriteLine(vbtab & vbtab & "<Placemark id=" & chr(&H22) & lineName & chr(&H22) & ">")
  filestreamOUT.WriteLine(vbtab & vbtab & vbtab & "<name>" & lineName & "</name>")
  filestreamOUT.WriteLine(vbtab & vbtab & vbtab & "<LineString id=" & chr(&H22) & lineName & chr(&H22) & ">")
  filestreamOUT.WriteLine(vbtab & vbtab & vbtab & vbtab & "<coordinates>")
  currentLineName = lineName
 else ' if it's the same put the coordiantes
  filestreamOUT.WriteLine(vbtab & vbtab & vbtab & vbtab & vbtab & arrKML(1,i) & "," & arrKML(2,i) & ",0  ")
 end if
next

filestreamOUT.WriteLine(vbtab & vbtab & vbtab & vbtab & "</coordinates>")
filestreamOUT.WriteLine(vbtab & vbtab & vbtab & "</LineString>")
filestreamOUT.WriteLine(vbtab & vbtab & "</Placemark>")
filestreamOUT.WriteLine("</Document>")
filestreamOUT.WriteLine("</kml>")

'for z = LBound(arrKML,2) to UBound(arrKML,2)
'    filestreamOUT.WriteLine(arrKML(3,z))
'Next
'
filestreamOUT.Close()
Set filestreamOUT = Nothing
msgbox "Done!"


Like mentioned is really cheesy and really easy to write.  You can probably do better job.  If you can fix my sloppy code!  All you have to do is safe what's in green to a text file and name it nav2kml.vbs in the directory of the navigation file.  Your navigation file should be a follows.

-122.19785 37.5686    101A
-122.19785 37.56863   101A
-122.19785 37.56865   101A


My suggestion is to put your nav into an Excel spreadsheet and export it to a .prn file where the column width is set to 11.  Long   Lat   Linename.  If you need to modify stuff, it's pretty easy to figure out what needs to be changed to suit your needs.  All the lines will be white written as one place.  Write the output file as a *.kml.  Import into GoogleEarth and whalla you get stuff like this!

Data from the San Francisco Bay.  Brought in from some navigation file.

No comments:

Post a Comment