HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Converting KML/XML to Javascript

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javascriptconvertingxmlkml

Problem

So, I have the boundaries of every states in the US, from the google map KML file.

This KML file is actually a XML file.

I'm converting this file to a JS file compatible with google map in order to draw polygons on my map.

Here is an excerpt from the file with all the coordinates:


  
    WA
    
    #style37
    
      
        
          1
          
            -122.402015585862,48.2252165511906 
            -122.368333031748,48.1281417374007 
            -122.216991980112,48.0074395523983 
            -122.230120864997,47.9691133009971 
            -122.302922293019,47.9502148146411 
            -122.394492319816,47.7741760704507 
            -122.414815251142,47.6641799154458 
            -122.382220450337,47.5954090424224 
            -122.392633724016,47.510242430349 
            -122.319738644767,47.3901148739843 
            -122.325376306437,47.3443234291417 
            -122.420837154063,47.3188444009071 
            
          
        
      
    
  


Here is my code to convert the KML file to the JS file.

```
require 'rubygems'
require 'xmlsimple'

def read_xmlfile(file_name)
file = File.open(file_name, "r")
data = file.read
file.close
return data
end

target = "states.js"

xml_data = read_xmlfile('Stateborders.xml')

data = XmlSimple.xml_in(xml_data, 'KeyToSymbol' => true)

content = "var stateBorders = {\n"
data[:document][0].each_with_index do |item, index|
states = item[1]
states.each_with_index do |state, index|
name = state[:name][0]
coord = state[:polygon][0][:outerboundaryis][0][:linearring][0][:coordinates][0].tr("\t", "").strip.gsub(/[\n]/,") ,new google.maps.LatLng(").gsub(" ) ,", "), ");
coord = state[:polygon][0][:outerboundaryis][0][:linearring][0][:coordinates][0].tr("\t", "").strip
coord2 = ""
coord.split("\n").each do |ll|
ll = ll.split(",")
lat = ll[1].to_s.strip
lat = lat.to_f.round(6).to_s
lon = ll[0].to_s.strip
lon = lon.to_f.round(6).to

Solution

@Flambino has already explained the main problems in your code. I'd only add: 1) why do you use each_with_index if you don't actually use the indexes? 2) Ruby would have a terrible standard library if you needed 4 lines just to read the contents of a file: contents = File.read(path).

That's how I'd write it:

require 'nokogiri'
require 'json'

kml = Nokogiri::XML(open("Stateborders.xml"))
coordinates_by_state = kml.css("Placemark").map do |placemark|
  state = placemark.at_css("name").text.strip
  coordinates = placemark.at_css("coordinates").text.strip.lines.map do |line|
    line.split(",").map { |coord| coord.to_f.round(6).to_s }
  end
  [state, coordinates]
end.to_h

jscode = "var stateBorders = #{coordinates_by_state.to_json};"
File.write("stateborders.js", jscode)

Code Snippets

require 'nokogiri'
require 'json'

kml = Nokogiri::XML(open("Stateborders.xml"))
coordinates_by_state = kml.css("Placemark").map do |placemark|
  state = placemark.at_css("name").text.strip
  coordinates = placemark.at_css("coordinates").text.strip.lines.map do |line|
    line.split(",").map { |coord| coord.to_f.round(6).to_s }
  end
  [state, coordinates]
end.to_h

jscode = "var stateBorders = #{coordinates_by_state.to_json};"
File.write("stateborders.js", jscode)

Context

StackExchange Code Review Q#46688, answer score: 4

Revisions (0)

No revisions yet.