Created
October 22, 2012 04:47
-
-
Save tsunagun/3929685 to your computer and use it in GitHub Desktop.
つくば市当番医リストからkmlファイルを生成するスクリプト
This file contains 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
# coding: UTF-8 | |
require 'open-uri' | |
require 'nokogiri' | |
require 'net/http' | |
require 'json' | |
require 'builder/xmlmarkup' | |
require 'nkf' | |
class MedicalInstitute | |
attr_accessor :name, :transcription, :departments, :address, :telephone, :lat, :long | |
end | |
def geocode(address) | |
address = URI.encode(address) | |
hash = Hash.new | |
baseUrl = "http://maps.google.com/maps/api/geocode/json" | |
reqUrl = "#{baseUrl}?address=#{address}&sensor=false&language=ja" | |
response = Net::HTTP.get_response(URI.parse(reqUrl)) | |
status = JSON.parse(response.body) | |
hash[:lat] = status['results'][0]['geometry']['location']['lat'] | |
hash[:long] = status['results'][0]['geometry']['location']['lng'] | |
return hash | |
end | |
mis = Hash.new{|hash, key| hash[key] = Array.new} | |
date = nil | |
uri = "http://www.city.tsukuba.ibaraki.jp/902/004371.html" | |
parser = Nokogiri::HTML(open(uri).read) | |
index = 0 | |
parser.xpath("//tbody/tr[@height='58']").each do |tr| | |
if index >= 2 | |
medical_institute = MedicalInstitute.new | |
j = 0 | |
tr.xpath("td").each do |td| | |
case j | |
when 0 | |
medical_institute.name = td.text | |
when 1 | |
medical_institute.address = "茨城県つくば市" + td.text | |
when 2 | |
medical_institute.departments = td.text.split("・") | |
when 3 | |
medical_institute.telephone = "029-" + td.text.gsub('-', '-') | |
end | |
j += 1 | |
end | |
# 読み取得 | |
medical_institute.transcription = tr.previous.at_xpath("td[not(@rowspan)]").text | |
# 日付取得 | |
date = tr.xpath("preceding::td[@rowspan='4']").last.text | |
# 緯度経度取得 | |
geo = geocode(medical_institute.address) | |
medical_institute.lat, medical_institute.long = geo[:lat], geo[:long] | |
mis[date] << medical_institute | |
end | |
index += 1 | |
end | |
mis.each do |day, medical_institutes| | |
file = "2012_10_#{NKF.nkf('-WwZ0', day.split('(').first)}.kml" | |
output = "" | |
xml = Builder::XmlMarkup.new(:target => output, :indent=>2) | |
xml.instruct! | |
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") { | |
xml.Document { | |
xml.Folder { | |
medical_institutes.each do |medical_institute| | |
xml.Placemark { | |
xml.name(medical_institute.name) | |
xml.description{ | |
xml.cdata!(" | |
<table> | |
<tr> | |
<td>医療機関名: </td> | |
<td>#{medical_institute.name} </td> | |
</tr> | |
<tr> | |
<td>住所: </td> | |
<td>#{medical_institute.address} </td> | |
</tr> | |
<tr> | |
<td>電話番号: </td> | |
<td>#{medical_institute.telephone} </td> | |
</tr> | |
<tr> | |
<td>診療科目: </td> | |
<td>#{medical_institute.departments.join(', ')} </td>} | |
</table> | |
") | |
} | |
xml.Point { | |
xml.coordinates("#{medical_institute.long},#{medical_institute.lat},0.0") | |
} | |
} | |
end | |
} | |
} | |
} | |
open(file, "w") do |f| | |
f.print output | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment