Remove legacy carto code

This commit is contained in:
Paul Chavard 2020-04-30 16:59:38 +02:00
parent 179a8b83b4
commit f0148184ca
4 changed files with 38 additions and 132 deletions

View file

@ -76,7 +76,7 @@ class Champs::CarteChamp < Champ
type: 'FeatureCollection', type: 'FeatureCollection',
id: stable_id, id: stable_id,
bbox: bounding_box, bbox: bounding_box,
features: (legacy_selections_utilisateur + except_selections_utilisateur).map(&:to_feature) features: geo_areas.map(&:to_feature)
} }
end end
@ -84,23 +84,6 @@ class Champs::CarteChamp < Champ
geo_areas.present? geo_areas.present?
end end
def selection_utilisateur_legacy_geometry
if selection_utilisateur_legacy?
selections_utilisateur.first.geometry
elsif selections_utilisateur.present?
{
type: 'MultiPolygon',
coordinates: selections_utilisateur.filter do |selection_utilisateur|
selection_utilisateur.geometry['type'] == 'Polygon'
end.map do |selection_utilisateur|
selection_utilisateur.geometry['coordinates']
end
}
else
nil
end
end
def selection_utilisateur_legacy_geo_area def selection_utilisateur_legacy_geo_area
geometry = selection_utilisateur_legacy_geometry geometry = selection_utilisateur_legacy_geometry
if geometry.present? if geometry.present?
@ -131,35 +114,18 @@ class Champs::CarteChamp < Champ
private private
def selection_utilisateur_legacy? def selection_utilisateur_legacy_geometry
if selections_utilisateur.size == 1 if selections_utilisateur.present?
geometry = selections_utilisateur.first.geometry {
return geometry && geometry['type'] == 'MultiPolygon' type: 'MultiPolygon',
end coordinates: selections_utilisateur.filter do |selection_utilisateur|
selection_utilisateur.geometry['type'] == 'Polygon'
false end.map do |selection_utilisateur|
end selection_utilisateur.geometry['coordinates']
end
def legacy_selections_utilisateur }
if selection_utilisateur_legacy?
selections_utilisateur.first.geometry['coordinates'].map do |coordinates|
GeoArea.new(
geometry: {
type: 'Polygon',
coordinates: coordinates
},
properties: {},
source: GeoArea.sources.fetch(:selection_utilisateur)
)
end
else else
selections_utilisateur nil
end
end
def except_selections_utilisateur
geo_areas.filter do |area|
area.source != GeoArea.sources.fetch(:selection_utilisateur)
end end
end end
end end

View file

@ -1,18 +0,0 @@
namespace :after_party do
desc 'Deployment task: split_geo_area_selection_multipolygons'
task split_geo_area_selection_multipolygons: :environment do
puts "Running deploy task 'split_geo_area_selection_multipolygons'"
Champs::CarteChamp.where.not(value: ['', '[]']).includes(:geo_areas).find_each do |champ|
if champ.send(:selection_utilisateur_legacy?)
legacy_selection_utilisateur = champ.selections_utilisateur.first
champ.send(:legacy_selections_utilisateur).each do |area|
champ.geo_areas << area
end
legacy_selection_utilisateur.destroy
end
end
AfterParty::TaskRecord.create version: '20200414104712'
end
end

View file

@ -58,5 +58,27 @@ FactoryBot.define do
} }
end end
end end
trait :multi_polygon do
geometry do
{
"type": "MultiPolygon",
"coordinates": [
[
[
[2.428439855575562, 46.538476837725796],
[2.4284291267395024, 46.53842148758162],
[2.4282521009445195, 46.53841410755813],
[2.42824137210846, 46.53847314771794],
[2.428284287452698, 46.53847314771794],
[2.428364753723145, 46.538487907747864],
[2.4284291267395024, 46.538491597754714],
[2.428439855575562, 46.538476837725796]
]
]
]
}
end
end
end end
end end

View file

@ -24,16 +24,9 @@ describe ChampSerializer do
end end
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, geo_areas: [geo_area].compact) }
let(:value) { nil } let(:geo_area) { create(:geo_area, :cadastre, :multi_polygon) }
let(:geo_area) { create(:geo_area, :cadastre, geometry: geo_json) } let(:geo_json) { attributes_for(:geo_area, :multi_polygon)[:geometry].stringify_keys }
let(:geo_json) do
{
"type" => 'MultiPolygon',
"coordinates" => coordinates
}
end
let(:coordinates) { [[[2.3859214782714844, 48.87442541960633], [2.3850631713867183, 48.87273183590832], [2.3809432983398438, 48.87081237174292], [2.3859214782714844, 48.87442541960633]]] }
let(:serialized_champ) { let(:serialized_champ) {
{ {
@ -56,29 +49,7 @@ describe ChampSerializer do
let(:serialized_value) { 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) } let(:geo_area) { create(:geo_area, :selection_utilisateur, :polygon) }
context 'value is empty' do
let(:geo_area) { nil }
context 'when value is nil' do
let(:value) { nil }
it { expect(champ.selection_utilisateur_legacy_geo_area).to be_nil }
end
context 'when value is empty array' do
let(:value) { '[]' }
it { expect(champ.selection_utilisateur_legacy_geo_area).to be_nil }
end
context 'when value is blank' do
let(:value) { '' }
it { expect(champ.selection_utilisateur_legacy_geo_area).to be_nil }
end
end
context 'old_api' do context 'old_api' do
let(:serialized_libelle) { "user geometry" } let(:serialized_libelle) { "user geometry" }
@ -87,14 +58,6 @@ describe ChampSerializer do
let(:serializable_object) { champ.selection_utilisateur_legacy_geo_area } let(:serializable_object) { champ.selection_utilisateur_legacy_geo_area }
context 'when value is coordinates' do context 'when value is coordinates' do
let(:value) { coordinates.to_json }
it { expect(subject).to eq(serialized_champ) }
end
context 'when value is geojson' do
let(:value) { geo_json.to_json }
it { expect(subject).to eq(serialized_champ) } it { expect(subject).to eq(serialized_champ) }
end end
end end
@ -120,33 +83,6 @@ describe ChampSerializer do
it { expect(subject).to eq(serialized_champ) } it { expect(subject).to eq(serialized_champ) }
end end
context 'when value is geojson' do
let(:value) { geo_json.to_json }
it { expect(subject).to eq(serialized_champ) }
end
context 'when value is nil' do
let(:value) { nil }
let(:serialized_value) { nil }
it { expect(subject).to eq(serialized_champ) }
end
context 'when value is empty array' do
let(:value) { '[]' }
let(:serialized_value) { nil }
it { expect(subject).to eq(serialized_champ) }
end
context 'when value is blank' do
let(:value) { '' }
let(:serialized_value) { nil }
it { expect(subject).to eq(serialized_champ) }
end
end end
end end