Skip to content

Instantly share code, notes, and snippets.

@thash
Created June 25, 2011 15:51
Show Gist options
  • Save thash/1046608 to your computer and use it in GitHub Desktop.
Save thash/1046608 to your computer and use it in GitHub Desktop.
GatherCSV for MongoDB 2d index
# -*- coding: utf-8 -*-
# 次のように加工する
# rr_cd,line_cd,station_cd,line_sort,station_sort,station_g_cd,r_type,rr_name,line_name,station_name,pref_cd,lon,lat,f_flag
# 28,28001,2800101,28001,2800101,2100201,2,東京メトロ,東京メトロ銀座線,浅草,13,139.797592,35.710733,1
# ↓
# 28,28001,2800101,28001,2800101,2100201,2,東京メトロ,東京メトロ銀座線,浅草,13,"{ lon : 139.797592, lat : 35.710733 }",1
require 'pp'
exit if ARGV == []
f = File.open(ARGV[0])
skipheader = 1
while line = f.gets
if skipheader == 1
header = line.gsub("lon,lat","location")
skipheader -= 1
puts header
else
oneline = ""
ary = line.chomp.split(",")
for i in 0...ary.length
if i == 13
oneline << ary[i] + "\n"
elsif i == 11
tmploc = '"{ lon : ' + ary[i] + ", "
elsif i == 12
tmploc << 'lat : ' + ary[i] + ' }"'
oneline << tmploc + ","
tmploc = ""
else
oneline << ary[i] + ","
end
end
puts oneline
end
end
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment