comparison pkg/imports/sr.go @ 961:a4c92e0ef2e1

Stored point cloud and bounding area of sounding result in database. WIP.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 17 Oct 2018 13:41:49 +0200
parents e23ae2c83427
children 1e2dce348cfb
comparison
equal deleted inserted replaced
960:e23ae2c83427 961:a4c92e0ef2e1
26 zip string 26 zip string
27 } 27 }
28 28
29 const SoundingResultDateFormat = "2006-01-02" 29 const SoundingResultDateFormat = "2006-01-02"
30 30
31 type SoundingResultDate struct{ time.Time } 31 type (
32 32 SoundingResultDate struct{ time.Time }
33 type SoundingResultMeta struct { 33
34 Date SoundingResultDate `json:"date"` 34 SoundingResultMeta struct {
35 Bottleneck string `json:"bottleneck"` 35 Date SoundingResultDate `json:"date"`
36 EPSG uint `json:"epsg"` 36 Bottleneck string `json:"bottleneck"`
37 DepthReference string `json:"depth-reference"` 37 EPSG uint `json:"epsg"`
38 } 38 DepthReference string `json:"depth-reference"`
39 }
40
41 Point struct {
42 X float64
43 Y float64
44 }
45 LineString []Point
46 Polygon []LineString
47 )
48
49 const (
50 insertPointsSQL = `
51 INSERT INTO waterway.sounding_results (
52 bottleneck_id,
53 date_info,
54 depth_reference,
55 point_cloud,
56 area
57 ) VALUES (
58 (SELECT bottleneck_id from waterway.bottlenecks where objnam = $1),
59 $2::date,
60 $3,
61 ST_Transform(ST_GeomFromWKB($4, $6::integer), 4326)::geography,
62 (SELECT CASE $5 IS NULL THEN
63 ST_Transform(
64 ST_ConcaveHull(
65 ST_Force2D(ST_GeomFromWKB($4, $6::integer)), 0.7), 4326)::geography
66 ELSE
67 ST_Transform(ST_GeomFromWKB($5, $6::integer), 4326)::geography
68 END)
69 )
70 RETURNING id`
71 )
39 72
40 func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error { 73 func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error {
41 var s string 74 var s string
42 if err := json.Unmarshal(data, &s); err != nil { 75 if err := json.Unmarshal(data, &s); err != nil {
43 return err 76 return err
92 case !b: 125 case !b:
93 return errors.New("Unexpected depth reference") 126 return errors.New("Unexpected depth reference")
94 } 127 }
95 128
96 err = conn.QueryRowContext(context.Background(), 129 err = conn.QueryRowContext(context.Background(),
97 `SELECT true FROM waterway.bottlenecks WHERE bottleneck_id = $1`, 130 `SELECT true FROM waterway.bottlenecks WHERE objnam = $1`,
98 m.Bottleneck).Scan(&b) 131 m.Bottleneck).Scan(&b)
99 switch { 132 switch {
100 case err == sql.ErrNoRows: 133 case err == sql.ErrNoRows:
101 return fmt.Errorf("Unknown bottleneck '%s'\n", m.Bottleneck) 134 return fmt.Errorf("Unknown bottleneck '%s'\n", m.Bottleneck)
102 case err != nil: 135 case err != nil:
156 return nil, err 189 return nil, err
157 } 190 }
158 defer r.Close() 191 defer r.Close()
159 return loadXYZReader(r) 192 return loadXYZReader(r)
160 } 193 }
161
162 type (
163 Point struct {
164 X float64
165 Y float64
166 }
167 LineString []Point
168 Polygon []LineString
169 )
170 194
171 func toPolygon(numParts int32, parts []int32, points []shp.Point) Polygon { 195 func toPolygon(numParts int32, parts []int32, points []shp.Point) Polygon {
172 out := make(Polygon, numParts) 196 out := make(Polygon, numParts)
173 pos := 0 197 pos := 0
174 for i := range out { 198 for i := range out {
267 polygon, err := loadBoundary(z) 291 polygon, err := loadBoundary(z)
268 if err != nil { 292 if err != nil {
269 return err 293 return err
270 } 294 }
271 295
272 _ = polygon 296 tx, err := conn.BeginTx(context.Background(), nil)
273 297 if err != nil {
274 // TODO: Send to database. 298 return err
275 299 }
300 defer tx.Rollback()
301
302 var id int64
303
304 err = tx.QueryRow(insertPointsSQL,
305 m.Bottleneck,
306 m.Date.Time,
307 m.DepthReference,
308 xyz.AsWKB(),
309 polygon.AsWBK(),
310 m.EPSG).Scan(&id)
311
312 if err != nil {
313 return err
314 }
315
316 // TODO: Build octree
317 // TODO: Generate iso-lines
318
319 return tx.Commit()
320 }
321
322 func (p Polygon) AsWBK() []byte {
323 if len(p) == 0 {
324 return nil
325 }
326 // TODO: Implement me!
276 return nil 327 return nil
277 } 328 }