2018-10-17 12:02:34 +02:00
|
|
|
class Champs::CarteController < ApplicationController
|
|
|
|
before_action :authenticate_logged_user!
|
|
|
|
|
2018-11-27 12:15:36 +01:00
|
|
|
EMPTY_GEO_JSON = '[]'
|
|
|
|
ERROR_GEO_JSON = ''
|
|
|
|
|
2018-10-17 12:02:34 +02:00
|
|
|
def show
|
|
|
|
@selector = ".carte-#{params[:position]}"
|
|
|
|
|
|
|
|
if params[:dossier].key?(:champs_attributes)
|
2018-11-29 11:40:07 +01:00
|
|
|
coordinates = params[:dossier][:champs_attributes][params[:position]][:value]
|
2018-10-17 12:02:34 +02:00
|
|
|
else
|
2018-11-29 11:40:07 +01:00
|
|
|
coordinates = params[:dossier][:champs_private_attributes][params[:position]][:value]
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|
|
|
|
|
2019-02-19 16:14:42 +01:00
|
|
|
@champ = if params[:champ_id].present?
|
|
|
|
Champ
|
2018-10-17 12:02:34 +02:00
|
|
|
.joins(:dossier)
|
|
|
|
.where(dossiers: { user_id: logged_user_ids })
|
2018-11-21 12:41:50 +01:00
|
|
|
.find(params[:champ_id])
|
2018-10-17 12:02:34 +02:00
|
|
|
else
|
2019-02-19 16:14:42 +01:00
|
|
|
TypeDeChamp
|
|
|
|
.joins(:procedure)
|
|
|
|
.where(procedures: { administrateur_id: logged_user_ids })
|
|
|
|
.find(params[:type_de_champ_id]).champ.build
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
geo_areas = []
|
|
|
|
|
2018-11-29 11:40:07 +01:00
|
|
|
if coordinates == EMPTY_GEO_JSON
|
2018-11-27 12:15:36 +01:00
|
|
|
@champ.value = nil
|
|
|
|
@champ.geo_areas = []
|
2018-11-29 11:40:07 +01:00
|
|
|
elsif coordinates == ERROR_GEO_JSON
|
2018-10-17 12:02:34 +02:00
|
|
|
@error = true
|
2018-11-21 12:42:13 +01:00
|
|
|
@champ.value = nil
|
|
|
|
@champ.geo_areas = []
|
2018-11-27 12:15:36 +01:00
|
|
|
else
|
2018-11-29 11:40:07 +01:00
|
|
|
coordinates = JSON.parse(coordinates)
|
2018-11-27 12:15:36 +01:00
|
|
|
|
2018-10-17 12:02:34 +02:00
|
|
|
if @champ.cadastres?
|
2018-12-10 11:31:07 +01:00
|
|
|
cadastres = ApiCartoService.generate_cadastre(coordinates)
|
2018-10-17 12:02:34 +02:00
|
|
|
geo_areas += cadastres.map do |cadastre|
|
|
|
|
cadastre[:source] = GeoArea.sources.fetch(:cadastre)
|
|
|
|
cadastre
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @champ.quartiers_prioritaires?
|
2018-12-10 11:31:07 +01:00
|
|
|
quartiers_prioritaires = ApiCartoService.generate_qp(coordinates)
|
2018-10-17 12:02:34 +02:00
|
|
|
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?
|
2018-12-10 11:31:07 +01:00
|
|
|
parcelles_agricoles = ApiCartoService.generate_rpg(coordinates)
|
2018-10-23 15:38:20 +02:00
|
|
|
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-29 11:40:07 +01:00
|
|
|
@champ.value = GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates)
|
2018-11-21 12:42:13 +01:00
|
|
|
end
|
2018-10-17 12:02:34 +02:00
|
|
|
|
|
|
|
if @champ.persisted?
|
|
|
|
@champ.save
|
|
|
|
end
|
2019-01-24 18:30:14 +01:00
|
|
|
|
|
|
|
rescue RestClient::ResourceNotFound
|
|
|
|
flash.alert = 'Les données cartographiques sont temporairement indisponibles. Réessayez dans un instant.'
|
|
|
|
response.status = 503
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|
|
|
|
end
|