Class: Telepac::V2014::CapLandParcelsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/telepac/v2014/cap_land_parcels_exchanger.rb

Direct Known Subclasses

Telepac::V2015::CapLandParcelsExchanger

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, deprecated?, exchanger_name, export, exporters, find, find_and_import, find_by, 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


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/exchangers/telepac/v2014/cap_land_parcels_exchanger.rb', line 4

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

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

    # Import 2015 islet
    file.each do |record|
      # if record.index == 0
      # check cap_statement presence for the consider year
      harvest_year = record.attributes['CAMPAGNE'].to_s
      campaign = Campaign.of(harvest_year)

      pacage_number = record.attributes['PACAGE'].to_s

      company = Entity.of_company
      siret_number = company.siret_number
      siret_number = '000000000000018' if siret_number.blank?
      cap_statement_attributes = {
        campaign: campaign,
        declarant: company,
        farm_name: company ? company.full_name : 'No declarant',
        pacage_number: pacage_number,
        siret_number: siret_number
      }
      ## find or create cap statement
      cap_statement = ::CapStatement.find_or_initialize_by(campaign: campaign, pacage_number: pacage_number)
      cap_statement.attributes = cap_statement_attributes
      cap_statement.save!
      # end

      islet_attributes = {
        cap_statement: cap_statement,
        islet_number: record.attributes['NUMERO'].to_s,
        town_number: record.attributes['COMMUNE'].to_s,
        shape: ::Charta.new_geometry(record.geometry).transform(:WGS84).to_rgeo
      }

      # find or create islet according to cap statement
      cap_islet = ::CapIslet.find_or_initialize_by(islet_attributes.slice(:islet_number, :cap_statement))
      cap_islet.attributes = islet_attributes
      cap_islet.save!

      # import into georeadings
      label = 'I' + '-' + cap_islet.cap_statement.pacage_number.to_s + '-' + cap_islet.cap_statement.campaign.harvest_year.to_s + '-' + cap_islet.islet_number.to_s
      georeadings_attributes = {
        name: label,
        number: label,
        nature: :polygon,
        content: cap_islet.shape
      }
      unless ::Georeading.find_by(georeadings_attributes.slice(:number))
        ::Georeading.create!(georeadings_attributes)
      end

      w.check_point
    end
  end
end