Newer
Older
tools / csv-jpeg2umap.rb
@HIROSE Yuuji HIROSE Yuuji on 15 Dec 2019 2 KB add sakata2umap.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'json'
require 'csv'

jbase = {"type" => "FeatureCollection"}
features = []
urlbase = "//www.koeki-prj.org/a/sktmap/2016"
mapname = "foomap"

def deg2dec(str)
  if /(\d+)d\s*(\d+)m\s*([0-9.]+)s/i =~ str
    $1.to_i + $2.to_f/60 + $3.to_f/3600
  end
end

def getlonlats(jpeg)
  jhead = `jhead #{jpeg}`
  lon=jhead.split("\n").grep(/^GPS Long/)[0].sub(/.*E /, "")
  lat=jhead.split("\n").grep(/^GPS Lat/)[0].sub(/.*N /, "")
  #date=jhead.split("\n").grep(/^Date\/Time/)[0].sub(/.*: /, "")
  [deg2dec(lon), deg2dec(lat)]
end

if !ARGV[0] then
  print(<<-EOF)
	Usage: #{$0} Sakatamap.csv
	EOF
  exit 2
end

while /^-(.*)/ =~ ARGV[0]
  case $1
  when "u"
    urlbase = ARGV[1]; ARGV.shift
  when "m"
    mapname = ARGV[1]; ARGV.shift
  end
  ARGV.shift
end
imgroot = File.join(urlbase, mapname)

# 表示名,種別,正式名称,画像ファイル,住所,電話番号,説明文,筆者,撮影者
csv = CSV.read(ARGV[0])
headings = csv.shift
# p headings
attrpos = Hash.new
i=0
for col in headings
  case col
  when /表示名/
    attrpos["name"] = i
  when /正式名称/
    attrpos["truename"] = i
  when /画像ファイル/
    attrpos["files"] = i
  when /説明文/
    attrpos["description"] = i
  when /(種別|住所|電話番号|筆者|撮影者)/
    attrpos[$1] = i
  end
  i+=1
end

features = Array.new
for row in csv do
  name, truename, files, desc =
    row.values_at(attrpos["name"], attrpos["truename"],
                  attrpos["files"], attrpos["description"])
  jpegs = files.split(/[;, ]/)
  # p name, truename, files, desc
  imgsrc = ""
  #p files
  lon, lat = getlonlats(jpegs[0]) if test(?s, jpegs[0])
  for f in jpegs
    imgsrc += sprintf("{{%s/%s}}\n", imgroot, f)
  end
  props = {
    "name"	=> name,
    "description" => sprintf("## %s\n%s\n%s", truename, desc, imgsrc)
  }
  for p in %w(種別 住所 電話番号 筆者 撮影者)
    if attrpos[p]
      val = row[attrpos[p]]
      props[p] = val if val
    end
  end
  features << {
    "type"	=> "Feature",
    "geometry"	=> {"type" => "Point", "coordinates" => [lon, lat]},
    "properties" => props
  }
end
jbase["features"] = features
puts JSON::pretty_generate(jbase)