Mercurial > gemma
annotate pkg/models/cross.go @ 636:7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 13 Sep 2018 04:11:36 +0200 |
parents | 6093016fac88 |
children | 756f3fc62da6 |
rev | line source |
---|---|
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
1 package models |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
2 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
3 import ( |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
4 "encoding/json" |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
5 "errors" |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
6 "time" |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
7 ) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
8 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
9 type ( |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
10 GeoJSONFeature string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
11 GeoJSONLineStringType string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
12 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
13 GeoJSONDate struct{ time.Time } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
14 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 GeoJSONLineString struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 Type GeoJSONLineStringType `json:"type"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
17 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
18 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 GeoJSONCoordinate struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 Lon float64 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
21 Lat float64 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
22 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
23 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
24 CrossSectionInputProperties struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
25 Bottleneck string `json:"bottleneck"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
26 Date GeoJSONDate `json:"date"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
27 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
28 |
636
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
29 GeoJSONLineCoordinates []GeoJSONCoordinate |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
30 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 CrossSectionInput struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
32 Type GeoJSONFeature `json:"type"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
33 Geometry GeoJSONLineString `json:"geometry"` |
636
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
34 Coordinates GeoJSONLineCoordinates `json:"coordinates"` |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
35 Properties CrossSectionInputProperties `json:"properties"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
36 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
37 ) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
38 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
39 var ( |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
40 errNoGeoJSONFeature = errors.New("Not a valid GeoJSON feature") |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
41 errNoGeoJSONLineStringType = errors.New("Not a valid GeoJSON linestring type") |
636
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
42 errTooLessComponents = errors.New("Too less components in coordinate") |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
43 errTooLessCoordinates = errors.New("Too less coordinates") |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
44 ) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
45 |
636
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
46 func (lc *GeoJSONLineCoordinates) UnmarshalJSON(data []byte) error { |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
47 var coords []GeoJSONCoordinate |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
48 if err := json.Unmarshal(data, &coords); err != nil { |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
49 return err |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
50 } |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
51 if len(coords) < 2 { |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
52 return errTooLessCoordinates |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
53 } |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
54 *lc = GeoJSONLineCoordinates(coords) |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
55 return nil |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
56 } |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
57 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
58 func (d *GeoJSONDate) UnmarshalJSON(data []byte) error { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
59 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
60 if err := json.Unmarshal(data, &s); err != nil { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
61 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
62 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
63 t, err := time.Parse("2006-01-02", s) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
64 if err != nil { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
65 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
66 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
67 *d = GeoJSONDate{t} |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
68 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
69 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
70 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
71 func (c *GeoJSONCoordinate) UnmarshalJSON(data []byte) error { |
636
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
72 var pos []float64 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
73 if err := json.Unmarshal(data, &pos); err != nil { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
74 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
75 } |
636
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
76 if len(pos) < 2 { |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
77 return errTooLessComponents |
7a867e48b3c2
Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
633
diff
changeset
|
78 } |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
79 *c = GeoJSONCoordinate{Lon: pos[0], Lat: pos[1]} |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
80 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
81 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
82 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
83 func (t *GeoJSONFeature) UnmarshalJSON(data []byte) error { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
84 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
85 if err := json.Unmarshal(data, &s); err != nil { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
86 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
87 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
88 if s != "Feature" { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
89 return errNoGeoJSONFeature |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
90 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
91 *t = GeoJSONFeature(s) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
92 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
93 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
94 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
95 func (t *GeoJSONLineStringType) UnmarshalJSON(data []byte) error { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
96 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
97 if err := json.Unmarshal(data, &s); err != nil { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
98 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
99 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
100 if s != "LineString" { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
101 return errNoGeoJSONLineStringType |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
102 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
103 *t = GeoJSONLineStringType(s) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
104 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
105 } |