diff pkg/imports/sr.go @ 3591:062dc9b54b86

SR import: Using context.Context aware SQL statements.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 04 Jun 2019 13:19:28 +0200
parents a9d140c7db8d
children eeeb7bf14217
line wrap: on
line diff
--- a/pkg/imports/sr.go	Tue Jun 04 12:44:22 2019 +0200
+++ b/pkg/imports/sr.go	Tue Jun 04 13:19:28 2019 +0200
@@ -174,6 +174,14 @@
       AND grwl.validity = bns.gauge_validity
 WHERE bns.objnam = $1 AND grwl.depth_reference like 'LDC%'
 `
+
+	reprojectPointsSingleBeamSQL = `
+SELECT ST_AsBinary(
+  ST_Transform(
+    ST_GeomFromWKB($1, $2::integer),
+	best_utm(ST_GeomFromWKB($1, $2::integer)))),
+	best_utm(ST_GeomFromWKB($1, $2::integer)
+`
 )
 
 func (sr *SoundingResult) isSingleBeam() bool {
@@ -257,7 +265,7 @@
 	}
 
 	// Is there a boundary shapefile in the ZIP archive?
-	polygon, err := loadBoundary(z)
+	boundary, err := loadBoundary(z)
 	if err != nil {
 		return nil, err
 	}
@@ -278,7 +286,7 @@
 			importID,
 			m,
 			xyz,
-			polygon,
+			boundary,
 		)
 	} else {
 		summary, err = sr.multiBeamScan(
@@ -288,7 +296,7 @@
 			importID,
 			m,
 			xyz,
-			polygon,
+			boundary,
 		)
 	}
 	if err != nil {
@@ -314,8 +322,29 @@
 	importID int64,
 	m *models.SoundingResultMeta,
 	xyz octree.MultiPointZ,
-	polygon polygonSlice,
+	boundary polygonSlice,
 ) (interface{}, error) {
+
+	feedback.Info("Processing as single beam scan.")
+
+	start := time.Now()
+
+	xyzWKB := xyz.AsWKB()
+	var reproj []byte
+	var epsg uint
+
+	if err := tx.QueryRowContext(
+		ctx,
+		reprojectPointsSingleBeamSQL,
+		xyzWKB,
+		m.EPSG,
+	).Scan(&reproj, &epsg); err != nil {
+		return nil, err
+	}
+
+	feedback.Info("Reprojecting points to EPSG %d took %v.",
+		epsg, time.Since(start))
+
 	// TODO: Implement me!
 	return nil, errors.New("Not implemented, yet!")
 }
@@ -327,8 +356,9 @@
 	importID int64,
 	m *models.SoundingResultMeta,
 	xyz octree.MultiPointZ,
-	polygon polygonSlice,
+	boundary polygonSlice,
 ) (interface{}, error) {
+
 	feedback.Info("Processing as multi beam scan.")
 	var (
 		id       int64
@@ -341,12 +371,14 @@
 
 	xyzWKB := xyz.AsWKB()
 
-	err := tx.QueryRow(insertHullSQL,
+	err := tx.QueryRowContext(
+		ctx,
+		insertHullSQL,
 		m.Bottleneck,
 		m.Date.Time,
 		m.DepthReference,
 		xyzWKB,
-		polygon.asWKB(),
+		boundary.asWKB(),
 		m.EPSG,
 	).Scan(
 		&id,
@@ -355,7 +387,7 @@
 		&epsg,
 		&hull,
 	)
-	xyz, polygon = nil, nil // not need from now on.
+	xyz, boundary = nil, nil // not need from now on.
 	feedback.Info("Calculating hull took %s.", time.Since(start))
 	if err != nil {
 		return nil, err
@@ -375,7 +407,9 @@
 
 	var reproj []byte
 
-	if err = tx.QueryRow(reprojectPointsSQL,
+	if err = tx.QueryRowContext(
+		ctx,
+		reprojectPointsSQL,
 		xyzWKB,
 		m.EPSG,
 		epsg,
@@ -428,7 +462,7 @@
 	h := sha1.New()
 	h.Write(octreeIndex)
 	checksum := hex.EncodeToString(h.Sum(nil))
-	_, err = tx.Exec(insertOctreeSQL, id, checksum, octreeIndex)
+	_, err = tx.ExecContext(ctx, insertOctreeSQL, id, checksum, octreeIndex)
 	if err != nil {
 		return nil, err
 	}
@@ -437,7 +471,7 @@
 	tree := builder.Tree()
 
 	start = time.Now()
-	err = generateContours(tree, tx, id)
+	err = generateContours(ctx, tx, tree, id)
 	if err != nil {
 		return nil, err
 	}
@@ -628,8 +662,13 @@
 	return shapeToPolygon(s)
 }
 
-func generateContours(tree *octree.Tree, tx *sql.Tx, id int64) error {
-	stmt, err := tx.Prepare(insertContourSQL)
+func generateContours(
+	ctx context.Context,
+	tx *sql.Tx,
+	tree *octree.Tree,
+	id int64,
+) error {
+	stmt, err := tx.PrepareContext(ctx, insertContourSQL)
 	if err != nil {
 		return err
 	}
@@ -644,7 +683,8 @@
 
 	octree.DoContours(tree, heights, func(res *octree.ContourResult) {
 		if err == nil && len(res.Lines) > 0 {
-			_, err = stmt.Exec(
+			_, err = stmt.ExecContext(
+				ctx,
 				id, res.Height, tree.EPSG,
 				res.Lines.AsWKB2D(),
 				contourTolerance)