annotate cmd/octree2contour/main.go @ 925:15bf101e1522

Send 2D X/Y vertices to the database directly instead of sending 3D data and dropping the Z value afterwards.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 06 Oct 2018 13:34:51 +0200
parents 52cb0b82b490
children 0a0013fbda4a
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")
921
52cb0b82b490 Simplify contour lines on storage
Tom Gottfried <tom@intevation.de>
parents: 732
diff changeset
16 step = flag.Float64("s", 0.5, "step width")
52cb0b82b490 Simplify contour lines on storage
Tom Gottfried <tom@intevation.de>
parents: 732
diff changeset
17 tol = flag.Float64("t", 0.1, "tolerance for simplification")
732
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
18 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
19 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
20 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
21 )
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
23 func processLevels(
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
24 tree *octree.Tree,
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
25 jobs <-chan float64,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
26 results chan<- result,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
27 wg *sync.WaitGroup,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
28 ) {
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
29 defer wg.Done()
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
30 for h := range jobs {
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
31 var lines octree.MultiLineStringZ
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
32 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
33 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
34 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
35 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
36 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
37 })
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
38 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
39 }
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
40 }
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
41
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
42 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
43
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
44 if *one {
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
45 var lines octree.MultiLineStringZ
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
46 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
47 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
48 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
49 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
50 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
51 })
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
52 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
53 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
54
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
55 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
56 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
57
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
58 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
59 go func() {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
60 for {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
61 select {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
62 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
63 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
64 case <-done:
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
65 return
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 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
68 }()
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
69
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
70 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
71 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
72 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
73 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
74 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
75 }
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
76 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
77 jobs <- h
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
78 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
79 close(jobs)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
80 wg.Wait()
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
81 done <- struct{}{}
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
82
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
83 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
84 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
85 })
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
86
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
87 return all
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
88 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
89
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 func main() {
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 flag.Parse()
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92
732
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
93 if *bottleneck == "" || *date == "" {
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
94 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
95 }
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
96
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
97 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
98 if err != nil {
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
99 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
100 }
39b5cf9a6b8f Store sounding results contour lines into right table.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 727
diff changeset
101
694
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 689
diff changeset
102 for _, fname := range flag.Args() {
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 log.Printf("processing %s\n", fname)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 start := time.Now()
727
41c8dc61f38f Moved octree loading stuff to octree package.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 726
diff changeset
105 tree, err := octree.LoadTree(fname)
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 if err != nil {
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 log.Printf("error: %v\n", err)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 continue
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 }
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 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
111 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
112 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
113 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
114 start = time.Now()
921
52cb0b82b490 Simplify contour lines on storage
Tom Gottfried <tom@intevation.de>
parents: 732
diff changeset
115 if err = store(all, tree.EPSG, *bottleneck, dateInfo, *tol); 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
116 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
117 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
118 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
119 }
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 }