#!/usr/bin/env ruby
# 使い方:
# ./spline.rb ポイントを保存した.csv つなぐ点を保存した.csv > 保存する.geojson
require 'json'
require 'csv'

# Latitude,Longitude,name,adress,description,type,id
power = CSV.read(ARGV[0], headers:true)

# start,end
lines = CSV.read(ARGV[1], headers:true)

# id => [緯度, 経度] の逆変換表を作る
id2lnglat = {}
power.each do |row|
  id, name = row["id"], row["name"]
  lat, lng = row["Latitude"].to_f, row["Longitude"].to_f
  id2lnglat[row["id"]] = {
    "coords" => [lng,lat],	# GeoJSONは緯度経度ではなく経度緯度
    "name" => name
  }
end

# つなぐ点のリストを作る
features = []
lines.each do |row|
  startId, endId = row["start"], row["end"]
  pos1, name1 = id2lnglat[startId].values
  pos2, name2 = id2lnglat[endId].values
  features << {
    "type" => "Feature",
    "geometry" => {
      "type" => "LineString",		# PolyLineのこと
      "coordinates" => [pos1, pos2],
    },
    "properties" => {
      "name" => "#{name1} -> #{name2}",
      "_umap_options" => {
        "color"		=> "Pink",		# 線の色
        "opacity"	=> 0.7,			# 不透明度
        "weight"	=> 5			# 太さ
      }
    }
  }
end
geojson = {
  type: "FeatureCollection",
  features: features
}
puts JSON.pretty_generate(geojson)
