view pkg/controllers/cross.go @ 648:cf62cb84fa23

Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 13 Sep 2018 16:54:52 +0200
parents 794592cad75a
children 7aeacd7f150b
line wrap: on
line source

package controllers

import (
	"database/sql"
	"net/http"

	"gemma.intevation.de/gemma/pkg/models"
)

// TODO: This is hardcoded on SLT's model of a concrete sounding result.
const crossSQL = `
SELECT ST_AsBinary((ST_Dump(ST_Multi(ST_3DIntersection(
  ST_Translate(
    ST_Extrude(
      ST_GeomFromWKB($1, 4326),
    0, 0, 100),
   0, 0, -150),
   geom)))).geom)
FROM b442017
WHERE ST_Intersects(geom, ST_GeomFromWKB($1, 4326))
`

func crossSection(
	input interface{},
	req *http.Request,
	db *sql.Conn,
) (jr JSONResult, err error) {

	csi := input.(*models.CrossSectionInput)

	// TODO: Use these properties in query
	_ = csi.Properties.Bottleneck
	_ = csi.Properties.Date

	var rows *sql.Rows

	rows, err = db.QueryContext(
		req.Context(), crossSQL, csi.Coordinates.AsWKB())
	if err != nil {
		return
	}
	defer rows.Close()

	var segments models.GeoJSONMultiLineCoordinatesZ

	for rows.Next() {
		var segment models.GeoJSONLineCoordinatesZ
		if err = rows.Scan(&segment); err != nil {
			return
		}
		segments = append(segments, segment)
	}

	if err = rows.Err(); err != nil {
		return
	}

	joined := segments.Join()

	jr = JSONResult{
		Result: &models.CrossSectionOutput{
			Type:        "Feature",
			Geometry:    "MultLineString",
			Coordinates: joined,
		},
	}

	return
}