Class: Ekylibre::GeoreadingsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/georeadings_exchanger.rb

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, exchanger_name, export, exporters, find, human_name, import, import!, importers, importers_selection, inherited, #initialize, register_exchanger

Constructor Details

This class inherits a constructor from ActiveExchanger::Base

Instance Method Details

#importObject


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
# File 'app/exchangers/ekylibre/georeadings_exchanger.rb', line 2

def import
  # Unzip file
  dir = w.tmp_dir
  Zip::File.open(file) do |zile|
    zile.each do |entry|
      entry.extract(dir.join(entry.name))
    end
  end

  mimetype = File.read(dir.join('mimetype')).to_s.strip
  nature = mimetype.split('.').last

  RGeo::Shapefile::Reader.open(dir.join('georeading.shp').to_s) do |file|
    # Set number of shapes
    w.count = file.size

    file.each do |record|
      # puts record.attributes['number'].inspect.red
      if record.geometry
        name = if !record.attributes['name'].blank?
                 # TODO: find how to fix non UTF-8 name
                 # puts record.attributes['name'].inspect.red
                 record.attributes['name'].mb_chars.downcase.capitalize
               else
                 record.attributes['number'].to_s.upcase
               end
        attributes = {
          name: name,
          number: record.attributes['number'].to_s.upcase,
          nature: nature
        }
        unless georeading = Georeading.find_by(attributes.slice(:number))
          georeading = Georeading.new(attributes)
        end
        georeading.content = record.geometry
        georeading.save!
      end
      w.check_point
    end
  end
end