Debian Codegraph @

benchmark/geo-select-generate-grn.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2 # -*- coding: utf-8 -*-
3
4 if ARGV.size != 2
5 puts "Usage: #{$0} SOURCE_CSV OUTPUT_GRN"
6 puts " e.g.: #{$0} fixtures/geo-select/13_2010.CSV fixtures/geo-select/load.grn"
7 exit(false)
8 end
9
10 csv, grn = ARGV
11
12 require "fileutils"
13 require "csv"
14
15 FileUtils.mkdir_p(File.dirname(grn))
16 File.open(grn, "w") do |output|
17 output.print(<<-EOH.strip)
18 table_create Addresses TABLE_HASH_KEY ShortText
19 column_create Addresses location COLUMN_SCALAR WGS84GeoPoint
20
21 table_create Locations TABLE_PAT_KEY WGS84GeoPoint
22 column_create Locations address COLUMN_INDEX Addresses location
23
24 load --table Addresses
25 [
26 ["_key", "location"]
27 EOH
28
29 headers = nil
30 csv_foreach_args = [csv]
31 csv_foreach_args << {:encoding => "UTF-8"} if defined?(Encoding)
32 CSV.foreach(*csv_foreach_args) do |row|
33 if headers.nil?
34 headers = row
35 else
36 record = {}
37 headers.each_with_index do |header, i|
38 record[header] = row[i]
39 end
40 central_value_p = record["代表フラグ"] == "1"
41 next unless central_value_p
42 name =
43 record["都道府県名"] + record["市区町村名"] +
44 record["大字・町丁目"] + record["街区符号・地番"]
45 location = "%sx%s" % [record["緯度"], record["経度"]]
46 output.print(",\n[\"#{name}\", \"#{location}\"]")
47 end
48 end
49 output.print(<<-EOF)
50
51 ]
52 EOF
53 end