changeset 963:e0825645f00e

merge
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 17 Oct 2018 15:22:38 +0200
parents d7f34791b18d (current diff) a4c92e0ef2e1 (diff)
children 1e2dce348cfb
files
diffstat 1 files changed, 70 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/sr.go	Wed Oct 17 15:22:21 2018 +0200
+++ b/pkg/imports/sr.go	Wed Oct 17 15:22:38 2018 +0200
@@ -28,14 +28,47 @@
 
 const SoundingResultDateFormat = "2006-01-02"
 
-type SoundingResultDate struct{ time.Time }
+type (
+	SoundingResultDate struct{ time.Time }
+
+	SoundingResultMeta struct {
+		Date           SoundingResultDate `json:"date"`
+		Bottleneck     string             `json:"bottleneck"`
+		EPSG           uint               `json:"epsg"`
+		DepthReference string             `json:"depth-reference"`
+	}
+
+	Point struct {
+		X float64
+		Y float64
+	}
+	LineString []Point
+	Polygon    []LineString
+)
 
-type SoundingResultMeta struct {
-	Date           SoundingResultDate `json:"date"`
-	Bottleneck     string             `json:"bottleneck"`
-	EPSG           uint               `json:"epsg"`
-	DepthReference string             `json:"depth-reference"`
-}
+const (
+	insertPointsSQL = `
+INSERT INTO waterway.sounding_results (
+  bottleneck_id,
+  date_info,
+  depth_reference,
+  point_cloud,
+  area
+) VALUES (
+  (SELECT bottleneck_id from waterway.bottlenecks where objnam = $1),
+  $2::date,
+  $3,
+  ST_Transform(ST_GeomFromWKB($4, $6::integer), 4326)::geography,
+  (SELECT CASE $5 IS NULL THEN
+    ST_Transform(
+      ST_ConcaveHull(
+          ST_Force2D(ST_GeomFromWKB($4, $6::integer)), 0.7), 4326)::geography
+  ELSE
+    ST_Transform(ST_GeomFromWKB($5, $6::integer), 4326)::geography
+  END)
+)
+RETURNING id`
+)
 
 func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error {
 	var s string
@@ -94,7 +127,7 @@
 	}
 
 	err = conn.QueryRowContext(context.Background(),
-		`SELECT true FROM waterway.bottlenecks WHERE bottleneck_id = $1`,
+		`SELECT true FROM waterway.bottlenecks WHERE objnam = $1`,
 		m.Bottleneck).Scan(&b)
 	switch {
 	case err == sql.ErrNoRows:
@@ -159,15 +192,6 @@
 	return loadXYZReader(r)
 }
 
-type (
-	Point struct {
-		X float64
-		Y float64
-	}
-	LineString []Point
-	Polygon    []LineString
-)
-
 func toPolygon(numParts int32, parts []int32, points []shp.Point) Polygon {
 	out := make(Polygon, numParts)
 	pos := 0
@@ -269,9 +293,36 @@
 		return err
 	}
 
-	_ = polygon
+	tx, err := conn.BeginTx(context.Background(), nil)
+	if err != nil {
+		return err
+	}
+	defer tx.Rollback()
+
+	var id int64
+
+	err = tx.QueryRow(insertPointsSQL,
+		m.Bottleneck,
+		m.Date.Time,
+		m.DepthReference,
+		xyz.AsWKB(),
+		polygon.AsWBK(),
+		m.EPSG).Scan(&id)
 
-	// TODO: Send to database.
+	if err != nil {
+		return err
+	}
+
+	// TODO: Build octree
+	// TODO: Generate iso-lines
 
+	return tx.Commit()
+}
+
+func (p Polygon) AsWBK() []byte {
+	if len(p) == 0 {
+		return nil
+	}
+	// TODO: Implement me!
 	return nil
 }