demarches-normaliennes/app/controllers/champs/carte_controller.rb

72 lines
2 KiB
Ruby
Raw Normal View History

2018-10-17 12:02:34 +02:00
class Champs::CarteController < ApplicationController
before_action :authenticate_logged_user!
def show
@selector = ".carte-#{params[:position]}"
if params[:dossier].key?(:champs_attributes)
geo_json = params[:dossier][:champs_attributes][params[:position]][:value]
else
geo_json = params[:dossier][:champs_private_attributes][params[:position]][:value]
end
if params[:champ_id].present?
@champ = Champ
.joins(:dossier)
.where(dossiers: { user_id: logged_user_ids })
.find(params[:champ_id])
2018-10-17 12:02:34 +02:00
else
@champ = Champs::CarteChamp.new(type_de_champ: TypeDeChamp.new(
type_champ: TypeDeChamp.type_champs.fetch(:carte),
options: {
quartiers_prioritaires: true,
cadastres: true
}
))
end
geo_areas = []
geo_json = geo_json.blank? ? [] : JSON.parse(geo_json)
2018-10-17 12:02:34 +02:00
if geo_json.first == ["error", "TooManyPolygons"]
@error = true
2018-11-21 12:42:13 +01:00
@champ.value = nil
@champ.geo_areas = []
elsif geo_json.present?
2018-10-17 12:02:34 +02:00
if @champ.cadastres?
cadastres = ModuleApiCartoService.generate_cadastre(geo_json)
geo_areas += cadastres.map do |cadastre|
cadastre[:source] = GeoArea.sources.fetch(:cadastre)
cadastre
end
end
if @champ.quartiers_prioritaires?
quartiers_prioritaires = ModuleApiCartoService.generate_qp(geo_json)
geo_areas += quartiers_prioritaires.map do |qp|
qp[:source] = GeoArea.sources.fetch(:quartier_prioritaire)
qp
end
end
2018-10-23 15:38:20 +02:00
if @champ.parcelles_agricoles?
parcelles_agricoles = ModuleApiCartoService.generate_rpg(geo_json)
geo_areas += parcelles_agricoles.map do |parcelle_agricole|
parcelle_agricole[:source] = GeoArea.sources.fetch(:parcelle_agricole)
parcelle_agricole
end
end
2018-10-17 12:02:34 +02:00
2018-11-21 12:42:13 +01:00
@champ.geo_areas = geo_areas.map do |geo_area|
GeoArea.new(geo_area)
end
2018-10-17 12:02:34 +02:00
2018-11-21 12:42:13 +01:00
@champ.value = geo_json.to_json
end
2018-10-17 12:02:34 +02:00
if @champ.persisted?
@champ.save
end
end
end