[Fix #508]Api GeoJson: export dossier.json_latlngs to geojson

This commit is contained in:
Simon Lehericey 2017-10-30 16:54:33 +01:00 committed by Mathieu Magnin
parent f34546cf2a
commit 28a047a64b
6 changed files with 128 additions and 1 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

@ -23,7 +23,11 @@ class DossierSerializer < ActiveModel::Serializer
has_many :types_de_piece_justificative
has_many :champs do
object.champs + object.quartier_prioritaires + object.cadastres
champs = object.champs + object.quartier_prioritaires + object.cadastres
if object.user_geometry.present?
champs << object.user_geometry
end
champs
end
def email
@ -41,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,3 @@
class UserGeometrySerializer < ActiveModel::Serializer
attributes :value, :type_de_champ
end

View file

@ -260,6 +260,20 @@ describe API::V1::DossiersController do
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

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