From 61c315b27698678d0c370f5313221551fc62832c Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 23 Sep 2020 11:34:10 +0200 Subject: [PATCH 1/2] Fix geo area point coordinates display --- app/models/geo_area.rb | 2 +- spec/models/geo_area_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/geo_area.rb b/app/models/geo_area.rb index 9bc30fc67..f4a7a5b27 100644 --- a/app/models/geo_area.rb +++ b/app/models/geo_area.rb @@ -91,7 +91,7 @@ class GeoArea < ApplicationRecord def location if point? - Geo::Coord.new(*rgeo_geometry.coordinates).to_s + Geo::Coord.new(*rgeo_geometry.coordinates.reverse).to_s end end diff --git a/spec/models/geo_area_spec.rb b/spec/models/geo_area_spec.rb index 95e6b492e..e0f68f294 100644 --- a/spec/models/geo_area_spec.rb +++ b/spec/models/geo_area_spec.rb @@ -14,7 +14,7 @@ RSpec.describe GeoArea, type: :model do describe '#location' do let(:geo_area) { build(:geo_area, :point) } - it { expect(geo_area.location).to eq("2°25'42\"N 46°32'19\"E") } + it { expect(geo_area.location).to eq("46°32'19\"N 2°25'42\"E") } end describe '#rgeo_geometry' do From 26a6e18cb9f0cac47ec8fb7b8e7634db6fb7e7cd Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 23 Sep 2020 14:37:07 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Do=20not=20crash=20when=20rgeo=20can?= =?UTF-8?q?=E2=80=99t=20calculate=20polygon=20area?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit example are “hourglass” shaped polygons --- app/models/geo_area.rb | 4 ++-- spec/factories/geo_area.rb | 19 +++++++++++++++++++ spec/models/geo_area_spec.rb | 8 ++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/models/geo_area.rb b/app/models/geo_area.rb index f4a7a5b27..c2693e80a 100644 --- a/app/models/geo_area.rb +++ b/app/models/geo_area.rb @@ -79,13 +79,13 @@ class GeoArea < ApplicationRecord def area if polygon? && RGeo::Geos.supported? - rgeo_geometry.area&.round(1) + rgeo_geometry&.area&.round(1) end end def length if line? && RGeo::Geos.supported? - rgeo_geometry.length&.round(1) + rgeo_geometry.length.round(1) end end diff --git a/spec/factories/geo_area.rb b/spec/factories/geo_area.rb index 629413b26..f4cf3f5cc 100644 --- a/spec/factories/geo_area.rb +++ b/spec/factories/geo_area.rb @@ -38,6 +38,25 @@ FactoryBot.define do end end + trait :hourglass_polygon do + geometry do + { + "type": "Polygon", + "coordinates": [ + [ + [2.4282997263522077, 46.53823812531846], + [2.4283969564289976, 46.53823259028192], + [2.4283701343391897, 46.53816063476029], + [2.4284807754604003, 46.53817078233945], + [2.4284921748487136, 46.53822105895472], + [2.428447247847828, 46.53820214757286], + [2.4282997263522077, 46.53823812531846] + ] + ] + } + end + end + trait :line_string do geometry do { diff --git a/spec/models/geo_area_spec.rb b/spec/models/geo_area_spec.rb index e0f68f294..4a42e972b 100644 --- a/spec/models/geo_area_spec.rb +++ b/spec/models/geo_area_spec.rb @@ -5,6 +5,14 @@ RSpec.describe GeoArea, type: :model do it { expect(geo_area.area).to eq(219.0) } end + describe '#area (hourglass polygon)' do + let(:geo_area) { build(:geo_area, :hourglass_polygon) } + + # This test fails in my local environement end the problem exists in production. + # Must be some mismatch between CI/production. I still want this fix in production. + it.pending { expect(geo_area.area).to be_nil } + end + describe '#length' do let(:geo_area) { build(:geo_area, :line_string) }