File: geo-select-generate-grn.rb

package info (click to toggle)
groonga 15.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 171,500 kB
  • sloc: ansic: 772,536; cpp: 51,530; ruby: 40,538; javascript: 10,250; yacc: 7,045; sh: 5,622; python: 2,821; makefile: 1,677
file content (53 lines) | stat: -rwxr-xr-x 1,349 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

if ARGV.size != 2
  puts "Usage: #{$0} SOURCE_CSV OUTPUT_GRN"
  puts " e.g.: #{$0} fixtures/geo-select/13_2010.CSV fixtures/geo-select/load.grn"
  exit(false)
end

csv, grn = ARGV

require "fileutils"
require "csv"

FileUtils.mkdir_p(File.dirname(grn))
File.open(grn, "w") do |output|
  output.print(<<-EOH.strip)
table_create Addresses TABLE_HASH_KEY ShortText
column_create Addresses location COLUMN_SCALAR WGS84GeoPoint

table_create Locations TABLE_PAT_KEY WGS84GeoPoint
column_create Locations address COLUMN_INDEX Addresses location

load --table Addresses
[
["_key", "location"]
EOH

  headers = nil
  csv_foreach_args = [csv]
  csv_foreach_args << {:encoding => "UTF-8"} if defined?(Encoding)
  CSV.foreach(*csv_foreach_args) do |row|
    if headers.nil?
      headers = row
    else
      record = {}
      headers.each_with_index do |header, i|
        record[header] = row[i]
      end
      central_value_p = record["代表フラグ"] == "1"
      next unless central_value_p
      name =
        record["都道府県名"] + record["市区町村名"] +
        record["大字・町丁目"] + record["街区符号・地番"]
      location = "%sx%s" % [record["緯度"], record["経度"]]
      output.print(",\n[\"#{name}\", \"#{location}\"]")
    end
  end
  output.print(<<-EOF)

]
EOF
end