#!/usr/bin/env ruby # coding: utf-8 # Copyright muflax , 2011 # License: GNU GPL 3 require "csv" Countries = { "Deutschland" => "DE", "Österreich" => "AT", "Schweiz" => "CH", } Coords = {} # get PLZ coordinates 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 end def coord_of country, plz if Coords[country].include? plz Coords[country][plz] else # approximate it Coords[country].keys.map{|x| [(x - plz).abs, x]}.min[1] end end kml = File.open("lw-german-survey.kml", "w") kml.write < German-speaking LW users EOL # read form data CSV.foreach("plz.csv", :headers => true) do |row| if Countries.include? row[1] country = Countries[row[1]] plz = row[2].to_i coord = coord_of(country, plz) kml.write < #{row[0]} #{coord[0]},#{coord[1]} EOL end end kml.write < EOL