changeset 633:6093016fac88

Cross sections: started with implementation of web service (WIP).
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 12 Sep 2018 19:51:46 +0200
parents f2097d2aa048
children 0c79068c46b7
files pkg/controllers/cross.go pkg/controllers/routes.go pkg/models/cross.go
diffstat 3 files changed, 115 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/controllers/cross.go	Wed Sep 12 19:51:46 2018 +0200
@@ -0,0 +1,22 @@
+package controllers
+
+import (
+	"database/sql"
+	"net/http"
+
+	"gemma.intevation.de/gemma/pkg/models"
+)
+
+func crossSection(
+	input interface{},
+	req *http.Request,
+	db *sql.Conn,
+) (jr JSONResult, err error) {
+
+	// TODO: Implement me!
+	csi := input.(*models.CrossSectionInput)
+
+	_ = csi
+
+	return
+}
--- a/pkg/controllers/routes.go	Wed Sep 12 17:25:26 2018 +0200
+++ b/pkg/controllers/routes.go	Wed Sep 12 19:51:46 2018 +0200
@@ -95,6 +95,13 @@
 		NoConn: true,
 	})).Methods(http.MethodGet)
 
+	// Cross sections
+
+	api.Handle("/cross-section", any(&JSONHandler{
+		Input:  func() interface{} { return new(models.CrossSectionInput) },
+		Handle: crossSection,
+	})).Methods(http.MethodPost)
+
 	// Token handling: Login/Logout.
 	api.HandleFunc("/login", login).
 		Methods(http.MethodPost)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/models/cross.go	Wed Sep 12 19:51:46 2018 +0200
@@ -0,0 +1,86 @@
+package models
+
+import (
+	"encoding/json"
+	"errors"
+	"time"
+)
+
+type (
+	GeoJSONFeature        string
+	GeoJSONLineStringType string
+
+	GeoJSONDate struct{ time.Time }
+
+	GeoJSONLineString struct {
+		Type GeoJSONLineStringType `json:"type"`
+	}
+
+	GeoJSONCoordinate struct {
+		Lon float64
+		Lat float64
+	}
+
+	CrossSectionInputProperties struct {
+		Bottleneck string      `json:"bottleneck"`
+		Date       GeoJSONDate `json:"date"`
+	}
+
+	CrossSectionInput struct {
+		Type        GeoJSONFeature              `json:"type"`
+		Geometry    GeoJSONLineString           `json:"geometry"`
+		Coordinates []GeoJSONCoordinate         `json:"coordinates"`
+		Properties  CrossSectionInputProperties `json:"properties"`
+	}
+)
+
+var (
+	errNoGeoJSONFeature        = errors.New("Not a valid GeoJSON feature")
+	errNoGeoJSONLineStringType = errors.New("Not a valid GeoJSON linestring type")
+)
+
+func (d *GeoJSONDate) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+	t, err := time.Parse("2006-01-02", s)
+	if err != nil {
+		return err
+	}
+	*d = GeoJSONDate{t}
+	return nil
+}
+
+func (c *GeoJSONCoordinate) UnmarshalJSON(data []byte) error {
+	pos := make([]float64, 2)
+	if err := json.Unmarshal(data, &pos); err != nil {
+		return err
+	}
+	*c = GeoJSONCoordinate{Lon: pos[0], Lat: pos[1]}
+	return nil
+}
+
+func (t *GeoJSONFeature) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+	if s != "Feature" {
+		return errNoGeoJSONFeature
+	}
+	*t = GeoJSONFeature(s)
+	return nil
+}
+
+func (t *GeoJSONLineStringType) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+	if s != "LineString" {
+		return errNoGeoJSONLineStringType
+	}
+	*t = GeoJSONLineStringType(s)
+	return nil
+}