Merge pull request #2827 from betagouv/carto-clean

Carto clean
This commit is contained in:
Paul Chavard 2018-10-15 17:15:27 +00:00 committed by GitHub
commit 22b3514a2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 65 additions and 126 deletions

View file

@ -19,8 +19,8 @@ class Users::CarteController < UsersController
dossier.cadastres.each(&:destroy) dossier.cadastres.each(&:destroy)
if geo_json.present? if geo_json.present?
ModuleApiCartoService.save_qp! dossier, geo_json ModuleApiCartoService.save_qp!(dossier, geo_json)
ModuleApiCartoService.save_cadastre! dossier, geo_json ModuleApiCartoService.save_cadastre!(dossier, geo_json)
end end
dossier.update!(json_latlngs: geo_json) dossier.update!(json_latlngs: geo_json)
@ -38,7 +38,7 @@ class Users::CarteController < UsersController
@error = true @error = true
else else
if @dossier.procedure.module_api_carto.quartiers_prioritaires? if @dossier.procedure.module_api_carto.quartiers_prioritaires?
quartiers_prioritaires = ModuleApiCartoService.generate_qp(geo_json).values quartiers_prioritaires = ModuleApiCartoService.generate_qp(geo_json)
@dossier.quartier_prioritaires.build(quartiers_prioritaires) @dossier.quartier_prioritaires.build(quartiers_prioritaires)
@data[:quartiersPrioritaires] = quartiers_prioritaires @data[:quartiersPrioritaires] = quartiers_prioritaires
end end

View file

@ -4,23 +4,22 @@ class CARTO::SGMAP::API
def self.search_qp(geojson) def self.search_qp(geojson)
url = [API_CARTO_URL, "quartiers-prioritaires", "search"].join("/") url = [API_CARTO_URL, "quartiers-prioritaires", "search"].join("/")
call(url, { geojson: geojson.to_s }) call(url, geojson)
end end
def self.search_cadastre(geojson) def self.search_cadastre(geojson)
url = [API_CARTO_URL, "cadastre", "geometrie"].join("/") url = [API_CARTO_URL, "cadastre", "geometrie"].join("/")
call(url, { geojson: geojson.to_s }) call(url, geojson)
end end
private private
def self.call(url, params = {}) def self.call(url, geojson)
verify_ssl_mode = OpenSSL::SSL::VERIFY_NONE verify_ssl_mode = OpenSSL::SSL::VERIFY_NONE
params = geojson.to_s
RestClient::Resource.new( client = RestClient::Resource.new(url, verify_ssl: verify_ssl_mode)
url, client.post(params, content_type: 'application/json')
verify_ssl: verify_ssl_mode
).post params[:geojson], content_type: 'application/json'
rescue RestClient::InternalServerError rescue RestClient::InternalServerError
raise RestClient::ResourceNotFound raise RestClient::ResourceNotFound

View file

@ -7,26 +7,23 @@ class CARTO::SGMAP::Cadastre::Adapter
@data_source ||= JSON.parse(CARTO::SGMAP::API.search_cadastre(@coordinates), symbolize_names: true) @data_source ||= JSON.parse(CARTO::SGMAP::API.search_cadastre(@coordinates), symbolize_names: true)
end end
def to_params def results
data_source[:features].map do |feature| data_source[:features].map do |feature|
tmp = filter_properties feature[:properties] filter_properties(feature[:properties]).merge({ geometry: feature[:geometry] })
tmp[:geometry] = feature[:geometry]
tmp
end end
end end
def filter_properties(properties) def filter_properties(properties)
{ properties.slice(
surface_intersection: properties[:surface_intersection], :surface_intersection,
surface_parcelle: properties[:surface_parcelle], :surface_parcelle,
numero: properties[:numero], :numero,
feuille: properties[:feuille], :feuille,
section: properties[:section], :section,
code_dep: properties[:code_dep], :code_dep,
nom_com: properties[:nom_com], :nom_com,
code_com: properties[:code_com], :code_com,
code_arr: properties[:code_arr] :code_arr
} )
end end
end end

View file

@ -7,16 +7,9 @@ class CARTO::SGMAP::QuartiersPrioritaires::Adapter
@data_source ||= JSON.parse(CARTO::SGMAP::API.search_qp(@coordinates), symbolize_names: true) @data_source ||= JSON.parse(CARTO::SGMAP::API.search_qp(@coordinates), symbolize_names: true)
end end
def to_params def results
params = {} data_source[:features].map do |feature|
feature[:properties].merge({ geometry: feature[:geometry] })
data_source[:features].each do |feature|
qp_code = feature[:properties][:code]
params[qp_code] = feature[:properties]
params[qp_code][:geometry] = feature[:geometry]
end end
params
end end
end end

View file

@ -4,18 +4,4 @@ class ModuleAPICarto < ApplicationRecord
validates :use_api_carto, presence: true, allow_blank: true, allow_nil: false validates :use_api_carto, presence: true, allow_blank: true, allow_nil: false
validates :quartiers_prioritaires, presence: true, allow_blank: true, allow_nil: false validates :quartiers_prioritaires, presence: true, allow_blank: true, allow_nil: false
validates :cadastre, presence: true, allow_blank: true, allow_nil: false validates :cadastre, presence: true, allow_blank: true, allow_nil: false
def classes
modules = ''
if quartiers_prioritaires?
modules += 'qp '
end
if cadastre?
modules += 'cadastre '
end
modules
end
end end

View file

@ -1,9 +1,9 @@
class ModuleApiCartoService class ModuleApiCartoService
def self.save_qp!(dossier, json_latlngs) def self.save_qp!(dossier, json_latlngs)
if dossier.procedure.module_api_carto.quartiers_prioritaires? if dossier.procedure.module_api_carto.quartiers_prioritaires?
qp_list = generate_qp JSON.parse(json_latlngs) qp_list = generate_qp(JSON.parse(json_latlngs))
qp_list.each_value do |qp| qp_list.each do |qp|
qp[:dossier_id] = dossier.id qp[:dossier_id] = dossier.id
qp[:geometry] = qp[:geometry].to_json qp[:geometry] = qp[:geometry].to_json
QuartierPrioritaire.create(qp) QuartierPrioritaire.create(qp)
@ -24,18 +24,18 @@ class ModuleApiCartoService
end end
def self.generate_qp(coordinates) def self.generate_qp(coordinates)
coordinates.inject({}) { |acc, coordinate| coordinates.flat_map do |coordinate|
acc.merge CARTO::SGMAP::QuartiersPrioritaires::Adapter.new( CARTO::SGMAP::QuartiersPrioritaires::Adapter.new(
coordinate.map { |element| [element['lng'], element['lat']] } coordinate.map { |element| [element['lng'], element['lat']] }
).to_params ).results
} end
end end
def self.generate_cadastre(coordinates) def self.generate_cadastre(coordinates)
coordinates.flat_map do |coordinate| coordinates.flat_map do |coordinate|
CARTO::SGMAP::Cadastre::Adapter.new( CARTO::SGMAP::Cadastre::Adapter.new(
coordinate.map { |element| [element['lng'], element['lat']] } coordinate.map { |element| [element['lng'], element['lat']] }
).to_params ).results
end end
end end
end end

View file

@ -1,19 +1,25 @@
- if dossier.procedure.module_api_carto.quartiers_prioritaires? - if dossier.procedure.module_api_carto.quartiers_prioritaires?
.col-md-9.col-lg-9#qp.col-md-3.col-lg-3.list .col-md-9.col-lg-9#qp.col-md-3.col-lg-3.list
%h3.text-info Quartiers prioritaires %h3.text-info Quartiers prioritaires
%ul - if dossier.quartier_prioritaires.blank?
- dossier.quartier_prioritaires.each do |qp| Aucun quartier prioritaire sur les zones séléctionnées
%li #{qp.commune} : #{qp.nom} - else
%ul
- dossier.quartier_prioritaires.each do |qp|
%li #{qp.commune} : #{qp.nom}
- if error.present? - if error.present?
%b Merci de dessiner une surface plus petite afin de récupérer les quartiers prioritaires. %b Merci de dessiner une surface plus petite afin de récupérer les quartiers prioritaires.
- if dossier.procedure.module_api_carto.cadastre? - if dossier.procedure.module_api_carto.cadastre?
.col-md-9.col-lg-9#cadastre.col-md-3.col-lg-3.list .col-md-9.col-lg-9#cadastre.col-md-3.col-lg-3.list
%h3.text-warning Parcelles cadastrales %h3.text-warning Parcelles cadastrales
%ul - if dossier.cadastres.blank?
- dossier.cadastres.each do |cadastre| Aucune parcelle cadastrale sur les zones séléctionnées
%li Parcelle nº #{cadastre.numero} - Feuille #{cadastre.code_arr} #{cadastre.section} #{cadastre.feuille} - else
%ul
- dossier.cadastres.each do |cadastre|
%li Parcelle nº #{cadastre.numero} - Feuille #{cadastre.code_arr} #{cadastre.section} #{cadastre.feuille}
- if error.present? - if error.present?
%b Merci de dessiner une surface plus petite afin de récupérer les parcelles cadastrales. %b Merci de dessiner une surface plus petite afin de récupérer les parcelles cadastrales.

View file

@ -114,8 +114,8 @@ shared_examples 'carte_controller_spec' do
before do before do
allow_any_instance_of(CARTO::SGMAP::QuartiersPrioritaires::Adapter) allow_any_instance_of(CARTO::SGMAP::QuartiersPrioritaires::Adapter)
.to receive(:to_params) .to receive(:results)
.and_return({ "QPCODE1234" => { :code => "QPCODE1234", :nom => "QP de test", :commune => "Paris", :geometry => { :type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] } } }) .and_return([{ :code => "QPCODE1234", :nom => "QP de test", :commune => "Paris", :geometry => { :type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] } }])
post :save, params: { dossier_id: dossier.id, selection: json_latlngs } post :save, params: { dossier_id: dossier.id, selection: json_latlngs }
end end
@ -161,7 +161,7 @@ shared_examples 'carte_controller_spec' do
before do before do
allow_any_instance_of(CARTO::SGMAP::Cadastre::Adapter) allow_any_instance_of(CARTO::SGMAP::Cadastre::Adapter)
.to receive(:to_params) .to receive(:results)
.and_return([{ :surface_intersection => "0.0006", :surface_parcelle => 11252.692583090324, :numero => "0013", :feuille => 1, :section => "CD", :code_dep => "30", :nom_com => "Le Grau-du-Roi", :code_com => "133", :code_arr => "000", :geometry => { :type => "MultiPolygon", :coordinates => [[[[4.134084, 43.5209193], [4.1346615, 43.5212035], [4.1346984, 43.521189], [4.135096, 43.5213848], [4.1350839, 43.5214122], [4.1352697, 43.521505], [4.1356278, 43.5211065], [4.1357402, 43.5207188], [4.1350935, 43.5203936], [4.135002, 43.5204366], [4.1346051, 43.5202412], [4.134584, 43.5202472], [4.1345572, 43.5202551], [4.134356, 43.5203137], [4.1342488, 43.5203448], [4.134084, 43.5209193]]]] } }]) .and_return([{ :surface_intersection => "0.0006", :surface_parcelle => 11252.692583090324, :numero => "0013", :feuille => 1, :section => "CD", :code_dep => "30", :nom_com => "Le Grau-du-Roi", :code_com => "133", :code_arr => "000", :geometry => { :type => "MultiPolygon", :coordinates => [[[[4.134084, 43.5209193], [4.1346615, 43.5212035], [4.1346984, 43.521189], [4.135096, 43.5213848], [4.1350839, 43.5214122], [4.1352697, 43.521505], [4.1356278, 43.5211065], [4.1357402, 43.5207188], [4.1350935, 43.5203936], [4.135002, 43.5204366], [4.1346051, 43.5202412], [4.134584, 43.5202472], [4.1345572, 43.5202551], [4.134356, 43.5203137], [4.1342488, 43.5203448], [4.134084, 43.5209193]]]] } }])
post :save, params: { dossier_id: dossier.id, selection: json_latlngs } post :save, params: { dossier_id: dossier.id, selection: json_latlngs }
@ -216,8 +216,8 @@ shared_examples 'carte_controller_spec' do
before do before do
allow_any_instance_of(CARTO::SGMAP::QuartiersPrioritaires::Adapter) allow_any_instance_of(CARTO::SGMAP::QuartiersPrioritaires::Adapter)
.to receive(:to_params) .to receive(:results)
.and_return({ "QPCODE1234" => { :code => "QPCODE1234", :geometry => { :type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] } } }) .and_return([{ :code => "QPCODE1234", :geometry => { :type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]] } }])
post :zones, params: { dossier_id: dossier.id, selection: json_latlngs.to_json }, format: 'js' post :zones, params: { dossier_id: dossier.id, selection: json_latlngs.to_json }, format: 'js'
end end

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe CARTO::SGMAP::Cadastre::Adapter do describe CARTO::SGMAP::Cadastre::Adapter do
subject { described_class.new(coordinates).to_params } subject { described_class.new(coordinates).results }
before do before do
stub_request(:post, "https://apicarto.sgmap.fr/cadastre/geometrie") stub_request(:post, "https://apicarto.sgmap.fr/cadastre/geometrie")
@ -16,15 +16,15 @@ describe CARTO::SGMAP::Cadastre::Adapter do
let(:body) { File.read('spec/support/files/geojson/response_cadastre.json') } let(:body) { File.read('spec/support/files/geojson/response_cadastre.json') }
it { expect(subject).to be_a_instance_of(Array) } it { expect(subject).to be_a_instance_of(Array) }
it { expect(subject.size).to eq 16 } it { expect(subject.size).to eq(16) }
describe 'Attribut filter' do describe 'Attribut filter' do
let(:adapter) { described_class.new(coordinates) } let(:adapter) { described_class.new(coordinates) }
subject { adapter.filter_properties adapter.data_source } subject { adapter.filter_properties(adapter.data_source[:features].first[:properties]) }
it { expect(subject.size).to eq 9 } it { expect(subject.size).to eq(9) }
it do it do
expect(subject.keys).to eq [ expect(subject.keys).to eq([
:surface_intersection, :surface_intersection,
:surface_parcelle, :surface_parcelle,
:numero, :numero,
@ -34,7 +34,7 @@ describe CARTO::SGMAP::Cadastre::Adapter do
:nom_com, :nom_com,
:code_com, :code_com,
:code_arr :code_arr
] ])
end end
end end

View file

@ -1,7 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe CARTO::SGMAP::QuartiersPrioritaires::Adapter do describe CARTO::SGMAP::QuartiersPrioritaires::Adapter do
subject { described_class.new(coordinates).to_params } subject { described_class.new(coordinates).results }
before do before do
stub_request(:post, "https://apicarto.sgmap.fr/quartiers-prioritaires/search") stub_request(:post, "https://apicarto.sgmap.fr/quartiers-prioritaires/search")
@ -15,18 +15,16 @@ describe CARTO::SGMAP::QuartiersPrioritaires::Adapter do
let(:status) { 200 } let(:status) { 200 }
let(:body) { File.read('spec/support/files/geojson/response_qp.json') } let(:body) { File.read('spec/support/files/geojson/response_qp.json') }
it { expect(subject).to be_a_instance_of(Hash) } it { expect(subject).to be_a_instance_of(Array) }
context 'Attributes' do context 'Attributes' do
let(:qp_code) { 'QP057019' } let(:qp_code) { 'QP057019' }
subject { super()[qp_code] } it { expect(subject.first[:code]).to eq(qp_code) }
it { expect(subject.first[:nom]).to eq('Hauts De Vallières') }
it { expect(subject.first[:commune]).to eq('Metz') }
it { expect(subject[:code]).to eq(qp_code) } it { expect(subject.first[:geometry]).to eq({ :type => "MultiPolygon", :coordinates => [[[[6.2136923480551, 49.1342109827851], [6.21416055031881, 49.1338823553928]]]] }) }
it { expect(subject[:nom]).to eq('Hauts De Vallières') }
it { expect(subject[:commune]).to eq('Metz') }
it { expect(subject[:geometry]).to eq({ :type => "MultiPolygon", :coordinates => [[[[6.2136923480551, 49.1342109827851], [6.21416055031881, 49.1338823553928]]]] }) }
end end
end end

View file

@ -10,44 +10,4 @@ describe ModuleAPICarto do
it { is_expected.to have_db_column(:quartiers_prioritaires) } it { is_expected.to have_db_column(:quartiers_prioritaires) }
it { is_expected.to have_db_column(:cadastre) } it { is_expected.to have_db_column(:cadastre) }
end end
describe '#classes' do
let(:module_api_carto) { create(:module_api_carto, quartiers_prioritaires: qp, cadastre: cadastre) }
context 'when module api carto qp is true' do
let(:qp) { true }
let(:cadastre) { false }
subject { module_api_carto.classes }
it { is_expected.to eq 'qp ' }
end
context 'when module api carto cadastre is true' do
let(:qp) { false }
let(:cadastre) { true }
subject { module_api_carto.classes }
it { is_expected.to eq 'cadastre ' }
end
context 'when module api carto qp is true and cadastre is true' do
let(:qp) { true }
let(:cadastre) { true }
subject { module_api_carto.classes }
it { is_expected.to eq 'qp cadastre ' }
end
context 'when module api carto qp is false and cadastre is false' do
let(:qp) { false }
let(:cadastre) { false }
subject { module_api_carto.classes }
it { is_expected.to eq '' }
end
end
end end