diff --git a/app/assets/javascripts/carte/cadastre.js b/app/assets/javascripts/carte/cadastre.js new file mode 100644 index 000000000..db3912df5 --- /dev/null +++ b/app/assets/javascripts/carte/cadastre.js @@ -0,0 +1,61 @@ +function cadastre_active() { + return $("#map.cadastre").length > 0 +} + +function get_cadastre(coordinates) { + if (!cadastre_active()) + return; + + var cadastre; + + $.ajax({ + method: 'post', + url: '/users/dossiers/' + dossier_id + '/carte/cadastre', + data: {coordinates: JSON.stringify(coordinates)}, + dataType: 'json', + async: false + }).done(function (data) { + cadastre = data + }); + + return cadastre['cadastres']; +} + +function display_cadastre(cadastre_list) { + if (!cadastre_active()) + return; + + cadastre_array = cadastre_list; + + $("#cadastre.list ul").html(''); + + new_cadastreLayer(); + + if (cadastre_array.length > 0) { + cadastre_array.forEach(function (cadastre) { + $("#cadastre.list ul").append('
  • Parcelle n°' + cadastre.numero + ' - Feuille ' + cadastre.code_arr + ' ' + cadastre.section + ' ' + cadastre.feuille+ '
  • '); + + cadastreItems.addData(cadastre.geometry); + }); + + cadastreItems.setStyle({ + fillColor: '#8a6d3b', + weight: 2, + opacity: 0.3, + color: 'white', + dashArray: '3', + fillOpacity: 0.7 + }) + } + else + $("#cadastre.list ul").html('
  • AUCUN
  • '); +} + +function new_cadastreLayer() { + if (typeof cadastreItems != 'undefined') + map.removeLayer(cadastreItems); + + cadastreItems = new L.GeoJSON(); + + cadastreItems.addTo(map); +} \ No newline at end of file diff --git a/app/assets/javascripts/carte.js b/app/assets/javascripts/carte/carte.js similarity index 65% rename from app/assets/javascripts/carte.js rename to app/assets/javascripts/carte/carte.js index 9722c28d4..7d9ae560d 100644 --- a/app/assets/javascripts/carte.js +++ b/app/assets/javascripts/carte/carte.js @@ -43,12 +43,20 @@ function default_position (){ return {lon: LON, lat: LAT, zoom: 13} } +function get_external_data (latLngs){ + + if (qp_active()) + display_qp(get_qp(latLngs)); + + if (cadastre_active()) + display_cadastre(get_cadastre(latLngs)); +} + function add_event_freeDraw() { freeDraw.on('markers', function (e) { $("#json_latlngs").val(JSON.stringify(e.latLngs)); - if (qp_active()) - display_qp(get_qp(e.latLngs)); + get_external_data(e.latLngs); }); $("#new").on('click', function (e) { @@ -78,58 +86,6 @@ function get_position() { return position; } -function qp_active() { - return $("#map.qp").length > 0 -} - -function get_qp(coordinates) { - if (!qp_active()) - return; - - var qp; - - $.ajax({ - method: 'post', - url: '/users/dossiers/' + dossier_id + '/carte/qp', - data: {coordinates: JSON.stringify(coordinates)}, - dataType: 'json', - async: false - }).done(function (data) { - qp = data - }); - - return qp['quartier_prioritaires']; -} - -function display_qp(qp_list) { - if (!qp_active()) - return; - - qp_array = jsObject_to_array(qp_list); - - $("#qp_list ul").html(''); - - new_qpLayer(); - - if (qp_array.length > 0) { - qp_array.forEach(function (qp) { - $("#qp_list ul").append('
  • ' + qp.commune + ' : ' + qp.nom + '
  • '); - - qpItems.addData(qp.geometry); - }); - } - else - $("#qp_list ul").html('
  • AUCUN
  • '); -} - -function new_qpLayer() { - if (typeof qpItems != 'undefined') - map.removeLayer(qpItems); - - qpItems = new L.GeoJSON(); - qpItems.addTo(map); -} - function jsObject_to_array(qp_list) { return Object.keys(qp_list).map(function (v) { return qp_list[v]; diff --git a/app/assets/javascripts/carte/qp.js b/app/assets/javascripts/carte/qp.js new file mode 100644 index 000000000..c53fdc1f2 --- /dev/null +++ b/app/assets/javascripts/carte/qp.js @@ -0,0 +1,60 @@ +function qp_active() { + return $("#map.qp").length > 0 +} + +function get_qp(coordinates) { + if (!qp_active()) + return; + + var qp; + + $.ajax({ + method: 'post', + url: '/users/dossiers/' + dossier_id + '/carte/qp', + data: {coordinates: JSON.stringify(coordinates)}, + dataType: 'json', + async: false + }).done(function (data) { + qp = data + }); + + return qp['quartier_prioritaires']; +} + +function display_qp(qp_list) { + if (!qp_active()) + return; + + qp_array = jsObject_to_array(qp_list); + + $("#qp.list ul").html(''); + + new_qpLayer(); + + if (qp_array.length > 0) { + qp_array.forEach(function (qp) { + $("#qp.list ul").append('
  • ' + qp.commune + ' : ' + qp.nom + '
  • '); + + qpItems.addData(qp.geometry); + }); + + qpItems.setStyle({ + fillColor: '#31708f', + weight: 2, + opacity: 0.3, + color: 'white', + dashArray: '3', + fillOpacity: 0.7 + }) + } + else + $("#qp.list ul").html('
  • AUCUN
  • '); +} + +function new_qpLayer() { + if (typeof qpItems != 'undefined') + map.removeLayer(qpItems); + + qpItems = new L.GeoJSON(); + qpItems.addTo(map); +} \ No newline at end of file diff --git a/app/assets/javascripts/franceconnect_kit.js b/app/assets/javascripts/franceconnect_kit.js index ae85ee7b2..db691788f 100644 --- a/app/assets/javascripts/franceconnect_kit.js +++ b/app/assets/javascripts/franceconnect_kit.js @@ -2,6 +2,6 @@ $(document).on('page:load', franceconnect_kit); $(document).ready(franceconnect_kit); function franceconnect_kit() { - setTimeout(init, 200); + franceConnectKit.init(); } diff --git a/app/assets/stylesheets/carte.scss b/app/assets/stylesheets/carte.scss index 1175313f9..ebbb98dc7 100644 --- a/app/assets/stylesheets/carte.scss +++ b/app/assets/stylesheets/carte.scss @@ -15,10 +15,24 @@ table { height: 600px; } - #map.qp { + #map.qp, #map.cadastre { @extend .col-md-9; @extend .col-lg-9; } + + .list { + @extend .col-md-3; + @extend .col-lg-3; + margin-bottom: 20px; + + h3 { + margin-top: 0px; + } + + ul li { + margin-bottom: 10px; + } + } } #infos_dossier { @@ -28,18 +42,7 @@ table { } } -#qp_list { - @extend .col-md-3; - @extend .col-lg-3; - h3 { - margin-top: 0px; - } - - ul li { - margin-bottom: 10px; - } -} #map section.overlay { position: absolute; diff --git a/app/controllers/users/carte_controller.rb b/app/controllers/users/carte_controller.rb index 48cf3bf1f..3ce7df336 100644 --- a/app/controllers/users/carte_controller.rb +++ b/app/controllers/users/carte_controller.rb @@ -60,6 +60,12 @@ class Users::CarteController < UsersController render json: {quartier_prioritaires: qp} end + def get_cadastre + cadastres = generate_cadastre JSON.parse(params[:coordinates]) + + render json: {cadastres: cadastres} + end + private def generate_qp coordinates @@ -72,4 +78,15 @@ class Users::CarteController < UsersController qp end + + def generate_cadastre coordinates + cadastre = [] + + coordinates.each_with_index do |coordinate, index | + coordinate = coordinates[index].map { |latlng| [latlng['lng'], latlng['lat']] } + cadastre << CARTO::SGMAP::Cadastre::Adapter.new(coordinate).to_params + end + + cadastre.flatten + end end diff --git a/app/models/module_api_carto.rb b/app/models/module_api_carto.rb index 81c8c159a..1b9e47df1 100644 --- a/app/models/module_api_carto.rb +++ b/app/models/module_api_carto.rb @@ -4,4 +4,13 @@ class ModuleAPICarto < ActiveRecord::Base validates :use_api_carto, 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 + + def classes + modules = '' + + modules += 'qp ' if quartiers_prioritaires? + modules += 'cadastre ' if cadastre? + + modules + end end diff --git a/app/services/geojson_service.rb b/app/services/geojson_service.rb index 8e75e7472..785a5d3a0 100644 --- a/app/services/geojson_service.rb +++ b/app/services/geojson_service.rb @@ -1,5 +1,5 @@ class GeojsonService - def self.to_json_polygon coordinates + def self.to_json_polygon_for_qp coordinates polygon = { geo: { type: "Polygon", @@ -9,4 +9,21 @@ class GeojsonService polygon.to_json end + + def self.to_json_polygon_for_cadastre coordinates + + polygon = { + geom: { + type: "Feature", + geometry: { + type: "Polygon", + coordinates: [ + coordinates + ] + } + } + } + + polygon.to_json + end end \ No newline at end of file diff --git a/app/views/users/carte/show.html.haml b/app/views/users/carte/show.html.haml index 74e2cfabd..aab40cb34 100644 --- a/app/views/users/carte/show.html.haml +++ b/app/views/users/carte/show.html.haml @@ -12,13 +12,18 @@ %br %br #carte_page.row + #map{class: @dossier.procedure.module_api_carto.classes } + - if @dossier.procedure.module_api_carto.quartiers_prioritaires - #map.qp - #qp_list - %h3 Quartiers prioritaites + #qp.list + %h3.text-info Quartiers prioritaites %ul - -else - #map + + - if @dossier.procedure.module_api_carto.cadastre + #cadastre.list + %h3.text-warning Cadastres + %ul + = form_tag(url_for({controller: :carte, action: :save, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST') do %br diff --git a/config/routes.rb b/config/routes.rb index 839499570..3b22f3761 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,8 @@ Rails.application.routes.draw do get '/carte/position' => 'carte#get_position' post '/carte/qp' => 'carte#get_qp' + post '/carte/cadastre' => 'carte#get_cadastre' + get '/carte' => 'carte#show' post '/carte' => 'carte#save' diff --git a/lib/carto/sgmap/cadastre/adapter.rb b/lib/carto/sgmap/cadastre/adapter.rb index 02dd0cb53..7d513f347 100644 --- a/lib/carto/sgmap/cadastre/adapter.rb +++ b/lib/carto/sgmap/cadastre/adapter.rb @@ -1,6 +1,6 @@ class CARTO::SGMAP::Cadastre::Adapter def initialize(coordinates) - @coordinates = GeojsonService.to_json_polygon(coordinates) + @coordinates = GeojsonService.to_json_polygon_for_cadastre(coordinates) end def data_source diff --git a/lib/carto/sgmap/quartiers_prioritaires/adapter.rb b/lib/carto/sgmap/quartiers_prioritaires/adapter.rb index d8d422229..855c3f30a 100644 --- a/lib/carto/sgmap/quartiers_prioritaires/adapter.rb +++ b/lib/carto/sgmap/quartiers_prioritaires/adapter.rb @@ -1,6 +1,6 @@ class CARTO::SGMAP::QuartiersPrioritaires::Adapter def initialize(coordinates) - @coordinates = GeojsonService.to_json_polygon(coordinates) + @coordinates = GeojsonService.to_json_polygon_for_qp(coordinates) end def data_source diff --git a/spec/models/module_api_carto_spec.rb b/spec/models/module_api_carto_spec.rb index 7672fd865..80fca6b82 100644 --- a/spec/models/module_api_carto_spec.rb +++ b/spec/models/module_api_carto_spec.rb @@ -10,4 +10,44 @@ describe ModuleAPICarto do it { is_expected.to have_db_column(:quartiers_prioritaires) } it { is_expected.to have_db_column(:cadastre) } 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 diff --git a/spec/services/geojson_service_spec.rb b/spec/services/geojson_service_spec.rb index c6bb3bb54..128663468 100644 --- a/spec/services/geojson_service_spec.rb +++ b/spec/services/geojson_service_spec.rb @@ -1,8 +1,23 @@ require 'spec_helper' describe GeojsonService do - describe '.toGeoJsonPolygon' do - subject { JSON.parse(described_class.to_json_polygon coordinates) } + let(:good_coordinates) { + [ + [5.93536376953125, + 48.91888968903368], + [5.93536376953125, + 49.26780455063753], + [7.094421386718749, + 49.26780455063753], + [7.094421386718749, + 48.91888968903368], + [5.93536376953125, + 48.91888968903368] + ] + } + + describe '.toGeoJsonPolygonForQp' do + subject { JSON.parse(described_class.to_json_polygon_for_qp coordinates) } describe 'coordinates are empty' do let(:coordinates) { '' } @@ -12,23 +27,30 @@ describe GeojsonService do end describe 'coordinates are informed' do - let(:coordinates) { - [ - [5.93536376953125, - 48.91888968903368], - [5.93536376953125, - 49.26780455063753], - [7.094421386718749, - 49.26780455063753], - [7.094421386718749, - 48.91888968903368], - [5.93536376953125, - 48.91888968903368] - ] - } + let(:coordinates) { good_coordinates } it { expect(subject['geo']['type']).to eq('Polygon') } it { expect(subject['geo']['coordinates']).to eq([coordinates]) } end end + + describe '.toGeoJsonPolygonForCadastre' do + subject { JSON.parse(described_class.to_json_polygon_for_cadastre coordinates) } + + describe 'coordinates are empty' do + let(:coordinates) { '' } + + it { expect(subject['geom']['type']).to eq('Feature') } + it { expect(subject['geom']['geometry']['type']).to eq('Polygon') } + it { expect(subject['geom']['geometry']['coordinates']).to eq([[coordinates]]) } + end + + describe 'coordinates are informed' do + let(:coordinates) { good_coordinates } + + it { expect(subject['geom']['type']).to eq('Feature') } + it { expect(subject['geom']['geometry']['type']).to eq('Polygon') } + it { expect(subject['geom']['geometry']['coordinates']).to eq([[coordinates]]) } + end + end end