annotate pkg/controllers/stretches.go @ 4304:fee5794f3b7a

Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 02 Sep 2019 17:57:44 +0200
parents 95786a675d70
children 0f467a839fe2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3245
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2099 by via donau
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package controllers
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
4304
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
17 "archive/zip"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
18 "context"
3245
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "database/sql"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
20 "encoding/csv"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
21 "fmt"
4304
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
22 "io"
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
23 "io/ioutil"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
24 "log"
3245
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 "net/http"
4304
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
26 "os"
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
27 "path/filepath"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
28 "runtime"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
29 "strings"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
30 "sync"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
31 "time"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
32
4304
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
33 "github.com/gorilla/mux"
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
34 "github.com/jonas-p/go-shp"
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
35
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
36 "gemma.intevation.de/gemma/pkg/config"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
37 "gemma.intevation.de/gemma/pkg/middleware"
4296
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
38 "gemma.intevation.de/gemma/pkg/wkb"
3245
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 )
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
41 const (
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
42 selectSectionBottlenecks = `
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
43 SELECT
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
44 distinct(b.objnam),
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
45 b.limiting
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
46 FROM waterway.sections s, waterway.bottlenecks b
3666
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
47 WHERE b.validity @> current_timestamp
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
48 AND ST_Intersects(b.area, s.area)
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
49 AND s.name = $1`
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
50
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
51 selectStretchBottlenecks = `
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
52 SELECT
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
53 distinct(b.objnam),
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
54 b.limiting
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
55 FROM waterway.stretches s, waterway.bottlenecks b
3666
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
56 WHERE b.validity @> current_timestamp
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
57 AND ST_Intersects(b.area, s.area)
db87f34805fb Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents: 3645
diff changeset
58 AND s.name = $1`
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
59 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
60
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
61 type (
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
62 stretchBottleneck struct {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
63 name string
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
64 limiting string
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
65 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
66
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
67 stretchBottlenecks []stretchBottleneck
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
68
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
69 fullStretchBottleneck struct {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
70 *stretchBottleneck
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
71 measurements availMeasurements
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
72 ldc []float64
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
73 breaks []float64
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
74 access func(*availMeasurement) float64
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
75 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
76 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
77
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
78 func (bns stretchBottlenecks) contains(limiting string) bool {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
79 for i := range bns {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
80 if bns[i].limiting == limiting {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
81 return true
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
82 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
83 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
84 return false
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
85 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
86
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
87 func loadFullStretchBottleneck(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
88 ctx context.Context,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
89 conn *sql.Conn,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
90 bn *stretchBottleneck,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
91 los int,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
92 from, to time.Time,
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
93 depthbreaks, widthbreaks []float64,
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
94 ) (*fullStretchBottleneck, error) {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
95 measurements, err := loadDepthValues(ctx, conn, bn.name, los, from, to)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
96 if err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
97 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
98 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
99 ldc, err := loadLDCReferenceValue(ctx, conn, bn.name)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
100 if err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
101 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
102 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
103
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
104 var access func(*availMeasurement) float64
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
105 var breaks []float64
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
106
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
107 switch bn.limiting {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
108 case "width":
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
109 access = (*availMeasurement).getWidth
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
110 breaks = widthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
111 case "depth":
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
112 access = (*availMeasurement).getDepth
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
113 breaks = depthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
114 default:
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
115 log.Printf(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
116 "warn: unknown limitation '%s'. default to 'depth'.\n",
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
117 bn.limiting)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
118 access = (*availMeasurement).getDepth
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
119 breaks = depthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
120 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
121
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
122 return &fullStretchBottleneck{
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
123 stretchBottleneck: bn,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
124 measurements: measurements,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
125 ldc: ldc,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
126 breaks: breaks,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
127 access: access,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
128 }, nil
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
129 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
130
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
131 func loadStretchBottlenecks(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
132 ctx context.Context,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
133 conn *sql.Conn,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
134 stretch bool,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
135 name string,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
136 ) (stretchBottlenecks, error) {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
137 var sql string
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
138 if stretch {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
139 sql = selectStretchBottlenecks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
140 } else {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
141 sql = selectSectionBottlenecks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
142 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
143
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
144 rows, err := conn.QueryContext(ctx, sql, name)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
145 if err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
146 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
147 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
148 defer rows.Close()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
149
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
150 var bns stretchBottlenecks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
151
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
152 for rows.Next() {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
153 var bn stretchBottleneck
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
154 if err := rows.Scan(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
155 &bn.name,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
156 &bn.limiting,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
157 ); err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
158 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
159 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
160 bns = append(bns, bn)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
161 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
162
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
163 if err := rows.Err(); err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
164 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
165 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
166
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
167 return bns, nil
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
168 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
169
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
170 func stretchAvailableFairwayDepth(rw http.ResponseWriter, req *http.Request) {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
171
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
172 vars := mux.Vars(req)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
173 stretch := vars["kind"] == "stretch"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
174 name := vars["name"]
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
175 mode := intervalMode(req.FormValue("mode"))
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
176
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
177 depthbreaks, widthbreaks := afdRefs, afdRefs
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
178
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
179 from, ok := parseFormTime(rw, req, "from", time.Now().AddDate(-1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
180 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
181 return
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
182 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
183
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
184 to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
185 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
186 return
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
187 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
188
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
189 if to.Before(from) {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
190 to, from = from, to
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
191 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
192
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
193 los, ok := parseFormInt(rw, req, "los", 1)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
194 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
195 return
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
196 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
197
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
198 conn := middleware.GetDBConn(req)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
199 ctx := req.Context()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
200
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
201 bns, err := loadStretchBottlenecks(ctx, conn, stretch, name)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
202 if err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
203 http.Error(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
204 rw, fmt.Sprintf("DB error: %v.", err),
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
205 http.StatusInternalServerError)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
206 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
207 }
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
208
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
209 if len(bns) == 0 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
210 http.Error(rw, "No bottlenecks found.", http.StatusNotFound)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
211 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
212 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
213
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
214 if b := req.FormValue("depthbreaks"); b != "" {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
215 depthbreaks = breaksToReferenceValue(b)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
216 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
217
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
218 if b := req.FormValue("widthbreaks"); b != "" {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
219 widthbreaks = breaksToReferenceValue(b)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
220 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
221
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
222 useDepth, useWidth := bns.contains("depth"), bns.contains("width")
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
223
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
224 if useDepth && useWidth && len(widthbreaks) != len(depthbreaks) {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
225 http.Error(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
226 rw,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
227 fmt.Sprintf("class breaks lengths differ: %d != %d",
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
228 len(widthbreaks), len(depthbreaks)),
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
229 http.StatusBadRequest,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
230 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
231 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
232 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
233
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
234 log.Printf("info: time interval: (%v - %v)\n", from, to)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
235
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
236 var loaded []*fullStretchBottleneck
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
237 var errors []error
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
238
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
239 for i := range bns {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
240 l, err := loadFullStretchBottleneck(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
241 ctx,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
242 conn,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
243 &bns[i],
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
244 los,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
245 from, to,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
246 depthbreaks, widthbreaks,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
247 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
248 if err != nil {
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
249 log.Printf("error: %v\n", err)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
250 errors = append(errors, err)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
251 continue
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
252 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
253 loaded = append(loaded, l)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
254 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
255
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
256 if len(loaded) == 0 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
257 http.Error(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
258 rw,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
259 fmt.Sprintf("No bottleneck loaded: %v", joinErrors(errors)),
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
260 http.StatusInternalServerError,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
261 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
262 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
263 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
264
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
265 n := runtime.NumCPU() / 2
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
266 if n == 0 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
267 n = 1
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
268 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
269
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
270 type result struct {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
271 label string
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
272 from time.Time
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
273 to time.Time
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
274 ldc []time.Duration
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
275 breaks []time.Duration
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
276 }
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
277
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
278 jobCh := make(chan *result)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
279
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
280 var wg sync.WaitGroup
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
281
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
282 for i := 0; i < n; i++ {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
283 wg.Add(1)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
284 go func() {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
285 defer wg.Done()
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
286 for res := range jobCh {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
287
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
288 var ldc, breaks []time.Duration
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
289
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
290 for _, bn := range loaded {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
291 l := bn.measurements.classify(
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
292 res.from, res.to,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
293 bn.ldc,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
294 bn.access,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
295 )
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
296 b := bn.measurements.classify(
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
297 res.from, res.to,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
298 bn.breaks,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
299 bn.access,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
300 )
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
301
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
302 if ldc == nil {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
303 ldc, breaks = l, b
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
304 } else {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
305 for i, v := range l {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
306 ldc[i] += v
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
307 }
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
308 for i, v := range b {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
309 breaks[i] += v
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
310 }
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
311 }
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
312 }
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
313
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
314 res.ldc = ldc
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
315 res.breaks = breaks
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
316 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
317 }()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
318 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
319
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
320 var results []*result
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
321
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
322 interval := intervals[mode](from, to)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
323
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
324 var breaks []float64
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
325
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
326 if useDepth {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
327 breaks = depthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
328 } else {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
329 breaks = widthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
330 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
331
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
332 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
333
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
334 res := &result{
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
335 label: label,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
336 from: pfrom,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
337 to: pto,
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
338 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
339 results = append(results, res)
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
340 jobCh <- res
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
341 }
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
342
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
343 close(jobCh)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
344 wg.Wait()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
345
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
346 rw.Header().Add("Content-Type", "text/csv")
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
347
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
348 out := csv.NewWriter(rw)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
349
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
350 // label, lnwl, classes
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
351 record := make([]string, 1+2+len(breaks)+1)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
352 record[0] = "# time"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
353 record[1] = "# < LDC [h]"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
354 record[2] = "# >= LDC [h]"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
355 for i, v := range breaks {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
356 if useDepth && useWidth {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
357 if i == 0 {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
358 record[3] = "# < break_1 [h]"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
359 }
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
360 record[i+4] = fmt.Sprintf("# >= break_%d", i+1)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
361 } else {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
362 if i == 0 {
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
363 record[3] = fmt.Sprintf("# < %.1f [h]", v)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
364 }
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
365 record[i+4] = fmt.Sprintf("# >= %.1f [h]", v)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
366 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
367 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
368
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
369 if err := out.Write(record); err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
370 // Too late for HTTP status message.
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
371 log.Printf("error: %v\n", err)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
372 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
373 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
374
3420
b9fc6c546610 fairway availability: Normalize results to look like as we only have one bottleneck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3405
diff changeset
375 // Normalize to look like as we have only one bottleneck.
b9fc6c546610 fairway availability: Normalize results to look like as we only have one bottleneck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3405
diff changeset
376 scale := 1 / float64(len(loaded))
b9fc6c546610 fairway availability: Normalize results to look like as we only have one bottleneck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3405
diff changeset
377
3470
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
378 empty := fmt.Sprintf("%.3f", 0.0)
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
379 for i := range record[1:] {
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
380 record[i+1] = empty
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
381 }
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
382
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
383 for _, r := range results {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
384 record[0] = r.label
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
385 for i, v := range r.ldc {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
386 record[1+i] = fmt.Sprintf("%.3f", v.Hours()*scale)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
387 }
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
388
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
389 for i, d := range r.breaks {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
390 record[3+i] = fmt.Sprintf("%.3f", d.Hours()*scale)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
391 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
392
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
393 if err := out.Write(record); err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
394 // Too late for HTTP status message.
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
395 log.Printf("error: %v\n", err)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
396 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
397 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
398 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
399
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
400 out.Flush()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
401 if err := out.Error(); err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
402 // Too late for HTTP status message.
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
403 log.Printf("error: %v\n", err)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
404 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
405 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
406
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
407 func joinErrors(errors []error) string {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
408 var b strings.Builder
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
409 for _, err := range errors {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
410 if b.Len() > 0 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
411 b.WriteString(", ")
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
412 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
413 b.WriteString(err.Error())
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
414 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
415 return b.String()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
416 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
417
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
418 func stretchAvailabilty(rw http.ResponseWriter, req *http.Request) {
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
419
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
420 vars := mux.Vars(req)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
421 stretch := vars["kind"] == "stretch"
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
422 name := vars["name"]
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
423 mode := intervalMode(req.FormValue("mode"))
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
424
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
425 if name == "" {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
426 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
427 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
428 fmt.Sprintf("Missing %s name", vars["kind"]),
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
429 http.StatusBadRequest,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
430 )
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
431 return
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
432 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
433
3519
a6128caca3c1 stretches/sections: availability. fixed typo in 'from' parameter parsing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3473
diff changeset
434 from, ok := parseFormTime(rw, req, "from", time.Now().AddDate(-1, 0, 0))
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
435 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
436 return
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
437 }
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
438
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
439 to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
440 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
441 return
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
442 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
443
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
444 if to.Before(from) {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
445 to, from = from, to
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
446 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
447
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
448 los, ok := parseFormInt(rw, req, "los", 1)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
449 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
450 return
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
451 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
452
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
453 depthbreaks, widthbreaks := afdRefs, afdRefs
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
454
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
455 if b := req.FormValue("depthbreaks"); b != "" {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
456 depthbreaks = breaksToReferenceValue(b)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
457 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
458
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
459 if b := req.FormValue("widthbreaks"); b != "" {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
460 widthbreaks = breaksToReferenceValue(b)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
461 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
462
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
463 conn := middleware.GetDBConn(req)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
464 ctx := req.Context()
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
465
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
466 bns, err := loadStretchBottlenecks(ctx, conn, stretch, name)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
467 if err != nil {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
468 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
469 rw, fmt.Sprintf("DB error: %v.", err),
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
470 http.StatusInternalServerError)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
471 return
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
472 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
473
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
474 if len(bns) == 0 {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
475 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
476 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
477 "No bottlenecks found.",
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
478 http.StatusNotFound,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
479 )
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
480 return
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
481 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
482
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
483 useDepth, useWidth := bns.contains("depth"), bns.contains("width")
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
484
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
485 if useDepth && useWidth && len(widthbreaks) != len(depthbreaks) {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
486 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
487 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
488 fmt.Sprintf("class breaks lengths differ: %d != %d",
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
489 len(widthbreaks), len(depthbreaks)),
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
490 http.StatusBadRequest,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
491 )
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
492 return
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
493 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
494
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
495 log.Printf("info: time interval: (%v - %v)\n", from, to)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
496
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
497 var loaded []*fullStretchBottleneck
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
498 var errors []error
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
499
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
500 for i := range bns {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
501 l, err := loadFullStretchBottleneck(
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
502 ctx,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
503 conn,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
504 &bns[i],
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
505 los,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
506 from, to,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
507 depthbreaks, widthbreaks,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
508 )
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
509 if err != nil {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
510 log.Printf("error: %v\n", err)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
511 errors = append(errors, err)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
512 continue
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
513 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
514 loaded = append(loaded, l)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
515 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
516
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
517 if len(loaded) == 0 {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
518 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
519 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
520 fmt.Sprintf("No bottleneck loaded: %v", joinErrors(errors)),
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
521 http.StatusInternalServerError,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
522 )
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
523 return
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
524 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
525
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
526 n := runtime.NumCPU() / 2
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
527 if n == 0 {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
528 n = 1
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
529 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
530
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
531 type result struct {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
532 label string
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
533 from time.Time
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
534 to time.Time
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
535 ldc []float64
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
536 breaks []float64
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
537 }
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
538
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
539 jobCh := make(chan *result)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
540
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
541 var wg sync.WaitGroup
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
542
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
543 for i := 0; i < n; i++ {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
544 wg.Add(1)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
545 go func() {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
546 defer wg.Done()
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
547 for res := range jobCh {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
548 var ldc, breaks []time.Duration
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
549
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
550 for _, bn := range loaded {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
551 l := bn.measurements.classify(
3764
ad93adaeb688 Section/Stretch avail: Fixed classification interval calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3666
diff changeset
552 res.from, res.to,
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
553 bn.ldc,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
554 (*availMeasurement).getValue,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
555 )
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
556
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
557 b := bn.measurements.classify(
3764
ad93adaeb688 Section/Stretch avail: Fixed classification interval calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3666
diff changeset
558 res.from, res.to,
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
559 bn.breaks,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
560 bn.access,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
561 )
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
562
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
563 if ldc == nil {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
564 ldc, breaks = l, b
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
565 } else {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
566 for i, v := range l {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
567 ldc[i] += v
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
568 }
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
569 for i, v := range b {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
570 breaks[i] += v
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
571 }
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
572 }
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
573 }
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
574
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
575 duration := res.to.Sub(res.from) * time.Duration(len(loaded))
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
576
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
577 res.ldc = durationsToPercentage(duration, ldc)
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
578 res.breaks = durationsToPercentage(duration, breaks)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
579 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
580 }()
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
581 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
582
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
583 var results []*result
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
584
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
585 interval := intervals[mode](from, to)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
586
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
587 var breaks []float64
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
588
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
589 if useDepth {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
590 breaks = depthbreaks
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
591 } else {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
592 breaks = widthbreaks
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
593 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
594
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
595 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
596
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
597 res := &result{
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
598 label: label,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
599 from: pfrom,
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
600 to: pto,
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
601 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
602 results = append(results, res)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
603
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
604 jobCh <- res
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
605 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
606
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
607 close(jobCh)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
608 wg.Wait()
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
609
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
610 rw.Header().Add("Content-Type", "text/csv")
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
611
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
612 out := csv.NewWriter(rw)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
613
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
614 // label, lnwl, classes
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
615 record := make([]string, 1+2+len(breaks)+1)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
616 record[0] = "# time"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
617 record[1] = "# < LDC [%%]"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
618 record[2] = "# >= LDC [%%]"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
619 for i, v := range breaks {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
620 if useDepth && useWidth {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
621 if i == 0 {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
622 record[3] = "# < break_1 [%%]"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
623 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
624 record[i+4] = fmt.Sprintf("# >= break_%d [%%]", i+1)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
625 } else {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
626 if i == 0 {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
627 record[3] = fmt.Sprintf("# < %.3f [%%]", v)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
628 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
629 record[i+4] = fmt.Sprintf("# >= %.3f [%%]", v)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
630 }
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
631 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
632
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
633 if err := out.Write(record); err != nil {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
634 // Too late for HTTP status message.
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
635 log.Printf("error: %v\n", err)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
636 return
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
637 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
638
3470
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
639 empty := fmt.Sprintf("%.3f", 0.0)
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
640 for i := range record[1:] {
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
641 record[i+1] = empty
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
642 }
104c4f683c74 fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3467
diff changeset
643
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
644 for _, res := range results {
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
645 record[0] = res.label
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
646
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
647 for i, v := range res.ldc {
3473
d66cae5be0a1 fairway availabilty: Fixed off-by-one array access when filling LDC classification into result rows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3470
diff changeset
648 record[1+i] = fmt.Sprintf("%.3f", v)
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
649 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
650
3467
7265adcc5baa Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3452
diff changeset
651 for i, v := range res.breaks {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
652 record[3+i] = fmt.Sprintf("%.3f", v)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
653 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
654
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
655 if err := out.Write(record); err != nil {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
656 // Too late for HTTP status message.
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
657 log.Printf("error: %v\n", err)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
658 return
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
659 }
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
660 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
661
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
662 out.Flush()
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
663 if err := out.Error(); err != nil {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
664 // Too late for HTTP status message.
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
665 log.Printf("error: %v\n", err)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
666 }
3245
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
667 }
4296
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
668
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
669 const (
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
670 selectStretchSQL = `
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
671 SELECT
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
672 id,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
673 isrs_asText(lower(stretch)),
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
674 isrs_asText(upper(stretch)),
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
675 ST_AsBinary(area::geometry),
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
676 objnam,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
677 nobjnam,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
678 date_info,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
679 source_organization
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
680 FROM waterway.stretches WHERE
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
681 staging_done AND
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
682 name = $1`
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
683
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
684 selectStretchCountriesSQL = `
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
685 SELECT country_code FROM waterway.stretch_countries
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
686 WHERE stretches_id = $1
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
687 ORDER BY country_code`
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
688 )
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
689
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
690 func stretchShapeDownload(rw http.ResponseWriter, req *http.Request) {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
691 vars := mux.Vars(req)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
692 name := vars["name"]
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
693
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
694 conn := middleware.GetDBConn(req)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
695 ctx := req.Context()
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
696
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
697 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
698 if err != nil {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
699 http.Error(
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
700 rw, fmt.Sprintf("DB error: %v.", err),
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
701 http.StatusInternalServerError)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
702 return
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
703 }
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
704 defer tx.Rollback()
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
705
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
706 var (
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
707 id int64
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
708 lower, upper string
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
709 data []byte
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
710 objnam string
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
711 nobjnam sql.NullString
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
712 dateInfo time.Time
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
713 source string
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
714 )
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
715
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
716 switch err := tx.QueryRowContext(ctx, selectStretchSQL, name).Scan(
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
717 &id,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
718 &lower, &upper,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
719 &data,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
720 &objnam, &nobjnam,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
721 &dateInfo,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
722 &source,
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
723 ); {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
724 case err == sql.ErrNoRows:
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
725 http.NotFound(rw, req)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
726 return
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
727 case err != nil:
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
728 http.Error(
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
729 rw, fmt.Sprintf("DB error: %v.", err),
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
730 http.StatusInternalServerError)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
731 return
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
732 }
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
733
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
734 var countries []string
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
735
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
736 if err := func() error {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
737 rows, err := tx.Query(selectStretchCountriesSQL, id)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
738 if err != nil {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
739 return err
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
740 }
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
741 defer rows.Close()
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
742
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
743 for rows.Next() {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
744 var country string
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
745 if err := rows.Scan(&country); err != nil {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
746 return err
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
747 }
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
748 countries = append(countries, country)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
749 }
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
750
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
751 return rows.Err()
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
752 }(); err != nil {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
753 http.Error(
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
754 rw, fmt.Sprintf("DB error: %v.", err),
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
755 http.StatusInternalServerError)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
756 return
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
757 }
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
758
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
759 var geom wkb.MultiPolygonGeom
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
760
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
761 if err := geom.FromWKB(data); err != nil {
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
762 http.Error(
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
763 rw, fmt.Sprintf("Decoding WKB error: %v.", err),
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
764 http.StatusInternalServerError)
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
765 return
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
766 }
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
767
4304
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
768 tmp := config.TmpDir()
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
769
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
770 dir, err := ioutil.TempDir(tmp, "stretch-download")
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
771 if err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
772 http.Error(
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
773 rw, fmt.Sprintf("Cannot create temp dir: %v.", err),
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
774 http.StatusInternalServerError)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
775 return
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
776 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
777 defer os.RemoveAll(dir)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
778
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
779 shpDir := filepath.Join(dir, "stretch")
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
780
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
781 if err := os.Mkdir(shpDir, os.ModePerm); err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
782 http.Error(
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
783 rw, fmt.Sprintf("Cannot create temp dir: %v.", err),
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
784 http.StatusInternalServerError)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
785 return
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
786 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
787
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
788 filename := filepath.Join(shpDir, "stretch")
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
789
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
790 if err := func() error {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
791 writer, err := shp.Create(filename, shp.POLYGON)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
792 if err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
793 return err
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
794 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
795 defer writer.Close()
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
796
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
797 // TODO: Write geometry
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
798
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
799 return nil
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
800 }(); err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
801 http.Error(
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
802 rw, fmt.Sprintf("creating shapefile failed: %v.", err),
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
803 http.StatusInternalServerError)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
804 return
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
805 }
4296
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
806
4304
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
807 entries, err := func() ([]os.FileInfo, error) {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
808 f, err := os.Open(shpDir)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
809 if err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
810 return nil, err
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
811 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
812 defer f.Close()
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
813 return f.Readdir(-1)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
814 }()
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
815 if err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
816 http.Error(
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
817 rw, fmt.Sprintf("cannot read directory: %v.", err),
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
818 http.StatusInternalServerError)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
819 return
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
820 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
821
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
822 rw.Header().Set("Content-Type", "application/zip")
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
823 rw.Header().Set("Content-Disposition", `attachment; filename="stretch.zip"`)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
824
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
825 zipfile := zip.NewWriter(rw)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
826 defer zipfile.Close()
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
827
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
828 now := time.Now()
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
829
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
830 base := filepath.Base(shpDir)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
831 for _, info := range entries {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
832 if !info.Mode().IsRegular() {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
833 continue
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
834 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
835 if err := func() error {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
836 srcFile, err := os.Open(filepath.Join(shpDir, info.Name()))
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
837 if err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
838 return err
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
839 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
840 defer srcFile.Close()
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
841 header := &zip.FileHeader{
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
842 Name: base + "/" + info.Name(),
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
843 Method: zip.Deflate,
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
844 Modified: now,
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
845 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
846 out, err := zipfile.CreateHeader(header)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
847 if err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
848 return err
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
849 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
850 _, err = io.Copy(out, srcFile)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
851 return err
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
852 }(); err != nil {
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
853 log.Printf("error: cannot write file: %v\n", err)
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
854 return
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
855 }
fee5794f3b7a Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4296
diff changeset
856 }
4296
95786a675d70 WIP: Started with downloading stretches as ESRI shapes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3764
diff changeset
857 }