Class: Telepac::V2015::LandParcelsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/telepac/v2015/land_parcels_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

#checkObject


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
# File 'app/exchangers/telepac/v2015/land_parcels_exchanger.rb', line 4

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

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

    file.each do |record|
      # check cap_statement presence for the consider year
      harvest_year = record.attributes['CAMPAGNE'].to_s
      campaign = Campaign.of(harvest_year.to_i)
      w.info 'Campaign exist' if campaign

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

      cap_statement_attributes = {
        campaign: campaign,
        declarant: Entity.of_company,
        farm_name: Entity.of_company.full_name,
        pacage_number: pacage_number,
        siret_number: Entity.of_company.siret_number
      }

      ## find or create cap statement
      unless cap_statement = CapStatement.find_by(campaign: campaign, pacage_number: pacage_number)
        w.info 'Cap statement will be created'
      end

      islet_number = record.attributes['NUMERO'].to_s
      # Find an existing islet or stop importing
      unless cap_islet = CapIslet.find_by(cap_statement: cap_statement, islet_number: islet_number)
        w.error "No way to find pacage #{pacage_number} - islet number #{islet_number}. You have to import islets first"
        valid = false
      end
    end
  end

  valid
end

#importObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/exchangers/telepac/v2015/land_parcels_exchanger.rb', line 51

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

  #############################################################################
  # Import landparcel_shapefile from TELEPAC From 2015
  # -- field_name
  # PACAGE
  # NUMERO_SI (land_parcel number)
  # NUMERO (land_parcel_cluster number id)
  # CAMPAGNE (campaign)
  # DPT_NUM (department zone number)
  # SURF_TOT (land_parcel_cluster area)
  # COMMUNE
  # TYPE (cf https://www3.telepac.agriculture.gouv.fr/telepac/pdf/tas/2015/Dossier-PAC-2015_notice_cultures-precisions.pdf)
  # CODE_VAR
  # NUMERO_PA
  # PROD_SEM
  # COMMERC
  # DEROB1
  # DEROB2
  # INTERRANG1
  # INTERRANG2
  # INTERRANG3
  # AGRIBIO
  # AIDEBIO
  # MARAICH
  # MAEC1_CODE
  # MAEC1CIBLE
  # MAEC2_CODE
  # MAEC2CIBLE
  # MAEC3_CODE
  # MAEC3CIBLE
  # AGROFOREST

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

    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.to_i)

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

      cap_statement_attributes = {
        campaign: campaign,
        entity: Entity.of_company,
        exploitation_name: Entity.of_company.full_name,
        pacage_number: pacage_number,
        siret_number: Entity.of_company.siret_number
      }

      ## find or create cap statement
      unless cap_statement = CapStatement.find_by(campaign: campaign, pacage_number: pacage_number)
        cap_statement = CapStatement.create!(cap_statement_attributes)
      end
      # end

      islet_number = record.attributes['NUMERO'].to_s

      # Find an existing islet or stop importing
      unless cap_islet = CapIslet.find_by(cap_statement: cap_statement, islet_number: islet_number)
        w.error 'Import Islets first'
      end

      cap_land_parcel_attributes = {
        cap_islet: cap_islet,
        land_parcel_number: record.attributes['NUMERO_SI'].to_s,
        main_crop_code: record.attributes['TYPE'].to_s,
        main_crop_commercialisation: (record.attributes['COMMERC'].to_s == '1' ? true : false),
        main_crop_precision: record.attributes['CODE_VAR'].to_s,
        main_crop_seed_production: (record.attributes['PROD_SEM'].to_s == '1' ? true : false),
        shape: ::Charta.new_geometry(record.geometry).transform(:WGS84).to_rgeo
      }

      # Find or create a cap land parcel
      unless cap_land_parcel = CapLandParcel.find_by(cap_land_parcel_attributes.slice(:land_parcel_number, :cap_islet))
        cap_land_parcel = CapLandParcel.create!(cap_land_parcel_attributes)
      end

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

      # Create activities if option true
      # if Preference.value(:create_activities_from_telepac, true)

      # TODO

      # end

      w.check_point
    end
  end
end