Last active
April 11, 2019 15:14
-
-
Save katsuyoshi/68decc5188efb5382dd4449162c4b019 to your computer and use it in GitHub Desktop.
Code for Akitaで公開している秋田市議会議員一般選挙ポスター掲示場設置場所一覧表(csv)ファイルからkml形式のファイルに変換
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rexml/document' | |
| require 'csv' | |
| REXML::Document.new.tap do |doc| | |
| doc << REXML::XMLDecl.new('1.0', 'UTF-8') | |
| REXML::Element.new('kml').tap do |kml| | |
| doc.add_element(kml) | |
| kml.add_attribute('xmlns','http://www.opengis.net/kml/2.2') | |
| REXML::Element.new('Document').tap do |root| | |
| kml.add_element(root) | |
| CSV.foreach('./poster.csv', encoding:"CP932:UTF-8", headers:true) do |row| | |
| REXML::Element.new('Placemark').tap do |placemark| | |
| root.add_element(placemark) | |
| REXML::Element.new('name').tap do |name| | |
| placemark.add_element(name) | |
| name.add_text("#{row['設置番号']}. #{row['設置場所']}") | |
| end | |
| REXML::Element.new('description').tap do |description| | |
| placemark.add_element(description) | |
| REXML::CData.new(<<EOS | |
| <table> | |
| <tr><th>設置番号</th><td>#{row['設置番号']}</td></tr> | |
| <tr><th>設置場所</th><td>#{row['設置場所']}</td></tr> | |
| <tr><th>設置住所</th><td>#{row['設置住所']}</td></tr> | |
| <tr><th>投票所名</th><td>#{row['投票所名']}</td></tr> | |
| <tr><th>緯度(世界測地系)</th><td>#{row['緯度(世界測地系)']}</td></tr> | |
| <tr><th>経度(世界測地系)</th><td>#{row['経度(世界測地系)']}</td></tr> | |
| </table> | |
| EOS | |
| ).tap do |cdata| | |
| description.add_text(cdata) | |
| end | |
| end | |
| REXML::Element.new('Point').tap do |point| | |
| placemark.add_element(point) | |
| REXML::Element.new('coordinates').tap do |coordinates| | |
| point.add_element(coordinates) | |
| coordinates.add_text("#{row['経度(世界測地系)']},#{row['緯度(世界測地系)']},0") | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| File.open("poster.kml", 'w') do |file| | |
| doc.write(file, indent=2) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment