annotate cmd/octree2contour/main.go @ 727:41c8dc61f38f

Moved octree loading stuff to octree package.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 22 Sep 2018 21:57:30 +0200
parents 5af9ab39e715
children 39b5cf9a6b8f
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 (
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 one = flag.Bool("o", false, "create only a single contour")
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 step = flag.Float64("s", 0.5, "step with")
680
c79c7be29a7a octree: Small fix in horizontal traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 678
diff changeset
17 max = flag.Float64("m", 10, "max height from lowest point")
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 )
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
20 func processLevels(
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
21 tree *octree.Tree,
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
22 jobs <-chan float64,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
23 results chan<- result,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
24 wg *sync.WaitGroup,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
25 ) {
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
26 defer wg.Done()
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
27 for h := range jobs {
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
28 var lines octree.MultiLineStringZ
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
29 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
30 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
31 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
32 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
33 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
34 })
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
35 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
36 }
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
37 }
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
38
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
39 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
40
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
41 if *one {
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
42 var lines octree.MultiLineStringZ
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
43 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
44 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
45 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
46 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
47 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
48 })
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
49 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
50 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
51
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
52 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
53 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
54
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
55 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
56 go func() {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
57 for {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
58 select {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
59 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
60 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
61 case <-done:
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
62 return
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
63 }
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
64 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
65 }()
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
66
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
67 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
68 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
69 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
70 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
71 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
72 }
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
73 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
74 jobs <- h
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
75 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
76 close(jobs)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
77 wg.Wait()
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
78 done <- struct{}{}
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
79
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
80 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
81 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
82 })
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
83
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
84 return all
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
85 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
86
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 func main() {
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 flag.Parse()
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
694
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 689
diff changeset
90 for _, fname := range flag.Args() {
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 log.Printf("processing %s\n", fname)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 start := time.Now()
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
93 tree, err := octree.LoadTree(fname)
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 if err != nil {
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 log.Printf("error: %v\n", err)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 continue
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 }
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 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
99 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
100 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
101 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
102 start = time.Now()
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
103 if err = store(all, tree.EPSG); 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
104 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
105 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
106 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
107 }
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 }