Class: Geonames::SHP
- Inherits:
-
Object
- Object
- Geonames::SHP
- Defined in:
- lib/geonames_local/data/shp.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(file) ⇒ SHP
constructor
A new instance of SHP.
- #parse ⇒ Object
- #parse_line(l) ⇒ Object
- #reduce! ⇒ Object
- #shp2pg ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(file) ⇒ SHP
Returns a new instance of SHP.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/geonames_local/data/shp.rb', line 8 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
71 72 73 |
# File 'lib/geonames_local/data/shp.rb', line 71 def self.import(file) new(file) end |
Instance Method Details
#parse ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/geonames_local/data/shp.rb', line 34 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
24 25 26 27 28 29 30 31 32 |
# File 'lib/geonames_local/data/shp.rb', line 24 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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/geonames_local/data/shp.rb', line 50 def reduce! hsh = Cache[:roads].group_by { |r| r.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 |
#shp2pg ⇒ Object
19 20 21 22 |
# File 'lib/geonames_local/data/shp.rb', line 19 def shp2pg info "Converting SRID" `shp2pgsql -D -as 4326 #{@file} nil > /tmp/#{@fname}.dump` end |