comparison 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
comparison
equal deleted inserted replaced
632:f2097d2aa048 633:6093016fac88
1 package models
2
3 import (
4 "encoding/json"
5 "errors"
6 "time"
7 )
8
9 type (
10 GeoJSONFeature string
11 GeoJSONLineStringType string
12
13 GeoJSONDate struct{ time.Time }
14
15 GeoJSONLineString struct {
16 Type GeoJSONLineStringType `json:"type"`
17 }
18
19 GeoJSONCoordinate struct {
20 Lon float64
21 Lat float64
22 }
23
24 CrossSectionInputProperties struct {
25 Bottleneck string `json:"bottleneck"`
26 Date GeoJSONDate `json:"date"`
27 }
28
29 CrossSectionInput struct {
30 Type GeoJSONFeature `json:"type"`
31 Geometry GeoJSONLineString `json:"geometry"`
32 Coordinates []GeoJSONCoordinate `json:"coordinates"`
33 Properties CrossSectionInputProperties `json:"properties"`
34 }
35 )
36
37 var (
38 errNoGeoJSONFeature = errors.New("Not a valid GeoJSON feature")
39 errNoGeoJSONLineStringType = errors.New("Not a valid GeoJSON linestring type")
40 )
41
42 func (d *GeoJSONDate) UnmarshalJSON(data []byte) error {
43 var s string
44 if err := json.Unmarshal(data, &s); err != nil {
45 return err
46 }
47 t, err := time.Parse("2006-01-02", s)
48 if err != nil {
49 return err
50 }
51 *d = GeoJSONDate{t}
52 return nil
53 }
54
55 func (c *GeoJSONCoordinate) UnmarshalJSON(data []byte) error {
56 pos := make([]float64, 2)
57 if err := json.Unmarshal(data, &pos); err != nil {
58 return err
59 }
60 *c = GeoJSONCoordinate{Lon: pos[0], Lat: pos[1]}
61 return nil
62 }
63
64 func (t *GeoJSONFeature) UnmarshalJSON(data []byte) error {
65 var s string
66 if err := json.Unmarshal(data, &s); err != nil {
67 return err
68 }
69 if s != "Feature" {
70 return errNoGeoJSONFeature
71 }
72 *t = GeoJSONFeature(s)
73 return nil
74 }
75
76 func (t *GeoJSONLineStringType) UnmarshalJSON(data []byte) error {
77 var s string
78 if err := json.Unmarshal(data, &s); err != nil {
79 return err
80 }
81 if s != "LineString" {
82 return errNoGeoJSONLineStringType
83 }
84 *t = GeoJSONLineStringType(s)
85 return nil
86 }