2011-12-11 19:49:57 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# coding: utf-8
|
|
|
|
# Copyright muflax <mail@muflax.com>, 2011
|
|
|
|
# License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
|
|
|
|
|
|
|
|
require "csv"
|
|
|
|
|
2011-12-11 20:10:51 +01:00
|
|
|
Countries = {
|
|
|
|
"Deutschland" => "DE",
|
|
|
|
"Österreich" => "AT",
|
|
|
|
"Schweiz" => "CH",
|
|
|
|
}
|
|
|
|
|
2011-12-11 19:49:57 +01:00
|
|
|
Coords = {}
|
|
|
|
|
|
|
|
# get PLZ coordinates
|
2011-12-11 20:10:51 +01:00
|
|
|
Countries.values.each do |country|
|
|
|
|
Coords[country] = {}
|
|
|
|
CSV.foreach("#{country}.csv") do |row|
|
|
|
|
Coords[country][row[0].to_i] = [row[1].to_f, row[2].to_f]
|
|
|
|
end
|
2011-12-11 19:49:57 +01:00
|
|
|
end
|
|
|
|
|
2011-12-11 20:10:51 +01:00
|
|
|
def coord_of country, plz
|
|
|
|
if Coords[country].include? plz
|
|
|
|
Coords[country][plz]
|
2011-12-11 19:49:57 +01:00
|
|
|
else # approximate it
|
2011-12-11 20:10:51 +01:00
|
|
|
Coords[country].keys.map{|x| [(x - plz).abs, x]}.min[1]
|
2011-12-11 19:49:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-11 20:10:51 +01:00
|
|
|
kml = File.open("lw-german-survey.kml", "w")
|
2011-12-11 19:49:57 +01:00
|
|
|
kml.write <<EOL
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<kml xmlns="http://www.opengis.net/kml/2.2">
|
|
|
|
<Folder>
|
2011-12-11 20:10:51 +01:00
|
|
|
<name>German-speaking LW users</name>
|
2011-12-11 19:49:57 +01:00
|
|
|
EOL
|
|
|
|
|
|
|
|
|
|
|
|
# read form data
|
|
|
|
CSV.foreach("plz.csv", :headers => true) do |row|
|
2011-12-11 20:10:51 +01:00
|
|
|
if Countries.include? row[1]
|
|
|
|
country = Countries[row[1]]
|
2011-12-11 19:49:57 +01:00
|
|
|
plz = row[2].to_i
|
2011-12-11 20:10:51 +01:00
|
|
|
coord = coord_of(country, plz)
|
2011-12-11 19:49:57 +01:00
|
|
|
kml.write <<EOL
|
|
|
|
<Placemark>
|
|
|
|
<name>#{row[0]}</name>
|
|
|
|
<Point>
|
|
|
|
<coordinates>#{coord[0]},#{coord[1]}</coordinates>
|
|
|
|
</Point>
|
|
|
|
</Placemark>
|
|
|
|
EOL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
kml.write <<EOL
|
|
|
|
</Folder>
|
|
|
|
</kml>
|
|
|
|
EOL
|