Merge pull request #924 from sgmap/geojson_api

Geojson api
This commit is contained in:
Mathieu Magnin 2017-11-28 14:47:31 +01:00 committed by GitHub
commit e07e60b6aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 298 additions and 4 deletions

View file

@ -373,6 +373,12 @@ class Dossier < ActiveRecord::Base
end
end
def user_geometry
if json_latlngs.present?
UserGeometry.new(json_latlngs)
end
end
private
def build_attestation

View file

@ -0,0 +1,40 @@
class UserGeometry
alias :read_attribute_for_serialization :send
def initialize(json_latlngs)
@json_latlngs = json_latlngs
end
def value
to_geo_json(@json_latlngs)
end
def type_de_champ
{
id: -1,
libelle: 'user_geometry',
type_champ: 'user_geometry',
order_place: -1,
descripton: ''
}
end
private
def to_geo_json(json_latlngs)
json = JSON.parse(json_latlngs)
coordinates = json.map do |lat_longs|
outbounds = lat_longs.map do |lat_long|
[lat_long['lng'], lat_long['lat']]
end
[outbounds]
end
{
type: 'MultiPolygon',
coordinates: coordinates
}
end
end

View file

@ -0,0 +1,17 @@
class CadastreSerializer < ActiveModel::Serializer
attributes :value, :type_de_champ
def value
object.geometry
end
def type_de_champ
{
id: -1,
libelle: 'cadastre',
type_champ: 'cadastre',
order_place: -1,
descripton: ''
}
end
end

View file

@ -18,11 +18,18 @@ class DossierSerializer < ActiveModel::Serializer
has_one :etablissement
has_many :cerfa
has_many :commentaires
has_many :champs
has_many :champs_private
has_many :pieces_justificatives
has_many :types_de_piece_justificative
has_many :champs do
champs = object.champs + object.quartier_prioritaires + object.cadastres
if object.user_geometry.present?
champs << object.user_geometry
end
champs
end
def email
object.user.try(:email)
end
@ -38,4 +45,19 @@ class DossierSerializer < ActiveModel::Serializer
def invites
object.invites_gestionnaires.pluck(:email)
end
private
def user_geometry(dossier)
{
value: dossier.geometry,
type_de_champ: {
id: -1,
libelle: 'user_geometry',
type_champ: 'user_geometry',
order_place: -1,
descripton: ''
}
}
end
end

View file

@ -0,0 +1,17 @@
class QuartierPrioritaireSerializer < ActiveModel::Serializer
attributes :value, :type_de_champ
def value
object.geometry
end
def type_de_champ
{
id: -1,
libelle: 'quartier prioritaire',
type_champ: 'quartier_prioritaire',
order_place: -1,
descripton: ''
}
end
end

View file

@ -0,0 +1,3 @@
class UserGeometrySerializer < ActiveModel::Serializer
attributes :value, :type_de_champ
end

View file

@ -97,6 +97,108 @@
"order_place": 1,
"description": "description de votre projet"
}
},
{
"value": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
2.3050735,
48.8401501
],
[
2.3052329,
48.8402106
],
[
2.3053759,
48.8400422
],
[
2.3050735,
48.8401501
]
]
]
]
},
"type_de_champ": {
"id": -1,
"libelle": "cadastre",
"type_champ": "cadastre",
"order_place": -1,
"descripton": ""
}
},
{
"value": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
2.3050735,
48.8401501
],
[
2.3052329,
48.8402106
],
[
2.3053759,
48.8400422
],
[
2.3050735,
48.8401501
]
]
]
]
},
"type_de_champ": {
"id": -1,
"libelle": "quartier prioritaire",
"type_champ": "quartier_prioritaire",
"order_place": -1,
"descripton": ""
}
},
{
"value": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
2.3049509525299072,
48.84028511554904
],
[
2.305165529251098,
48.84014035882062
],
[
2.3049670457839966,
48.84005562298059
],
[
2.3049509525299072,
48.84028511554904
]
]
]
]
},
"type_de_champ": {
"id": -1,
"libelle": "user_geometry",
"type_champ": "user_geometry",
"order_place": -1,
"descripton": ""
}
}
],
"champs_private": [
@ -155,9 +257,9 @@
"direction": "direction SGMAP",
"archived_at": null,
"geographic_information": {
"use_api_carto": false,
"quartiers_prioritaires": false,
"cadastre": false
"use_api_carto": true,
"quartiers_prioritaires": true,
"cadastre": true
},
"types_de_champ": [
{

View file

@ -234,6 +234,46 @@ describe API::V1::DossiersController do
it { expect(subject[:type_champ]).to eq('text') }
end
end
context 'when the dossier includes a quartier prioritaire' do
before do
dossier.quartier_prioritaires << create(:quartier_prioritaire)
end
subject do
super().find { |champ| champ[:type_de_champ][:type_champ] == 'quartier_prioritaire' }
end
it { expect(subject[:type_de_champ]).to match({ id: -1, libelle: 'quartier prioritaire', type_champ: 'quartier_prioritaire', order_place: -1, descripton: ''}) }
it { expect(subject[:value]).to match(dossier.quartier_prioritaires.first.geometry.symbolize_keys) }
end
context 'when the dossier includes a cadastre' do
before do
dossier.cadastres << create(:cadastre)
end
subject do
super().find { |champ| champ[:type_de_champ][:type_champ] == 'cadastre' }
end
it { expect(subject[:type_de_champ]).to match({ id: -1, libelle: 'cadastre', type_champ: 'cadastre', order_place: -1, descripton: ''}) }
it { expect(subject[:value]).to match(dossier.cadastres.first.geometry.symbolize_keys) }
end
context 'when the dossier includes some user geometry' do
before do
dossier.json_latlngs = '[[{"lat": 2.0, "lng": 102.0}, {"lat": 3.0, "lng": 103.0}, {"lat": 2.0, "lng": 102.0}]]'
dossier.save
end
subject do
super().find { |champ| champ[:type_de_champ][:type_champ] == 'user_geometry' }
end
it { expect(subject[:type_de_champ]).to match({ id: -1, libelle: 'user_geometry', type_champ: 'user_geometry', order_place: -1, descripton: ''}) }
it { expect(subject[:value]).to match(UserGeometry.new(dossier.json_latlngs).value) }
end
end
describe 'champs_private' do

View file

@ -3,5 +3,6 @@ FactoryGirl.define do
numero '001'
feuille 1
section 'OM'
geometry '{"type": "MultiPolygon", "coordinates": [[[[2.37112834276229, 48.8773116214902], [2.37163254350824, 48.8775780792784], [2.37112834276229, 48.8773116214902]]]]}'
end
end

View file

@ -3,5 +3,6 @@ FactoryGirl.define do
code 'QPcode'
commune 'Paris'
nom 'Test des QP'
geometry '{"type": "MultiPolygon", "coordinates": [[[[2.37112834276229, 48.8773116214902], [2.37163254350824, 48.8775780792784], [2.37112834276229, 48.8773116214902]]]]}'
end
end

View file

@ -967,4 +967,49 @@ describe Dossier do
it { is_expected.to eq("#{dossier.individual.nom} #{dossier.individual.prenom}") }
end
end
describe 'geometry' do
let(:dossier) { create(:dossier, json_latlngs: json_latlngs) }
let(:json_latlngs) { nil }
subject{ dossier.user_geometry }
context 'when there are no map' do
it { is_expected.to eq(nil) }
end
context 'when there are 2 polygones' do
let(:json_latlngs) do
'[[{"lat": 2.0, "lng": 102.0}, {"lat": 3.0, "lng": 103.0}, {"lat": 2.0, "lng": 102.0}],
[{"lat": 2.0, "lng": 102.0}, {"lat": 3.0, "lng": 103.0}, {"lat": 2.0, "lng": 102.0}]]'
end
let(:expected) do
{
"type": "MultiPolygon",
"coordinates":
[
[
[
[102.0, 2.0],
[103.0, 3.0],
[102.0, 2.0]
]
],
[
[
[102.0, 2.0],
[103.0, 3.0],
[102.0, 2.0]
]
]
]
}
end
subject{ dossier.user_geometry.value }
it { is_expected.to eq(expected) }
end
end
end