Mercurial > gemma
annotate pkg/models/cross.go @ 2561:4d5f419a2318
client:pdf-gen: improve box drawing
* handle the case when the rectangle is not rounded (rounding=0)
author | Fadi Abbud <fadi.abbud@intevation.de> |
---|---|
date | Fri, 08 Mar 2019 16:49:40 +0100 |
parents | 63475c8e710e |
children | a347468ea545 |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
895
diff
changeset
|
13 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
14 package models |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
15 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
17 "bytes" |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
18 "encoding/binary" |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
19 "encoding/json" |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
20 "errors" |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
21 "fmt" |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
22 "math" |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
23 "time" |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
24 |
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
25 "gemma.intevation.de/gemma/pkg/wkb" |
633
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 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
28 type ( |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
29 GeoJSONFeature string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
30 GeoJSONLineStringType string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
31 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
32 GeoJSONDate struct{ time.Time } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
33 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
34 GeoJSONLineString struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
35 Type GeoJSONLineStringType `json:"type"` |
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 GeoJSONCoordinate struct { |
653
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
39 Lat float64 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
40 Lon float64 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
41 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
42 |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
43 GeoJSONCoordinateZ struct { |
653
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
44 Lat float64 |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
45 Lon float64 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
46 Z float64 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
47 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
48 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
49 CrossSectionInputProperties struct { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
50 Bottleneck string `json:"bottleneck"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
51 Date GeoJSONDate `json:"date"` |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
52 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
53 |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
54 GeoJSONLineCoordinates []GeoJSONCoordinate |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
55 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
|
56 |
648
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
57 GeoJSONMultiLineCoordinatesZ []GeoJSONLineCoordinatesZ |
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
58 |
653
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
59 CrossSectionInputGeometry struct { |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
60 Type GeoJSONLineStringType `json:"type"` |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
61 Coordinates GeoJSONLineCoordinates `json:"coordinates"` |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
62 } |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
63 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
64 CrossSectionInput struct { |
653
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
65 Type GeoJSONFeature `json:"type"` |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
66 Geometry CrossSectionInputGeometry `json:"geometry"` |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
67 Properties CrossSectionInputProperties `json:"properties"` |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
68 } |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
69 |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
70 CrossSectionOutputGeometry struct { |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
71 Type string `json:"type"` |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
72 Coordinates GeoJSONMultiLineCoordinatesZ `json:"coordinates"` |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
73 } |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
74 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
75 CrossSectionOutput struct { |
653
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
76 Type string `json:"type"` |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
77 Geometry CrossSectionOutputGeometry `json:"geometry"` |
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
78 Properties map[string]interface{} `json:"properties"` |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
79 } |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
80 ) |
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 var ( |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 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
|
87 ) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
88 |
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 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
|
90 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
|
91 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
|
92 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
|
93 } |
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 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
|
95 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
|
96 } |
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
|
97 *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
|
98 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
|
99 } |
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
|
100 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
101 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
|
102 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
103 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
|
104 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
105 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
106 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
|
107 if err != nil { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
108 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
109 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
110 *d = GeoJSONDate{t} |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
111 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
112 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
113 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
114 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
|
115 var pos []float64 |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
116 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
|
117 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
118 } |
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
|
119 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
|
120 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
|
121 } |
653
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
122 *c = GeoJSONCoordinate{Lat: pos[0], Lon: pos[1]} |
633
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
123 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
124 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
125 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
126 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
|
127 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
128 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
|
129 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
130 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
131 if s != "Feature" { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
132 return errNoGeoJSONFeature |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
133 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
134 *t = GeoJSONFeature(s) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
135 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
136 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
137 |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
138 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
|
139 var s string |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
140 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
|
141 return err |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
142 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
143 if s != "LineString" { |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
144 return errNoGeoJSONLineStringType |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
145 } |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
146 *t = GeoJSONLineStringType(s) |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
147 return nil |
6093016fac88
Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff
changeset
|
148 } |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
149 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
150 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
|
151 |
1683
509e8728e846
Fixed pre-calculated size of 2D WKB line strings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1324
diff
changeset
|
152 size := 1 + 4 + 4 + len(lc)*(2*8) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
153 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
154 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
|
155 |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
156 binary.Write(buf, binary.LittleEndian, wkb.NDR) |
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
157 binary.Write(buf, binary.LittleEndian, wkb.LineString) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
158 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
|
159 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
160 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
|
161 c := &lc[i] |
653
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
162 binary.Write(buf, binary.LittleEndian, math.Float64bits(c.Lat)) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
163 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
|
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 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
|
167 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
168 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
169 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
|
170 var buf bytes.Buffer |
653
7aeacd7f150b
Cross sections: Take and produce valid GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
652
diff
changeset
|
171 fmt.Fprintf(&buf, "[%.8f,%.8f,%.8f]", cz.Lat, cz.Lon, cz.Z) |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
172 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
|
173 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
174 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 return errNoByteSlice |
643
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
179 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
180 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
|
181 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
182 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
183 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
|
184 |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
185 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
|
186 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
187 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
|
188 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
189 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
|
190 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
191 switch { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
192 case err != nil: |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
193 return err |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
194 case endian == wkb.NDR: |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
195 order = binary.LittleEndian |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
196 case endian == wkb.XDR: |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
197 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
|
198 default: |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
199 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
|
200 } |
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 var geomType uint32 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
203 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
|
204 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
205 switch { |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
206 case err != nil: |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
207 return err |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
208 case geomType != wkb.LineStringZ: |
645
794592cad75a
Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
643
diff
changeset
|
209 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
|
210 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
211 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
212 var num uint32 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
213 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
|
214 return err |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
215 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
216 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
217 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
|
218 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
219 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
|
220 c := &coords[i] |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
221 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
|
222 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
|
223 return err |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
224 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
225 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
226 } |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
227 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
228 *lcz = coords |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
229 |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
230 return nil |
756f3fc62da6
Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
636
diff
changeset
|
231 } |
648
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
232 |
651
ce18231825a2
Cross sections: Small details on Equals Method of GeoJSONCoordinateZ (forget to commit).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
648
diff
changeset
|
233 func (cz GeoJSONCoordinateZ) Equals(other GeoJSONCoordinateZ) bool { |
648
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
234 const ( |
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
235 xyEps = 1e-7 |
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
236 zEps = 1e-5 |
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
237 ) |
651
ce18231825a2
Cross sections: Small details on Equals Method of GeoJSONCoordinateZ (forget to commit).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
648
diff
changeset
|
238 return math.Abs(cz.Lat-other.Lat) < xyEps && |
648
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
239 math.Abs(cz.Lon-other.Lon) < xyEps && |
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
240 math.Abs(cz.Z-other.Z) < zEps |
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
241 } |
cf62cb84fa23
Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
645
diff
changeset
|
242 |
652
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
243 func deg2rad(d float64) float64 { return d * math.Pi / 180.0 } |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
244 |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
245 func (cz GeoJSONCoordinateZ) Distance(other GeoJSONCoordinateZ) float64 { |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
246 |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
247 const EarthRadius = 6378137.0 |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
248 |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
249 dLat := deg2rad(other.Lat - cz.Lat) |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
250 dLng := math.Abs(deg2rad(other.Lon - cz.Lon)) |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
251 |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
252 if dLng > math.Pi { |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
253 dLng = 2*math.Pi - dLng |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
254 } |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
255 |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
256 x := dLng * math.Cos(deg2rad((cz.Lat+other.Lat)/2.0)) |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
257 return math.Sqrt(dLat*dLat+x*x) * EarthRadius |
f5ecd1d72a6e
Cross section: Replaced naive O(N^2) segment joining with a more clever one.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
651
diff
changeset
|
258 } |
808
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
259 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
260 func (mls *GeoJSONMultiLineCoordinatesZ) FromWKB(data []byte) error { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
261 r := bytes.NewReader(data) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
262 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
263 var order binary.ByteOrder |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
264 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
265 endian, err := r.ReadByte() |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
266 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
267 switch { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
268 case err != nil: |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
269 return err |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
270 case endian == wkb.NDR: |
808
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
271 order = binary.LittleEndian |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
272 case endian == wkb.XDR: |
808
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
273 order = binary.BigEndian |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
274 default: |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
275 return fmt.Errorf("unknown byte order %x", endian) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
276 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
277 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
278 var geomType uint32 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
279 err = binary.Read(r, order, &geomType) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
280 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
281 switch { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
282 case err != nil: |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
283 return err |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
284 case geomType != wkb.MultiLineStringZ: |
808
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
285 return fmt.Errorf("unknown geometry type %d", geomType) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
286 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
287 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
288 var numLines uint32 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
289 if err = binary.Read(r, order, &numLines); err != nil { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
290 return err |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
291 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
292 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
293 lines := make(GeoJSONMultiLineCoordinatesZ, numLines) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
294 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
295 for i := range lines { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
296 endian, err = r.ReadByte() |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
297 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
298 switch { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
299 case err != nil: |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
300 return err |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
301 case endian == wkb.NDR: |
808
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
302 order = binary.LittleEndian |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
303 case endian == wkb.XDR: |
808
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
304 order = binary.BigEndian |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
305 default: |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
306 return fmt.Errorf("unknown byte order %x", endian) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
307 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
308 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
309 err = binary.Read(r, order, &geomType) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
310 switch { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
311 case err != nil: |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
312 return err |
2471
63475c8e710e
Moved WKB constants to own package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1683
diff
changeset
|
313 case geomType != wkb.LineStringZ: |
808
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
314 return fmt.Errorf("unknown geometry type %d", geomType) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
315 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
316 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
317 var numPoints uint32 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
318 if err = binary.Read(r, order, &numPoints); err != nil { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
319 return err |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
320 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
321 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
322 points := make(GeoJSONLineCoordinatesZ, numPoints) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
323 for j := range points { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
324 var lat, lon, z uint64 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
325 if err = binary.Read(r, order, &lat); err != nil { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
326 return err |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
327 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
328 if err = binary.Read(r, order, &lon); err != nil { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
329 return err |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
330 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
331 if err = binary.Read(r, order, &z); err != nil { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
332 return err |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
333 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
334 c := &points[j] |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
335 c.Lat = math.Float64frombits(lat) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
336 c.Lon = math.Float64frombits(lon) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
337 c.Z = math.Float64frombits(z) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
338 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
339 lines[i] = points |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
340 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
341 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
342 *mls = lines |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
343 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
344 return nil |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
345 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
346 |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
347 func (mls *GeoJSONMultiLineCoordinatesZ) Scan(src interface{}) error { |
895
3bb275235c89
Let GeoJSONMultiLineCoordinatesZ be nil when scanned from DB.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
808
diff
changeset
|
348 if src == nil { |
3bb275235c89
Let GeoJSONMultiLineCoordinatesZ be nil when scanned from DB.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
808
diff
changeset
|
349 return nil |
3bb275235c89
Let GeoJSONMultiLineCoordinatesZ be nil when scanned from DB.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
808
diff
changeset
|
350 } |
808
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
351 data, ok := src.([]byte) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
352 if !ok { |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
353 return errNoByteSlice |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
354 } |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
355 return mls.FromWKB(data) |
e10a8a682297
Cross sections: Clip result against area of the sounding result.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
801
diff
changeset
|
356 } |