changeset 4912:bfd8ef836998 fairway-marks-import

Fix handling of attribute dirimp It's defined as a comma separated list of integers in the IENC feature catalogue.
author Tom Gottfried <tom@intevation.de>
date Mon, 10 Feb 2020 18:11:52 +0100
parents bcb8b69e4358
children 8c1a3d5e3962
files pkg/imports/fm_bcnlat.go schema/gemma.sql
diffstat 2 files changed, 37 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/fm_bcnlat.go	Mon Feb 10 18:02:22 2020 +0100
+++ b/pkg/imports/fm_bcnlat.go	Mon Feb 10 18:11:52 2020 +0100
@@ -16,7 +16,7 @@
 import (
 	"context"
 	"database/sql"
-	"strconv"
+	"strings"
 	"time"
 
 	"gemma.intevation.de/gemma/pkg/pgxutils"
@@ -104,11 +104,10 @@
   colpat,
   condtn,
   bcnshp,
-  catlam,
-  dirimp
+  catlam
 )
 SELECT newfm, $3, $4, $5, $6, $7, $8, $9,
-    $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21
+    $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20
   FROM ST_Transform(ST_GeomFromWKB($1, $2::integer), 4326) AS newfm (newfm)
   WHERE pg_has_role('sys_admin', 'MEMBER')
     OR ST_Intersects((select a from a),
@@ -117,12 +116,16 @@
   CAST((geom,
       datsta, datend, persta, perend, objnam, nobjnm, inform, ninfom,
       scamin, picrep, txtdsc, sordat, sorind,
-      0, colour, colpat, condtn, bcnshp, catlam, dirimp
+      0, colour, colpat, condtn, bcnshp, catlam
     ) AS waterway.fairway_marks_bcnlat)
   )
   DO NOTHING
 RETURNING id
 `
+	insertDirimpSQL = `
+INSERT INTO waterway.fairway_marks_bcnlat_dirimps (fm_bcnlat_id, dirimp)
+  VALUES ($1, $2)
+`
 )
 
 // Do executes the actual import.
@@ -161,6 +164,12 @@
 	}
 	defer insertStmt.Close()
 
+	insertDirimpStmt, err := tx.PrepareContext(ctx, insertDirimpSQL)
+	if err != nil {
+		return nil, err
+	}
+	defer insertDirimpStmt.Close()
+
 	savepoint := Savepoint(ctx, tx, "feature")
 
 	var (
@@ -178,13 +187,6 @@
 			catlam = sql.NullInt64{Int64: *f.props.IENCCatlam, Valid: true}
 		}
 
-		var dirimp sql.NullInt64
-		if f.props.Dirimp != nil {
-			if value, err := strconv.ParseInt(*f.props.Dirimp, 10, 64); err == nil {
-				dirimp = sql.NullInt64{Int64: value, Valid: true}
-			}
-		}
-
 		var fmid int64
 		err := savepoint(func() error {
 			err := insertStmt.QueryRowContext(
@@ -209,18 +211,31 @@
 				f.props.Condtn,
 				f.props.Bcnshp,
 				catlam,
-				dirimp,
 			).Scan(&fmid)
 			return err
 		})
 		switch {
 		case err == sql.ErrNoRows:
 			outsideOrDup++
-			// ignore -> filtered by responsibility_areas
+			// ignore -> filtered by responsibility area or a duplicate
+			// TODO: handle eventual changes to dirimp
 		case err != nil:
 			feedback.Error(pgxutils.ReadableError{Err: err}.Error())
 		default:
 			features++
+
+			if f.props.Dirimp != nil && *f.props.Dirimp != "" {
+				dirimps := strings.Split(*f.props.Dirimp, ",")
+				for _, dirimp := range dirimps {
+					if err := savepoint(func() error {
+						_, err := insertDirimpStmt.ExecContext(
+							ctx, fmid, dirimp)
+						return err
+					}); err != nil {
+						feedback.Warn(pgxutils.ReadableError{Err: err}.Error())
+					}
+				}
+			}
 		}
 	}
 
--- a/schema/gemma.sql	Mon Feb 10 18:02:22 2020 +0100
+++ b/schema/gemma.sql	Mon Feb 10 18:11:52 2020 +0100
@@ -867,8 +867,7 @@
         colpat varchar,
         condtn int,
         bcnshp int,
-        catlam int,
-        dirimp smallint REFERENCES dirimps
+        catlam int
     ) INHERITS (fairway_marks)
     -- Prevent identical entries using composite type comparison
     -- (i.e. considering two NULL values in a field equal):
@@ -877,9 +876,15 @@
         ((CAST((geom,
                 datsta, datend, persta, perend, objnam, nobjnm, inform, ninfom,
                 scamin, picrep, txtdsc, sordat, sorind,
-                0, colour, colpat, condtn, bcnshp, catlam, dirimp
+                0, colour, colpat, condtn, bcnshp, catlam
             ) AS fairway_marks_bcnlat)
         ))
+
+    CREATE TABLE fairway_marks_bcnlat_dirimps (
+        fm_bcnlat_id int REFERENCES fairway_marks_bcnlat,
+        dirimp smallint REFERENCES dirimps,
+        PRIMARY KEY (fm_bcnlat_id, dirimp)
+    )
 ;