From 9cb612bb3d1ce15f0c4172924229848e95ae5b31 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 15 Apr 2020 15:43:38 +0200 Subject: [PATCH] Show area and length on champ carto selections utilisateur --- Gemfile | 2 + Gemfile.lock | 5 ++ app/helpers/champ_helper.rb | 11 ++++ app/models/geo_area.rb | 32 +++++++++++- .../shared/champs/carte/_geo_areas.html.haml | 7 +++ spec/models/geo_area_spec.rb | 50 +++++++++++++++++++ 6 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 spec/models/geo_area_spec.rb diff --git a/Gemfile b/Gemfile index be4cfbaea..acc6af262 100644 --- a/Gemfile +++ b/Gemfile @@ -25,11 +25,13 @@ gem 'devise' # Gestion des comptes utilisateurs gem 'devise-async' gem 'discard' gem 'dotenv-rails', require: 'dotenv/rails-now' # dotenv should always be loaded before rails +gem 'ffi-geos' gem 'flipper' gem 'flipper-active_record' gem 'flipper-ui' gem 'font-awesome-rails' gem 'fugit' +gem 'geo_coord', require: "geo/coord" gem 'geocoder' gem 'gon' gem 'graphql' diff --git a/Gemfile.lock b/Gemfile.lock index 5544e433f..0958a8495 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -215,6 +215,8 @@ GEM faraday (0.15.4) multipart-post (>= 1.2, < 3) ffi (1.12.2) + ffi-geos (2.1.0) + ffi (>= 1.0.0) flipper (0.17.2) flipper-active_record (0.17.2) activerecord (>= 4.2, < 7) @@ -242,6 +244,7 @@ GEM fugit (1.3.3) et-orbi (~> 1.1, >= 1.1.8) raabro (~> 1.1) + geo_coord (0.1.0) geocoder (1.6.0) globalid (0.4.2) activesupport (>= 4.2.0) @@ -750,11 +753,13 @@ DEPENDENCIES discard dotenv-rails factory_bot + ffi-geos flipper flipper-active_record flipper-ui font-awesome-rails fugit + geo_coord geocoder gon graphql diff --git a/app/helpers/champ_helper.rb b/app/helpers/champ_helper.rb index b41d81fa2..36416f759 100644 --- a/app/helpers/champ_helper.rb +++ b/app/helpers/champ_helper.rb @@ -49,6 +49,17 @@ module ChampHelper "#{geo_area.commune} : #{geo_area.nom}" when GeoArea.sources.fetch(:parcelle_agricole) "Culture : #{geo_area.culture} - Surface : #{geo_area.surface} ha" + when GeoArea.sources.fetch(:selection_utilisateur) + if geo_area.polygon? + capture do + concat "Une aire de surface #{geo_area.area} m" + concat content_tag(:sup, "2") + end + elsif geo_area.line? + "Une ligne longue de #{geo_area.length} m" + elsif geo_area.point? + "Un point situé à #{geo_area.location}" + end end end end diff --git a/app/models/geo_area.rb b/app/models/geo_area.rb index ce89d7e03..60e59afd9 100644 --- a/app/models/geo_area.rb +++ b/app/models/geo_area.rb @@ -36,7 +36,7 @@ class GeoArea < ApplicationRecord { type: 'Feature', geometry: geometry, - properties: properties.merge(source: source) + properties: properties.merge(source: source, area: area, length: length).compact } end @@ -53,4 +53,34 @@ class GeoArea < ApplicationRecord ) end end + + def area + if polygon? + rgeo_geometry.area.round(1) + end + end + + def length + if line? + rgeo_geometry.length.round(1) + end + end + + def location + if point? + Geo::Coord.new(*rgeo_geometry.coordinates).to_s + end + end + + def line? + geometry['type'] == 'LineString' + end + + def polygon? + geometry['type'] == 'Polygon' + end + + def point? + geometry['type'] == 'Point' + end end diff --git a/app/views/shared/champs/carte/_geo_areas.html.haml b/app/views/shared/champs/carte/_geo_areas.html.haml index 6f3565e3d..6f5f24b13 100644 --- a/app/views/shared/champs/carte/_geo_areas.html.haml +++ b/app/views/shared/champs/carte/_geo_areas.html.haml @@ -1,3 +1,10 @@ +- if champ.selections_utilisateur.present? + .areas-title Sélections utilisateur + .areas + %ul + - champ.selections_utilisateur.each do |geo_area| + %li= geo_area_label(geo_area) + - if champ.quartiers_prioritaires? .areas-title Quartiers prioritaires .areas diff --git a/spec/models/geo_area_spec.rb b/spec/models/geo_area_spec.rb new file mode 100644 index 000000000..08ede4acd --- /dev/null +++ b/spec/models/geo_area_spec.rb @@ -0,0 +1,50 @@ +RSpec.describe GeoArea, type: :model do + describe '#area' do + let(:geo_area) do + create(:geo_area, geometry: { + "type": "Polygon", + "coordinates": [ + [ + [2.428439855575562, 46.538476837725796], + [2.4284291267395024, 46.53842148758162], + [2.4282521009445195, 46.53841410755813], + [2.42824137210846, 46.53847314771794], + [2.428284287452698, 46.53847314771794], + [2.428364753723145, 46.538487907747864], + [2.4284291267395024, 46.538491597754714], + [2.428439855575562, 46.538476837725796] + ] + ] + }) + end + + it { expect(geo_area.area).to eq(219.0) } + end + + describe '#length' do + let(:geo_area) do + create(:geo_area, geometry: { + "type": "LineString", + "coordinates": [ + [2.4282521009445195, 46.53841410755813], + [2.42824137210846, 46.53847314771794], + [2.428284287452698, 46.53847314771794], + [2.4284291267395024, 46.538491597754714] + ] + }) + end + + it { expect(geo_area.length).to eq(30.8) } + end + + describe '#location' do + let(:geo_area) do + create(:geo_area, geometry: { + "type": "Point", + "coordinates": [2.428439855575562, 46.538476837725796] + }) + end + + it { expect(geo_area.location).to eq("2°25'42\"N 46°32'19\"E") } + end +end