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
|
|
|
ERROR_GEO_JSON = ''
|
|
|
|
|
2018-10-17 12:02:34 +02:00
|
|
|
def show
|
|
|
|
@selector = ".carte-#{params[:position]}"
|
|
|
|
|
2020-04-14 10:24:30 +02:00
|
|
|
feature_collection = if params[:dossier].key?(:champs_attributes)
|
|
|
|
params[:dossier][:champs_attributes][params[:position]][:value]
|
2018-10-17 12:02:34 +02:00
|
|
|
else
|
2020-04-14 10:24:30 +02:00
|
|
|
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?
|
2019-06-27 16:26:07 +02:00
|
|
|
policy_scope(Champ).find(params[:champ_id])
|
2018-10-17 12:02:34 +02:00
|
|
|
else
|
2019-06-27 16:26:07 +02:00
|
|
|
policy_scope(TypeDeChamp).find(params[:type_de_champ_id]).champ.build
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
geo_areas = []
|
|
|
|
|
2020-04-14 10:24:30 +02:00
|
|
|
if feature_collection == ERROR_GEO_JSON
|
2018-10-17 12:02:34 +02:00
|
|
|
@error = true
|
2018-11-27 12:15:36 +01:00
|
|
|
else
|
2020-04-14 10:24:30 +02:00
|
|
|
feature_collection = JSON.parse(feature_collection, symbolize_names: true)
|
2018-11-27 12:15:36 +01:00
|
|
|
|
2018-10-17 12:02:34 +02:00
|
|
|
if @champ.cadastres?
|
2020-04-14 10:24:30 +02:00
|
|
|
populate_cadastres(feature_collection)
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|
|
|
|
|
2020-04-14 10:24:30 +02:00
|
|
|
geo_areas = GeoArea.from_feature_collection(feature_collection)
|
2018-11-21 12:42:13 +01:00
|
|
|
end
|
2018-10-17 12:02:34 +02:00
|
|
|
|
|
|
|
if @champ.persisted?
|
2020-04-14 10:24:30 +02:00
|
|
|
@champ.update(value: nil, geo_areas: geo_areas)
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|
2020-03-05 13:50:38 +01:00
|
|
|
rescue ApiCarto::API::ResourceNotFound
|
2019-01-24 18:30:14 +01:00
|
|
|
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
|
2020-04-09 17:32:20 +02:00
|
|
|
|
2020-05-05 15:09:29 +02:00
|
|
|
def index
|
|
|
|
@selector = ".carte-#{params[:champ_id]}"
|
|
|
|
@champ = policy_scope(Champ).find(params[:champ_id])
|
|
|
|
@update_cadastres = params[:cadastres]
|
|
|
|
|
|
|
|
if @champ.cadastres? && @update_cadastres
|
|
|
|
@champ.geo_areas.cadastres.destroy_all
|
|
|
|
@champ.geo_areas += GeoArea.from_feature_collection(cadastres_features_collection(@champ.to_feature_collection))
|
|
|
|
@champ.save!
|
|
|
|
end
|
|
|
|
rescue ApiCarto::API::ResourceNotFound
|
|
|
|
flash.alert = 'Les données cartographiques sont temporairement indisponibles. Réessayez dans un instant.'
|
|
|
|
response.status = 503
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
champ = policy_scope(Champ).find(params[:champ_id])
|
|
|
|
geo_area = champ.geo_areas.selections_utilisateur.new
|
2020-05-07 12:06:42 +02:00
|
|
|
save_geometry!(geo_area, params_feature)
|
2020-05-05 15:09:29 +02:00
|
|
|
|
|
|
|
render json: { feature: geo_area.to_feature }, status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
champ = policy_scope(Champ).find(params[:champ_id])
|
|
|
|
geo_area = champ.geo_areas.selections_utilisateur.find(params[:id])
|
2020-05-07 12:06:42 +02:00
|
|
|
save_geometry!(geo_area, params_feature)
|
2020-05-05 15:09:29 +02:00
|
|
|
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
champ = policy_scope(Champ).find(params[:champ_id])
|
|
|
|
champ.geo_areas.selections_utilisateur.find(params[:id]).destroy!
|
|
|
|
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
2020-05-07 12:06:42 +02:00
|
|
|
def import
|
|
|
|
champ = policy_scope(Champ).find(params[:champ_id])
|
|
|
|
params_features.each do |feature|
|
|
|
|
geo_area = champ.geo_areas.selections_utilisateur.new
|
|
|
|
save_geometry!(geo_area, feature)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: champ.to_feature_collection, status: :created
|
|
|
|
end
|
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
private
|
|
|
|
|
2020-04-14 10:24:30 +02:00
|
|
|
def populate_cadastres(feature_collection)
|
|
|
|
coordinates = feature_collection[:features].filter do |feature|
|
|
|
|
feature[:geometry][:type] == 'Polygon'
|
|
|
|
end.map do |feature|
|
|
|
|
feature[:geometry][:coordinates][0].map { |(lng, lat)| { 'lng' => lng, 'lat' => lat } }
|
|
|
|
end
|
|
|
|
|
|
|
|
if coordinates.present?
|
|
|
|
cadastres = ApiCartoService.generate_cadastre(coordinates)
|
|
|
|
|
|
|
|
feature_collection[:features] += cadastres.map do |cadastre|
|
|
|
|
{
|
|
|
|
type: 'Feature',
|
|
|
|
geometry: cadastre.delete(:geometry),
|
|
|
|
properties: cadastre.merge(source: GeoArea.sources.fetch(:cadastre))
|
2020-04-09 17:32:20 +02:00
|
|
|
}
|
2020-04-14 10:24:30 +02:00
|
|
|
end
|
2020-04-09 17:32:20 +02:00
|
|
|
end
|
|
|
|
end
|
2020-05-05 15:09:29 +02:00
|
|
|
|
2020-05-07 12:06:42 +02:00
|
|
|
def params_feature
|
|
|
|
params[:feature]
|
|
|
|
end
|
|
|
|
|
|
|
|
def params_features
|
2020-05-07 18:30:19 +02:00
|
|
|
params[:features]
|
2020-05-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def save_geometry!(geo_area, feature)
|
|
|
|
geo_area.geometry = feature[:geometry]
|
2020-05-05 15:09:29 +02:00
|
|
|
geo_area.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def cadastres_features_collection(feature_collection)
|
|
|
|
coordinates = feature_collection[:features].filter do |feature|
|
|
|
|
feature[:properties][:source] == GeoArea.sources.fetch(:selection_utilisateur) && feature[:geometry]['type'] == 'Polygon'
|
|
|
|
end.map do |feature|
|
|
|
|
feature[:geometry]['coordinates'][0].map { |(lng, lat)| { 'lng' => lng, 'lat' => lat } }
|
|
|
|
end
|
|
|
|
|
|
|
|
if coordinates.present?
|
|
|
|
cadastres = ApiCartoService.generate_cadastre(coordinates)
|
|
|
|
|
|
|
|
{
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: cadastres.map do |cadastre|
|
|
|
|
{
|
|
|
|
type: 'Feature',
|
|
|
|
geometry: cadastre.delete(:geometry),
|
|
|
|
properties: cadastre.merge(source: GeoArea.sources.fetch(:cadastre))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: []
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|