Friday, February 12, 2010

KML to Lat/Long Array in Ruby

Several of the next applications of the qrpTracker codebase will require me to draw rudimentary maps on an LCD screen. I've been using Google Earth's polygon tool to make polygons that roughly correspond to, say, Prince Edward Island (in the small scale) and North and South America (in the large scale). Google Earth will allow you to copy the corresponding kml fragment. The following code snippet will turn that kml polygon into a C array of two point arrays, suitable for storing in your Arduino as Progmem.


foo = File.open("maritimes.kml")
foo.each {|line|
line.scan(/([-]?[0-9]+\.[0-9]*),([-]?[0-9]+\.[0-9]*),0/).each {|matching|
puts "{#{matching[0]},#{matching[1]}},"
}
}

Obviously, you want to store the kml in a file and change the file name in the above when necessary.

When using this, be sure your selected object doesn't have multiple polygons, because the above code will blithely join them together, making a mess.

No comments:

Post a Comment