Skip to content

Instantly share code, notes, and snippets.

@darashi
Created February 28, 2009 07:37
Show Gist options
  • Save darashi/71907 to your computer and use it in GitHub Desktop.
Save darashi/71907 to your computer and use it in GitHub Desktop.
SAPICA履歴をKMLに変換する
#!/usr/bin/env ruby1.9
# encoding: utf-8
require 'cgi'
require 'open-uri'
require 'rubygems'
require 'nokogiri'
require 'pit'
appid = Pit.get('ydn', :require => {
'id' => 'your api id'
})['id']
class LocalSearch
def initialize(appid)
@appid = appid
@cache = {}
end
def search(query)
return @cache[query] if @cache[query]
q = CGI.escape(query)
url = "http://map.yahooapis.jp/LocalSearchService/V1/LocalSearch?appid=#{@appid}&p=#{q}"
doc = Nokogiri::XML(URI(url).read)
c = doc.xpath('/LocalSearchResult/Item/DatumWgs84')
latlon = [:Lat, :Lon].map {|el| (c/el).text.to_f}
@cache[query] = latlon
end
attr_reader :cache
end
# 位置情報をルックアップ
ls = LocalSearch.new(appid)
paths = []
places = {}
ARGF.each do |l|
a = l.chomp.split(/,/)
from = a[2]
to = a[3]
if a[1] =~ /^(?:ポイント)?出場$/
c_from = ls.search(from+"駅")
c_to = ls.search(to+"駅")
paths << [a[0], "#{from}->#{to}", c_from, c_to]
places[from] = c_from
places[to] = c_to
end
end
# KMLを書き出し
builder = Nokogiri::XML::Builder.new {
kml('kmlns' => 'http://earth.google.com/kml/2.1') {
Document {
name 'my sapica'
Style('id' => 'linestring') {
LineStyle {
color '33ff0000'
width '20'
}
}
for date, desc, from, to in paths
Placemark {
name date
description desc
styleUrl '#linestring'
LineString {
coordinates from.reverse.join(',')+' '+to.reverse.join(',')
}
}
end
for n, c in places
Placemark {
name n
Point {
coordinates c.reverse.join(',')
}
}
end
}
}
}
print builder.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment