annotate pkg/controllers/shapestretches.go @ 5683:31973f6f5cca sr-v2

Get rid of deprecation warnings from staticcheck.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 11 Feb 2024 21:37:00 +0100
parents 5f47eeea988d
children 6270951dda28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2019 by via donau
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package controllers
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 "archive/zip"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "fmt"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "io"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "net/http"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "os"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 "path/filepath"
4309
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
24 "strings"
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 "time"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 "github.com/gorilla/mux"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 "github.com/jonas-p/go-shp"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 "gemma.intevation.de/gemma/pkg/config"
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4389
diff changeset
31 "gemma.intevation.de/gemma/pkg/log"
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 "gemma.intevation.de/gemma/pkg/middleware"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 "gemma.intevation.de/gemma/pkg/wkb"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 )
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 const (
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 selectStretchSQL = `
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 SELECT
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 id,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 isrs_asText(lower(stretch)),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 isrs_asText(upper(stretch)),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 ST_AsBinary(area::geometry),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 objnam,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 nobjnam,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 date_info,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 source_organization
4389
5e38667f740c Use stretches as areas of responsibility.
Sascha Wilde <wilde@intevation.de>
parents: 4309
diff changeset
47 FROM users.stretches WHERE
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 staging_done AND
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 name = $1`
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 selectStretchCountriesSQL = `
4389
5e38667f740c Use stretches as areas of responsibility.
Sascha Wilde <wilde@intevation.de>
parents: 4309
diff changeset
52 SELECT country FROM users.stretch_countries
5e38667f740c Use stretches as areas of responsibility.
Sascha Wilde <wilde@intevation.de>
parents: 4309
diff changeset
53 WHERE stretch_id = $1
5e38667f740c Use stretches as areas of responsibility.
Sascha Wilde <wilde@intevation.de>
parents: 4309
diff changeset
54 ORDER BY country`
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 )
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56
4307
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
57 func flattenPoints(mp wkb.MultiPolygonGeom) []shp.Point {
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
58
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
59 var n int
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
60 for _, p := range mp {
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
61 for _, r := range p {
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
62 n += len(r)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
63 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
64 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
65
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
66 ps := make([]shp.Point, n)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
67 var i int
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
68 for _, p := range mp {
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
69 for _, r := range p {
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
70 for _, v := range r {
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
71 ps[i].X = v.X
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
72 ps[i].Y = v.Y
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
73 i++
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
74 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
75 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
76 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
77
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
78 return ps
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
79 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
80
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
81 func asShpPolygon(mp wkb.MultiPolygonGeom) *shp.Polygon {
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
82
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
83 points := flattenPoints(mp)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
84
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
85 var numParts int
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
86 for _, pl := range mp {
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
87 numParts += len(pl)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
88 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
89
4308
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
90 p := &shp.Polygon{}
4307
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
91 p.NumParts = int32(numParts)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
92 p.NumPoints = int32(len(points))
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
93 p.Parts = make([]int32, numParts)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
94
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
95 var marker int32
4308
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
96 var i int
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
97 for _, pl := range mp {
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
98 for _, r := range pl {
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
99 p.Parts[i] = marker
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
100 i++
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
101 marker += int32(len(r))
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
102 }
4307
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
103 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
104
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
105 p.Points = points
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
106 p.Box = shp.BBoxFromPoints(points)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
107 return p
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
108 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
109
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 func stretchShapeDownload(rw http.ResponseWriter, req *http.Request) {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 vars := mux.Vars(req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 name := vars["name"]
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 conn := middleware.GetDBConn(req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 ctx := req.Context()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 rw, fmt.Sprintf("DB error: %v.", err),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 defer tx.Rollback()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 var (
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 id int64
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 lower, upper string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 data []byte
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 objnam string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 nobjnam sql.NullString
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 dateInfo time.Time
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 source string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 )
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 switch err := tx.QueryRowContext(ctx, selectStretchSQL, name).Scan(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 &id,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138 &lower, &upper,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 &data,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 &objnam, &nobjnam,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 &dateInfo,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 &source,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 ); {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 case err == sql.ErrNoRows:
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 http.NotFound(rw, req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 case err != nil:
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 rw, fmt.Sprintf("DB error: %v.", err),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 var countries []string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 if err := func() error {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 rows, err := tx.Query(selectStretchCountriesSQL, id)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161 defer rows.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 for rows.Next() {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 var country string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 if err := rows.Scan(&country); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 countries = append(countries, country)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 return rows.Err()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 }(); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 rw, fmt.Sprintf("DB error: %v.", err),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 var geom wkb.MultiPolygonGeom
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 if err := geom.FromWKB(data); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 rw, fmt.Sprintf("Decoding WKB error: %v.", err),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 tmp := config.TmpDir()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189
5683
31973f6f5cca Get rid of deprecation warnings from staticcheck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5490
diff changeset
190 dir, err := os.MkdirTemp(tmp, "stretch-download")
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 rw, fmt.Sprintf("Cannot create temp dir: %v.", err),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
197 defer os.RemoveAll(dir)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
198
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
199 shpDir := filepath.Join(dir, "stretch")
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
201 if err := os.Mkdir(shpDir, os.ModePerm); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
202 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
203 rw, fmt.Sprintf("Cannot create temp dir: %v.", err),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208 filename := filepath.Join(shpDir, "stretch")
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
210 if err := func() error {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 writer, err := shp.Create(filename, shp.POLYGON)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
213 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
214 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
215 defer writer.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
216
4309
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
217 fields := []shp.Field{
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
218 shp.StringField("NAME", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
219 shp.StringField("OBJNAM", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
220 shp.StringField("NOBJNAM", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
221 shp.StringField("LOWER", 25),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
222 shp.StringField("UPPER", 25),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
223 shp.StringField("SOURCE", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
224 shp.StringField("DATEINFO", 25),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
225 shp.StringField("COUNTRIES", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
226 }
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
227
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
228 writer.SetFields(fields)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
229
4307
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
230 p := asShpPolygon(geom)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
231
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
232 writer.Write(p)
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
233
4309
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
234 write := func(field int, v interface{}) {
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
235 if err == nil {
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
236 err = writer.WriteAttribute(0, field, v)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
237 }
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
238 }
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
239
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
240 write(0, name)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
241 write(1, objnam)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
242 if nobjnam.Valid {
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
243 write(2, nobjnam.String)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
244 }
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
245 write(3, lower)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
246 write(4, upper)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
247 write(5, source)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
248 write(6, dateInfo.Format(time.RFC3339))
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
249 if len(countries) > 0 {
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
250 write(7, strings.Join(countries, ","))
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
251 }
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
252
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
253 return err
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
254 }(); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
255 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
256 rw, fmt.Sprintf("creating shapefile failed: %v.", err),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
257 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
258 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
259 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
260
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
261 entries, err := func() ([]os.FileInfo, error) {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
262 f, err := os.Open(shpDir)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
263 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
264 return nil, err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
265 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
266 defer f.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
267 return f.Readdir(-1)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
268 }()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
269 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
270 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
271 rw, fmt.Sprintf("cannot read directory: %v.", err),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
272 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
273 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
274 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
275
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
276 rw.Header().Set("Content-Type", "application/zip")
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
277 rw.Header().Set("Content-Disposition", `attachment; filename="stretch.zip"`)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
278
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
279 zipfile := zip.NewWriter(rw)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
280 defer zipfile.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
281
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
282 now := time.Now()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
283
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
284 base := filepath.Base(shpDir)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
285 for _, info := range entries {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
286 if !info.Mode().IsRegular() {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
287 continue
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
288 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
289 if err := func() error {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
290 srcFile, err := os.Open(filepath.Join(shpDir, info.Name()))
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
291 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
292 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
293 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
294 defer srcFile.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
295 header := &zip.FileHeader{
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
296 Name: base + "/" + info.Name(),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
297 Method: zip.Deflate,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
298 Modified: now,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
299 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
300 out, err := zipfile.CreateHeader(header)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
301 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
302 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
303 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
304 _, err = io.Copy(out, srcFile)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
305 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
306 }(); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4389
diff changeset
307 log.Errorf("cannot write file: %v\n", err)
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
308 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
309 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
310 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
311 }