Add get_cadastre on TPS carte page and display on leaflet map

This commit is contained in:
Xavier J 2016-01-15 11:53:00 +01:00
parent 6d4b29cf36
commit 2ce8e4734b
14 changed files with 283 additions and 91 deletions

View file

@ -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('<li> Parcelle n°' + cadastre.numero + ' - Feuille ' + cadastre.code_arr + ' ' + cadastre.section + ' ' + cadastre.feuille+ '</li>');
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('<li>AUCUN</li>');
}
function new_cadastreLayer() {
if (typeof cadastreItems != 'undefined')
map.removeLayer(cadastreItems);
cadastreItems = new L.GeoJSON();
cadastreItems.addTo(map);
}

View file

@ -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('<li>' + qp.commune + ' : ' + qp.nom + '</li>');
qpItems.addData(qp.geometry);
});
}
else
$("#qp_list ul").html('<li>AUCUN</li>');
}
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];

View file

@ -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('<li>' + qp.commune + ' : ' + qp.nom + '</li>');
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('<li>AUCUN</li>');
}
function new_qpLayer() {
if (typeof qpItems != 'undefined')
map.removeLayer(qpItems);
qpItems = new L.GeoJSON();
qpItems.addTo(map);
}

View file

@ -2,6 +2,6 @@ $(document).on('page:load', franceconnect_kit);
$(document).ready(franceconnect_kit);
function franceconnect_kit() {
setTimeout(init, 200);
franceConnectKit.init();
}

View file

@ -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;

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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'

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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