comparison pkg/octree/tin.go @ 971:f9fb6c399f3f

Moved generating of tins to octree package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 18 Oct 2018 11:52:13 +0200
parents a4fe07a21ba7
children a244b18cb916
comparison
equal deleted inserted replaced
970:166782ebab96 971:f9fb6c399f3f
1 package octree 1 package octree
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "context"
6 "database/sql"
5 "encoding/binary" 7 "encoding/binary"
6 "errors" 8 "errors"
7 "fmt" 9 "fmt"
8 "io" 10 "io"
9 "log" 11 "log"
10 "math" 12 "math"
13 "time"
11 ) 14 )
12 15
13 var ( 16 var (
14 errNoByteSlice = errors.New("Not a byte slice") 17 errNoByteSlice = errors.New("Not a byte slice")
15 errTooLessPoints = errors.New("Too less points") 18 errTooLessPoints = errors.New("Too less points")
163 } 166 }
164 167
165 return nil 168 return nil
166 } 169 }
167 170
171 const (
172 tinSQLPrefix = `WITH trans AS (
173 SELECT
174 ST_Buffer(ST_Transform(area::geometry, $1::int), 0.001) AS area,
175 ST_Transform(point_cloud::geometry, $1::int) AS point_cloud
176 FROM waterway.sounding_results
177 `
178 tinSQLSuffix = `
179 ),
180 triangles AS (
181 SELECT t.geom AS geom, ST_MakePolygon(ST_ExteriorRing(t.geom)) AS poly FROM (
182 SELECT (ST_Dump(
183 ST_DelaunayTriangles(point_cloud, 0, 2))).geom
184 FROM trans) t
185 )
186 SELECT ST_AsBinary(ST_Collect(triangles.geom)) FROM triangles, trans
187 WHERE ST_Covers(trans.area, triangles.poly)`
188
189 loadTinSQL = tinSQLPrefix + `WHERE bottleneck_id = $2 AND date_info = $3` + tinSQLSuffix
190 loadTinByIDSQL = tinSQLPrefix + `WHERE id = $2` + tinSQLSuffix
191 )
192
193 func GenerateTin(
194 conn *sql.Conn,
195 ctx context.Context,
196 bottleneck string,
197 date time.Time,
198 epsg uint32,
199 ) (*Tin, error) {
200 var tin Tin
201 err := conn.QueryRowContext(ctx, loadTinSQL, epsg, bottleneck, date).Scan(&tin)
202 switch {
203 case err == sql.ErrNoRows:
204 return nil, nil
205 case err != nil:
206 return nil, err
207 }
208 tin.EPSG = epsg
209 return &tin, nil
210 }
211
212 func GenerateTinByID(
213 conn *sql.Conn,
214 ctx context.Context,
215 id int64,
216 epsg uint32,
217 ) (*Tin, error) {
218 var tin Tin
219 err := conn.QueryRowContext(ctx, loadTinByIDSQL, epsg, id).Scan(&tin)
220 switch {
221 case err == sql.ErrNoRows:
222 return nil, nil
223 case err != nil:
224 return nil, err
225 }
226 tin.EPSG = epsg
227 return &tin, nil
228 }
229
168 func (t *Tin) Scan(raw interface{}) error { 230 func (t *Tin) Scan(raw interface{}) error {
169 231 if raw == nil {
232 return nil
233 }
170 data, ok := raw.([]byte) 234 data, ok := raw.([]byte)
171 if !ok { 235 if !ok {
172 return errNoByteSlice 236 return errNoByteSlice
173 } 237 }
174 return t.FromWKB(data) 238 return t.FromWKB(data)