Save selection utilisateur as geo area
This commit is contained in:
parent
4325bc0689
commit
c75e39884e
8 changed files with 94 additions and 46 deletions
|
@ -55,11 +55,15 @@ class Champs::CarteController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
selection_utilisateur = ApiCartoService.generate_selection_utilisateur(coordinates)
|
||||||
|
selection_utilisateur[:source] = GeoArea.sources.fetch(:selection_utilisateur)
|
||||||
|
geo_areas << selection_utilisateur
|
||||||
|
|
||||||
@champ.geo_areas = geo_areas.map do |geo_area|
|
@champ.geo_areas = geo_areas.map do |geo_area|
|
||||||
GeoArea.new(geo_area)
|
GeoArea.new(geo_area)
|
||||||
end
|
end
|
||||||
|
|
||||||
@champ.value = GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates)
|
@champ.value = coordinates.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
if @champ.persisted?
|
if @champ.persisted?
|
||||||
|
|
|
@ -19,6 +19,10 @@ class Champs::CarteChamp < Champ
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def selection_utilisateur
|
||||||
|
geo_areas.find(&:selection_utilisateur?)
|
||||||
|
end
|
||||||
|
|
||||||
def cadastres?
|
def cadastres?
|
||||||
type_de_champ&.cadastres && type_de_champ.cadastres != '0'
|
type_de_champ&.cadastres && type_de_champ.cadastres != '0'
|
||||||
end
|
end
|
||||||
|
@ -45,6 +49,49 @@ class Champs::CarteChamp < Champ
|
||||||
|
|
||||||
def geo_json
|
def geo_json
|
||||||
@geo_json ||= begin
|
@geo_json ||= begin
|
||||||
|
geo_area = selection_utilisateur
|
||||||
|
|
||||||
|
if geo_area
|
||||||
|
geo_area.geometry
|
||||||
|
else
|
||||||
|
geo_json_from_value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def selection_utilisateur_size
|
||||||
|
if geo_json.present?
|
||||||
|
geo_json['coordinates'].size
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_render_data
|
||||||
|
{
|
||||||
|
position: position,
|
||||||
|
selection: user_geo_area&.geometry,
|
||||||
|
quartiersPrioritaires: quartiers_prioritaires? ? quartiers_prioritaires.as_json(except: :properties) : [],
|
||||||
|
cadastres: cadastres? ? cadastres.as_json(except: :properties) : [],
|
||||||
|
parcellesAgricoles: parcelles_agricoles? ? parcelles_agricoles.as_json(except: :properties) : []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def user_geo_area
|
||||||
|
geo_area = selection_utilisateur
|
||||||
|
|
||||||
|
if geo_area.present?
|
||||||
|
geo_area
|
||||||
|
elsif geo_json_from_value.present?
|
||||||
|
GeoArea.new(
|
||||||
|
geometry: geo_json_from_value,
|
||||||
|
source: GeoArea.sources.fetch(:selection_utilisateur)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def geo_json_from_value
|
||||||
|
@geo_json_from_value ||= begin
|
||||||
parsed_value = value.blank? ? nil : JSON.parse(value)
|
parsed_value = value.blank? ? nil : JSON.parse(value)
|
||||||
# We used to store in the value column a json array with coordinates.
|
# We used to store in the value column a json array with coordinates.
|
||||||
if parsed_value.is_a?(Array)
|
if parsed_value.is_a?(Array)
|
||||||
|
@ -62,34 +109,11 @@ class Champs::CarteChamp < Champ
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def selection_utilisateur_size
|
|
||||||
if geo_json.present?
|
|
||||||
geo_json['coordinates'].size
|
|
||||||
else
|
|
||||||
0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_render_data
|
|
||||||
{
|
|
||||||
position: position,
|
|
||||||
selection: geo_json,
|
|
||||||
quartiersPrioritaires: quartiers_prioritaires? ? quartiers_prioritaires.as_json(except: :properties) : [],
|
|
||||||
cadastres: cadastres? ? cadastres.as_json(except: :properties) : [],
|
|
||||||
parcellesAgricoles: parcelles_agricoles? ? parcelles_agricoles.as_json(except: :properties) : []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def user_geo_area
|
|
||||||
if geo_json.present?
|
|
||||||
GeoArea.new(
|
|
||||||
geometry: geo_json,
|
|
||||||
source: GeoArea.sources.fetch(:selection_utilisateur)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def for_api
|
def for_api
|
||||||
geo_json&.to_json
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def for_export
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,4 +30,8 @@ class GeoArea < ApplicationRecord
|
||||||
scope :quartiers_prioritaires, -> { where(source: sources.fetch(:quartier_prioritaire)) }
|
scope :quartiers_prioritaires, -> { where(source: sources.fetch(:quartier_prioritaire)) }
|
||||||
scope :cadastres, -> { where(source: sources.fetch(:cadastre)) }
|
scope :cadastres, -> { where(source: sources.fetch(:cadastre)) }
|
||||||
scope :parcelles_agricoles, -> { where(source: sources.fetch(:parcelle_agricole)) }
|
scope :parcelles_agricoles, -> { where(source: sources.fetch(:parcelle_agricole)) }
|
||||||
|
|
||||||
|
def selection_utilisateur?
|
||||||
|
source == self.class.sources.fetch(:selection_utilisateur)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -39,7 +39,9 @@ class DossierSerializer < ActiveModel::Serializer
|
||||||
|
|
||||||
if champ_carte.present?
|
if champ_carte.present?
|
||||||
carto_champs = champ_carte.geo_areas.to_a
|
carto_champs = champ_carte.geo_areas.to_a
|
||||||
carto_champs << champ_carte.user_geo_area
|
if !carto_champs.find(&:selection_utilisateur?)
|
||||||
|
carto_champs << champ_carte.user_geo_area
|
||||||
|
end
|
||||||
champs += carto_champs.compact
|
champs += carto_champs.compact
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,4 +22,10 @@ class ApiCartoService
|
||||||
).results
|
).results
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.generate_selection_utilisateur(coordinates)
|
||||||
|
{
|
||||||
|
geometry: JSON.parse(GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates))
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,5 +9,9 @@ FactoryBot.define do
|
||||||
nom { 'XYZ' }
|
nom { 'XYZ' }
|
||||||
commune { 'Paris' }
|
commune { 'Paris' }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
trait :selection_utilisateur do
|
||||||
|
source { GeoArea.sources.fetch(:selection_utilisateur) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,9 +3,9 @@ require 'spec_helper'
|
||||||
describe Champs::CarteChamp do
|
describe Champs::CarteChamp do
|
||||||
let(:champ) { Champs::CarteChamp.new(value: value) }
|
let(:champ) { Champs::CarteChamp.new(value: value) }
|
||||||
let(:value) { '' }
|
let(:value) { '' }
|
||||||
let(:geo_json) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) }
|
|
||||||
let(:coordinates) { [[{ "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }, { "lat" => 48.87273183590832, "lng" => 2.3850631713867183 }, { "lat" => 48.87081237174292, "lng" => 2.3809432983398438 }, { "lat" => 48.8712640169951, "lng" => 2.377510070800781 }, { "lat" => 48.87510283703279, "lng" => 2.3778533935546875 }, { "lat" => 48.87544154230615, "lng" => 2.382831573486328 }, { "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }]] }
|
let(:coordinates) { [[{ "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }, { "lat" => 48.87273183590832, "lng" => 2.3850631713867183 }, { "lat" => 48.87081237174292, "lng" => 2.3809432983398438 }, { "lat" => 48.8712640169951, "lng" => 2.377510070800781 }, { "lat" => 48.87510283703279, "lng" => 2.3778533935546875 }, { "lat" => 48.87544154230615, "lng" => 2.382831573486328 }, { "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }]] }
|
||||||
let(:parsed_geo_json) { JSON.parse(geo_json) }
|
let(:geo_json_as_string) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) }
|
||||||
|
let(:geo_json) { JSON.parse(geo_json_as_string) }
|
||||||
|
|
||||||
describe '#to_render_data' do
|
describe '#to_render_data' do
|
||||||
subject { champ.to_render_data }
|
subject { champ.to_render_data }
|
||||||
|
@ -47,15 +47,15 @@ describe Champs::CarteChamp do
|
||||||
context 'when the value is coordinates' do
|
context 'when the value is coordinates' do
|
||||||
let(:value) { coordinates.to_json }
|
let(:value) { coordinates.to_json }
|
||||||
|
|
||||||
let(:selection) { parsed_geo_json }
|
let(:selection) { geo_json }
|
||||||
|
|
||||||
it { is_expected.to eq(render_data) }
|
it { is_expected.to eq(render_data) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the value is geojson' do
|
context 'when the value is geojson' do
|
||||||
let(:value) { geo_json }
|
let(:value) { geo_json.to_json }
|
||||||
|
|
||||||
let(:selection) { parsed_geo_json }
|
let(:selection) { geo_json }
|
||||||
|
|
||||||
it { is_expected.to eq(render_data) }
|
it { is_expected.to eq(render_data) }
|
||||||
end
|
end
|
||||||
|
@ -89,7 +89,7 @@ describe Champs::CarteChamp do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the value is geojson' do
|
context 'when the value is geojson' do
|
||||||
let(:value) { geo_json }
|
let(:value) { geo_json.to_json }
|
||||||
|
|
||||||
it { is_expected.to eq(1) }
|
it { is_expected.to eq(1) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,9 +26,9 @@ describe ChampSerializer do
|
||||||
context 'when type champ is carte' do
|
context 'when type champ is carte' do
|
||||||
let(:champ) { create(:champ_carte, value: value, geo_areas: [geo_area].compact) }
|
let(:champ) { create(:champ_carte, value: value, geo_areas: [geo_area].compact) }
|
||||||
let(:value) { nil }
|
let(:value) { nil }
|
||||||
let(:geo_area) { create(:geo_area, geometry: parsed_geo_json) }
|
let(:geo_area) { create(:geo_area, geometry: geo_json) }
|
||||||
let(:parsed_geo_json) { JSON.parse(geo_json) }
|
let(:geo_json_as_string) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) }
|
||||||
let(:geo_json) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) }
|
let(:geo_json) { JSON.parse(geo_json_as_string) }
|
||||||
let(:coordinates) { [[{ "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }, { "lat" => 48.87273183590832, "lng" => 2.3850631713867183 }, { "lat" => 48.87081237174292, "lng" => 2.3809432983398438 }, { "lat" => 48.8712640169951, "lng" => 2.377510070800781 }, { "lat" => 48.87510283703279, "lng" => 2.3778533935546875 }, { "lat" => 48.87544154230615, "lng" => 2.382831573486328 }, { "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }]] }
|
let(:coordinates) { [[{ "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }, { "lat" => 48.87273183590832, "lng" => 2.3850631713867183 }, { "lat" => 48.87081237174292, "lng" => 2.3809432983398438 }, { "lat" => 48.8712640169951, "lng" => 2.377510070800781 }, { "lat" => 48.87510283703279, "lng" => 2.3778533935546875 }, { "lat" => 48.87544154230615, "lng" => 2.382831573486328 }, { "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }]] }
|
||||||
|
|
||||||
let(:serialized_champ) {
|
let(:serialized_champ) {
|
||||||
|
@ -49,10 +49,14 @@ describe ChampSerializer do
|
||||||
let(:serialized_id) { -1 }
|
let(:serialized_id) { -1 }
|
||||||
let(:serialized_description) { "" }
|
let(:serialized_description) { "" }
|
||||||
let(:serialized_order_place) { -1 }
|
let(:serialized_order_place) { -1 }
|
||||||
let(:serialized_value) { parsed_geo_json }
|
let(:serialized_value) { geo_json }
|
||||||
|
|
||||||
context 'and geo_area is selection_utilisateur' do
|
context 'and geo_area is selection_utilisateur' do
|
||||||
|
let(:geo_area) { create(:geo_area, :selection_utilisateur, geometry: geo_json) }
|
||||||
|
|
||||||
context 'value is empty' do
|
context 'value is empty' do
|
||||||
|
let(:geo_area) { nil }
|
||||||
|
|
||||||
context 'when value is nil' do
|
context 'when value is nil' do
|
||||||
let(:value) { nil }
|
let(:value) { nil }
|
||||||
|
|
||||||
|
@ -85,7 +89,7 @@ describe ChampSerializer do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when value is geojson' do
|
context 'when value is geojson' do
|
||||||
let(:value) { geo_json }
|
let(:value) { geo_json.to_json }
|
||||||
|
|
||||||
it { expect(subject).to eq(serialized_champ) }
|
it { expect(subject).to eq(serialized_champ) }
|
||||||
end
|
end
|
||||||
|
@ -105,7 +109,7 @@ describe ChampSerializer do
|
||||||
let(:serialized_order_place) { champ.order_place }
|
let(:serialized_order_place) { champ.order_place }
|
||||||
let(:serialized_libelle) { champ.libelle }
|
let(:serialized_libelle) { champ.libelle }
|
||||||
let(:serialized_type_champ) { champ.type_champ }
|
let(:serialized_type_champ) { champ.type_champ }
|
||||||
let(:serialized_value) { geo_json }
|
let(:serialized_value) { nil }
|
||||||
|
|
||||||
context 'when value is coordinates' do
|
context 'when value is coordinates' do
|
||||||
let(:value) { coordinates.to_json }
|
let(:value) { coordinates.to_json }
|
||||||
|
@ -114,7 +118,7 @@ describe ChampSerializer do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when value is geojson' do
|
context 'when value is geojson' do
|
||||||
let(:value) { geo_json }
|
let(:value) { geo_json.to_json }
|
||||||
|
|
||||||
it { expect(subject).to eq(serialized_champ) }
|
it { expect(subject).to eq(serialized_champ) }
|
||||||
end
|
end
|
||||||
|
@ -147,7 +151,7 @@ describe ChampSerializer do
|
||||||
it {
|
it {
|
||||||
expect(subject[:geo_areas].first).to include(
|
expect(subject[:geo_areas].first).to include(
|
||||||
source: GeoArea.sources.fetch(:cadastre),
|
source: GeoArea.sources.fetch(:cadastre),
|
||||||
geometry: parsed_geo_json,
|
geometry: geo_json,
|
||||||
numero: '42',
|
numero: '42',
|
||||||
feuille: 'A11'
|
feuille: 'A11'
|
||||||
)
|
)
|
||||||
|
@ -165,13 +169,13 @@ describe ChampSerializer do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'and geo_area is quartier_prioritaire' do
|
context 'and geo_area is quartier_prioritaire' do
|
||||||
let(:geo_area) { create(:geo_area, :quartier_prioritaire, geometry: parsed_geo_json) }
|
let(:geo_area) { create(:geo_area, :quartier_prioritaire, geometry: geo_json) }
|
||||||
|
|
||||||
context 'new_api' do
|
context 'new_api' do
|
||||||
it {
|
it {
|
||||||
expect(subject[:geo_areas].first).to include(
|
expect(subject[:geo_areas].first).to include(
|
||||||
source: GeoArea.sources.fetch(:quartier_prioritaire),
|
source: GeoArea.sources.fetch(:quartier_prioritaire),
|
||||||
geometry: parsed_geo_json,
|
geometry: geo_json,
|
||||||
nom: 'XYZ',
|
nom: 'XYZ',
|
||||||
commune: 'Paris'
|
commune: 'Paris'
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue