Class: Ekylibre::BuildingsJsonExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/buildings_json_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, 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


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/exchangers/ekylibre/buildings_json_exchanger.rb', line 13

def import
  clusters = JSON.parse(file.read)
  if clusters['type'] == 'FeatureCollection'
    clusters['features'].each do |feature|
      properties = feature['properties']
      shape = ::Charta.from_geojson(feature).convert_to(:multi_polygon)
      variant = ProductNatureVariant.import_from_nomenclature(:building)
      building = Building.create!(properties.slice('name').merge(initial_shape: shape, variant: variant))
      divisions = properties['divisions']
      next unless divisions && divisions.any?
      divisions['features'].each do |division|
        properties = division['properties']
        shape = ::Charta.from_geojson(division).convert_to(:multi_polygon)
        variant = ProductNatureVariant.import_from_nomenclature(:building_division)
        BuildingDivision.create!(properties.slice('name').merge(initial_shape: shape, variant: variant, initial_owner: Entity.of_company))
        # FIXME: We lose level (storey) and building information on division recording
      end
    end
  else
    raise ActiveExchanger::NotWellFormedFileError, 'File seems to be JSON but not GeoJSON.'
  end
end