Mercurial > gemma
annotate pkg/models/cross.go @ 645:794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 13 Sep 2018 12:59:18 +0200 |
parents | 756f3fc62da6 |
children | cf62cb84fa23 |
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 ( |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
4 "bytes" |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
5 "encoding/binary" |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
6 "encoding/json" |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
7 "errors" |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
8 "fmt" |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
9 "math" |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
10 "time" |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
11 ) |
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 type ( |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
14 GeoJSONFeature string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 GeoJSONLineStringType string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
17 GeoJSONDate struct{ time.Time } |
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 GeoJSONLineString struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 Type GeoJSONLineStringType `json:"type"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
21 } |
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 GeoJSONCoordinate struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
24 Lon float64 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
25 Lat float64 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
26 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
27 |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
28 GeoJSONCoordinateZ struct { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
29 Lon float64 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
30 Lat float64 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
31 Z float64 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
32 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
33 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
34 CrossSectionInputProperties struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
35 Bottleneck string `json:"bottleneck"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
36 Date GeoJSONDate `json:"date"` |
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 |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
39 GeoJSONLineCoordinates []GeoJSONCoordinate |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
40 GeoJSONLineCoordinatesZ []GeoJSONCoordinateZ |
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
|
41 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
42 CrossSectionInput struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
43 Type GeoJSONFeature `json:"type"` |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
44 Geometry GeoJSONLineStringType `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
|
45 Coordinates GeoJSONLineCoordinates `json:"coordinates"` |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
46 Properties CrossSectionInputProperties `json:"properties"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
47 } |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
48 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
49 CrossSectionOutput struct { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
50 Type string `json:"type"` |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
51 Geometry string `json:"geometry"` |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
52 Coordinates []GeoJSONLineCoordinatesZ `json:"coordinates"` |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
53 } |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
54 ) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
55 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
56 var ( |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 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
|
61 ) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
62 |
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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 } |
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
|
68 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
|
69 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
|
70 } |
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
|
71 *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
|
72 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
|
73 } |
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
|
74 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
75 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
|
76 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
77 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
|
78 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
79 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
80 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
|
81 if err != nil { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
82 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
83 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
84 *d = GeoJSONDate{t} |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
85 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
86 } |
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 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
|
89 var pos []float64 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
90 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
|
91 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
92 } |
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
|
93 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
|
94 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
|
95 } |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
96 *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
|
97 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
98 } |
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 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
|
101 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
102 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
|
103 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
104 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
105 if s != "Feature" { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
106 return errNoGeoJSONFeature |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
107 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
108 *t = GeoJSONFeature(s) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
109 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
110 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
111 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
112 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
|
113 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
114 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
|
115 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
116 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
117 if s != "LineString" { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
118 return errNoGeoJSONLineStringType |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
119 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
120 *t = GeoJSONLineStringType(s) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
121 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
122 } |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
123 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
124 const ( |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
125 wkbXDR byte = 0 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
126 wkbNDR byte = 1 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
127 wkbPoint uint32 = 1 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
128 wkbPointZ uint32 = 1000 + 1 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
129 wkbLineString uint32 = 2 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
130 wkbLineStringZ uint32 = 1000 + 2 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
131 ) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
132 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
133 func (lc GeoJSONLineCoordinates) AsWKB() []byte { |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
134 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
135 size := 1 + 4 + 4 + len(lc)*(1+4+2*8) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
136 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
137 buf := bytes.NewBuffer(make([]byte, 0, size)) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
138 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
139 binary.Write(buf, binary.LittleEndian, wkbNDR) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
140 binary.Write(buf, binary.LittleEndian, wkbLineString) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
141 binary.Write(buf, binary.LittleEndian, uint32(len(lc))) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
142 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
143 for i := range lc { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
144 c := &lc[i] |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
145 binary.Write(buf, binary.LittleEndian, math.Float64bits(c.Lon)) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
146 binary.Write(buf, binary.LittleEndian, math.Float64bits(c.Lat)) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
147 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
148 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
149 return buf.Bytes() |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
150 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
151 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
152 func (cz GeoJSONCoordinateZ) MarshalJSON() ([]byte, error) { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
153 var buf bytes.Buffer |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
154 fmt.Fprintf(&buf, "[%.8f,%.8f,%.8f]", cz.Lon, cz.Lat, cz.Z) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
155 return buf.Bytes(), nil |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
156 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
157 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
158 func (lcz *GeoJSONLineCoordinatesZ) Scan(src interface{}) error { |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
159 data, ok := src.([]byte) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
160 if !ok { |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
161 return errNoByteSlice |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
162 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
163 return lcz.FromWKB(data) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
164 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
165 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
166 func (lcz *GeoJSONLineCoordinatesZ) FromWKB(data []byte) error { |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
167 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
168 r := bytes.NewReader(data) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
169 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
170 endian, err := r.ReadByte() |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
171 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
172 var order binary.ByteOrder |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
173 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
174 switch { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
175 case err != nil: |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
176 return err |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
177 case endian == wkbNDR: |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
178 order = binary.LittleEndian |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
179 case endian == wkbXDR: |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
180 order = binary.BigEndian |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
181 default: |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
182 return fmt.Errorf("unknown byte order %x", endian) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
183 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
184 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
185 var geomType uint32 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
186 err = binary.Read(r, order, &geomType) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
187 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
188 switch { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
189 case err != nil: |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
190 return err |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
191 case geomType != wkbLineStringZ: |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
192 return fmt.Errorf("unknown geometry type %x", geomType) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
193 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
194 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
195 var num uint32 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
196 if err = binary.Read(r, order, &num); err != nil { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
197 return err |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
198 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
199 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
200 coords := make(GeoJSONLineCoordinatesZ, num) |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
201 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
202 for i := range coords { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
203 c := &coords[i] |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
204 for _, addr := range []*float64{&c.Lat, &c.Lon, &c.Z} { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
205 if err = binary.Read(r, order, addr); err != nil { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
206 return err |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
207 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
208 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
209 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
210 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
211 *lcz = coords |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
212 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
213 return nil |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
214 } |