Newer
Older
Ruby / #spline22.rb#
#!/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
  p name1
  pos2, name2 = id2lnglat[endId].values
  features << {
    "type" => "Feature",
    "geometry" => {
      "type" => "LineString",		# PolyLineのこと
      "coordinates" => [pos1, pos2],
    },
    "properties" => {
      "name" => "#{name1} -> #{name2}",
      "_umap_options" => {
        "color"		=> "DarkGreen",		# 線の色
        "opacity"	=> 0.9,			# 不透明度
        "weight"	=> 5			# 太さ
      }
    }
  }

  #発電所間をつなぐ線の上にマーカーを足す
  station1=['(pos1[0]+(pos2[0]-pos1[0])*1/3)','( pos1[1]+(pos2[1]-pos1[1])*1/3)']
  station2=['(pos1[0]+(pos2[0]-pos1[0])*2/3)','( pos1[1]+(pos2[1]-pos1[1])*2/3)']
  [station1,station2].each do |st|
    features << {
      "type" => "Feature",
      "geometry" => {
        "type" => "Point",          #マーカー
        "coordinates" => st
      },
      "properties" => {
        "name" => "#{name1} -> #{name2} の間の駅1",
        "_umap_options" => {
          "iconClass"     => "Ball",              # まち針
          "color"         => "Pink",              # マーカーの色
        }
      }
    }
  end
end

geojson = {
  type: "FeatureCollection",
  features: features
}
puts JSON.pretty_generate(geojson)