changeset 4256:4ca884dfc470

Prevent heights duplicates in iso line calculations.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 23 Aug 2019 18:02:01 +0200
parents f1f1395f141b
children 671441357db0
files pkg/common/floats.go pkg/controllers/diff.go pkg/imports/isr.go pkg/imports/sr.go
diffstat 4 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/common/floats.go	Fri Aug 23 18:02:01 2019 +0200
@@ -0,0 +1,31 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2019 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package common
+
+import "sort"
+
+// DedupFloat64s sorts arr and removes duplicates in place.
+// Returns a sub slice of arr only containing the unique elements.
+func DedupFloat64s(arr []float64) []float64 {
+	sort.Float64s(arr)
+	for i := 1; i < len(arr); {
+		if arr[i-1] == arr[i] {
+			copy(arr[i:], arr[i+1:])
+			arr = arr[:len(arr)-1]
+		} else {
+			i++
+		}
+	}
+	return arr
+}
--- a/pkg/controllers/diff.go	Fri Aug 23 16:33:02 2019 +0200
+++ b/pkg/controllers/diff.go	Fri Aug 23 18:02:01 2019 +0200
@@ -289,6 +289,8 @@
 		return
 	}
 
+	heights = common.DedupFloat64s(heights)
+
 	octree.DoContours(tree, heights, func(res *octree.ContourResult) {
 		if err == nil && len(res.Lines) > 0 {
 			_, err = stmt.ExecContext(
--- a/pkg/imports/isr.go	Fri Aug 23 16:33:02 2019 +0200
+++ b/pkg/imports/isr.go	Fri Aug 23 18:02:01 2019 +0200
@@ -18,6 +18,7 @@
 	"database/sql"
 	"time"
 
+	"gemma.intevation.de/gemma/pkg/common"
 	"gemma.intevation.de/gemma/pkg/octree"
 )
 
@@ -175,6 +176,7 @@
 			return err
 		}
 		hs := octree.ExtrapolateClassBreaks(heights, tree.Min.Z, tree.Max.Z)
+		hs = common.DedupFloat64s(hs)
 
 		// Delete the old contour lines.
 		if _, err := tx.ExecContext(ctx, deleteContourLinesSQL, sr); err != nil {
--- a/pkg/imports/sr.go	Fri Aug 23 16:33:02 2019 +0200
+++ b/pkg/imports/sr.go	Fri Aug 23 18:02:01 2019 +0200
@@ -869,6 +869,8 @@
 		log.Printf("%.2f - %.2f\n", tree.Min.Z, tree.Max.Z)
 	*/
 
+	heights = common.DedupFloat64s(heights)
+
 	octree.DoContours(tree, heights, func(res *octree.ContourResult) {
 		if err == nil && len(res.Lines) > 0 {
 			_, err = stmt.ExecContext(