cleanup selection_utilisateur geo_json
This commit is contained in:
parent
f51f68ec03
commit
35d2bc36fc
4 changed files with 56 additions and 19 deletions
|
@ -8,9 +8,9 @@ class Champs::CarteController < ApplicationController
|
|||
@selector = ".carte-#{params[:position]}"
|
||||
|
||||
if params[:dossier].key?(:champs_attributes)
|
||||
geo_json = params[:dossier][:champs_attributes][params[:position]][:value]
|
||||
coordinates = params[:dossier][:champs_attributes][params[:position]][:value]
|
||||
else
|
||||
geo_json = params[:dossier][:champs_private_attributes][params[:position]][:value]
|
||||
coordinates = params[:dossier][:champs_private_attributes][params[:position]][:value]
|
||||
end
|
||||
|
||||
if params[:champ_id].present?
|
||||
|
@ -30,18 +30,18 @@ class Champs::CarteController < ApplicationController
|
|||
|
||||
geo_areas = []
|
||||
|
||||
if geo_json == EMPTY_GEO_JSON
|
||||
if coordinates == EMPTY_GEO_JSON
|
||||
@champ.value = nil
|
||||
@champ.geo_areas = []
|
||||
elsif geo_json == ERROR_GEO_JSON
|
||||
elsif coordinates == ERROR_GEO_JSON
|
||||
@error = true
|
||||
@champ.value = nil
|
||||
@champ.geo_areas = []
|
||||
else
|
||||
geo_json = JSON.parse(geo_json)
|
||||
coordinates = JSON.parse(coordinates)
|
||||
|
||||
if @champ.cadastres?
|
||||
cadastres = ModuleApiCartoService.generate_cadastre(geo_json)
|
||||
cadastres = ModuleApiCartoService.generate_cadastre(coordinates)
|
||||
geo_areas += cadastres.map do |cadastre|
|
||||
cadastre[:source] = GeoArea.sources.fetch(:cadastre)
|
||||
cadastre
|
||||
|
@ -49,7 +49,7 @@ class Champs::CarteController < ApplicationController
|
|||
end
|
||||
|
||||
if @champ.quartiers_prioritaires?
|
||||
quartiers_prioritaires = ModuleApiCartoService.generate_qp(geo_json)
|
||||
quartiers_prioritaires = ModuleApiCartoService.generate_qp(coordinates)
|
||||
geo_areas += quartiers_prioritaires.map do |qp|
|
||||
qp[:source] = GeoArea.sources.fetch(:quartier_prioritaire)
|
||||
qp
|
||||
|
@ -57,7 +57,7 @@ class Champs::CarteController < ApplicationController
|
|||
end
|
||||
|
||||
if @champ.parcelles_agricoles?
|
||||
parcelles_agricoles = ModuleApiCartoService.generate_rpg(geo_json)
|
||||
parcelles_agricoles = ModuleApiCartoService.generate_rpg(coordinates)
|
||||
geo_areas += parcelles_agricoles.map do |parcelle_agricole|
|
||||
parcelle_agricole[:source] = GeoArea.sources.fetch(:parcelle_agricole)
|
||||
parcelle_agricole
|
||||
|
@ -68,7 +68,7 @@ class Champs::CarteController < ApplicationController
|
|||
GeoArea.new(geo_area)
|
||||
end
|
||||
|
||||
@champ.value = geo_json.to_json
|
||||
@champ.value = GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates)
|
||||
end
|
||||
|
||||
if @champ.persisted?
|
||||
|
|
|
@ -44,18 +44,18 @@ class Champs::CarteChamp < Champ
|
|||
end
|
||||
|
||||
def geo_json
|
||||
@geo_json ||= value.blank? ? [] : JSON.parse(value)
|
||||
@geo_json ||= value.blank? ? nil : JSON.parse(value)
|
||||
end
|
||||
|
||||
def user_geometry
|
||||
{
|
||||
type: 'Polygon',
|
||||
coordinates: [
|
||||
geo_json[0].map do |polygon|
|
||||
[polygon['lng'], polygon['lat']]
|
||||
end
|
||||
]
|
||||
}
|
||||
# We used to store in the value column a json array with coordinates.
|
||||
if geo_json.is_a?(Array)
|
||||
# If it is a coordinates array, format it as a GEO-JSON
|
||||
GeojsonService.to_json_polygon_for_selection_utilisateur(geo_json)
|
||||
else
|
||||
# It is already a GEO-JSON
|
||||
geo_json
|
||||
end
|
||||
end
|
||||
|
||||
def user_geo_area
|
||||
|
|
|
@ -36,4 +36,21 @@ class GeojsonService
|
|||
|
||||
polygon.to_json
|
||||
end
|
||||
|
||||
def self.to_json_polygon_for_selection_utilisateur(coordinates)
|
||||
coordinates = coordinates.map do |lat_longs|
|
||||
outbounds = lat_longs.map do |lat_long|
|
||||
[lat_long['lng'], lat_long['lat']]
|
||||
end
|
||||
|
||||
[outbounds]
|
||||
end
|
||||
|
||||
polygon = {
|
||||
type: 'MultiPolygon',
|
||||
coordinates: coordinates
|
||||
}
|
||||
|
||||
polygon.to_json
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue