annotate pkg/octree/contours.go @ 976:c397fdd8c327

Generate the contour lines of the sounding result during the import, too.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 18 Oct 2018 17:05:54 +0200
parents
children 4a2ca0e20006
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
976
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package octree
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import (
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 "database/sql"
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 "runtime"
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 "sync"
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 )
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 const (
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 contourStepWidth = 0.5
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 contourTolerance = 0.1
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 )
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 type contourResult struct {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 height float64
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 lines MultiLineStringZ
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 const (
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 insertContourSQL = `
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 INSERT INTO waterway.sounding_results_contour_lines (
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 sounding_result_id,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 height,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 lines
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 )
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 SELECT
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 $1,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 $2,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 ST_Transform(
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 ST_Multi(
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 ST_CollectionExtract(
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 ST_Intersection(
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 ST_Transform(sr.area::geometry, $3::integer),
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 ST_SimplifyPreserveTopology(
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 ST_GeomFromWKB($4, $3::integer),
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 $5
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 )
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 ),
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 2
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 )
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 ),
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 4326
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 )
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 FROM waterway.sounding_results sr
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 WHERE id = $1
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 `
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 )
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 func generateContours(tree *Tree, tx *sql.Tx, id int64) error {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 stmt, err := tx.Prepare(insertContourSQL)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 if err != nil {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 return err
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 defer stmt.Close()
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 doContours(tree, contourStepWidth, func(res *contourResult) {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 if err == nil {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 _, err = stmt.Exec(
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 id, res.height, tree.EPSG,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 res.lines.AsWKB2D(),
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 contourTolerance)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 })
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 return err
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 func doContours(tree *Tree, step float64, store func(*contourResult)) {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 results := make(chan *contourResult)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 done := make(chan struct{})
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 jobs := make(chan float64)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 go func() {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 for {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 select {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 case r := <-results:
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 store(r)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 case <-done:
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 return
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 }()
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 var wg sync.WaitGroup
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 for i, n := 0, runtime.NumCPU(); i < n; i++ {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 wg.Add(1)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 go processLevels(tree, jobs, results, &wg)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 for h := tree.Min.Z; h <= tree.Max.Z; h += step {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 jobs <- h
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 close(jobs)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 wg.Wait()
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 done <- struct{}{}
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 func processLevels(
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 tree *Tree,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 jobs <-chan float64,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 results chan<- *contourResult,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 wg *sync.WaitGroup,
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 ) {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 defer wg.Done()
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 for h := range jobs {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 var lines MultiLineStringZ
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 tree.Horizontal(h, func(t *Triangle) {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 line := t.IntersectHorizontal(h)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 if len(line) > 1 {
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 lines = append(lines, line)
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 })
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 lines = lines.Merge()
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 results <- &contourResult{h, lines}
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 }
c397fdd8c327 Generate the contour lines of the sounding result during the import, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 }