Merge pull request #3193 from tchak/carte-use-geojson

Save selection utilisateur as geo area
This commit is contained in:
Paul Chavard 2019-08-01 11:47:36 +02:00 committed by GitHub
commit 95af8560b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 122 additions and 46 deletions

View file

@ -55,11 +55,15 @@ class Champs::CarteController < ApplicationController
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|
GeoArea.new(geo_area)
end
@champ.value = GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates)
@champ.value = coordinates.to_json
end
if @champ.persisted?

View file

@ -19,6 +19,10 @@ class Champs::CarteChamp < Champ
end
end
def selection_utilisateur
geo_areas.find(&:selection_utilisateur?)
end
def cadastres?
type_de_champ&.cadastres && type_de_champ.cadastres != '0'
end
@ -45,6 +49,49 @@ class Champs::CarteChamp < Champ
def geo_json
@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)
# We used to store in the value column a json array with coordinates.
if parsed_value.is_a?(Array)
@ -62,34 +109,11 @@ class Champs::CarteChamp < Champ
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
geo_json&.to_json
nil
end
def for_export
nil
end
end

View file

@ -30,4 +30,8 @@ class GeoArea < ApplicationRecord
scope :quartiers_prioritaires, -> { where(source: sources.fetch(:quartier_prioritaire)) }
scope :cadastres, -> { where(source: sources.fetch(:cadastre)) }
scope :parcelles_agricoles, -> { where(source: sources.fetch(:parcelle_agricole)) }
def selection_utilisateur?
source == self.class.sources.fetch(:selection_utilisateur)
end
end

View file

@ -39,7 +39,9 @@ class DossierSerializer < ActiveModel::Serializer
if champ_carte.present?
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
end
end

View file

@ -22,4 +22,10 @@ class ApiCartoService
).results
end
end
def self.generate_selection_utilisateur(coordinates)
{
geometry: JSON.parse(GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates))
}
end
end

View file

@ -0,0 +1,28 @@
namespace :after_party do
desc 'Deployment task: migrate_geo_area_data'
task migrate_geo_area_data: :environment do
puts "Running deploy task 'migrate_geo_area_data'"
progress = ProgressReport.new(Champs::CarteChamp.count)
Champs::CarteChamp.includes(:geo_areas).find_each do |champ|
geo_area = champ.geo_areas.find(&:selection_utilisateur?)
geo_json = champ.geo_json_from_value
if geo_area.blank? && geo_json.present?
GeoArea.create(
champ: champ,
geometry: geo_json,
source: GeoArea.sources.fetch(:selection_utilisateur)
)
progress.inc
end
end
progress.finish
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord.create version: '20190731152733'
end
end

View file

@ -9,5 +9,9 @@ FactoryBot.define do
nom { 'XYZ' }
commune { 'Paris' }
end
trait :selection_utilisateur do
source { GeoArea.sources.fetch(:selection_utilisateur) }
end
end
end

View file

@ -3,9 +3,9 @@ require 'spec_helper'
describe Champs::CarteChamp do
let(:champ) { Champs::CarteChamp.new(value: 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(: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
subject { champ.to_render_data }
@ -47,15 +47,15 @@ describe Champs::CarteChamp do
context 'when the value is coordinates' do
let(:value) { coordinates.to_json }
let(:selection) { parsed_geo_json }
let(:selection) { geo_json }
it { is_expected.to eq(render_data) }
end
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) }
end
@ -89,7 +89,7 @@ describe Champs::CarteChamp do
end
context 'when the value is geojson' do
let(:value) { geo_json }
let(:value) { geo_json.to_json }
it { is_expected.to eq(1) }
end

View file

@ -26,9 +26,9 @@ describe ChampSerializer do
context 'when type champ is carte' do
let(:champ) { create(:champ_carte, value: value, geo_areas: [geo_area].compact) }
let(:value) { nil }
let(:geo_area) { create(:geo_area, geometry: parsed_geo_json) }
let(:parsed_geo_json) { JSON.parse(geo_json) }
let(:geo_json) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) }
let(:geo_area) { create(:geo_area, geometry: geo_json) }
let(:geo_json_as_string) { 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(:serialized_champ) {
@ -49,10 +49,14 @@ describe ChampSerializer do
let(:serialized_id) { -1 }
let(:serialized_description) { "" }
let(:serialized_order_place) { -1 }
let(:serialized_value) { parsed_geo_json }
let(:serialized_value) { geo_json }
context 'and geo_area is selection_utilisateur' do
let(:geo_area) { create(:geo_area, :selection_utilisateur, geometry: geo_json) }
context 'value is empty' do
let(:geo_area) { nil }
context 'when value is nil' do
let(:value) { nil }
@ -85,7 +89,7 @@ describe ChampSerializer do
end
context 'when value is geojson' do
let(:value) { geo_json }
let(:value) { geo_json.to_json }
it { expect(subject).to eq(serialized_champ) }
end
@ -105,7 +109,7 @@ describe ChampSerializer do
let(:serialized_order_place) { champ.order_place }
let(:serialized_libelle) { champ.libelle }
let(:serialized_type_champ) { champ.type_champ }
let(:serialized_value) { geo_json }
let(:serialized_value) { nil }
context 'when value is coordinates' do
let(:value) { coordinates.to_json }
@ -114,7 +118,7 @@ describe ChampSerializer do
end
context 'when value is geojson' do
let(:value) { geo_json }
let(:value) { geo_json.to_json }
it { expect(subject).to eq(serialized_champ) }
end
@ -147,7 +151,7 @@ describe ChampSerializer do
it {
expect(subject[:geo_areas].first).to include(
source: GeoArea.sources.fetch(:cadastre),
geometry: parsed_geo_json,
geometry: geo_json,
numero: '42',
feuille: 'A11'
)
@ -165,13 +169,13 @@ describe ChampSerializer do
end
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
it {
expect(subject[:geo_areas].first).to include(
source: GeoArea.sources.fetch(:quartier_prioritaire),
geometry: parsed_geo_json,
geometry: geo_json,
nom: 'XYZ',
commune: 'Paris'
)