view pkg/models/cross.go @ 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
children 7a867e48b3c2
line wrap: on
line source

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
}