annotate cmd/octree2contour/main.go @ 732:39b5cf9a6b8f

Store sounding results contour lines into right table.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 23 Sep 2018 19:51:02 +0200
parents 41c8dc61f38f
children 52cb0b82b490
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package main
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import (
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 "flag"
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 "log"
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
6 "runtime"
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
7 "sort"
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
8 "sync"
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 "time"
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
10
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
11 "gemma.intevation.de/gemma/pkg/octree"
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 )
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 var (
732
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
15 one = flag.Bool("o", false, "create only a single contour")
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
16 step = flag.Float64("s", 0.5, "step with")
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
17 max = flag.Float64("m", 10, "max height from lowest point")
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
18 bottleneck = flag.String("bottleneck", "", "bottleneck id")
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
19 date = flag.String("date", "", "date info")
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 )
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
22 func processLevels(
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
23 tree *octree.Tree,
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
24 jobs <-chan float64,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
25 results chan<- result,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
26 wg *sync.WaitGroup,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
27 ) {
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
28 defer wg.Done()
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
29 for h := range jobs {
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
30 var lines octree.MultiLineStringZ
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
31 tree.Horizontal(h, func(t *octree.Triangle) {
726
5af9ab39e715 Renamed a few types to uppercase names to prepare the move to the octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 694
diff changeset
32 line := t.IntersectHorizontal(h)
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
33 if len(line) > 1 {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
34 lines = append(lines, line)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
35 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
36 })
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
37 results <- result{h, lines}
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
38 }
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
39 }
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
40
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
41 func process(tree *octree.Tree) []result {
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
42
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
43 if *one {
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
44 var lines octree.MultiLineStringZ
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
45 tree.Horizontal(*step, func(t *octree.Triangle) {
726
5af9ab39e715 Renamed a few types to uppercase names to prepare the move to the octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 694
diff changeset
46 line := t.IntersectHorizontal(*step)
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
47 if len(line) > 0 {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
48 lines = append(lines, line)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
49 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
50 })
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
51 return []result{{*step, lines}}
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
52 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
53
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
54 results := make(chan result)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
55 done := make(chan struct{})
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
56
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
57 var all []result
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
58 go func() {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
59 for {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
60 select {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
61 case r := <-results:
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
62 all = append(all, r)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
63 case <-done:
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
64 return
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
65 }
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
66 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
67 }()
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
68
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
69 var wg sync.WaitGroup
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
70 jobs := make(chan float64)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
71 for i, n := 0, runtime.NumCPU(); i < n; i++ {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
72 wg.Add(1)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
73 go processLevels(tree, jobs, results, &wg)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
74 }
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
75 for h := tree.Min.Z; h <= tree.Max.Z; h += *step {
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
76 jobs <- h
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
77 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
78 close(jobs)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
79 wg.Wait()
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
80 done <- struct{}{}
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
81
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
82 sort.Slice(all, func(i, j int) bool {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
83 return all[i].h < all[j].h
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
84 })
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
85
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
86 return all
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
87 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
88
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 func main() {
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 flag.Parse()
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91
732
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
92 if *bottleneck == "" || *date == "" {
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
93 log.Fatalln("missing bottleneck or date option.")
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
94 }
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
95
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
96 dateInfo, err := time.Parse("2006-01-02", *date)
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
97 if err != nil {
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
98 log.Fatalf("error: %v\n", err)
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
99 }
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
100
694
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 689
diff changeset
101 for _, fname := range flag.Args() {
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 log.Printf("processing %s\n", fname)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 start := time.Now()
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
104 tree, err := octree.LoadTree(fname)
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 if err != nil {
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 log.Printf("error: %v\n", err)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 continue
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 }
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 log.Printf("loading took: %v\n", time.Since(start))
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
110 start = time.Now()
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
111 all := process(tree)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
112 log.Printf("processing took: %v\n", time.Since(start))
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
113 start = time.Now()
732
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
114 if err = store(all, tree.EPSG, *bottleneck, dateInfo); err != nil {
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
115 log.Printf("error: %v\n", err)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
116 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
117 log.Printf("storing took: %v\n", time.Since(start))
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 }
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 }