changeset 2171:543b5d071a69

Fairway dimension imports: Only delete features with the same level of services as the imported.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 Feb 2019 16:52:18 +0100
parents 3bfe48e32f20
children 7e2c77ccc02f
files pkg/imports/fd.go
diffstat 1 files changed, 24 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/fd.go	Fri Feb 08 16:20:26 2019 +0100
+++ b/pkg/imports/fd.go	Fri Feb 08 16:52:18 2019 +0100
@@ -17,7 +17,6 @@
 	"context"
 	"database/sql"
 	"encoding/json"
-	"errors"
 	"fmt"
 	"io"
 	"time"
@@ -95,8 +94,16 @@
 	tx *sql.Tx,
 	id int64,
 ) error {
+	// We only want to delete the features of the same LOS
+	// as the imported.
+	var los int64
+	if err := tx.QueryRowContext(ctx, fdFindLOSSQL, id).Scan(&los); err != nil {
+		// Should always return a row because import will exit as unchanged
+		// if no new features were found while inserting.
+		return err
+	}
 	// Delete the old features.
-	if _, err := tx.ExecContext(ctx, deleteFairwayDimensionSQL); err != nil {
+	if _, err := tx.ExecContext(ctx, deleteFairwayDimensionSQL, los); err != nil {
 		return err
 	}
 
@@ -118,12 +125,20 @@
 }
 
 const (
+	fdFindLOSSQL = `
+SELECT level_of_service FROM waterway.fairway_dimensions
+WHERE id IN (
+  SELECT key FROM import.track_imports
+  WHERE import_id = $1 AND
+        relation = 'waterway.fairway_dimensions'::regclass)
+LIMIT 1`
+
 	fdStageDoneSQL = `
 UPDATE waterway.fairway_dimensions SET staging_done = true
 WHERE id IN (
-  SELECT key from import.track_imports
+  SELECT key FROM import.track_imports
   WHERE import_id = $1 AND
-		relation = 'waterway.fairway_dimensions'::regclass)`
+        relation = 'waterway.fairway_dimensions'::regclass)`
 
 	deleteFairwayDimensionSQL = `
 WITH resp AS (
@@ -135,10 +150,11 @@
 DELETE FROM waterway.fairway_dimensions
 WHERE ST_Covers(
   (SELECT a FROM resp),
-  ST_Transform(area::geometry, (SELECT t FROM resp))) AND staging_done = true
-`
+  ST_Transform(area::geometry, (SELECT t FROM resp)))
+  AND staging_done
+  AND level_of_service = $1::smallint`
 
-	// The ST_MakeValid (line125) and ST_Buffer (line124) are a workarround to
+	// The ST_MakeValid and ST_Buffer below are a workarround to
 	// avoid errors due to reprojection.
 	insertFairwayDimensionSQL = `
 WITH resp AS (
@@ -327,9 +343,7 @@
 	}
 
 	if features == 0 {
-		err := errors.New("No features found")
-		feedback.Error("%v", err)
-		return nil, err
+		return nil, UnchangedError("No features found")
 	}
 
 	if err = tx.Commit(); err == nil {