Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Export A GoogleEarth KML File To CSV Output

DZone's Guide to

Export A GoogleEarth KML File To CSV Output

·
Free Resource
# Parses a GoogleEarth KML file and writes the pertinent data to a CSV file

#
 
# 
 
  
# 
  
   
#   
   
    Folder Name
   
#   
   
    1
   
#   
   
    
# 		... Screen Overlays
#   
   
#   
   
    
#     
    
     Document Name
    
#     
    
     1
    
#     
    
     
# 		... Schema Def ...
#     
    
#     
    
#     
    
     
#       
     
      FolderName
     
#       
     
      
# 	      
      
       Location name 1
      
# 	      
      
       ...Description text...
      
# 	      
      
       #khStyle16037
      
# 	      
      
       
# 	        
       
        relativeToGround
       
# 	        
       
        -81.85829063169155,29.12257052899974,100
       
# 	      
      
# 	      
      
       Column 1 data
      
# 	      
      
       Column 2 data
      
#       
     
#       
     
      
#         
      
       Location name 2
      
#         
      
       ...Description text...
      
#         
      
       #khStyle16037
      
#         
      
       
#           
       
        relativeToGround
       
#           
       
        -81.85829063169155,29.12257052899974,100
       
#         
      
#         
      
       Column 1 data
      
#         
      
       Column 2 data
      
#       
     
# 	
# 		... More Locations ...
#    
    
#   
   
# 
  
# 
 



require "rexml/document"
include REXML
kmlroot = (Document.new File.new "data.kml").root
nodes = kmlroot.elements.to_a("//Location")

begin
	f = File.open("data.csv", "w")
	f << "id,column1,column2,latitude,longitude"\n"
	id = 1
	nodes.each { |node|
		column1 = node.elements["column1"].text
		column2 = node.elements["column2"].text
		coords = node.elements["Point"].elements["coordinates"].text.split(",")
		f << [id, column1, column2, coords[1], coords[0] ].join(",") << "\n"
		id += 1
	}
ensure
	f.close
end
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}