Implement atomic operations on MapEditor
This commit is contained in:
parent
d3ea20968e
commit
05e408225b
9 changed files with 258 additions and 101 deletions
|
@ -40,6 +40,44 @@ class Champs::CarteController < ApplicationController
|
|||
response.status = 503
|
||||
end
|
||||
|
||||
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
|
||||
save_geometry!(geo_area)
|
||||
|
||||
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])
|
||||
save_geometry!(geo_area)
|
||||
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def populate_cadastres(feature_collection)
|
||||
|
@ -61,4 +99,37 @@ class Champs::CarteController < ApplicationController
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def save_geometry!(geo_area)
|
||||
geo_area.geometry = params[:feature][:geometry]
|
||||
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
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue