Class: Ekylibre::CultivableZonesExchanger

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

Overview

Import a list of cultivable zone from georeadings Prefer ekylibre/cultivable_zones_json to import directly cultivable zones REMOVEME This exchanger is not very useful in standalone mode

Constant Summary collapse

DEFAULT_SHAPE =
Charta.new_geometry('MULTIPOLYGON(((0 0,0 1,1 1,1 0,0 0)))').freeze

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

#find_or_init_by_number(work_number) ⇒ Object


12
13
14
# File 'app/exchangers/ekylibre/cultivable_zones_exchanger.rb', line 12

def find_or_init_by_number(work_number)
  CultivableZone.find_or_initialize_by(work_number: work_number)
end

#find_zone_by_matching_shape(shape) ⇒ Object


8
9
10
# File 'app/exchangers/ekylibre/cultivable_zones_exchanger.rb', line 8

def find_zone_by_matching_shape(shape)
  CultivableZone.shape_covering(shape, 0.02).first || CultivableZone.shape_matching(shape, 0.02).first
end

#importObject

Create or updates cultivable zones


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

def import
  rows = CSV.read(file, headers: true).delete_if { |r| r[0].blank? }
  w.count = rows.size

  rows.each do |row|
    r = {
      name: (row[0].blank? ? nil : row[0].to_s),
      nature: (row[1].blank? ? nil : row[1].to_sym),
      code: (row[2].blank? ? nil : row[2].to_s),
      georeading_number: (row[3].blank? ? nil : row[3].to_s),
      soil_nature: (row[4].blank? ? nil : row[4].to_sym),
      owner_name: (row[5].blank? ? nil : row[5].to_s),
      farmer_name: (row[6].blank? ? nil : row[6].to_s)
    }.to_struct

    # check if existing CultivableZone cover or overlap a current object to import
    georeading = Georeading.find_by(number: r.georeading_number) ||
                 Georeading.find_by(name: r.georeading_number)
    default_zone = find_or_init_by_number(r.code)
    
    if georeading
      shape = georeading.content
      zone = find_zone_by_matching_shape(shape)&.tap { |z| z.shape = shape }
      zone ||= default_zone.tap { |z| z.shape = shape }
    end

    Rails.logger.warn "Cannot find georeading: #{r.georeading_number}" unless zone
    zone ||= default_zone.tap { |z| z.shape = DEFAULT_SHAPE.dup }

    # update name & code anyway
    zone.name = r.name if r.name
    zone.work_number = r.code if r.code
    if r.soil_nature && Nomen::SoilNature[r.soil_nature]
      zone.soil_nature ||= r.soil_nature
    end

    # link or update the owner if exist
    if r.owner_name
      owner = Entity.find_by('full_name ILIKE ?', r.owner_name.strip)
      zone.owner ||= owner if owner
    end

    # link or update the farmer if exist
    if r.farmer_name
      farmer = Entity.find_by('full_name ILIKE ?', r.farmer_name.strip)
      zone.farmer ||= farmer if farmer
    end

    zone.save!
    w.check_point
  end
end