Merge pull request #9275 from tchak/validate-geo-json

ETQ Instructeur, je veux que les GeoJSON déposés par les utilisateurs soient valides
This commit is contained in:
Paul Chavard 2023-07-19 08:01:01 +00:00 committed by GitHub
commit 4f4e68719a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 7 deletions

View file

@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14
image: postgis/postgis:14-3.3
env:
POSTGRES_USER: tps_test
POSTGRES_DB: tps_test
@ -58,7 +58,7 @@ jobs:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14
image: postgis/postgis:14-3.3
env:
POSTGRES_USER: tps_test
POSTGRES_DB: tps_test
@ -100,7 +100,7 @@ jobs:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14
image: postgis/postgis:14-3.3
env:
POSTGRES_USER: tps_test
POSTGRES_DB: tps_test

View file

@ -1,7 +1,13 @@
class GeojsonService
def self.valid?(json)
schemer = JSONSchemer.schema(Rails.root.join('app/schemas/geojson.json'))
schemer.valid?(json)
if schemer.valid?(json)
if ActiveRecord::Base.connection.execute("SELECT 1 as one FROM pg_extension WHERE extname = 'postgis';").count.zero?
true
else
ActiveRecord::Base.connection.exec_query('select ST_IsValid(ST_GeomFromGeoJSON($1)) as valid;', 'ValidateGeoJSON', [json.to_json]).first['valid']
end
end
end
def self.to_json_polygon_for_cadastre(coordinates)

View file

@ -103,5 +103,16 @@ module TPS
config.active_record.encryption.primary_key = Rails.application.secrets.active_record_encryption.fetch(:primary_key)
config.active_record.encryption.key_derivation_salt = Rails.application.secrets.active_record_encryption.fetch(:key_derivation_salt)
# Copied from rgeo/activerecord-postgis-adapter
ActiveRecord::SchemaDumper.ignore_tables |= [
'geography_columns',
'geometry_columns',
'layer',
'raster_columns',
'raster_overviews',
'spatial_ref_sys',
'topology'
]
end
end

View file

@ -207,3 +207,6 @@ COJO_JWT_RSA_PRIVATE_KEY=""
COJO_JWT_ISS=""
API_COJO_URL=""
# Set to `disabled` if you want to diable postgis
POSTGIS_EXTENSION_DISABLED=""

View file

@ -0,0 +1,7 @@
class EnablePostgis < ActiveRecord::Migration[7.0]
def change
if ENV['POSTGIS_EXTENSION_DISABLED'] != 'disabled' && ActiveRecord::Base.connection.execute("SELECT 1 as one FROM pg_extension WHERE extname = 'postgis';").count.zero?
enable_extension :postgis
end
end
end

View file

@ -10,10 +10,11 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_07_18_113820) do
ActiveRecord::Schema[7.0].define(version: 2023_07_18_113920) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
enable_extension "postgis"
enable_extension "unaccent"
create_table "action_text_rich_texts", force: :cascade do |t|

View file

@ -57,12 +57,12 @@ RSpec.describe GeoArea, type: :model do
it { expect(geo_area.errors).to have_key(:geometry) }
end
context.skip 'invalid_right_hand_rule_polygon' do
context 'invalid_right_hand_rule_polygon' do
let(:geo_area) { build(:geo_area, :invalid_right_hand_rule_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
context.skip 'hourglass_polygon' do
context 'hourglass_polygon' do
let(:geo_area) { build(:geo_area, :hourglass_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end