#!/usr/bin/env ruby # -*- coding: utf-8 -*- Encoding.default_external = 'utf-8' require 'nokogiri' require 'json' year = Time.at(Time.now-90*3600*24).year imgurlrepo = "https://www.yatex.org/gitbucket/sakatamap/pubphoto" imgurldir = "raw/master/#{year}" imgurlmap = "map" outfile = "/dev/stdout" quiet = false usage = (<<-EOF).gsub(/^\t/, "") #{File.basename($0)} [Options] SakataMapKMLfile 前提: 写真のGitBucketリポジトリのベースが #{imgurlrepo} でさらにその年の #{imgurldir} に、マップ名ごとのディレクトリを 作成して1280ピクセルの写真を置くことを仮定している。 オプション: -y 年 GitBucketリポジトリ年ディレクトリ -m マップ名 GitBucket内のマップ名 -d ディレクトリ名 raw/master/ 以外のものにしたい場合 -o 書き出しGeoJSON名 EOF while ARGV[0] && /^-[A-Z]/i =~ ARGV[0] case ARGV[0] when "-m" imgurlmap = ARGV[1]; ARGV.shift when "-r" imgurlrepo = ARGV[1]; ARGV.shift when "-y" imgurldir = "raw/master/" + ARGV[1]; ARGV.shift when "-d" imgurldir = ARGV[1]; ARGV.shift when "-o" outfile = ARGV[1]; ARGV.shift when "-q" quiet = true end ARGV.shift end unless ARGV[0] print usage; exit 0 end doc = Nokogiri::XML(open(ARGV[0]){|f|f.read}) i=0 features=[] gj = { "type" => "FeatureCollection", } doc.css("Placemark").each do |d| child = d.children desc = child.css('SimpleData[name="説明文"]')[0] imgs = child.css('SimpleData[name="画像ファイル名"]')[0] # Transplant image file names to {{JPEG}} format of uMap if imgs && imgs.text > '' umap = imgs.text.split(/;\s*/).collect{|i| sprintf("{{%s/%s/%s/%s}}", imgurlrepo, imgurldir, imgurlmap, i) }.join("\n") desc.inner_html = desc.text+"\n#{umap}" imgs.remove i+=1 end # Construct GeoJSON for uMap style props = {} d.css("SimpleData").each do |f| case f["name"] when "表示名" props["name"] = f.text when "説明文" props["description"] = f.text else props[f["name"]] = f.text end end next unless d.css("hogehoge")[0] lonlat = d.css("coordinates")[0].text.split(",") features << { "type" => "Feature", "geometry" => {"type"=>"Point", "coordinates"=>lonlat}, "properties" => props } end gj["features"] = features open(outfile, "w"){|w| w.write(JSON.pretty_generate(gj))} quiet || STDERR.printf("Output to %s with %d records replaced\n", outfile, i)