diff pkg/imports/sr.go @ 4563:a4042ab02e05 iso-areas

Store geometries in database.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 03 Oct 2019 10:05:01 +0200
parents 5cc4042cf07c
children 6b107d4e6810
line wrap: on
line diff
--- a/pkg/imports/sr.go	Wed Oct 02 18:35:09 2019 +0200
+++ b/pkg/imports/sr.go	Thu Oct 03 10:05:01 2019 +0200
@@ -100,7 +100,10 @@
 
 func (srJobCreator) Depends() [2][]string {
 	return [2][]string{
-		{"sounding_results", "sounding_results_contour_lines"},
+		{"sounding_results",
+			"sounding_results_contour_lines",
+			"sounding_results_iso_areas",
+		},
 		{"bottlenecks"},
 	}
 }
@@ -206,7 +209,7 @@
             ST_MakeValid(ST_GeomFromWKB($4, $3::integer)), 2)),
           $5
         ),
-        2
+        3
       )
     ),
     4326
@@ -990,6 +993,7 @@
 
 	areas := make([]wkb.MultiPolygonGeom, len(heights))
 
+	// TODO: Check if this correct!
 	reprojX := common.Linear(1, min.X, float64(xcells+1), max.X)
 	reprojY := common.Linear(1, min.Y, float64(ycells+1), max.Y)
 
@@ -999,17 +1003,18 @@
 		defer wg.Done()
 		for hIdx := range cnts {
 			c := tracer.Contours(heights[hIdx])
+
+			// We need to bring it back to the
+			// none raster coordinate system.
 			a := make(wkb.MultiPolygonGeom, len(c))
-
 			for i, pl := range c {
-
 				shell := make(wkb.LinearRingGeom, len(pl))
 				for j, pt := range pl {
-					x := reprojX(pt.X)
-					y := reprojY(pt.Y)
-					shell[j] = wkb.PointGeom{X: x, Y: y}
+					shell[j] = wkb.PointGeom{
+						X: reprojX(pt.X),
+						Y: reprojY(pt.Y),
+					}
 				}
-
 				a[i] = wkb.PolygonGeom{shell}
 			}
 			areas[hIdx] = a
@@ -1033,7 +1038,9 @@
 	// Raster and tracer are not needed any more.
 	raster, tracer = nil, nil
 
-	return storeAreas(ctx, tx, feedback, areas, heights)
+	return storeAreas(
+		ctx, tx, feedback,
+		areas, tree.EPSG, heights, id)
 }
 
 func storeAreas(
@@ -1041,7 +1048,9 @@
 	tx *sql.Tx,
 	feedback Feedback,
 	areas []wkb.MultiPolygonGeom,
+	epsg uint32,
 	heights []float64,
+	id int64,
 ) error {
 	feedback.Info("Storing iso areas")
 	total := time.Now()
@@ -1056,7 +1065,19 @@
 	}
 	defer stmt.Close()
 
-	// TODO: Implement me!
+	for i, a := range areas {
+		if len(a) == 0 {
+			continue
+		}
+		if _, err := stmt.ExecContext(
+			ctx,
+			id, heights[i], epsg,
+			a.AsWKB(),
+			contourTolerance,
+		); err != nil {
+			return err
+		}
+	}
 
 	return nil
 }