From 5b1346e95c8d9cf07d42a9700df68ff2646699cb Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Sat, 21 Dec 2019 02:41:28 +0100
Subject: Database: add/fix consistency checks

---
 data/sql/commit.sql | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

(limited to 'data/sql/commit.sql')

diff --git a/data/sql/commit.sql b/data/sql/commit.sql
index d7bc7b4a..73e41d64 100644
--- a/data/sql/commit.sql
+++ b/data/sql/commit.sql
@@ -39,6 +39,30 @@ FOR EACH ROW BEGIN
     SELECT RAISE(ABORT, 'Arg! there is now a EPSG:102100 object. Hack in createFromUserInput() will no longer work')
         WHERE EXISTS(SELECT 1 FROM crs_view WHERE auth_name = 'EPSG' AND code = '102100');
 
+    -- check coordinate_operation_view "foreign keys"
+    SELECT RAISE(ABORT, 'One coordinate_operation has a broken source_crs link')
+        WHERE EXISTS (SELECT * FROM coordinate_operation_view cov WHERE
+                      cov.source_crs_auth_name || cov.source_crs_code NOT IN
+                      (SELECT auth_name || code FROM crs_view));
+    SELECT RAISE(ABORT, 'One coordinate_operation has a broken target_crs link')
+        WHERE EXISTS (SELECT * FROM coordinate_operation_view cov WHERE
+                      cov.target_crs_auth_name || cov.target_crs_code NOT IN
+                      (SELECT auth_name || code FROM crs_view));
+
+    -- check that grids with NTv2 method are properly registered
+    SELECT RAISE(ABORT, 'One grid_transformation with NTv2 has not its source_crs in geodetic_crs table with type = ''geographic 2D''')
+        WHERE EXISTS (SELECT * FROM grid_transformation g WHERE
+                      g.method_name = 'NTv2' AND
+                      g.source_crs_auth_name || g.source_crs_code NOT IN
+                      (SELECT auth_name || code FROM geodetic_crs
+                       WHERE type = 'geographic 2D'));
+    SELECT RAISE(ABORT, 'One grid_transformation with NTv2 has not its target_crs in geodetic_crs table with type = ''geographic 2D''')
+        WHERE EXISTS (SELECT * FROM grid_transformation g WHERE
+                      g.method_name = 'NTv2' AND
+                      g.target_crs_auth_name || g.target_crs_code NOT IN
+                      (SELECT auth_name || code FROM geodetic_crs
+                       WHERE type = 'geographic 2D'));
+
     -- check that grids with HEIGHT_TO_GEOGRAPHIC3D method are properly registered
     SELECT RAISE(ABORT, 'One grid_transformation with HEIGHT_TO_GEOGRAPHIC3D has not its source_crs in vertical_crs table')
         WHERE EXISTS (SELECT * FROM grid_transformation g WHERE
@@ -55,12 +79,15 @@ FOR EACH ROW BEGIN
     -- check that grids with Geographic3D to GravityRelatedHeight method are properly registered
     SELECT RAISE(ABORT, 'One grid_transformation with Geographic3D to GravityRelatedHeight has not its target_crs in vertical_crs table')
         WHERE EXISTS (SELECT * FROM grid_transformation g WHERE
-                      g.method_code LIKE 'Geographic3D to GravityRelatedHeight%' AND
+                      g.deprecated = 0 AND
+                      g.method_name LIKE 'Geographic3D to GravityRelatedHeight%' AND
                       g.target_crs_auth_name || g.target_crs_code NOT IN
                       (SELECT auth_name || code FROM vertical_crs));
     SELECT RAISE(ABORT, 'One grid_transformation with Geographic3D to GravityRelatedHeight has not its source_crs in geodetic_crs table with type = ''geographic 3D''')
         WHERE EXISTS (SELECT * FROM grid_transformation g WHERE
-                      g.method_code LIKE 'Geographic3D to GravityRelatedHeight%' AND
+                      g.deprecated = 0 AND
+                      g.method_name LIKE 'Geographic3D to GravityRelatedHeight%' AND
+                      NOT (g.auth_name = 'EPSG' AND g.code IN (7648, 7649, 7650)) AND -- those are wrongly registered as they use a geocentric CRS. Reported to EPSG
                       g.source_crs_auth_name || g.source_crs_code NOT IN
                       (SELECT auth_name || code FROM geodetic_crs
                        WHERE type = 'geographic 3D'));
-- 
cgit v1.2.3

