Class: Ekylibre::CultivableZonesJsonExchanger

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

Overview

Import a GeoJSON file (as FeatureCollection) with `name` and `number` properties for each feature.

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

def check

valid = true
clusters = JSON.parse(file.read).deep_symbolize_keys
unless clusters[:type] == 'FeatureCollection'
  w.error 'Invalid format'
  valid = false
end
valid

end


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/exchangers/ekylibre/cultivable_zones_json_exchanger.rb', line 15

def import
  clusters = JSON.parse(file.read)
  ensure_clusters_valid_geojson(clusters)
  clusters['features'].each do |feature|
    shape = ::Charta.from_geojson(feature)
    properties = feature['properties']
    attributes = properties.slice('work_number')
                           .merge(shape: shape)

    cultivable_zone = zones_overlapping(shape).first ||
                      CultivableZone.new(attributes)

    cultivable_zone.name = properties['name'] if properties['name']
    cultivable_zone.save!
  end
end