Class: Geonames::SHP

Inherits:
Object
  • Object
show all
Defined in:
lib/geonames_local/data/shp.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ SHP

Returns a new instance of SHP.



7
8
9
10
11
12
13
14
15
16
# File 'lib/geonames_local/data/shp.rb', line 7

def initialize(file)
  @file = file
  @fname = file.split('/')[-1] rescue nil
  @type = Object.module_eval("::#{Opt[:type].capitalize}", __FILE__, __LINE__)
  # @ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
  @sample = nil
  if file
    shp2pg; parse; write
  end
end

Class Method Details

.import(file) ⇒ Object



70
71
72
# File 'lib/geonames_local/data/shp.rb', line 70

def self.import(file)
  new(file)
end

Instance Method Details

#parseObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/geonames_local/data/shp.rb', line 33

def parse
  info 'Parsing dump'
  start = Time.now
  red = 0
  File.open("/tmp/#{@fname}.dump") do |f|
    while line = f.gets
      if record = parse_line(line.chomp)
        @table ||= record.table
        Cache[@table] << record
        red += 1
      end
    end
  end
  info "#{red} parsed. #{Time.now - start}s"
end

#parse_line(l) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/geonames_local/data/shp.rb', line 23

def parse_line(l)
  return if l =~ /^SET\s|^BEGIN;|^COPY\s|^END;|^\\/
  utf = l.encode('UTF-8')
  unless @sample
    info "Free sample\n" + utf.inspect
    @sample = true
  end
  @type.new(Opt[:map], utf, Opt[:nation].upcase, Opt[:city])
end

#reduce!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/geonames_local/data/shp.rb', line 49

def reduce!
  hsh = Cache[:roads].group_by(&:name)
  arr = []
  hsh.map do |_key, vals|
    first = vals.delete_at(0)
    #        p vals[0].geom.geometries.concat(vals[1].geom.geometries)
    vals.map(&:geom).each do |g|
      first.geom.geometries.concat g.geometries
    end
    # = GeoRuby::SimpleFeatures::MultiLineString.
    #  from_line_strings([*vals.map(&:geom).map(&:geometries)])
    first
  end
end

#shp2pgObject



18
19
20
21
# File 'lib/geonames_local/data/shp.rb', line 18

def shp2pg
  info 'Converting SRID'
  `shp2pgsql -D -as 4326 #{@file} nil > /tmp/#{@fname}.dump`
end

#writeObject



64
65
66
67
68
# File 'lib/geonames_local/data/shp.rb', line 64

def write
  db = Postgis.new(Opt[:db])
  Geonames::CLI.do_write(db, Cache[:zones])
  Geonames::CLI.do_write(db, reduce!)
end