annotate pkg/controllers/shapestretches.go @ 5490:5f47eeea988d logging

Use own logging package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Sep 2021 17:45:39 +0200
parents 5e38667f740c
children 31973f6f5cca
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 "io/ioutil"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "net/http"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 "os"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 "path/filepath"
4309
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
25 "strings"
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 "time"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 "github.com/gorilla/mux"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 "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
30
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 "gemma.intevation.de/gemma/pkg/config"
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4389
diff changeset
32 "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
33 "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
34 "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
35 )
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 const (
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 selectStretchSQL = `
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 SELECT
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 id,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 isrs_asText(lower(stretch)),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 isrs_asText(upper(stretch)),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 ST_AsBinary(area::geometry),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 objnam,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 nobjnam,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 date_info,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 source_organization
4389
5e38667f740c Use stretches as areas of responsibility.
Sascha Wilde <wilde@intevation.de>
parents: 4309
diff changeset
48 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
49 staging_done AND
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 name = $1`
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 selectStretchCountriesSQL = `
4389
5e38667f740c Use stretches as areas of responsibility.
Sascha Wilde <wilde@intevation.de>
parents: 4309
diff changeset
53 SELECT country FROM users.stretch_countries
5e38667f740c Use stretches as areas of responsibility.
Sascha Wilde <wilde@intevation.de>
parents: 4309
diff changeset
54 WHERE stretch_id = $1
5e38667f740c Use stretches as areas of responsibility.
Sascha Wilde <wilde@intevation.de>
parents: 4309
diff changeset
55 ORDER BY country`
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 )
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57
4307
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
58 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
59
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
60 var n int
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
61 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
62 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
63 n += len(r)
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
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
67 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
68 var i int
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
69 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
70 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
71 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
72 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
73 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
74 i++
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
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
79 return ps
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
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
82 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
83
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
84 points := flattenPoints(mp)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
85
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
86 var numParts int
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
87 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
88 numParts += len(pl)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
89 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
90
4308
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
91 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
92 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
93 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
94 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
95
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
96 var marker int32
4308
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
97 var i int
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
98 for _, pl := range mp {
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
99 for _, r := range pl {
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
100 p.Parts[i] = marker
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
101 i++
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
102 marker += int32(len(r))
2c83d32bdd1b Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4307
diff changeset
103 }
4307
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
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
106 p.Points = points
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
107 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
108 return p
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
109 }
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
110
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 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
112 vars := mux.Vars(req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 name := vars["name"]
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 conn := middleware.GetDBConn(req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 ctx := req.Context()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 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
119 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 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
122 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 defer tx.Rollback()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 var (
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 id int64
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 lower, upper string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 data []byte
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 objnam string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 nobjnam sql.NullString
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 dateInfo time.Time
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 source string
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 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
138 &id,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 &lower, &upper,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 &data,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 &objnam, &nobjnam,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 &dateInfo,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 &source,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 ); {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 case err == sql.ErrNoRows:
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 http.NotFound(rw, req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 case err != nil:
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 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
151 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 return
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 var countries []string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 if err := func() error {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 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
159 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 defer rows.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 for rows.Next() {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 var country string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 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
167 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 countries = append(countries, country)
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 return rows.Err()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 }(); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 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
176 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 return
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 var geom wkb.MultiPolygonGeom
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 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
183 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 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
185 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 return
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189 tmp := config.TmpDir()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
190
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191 dir, err := ioutil.TempDir(tmp, "stretch-download")
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 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
195 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
197 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
198 defer os.RemoveAll(dir)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
199
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200 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
201
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
202 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
203 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 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
205 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 return
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209 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
210
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 if err := func() error {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212 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
213 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
214 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
215 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
216 defer writer.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
217
4309
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
218 fields := []shp.Field{
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
219 shp.StringField("NAME", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
220 shp.StringField("OBJNAM", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
221 shp.StringField("NOBJNAM", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
222 shp.StringField("LOWER", 25),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
223 shp.StringField("UPPER", 25),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
224 shp.StringField("SOURCE", 50),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
225 shp.StringField("DATEINFO", 25),
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
226 shp.StringField("COUNTRIES", 50),
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
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
229 writer.SetFields(fields)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
230
4307
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
231 p := asShpPolygon(geom)
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
232
e5a831ecd557 Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4305
diff changeset
233 writer.Write(p)
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234
4309
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
235 write := func(field int, v interface{}) {
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
236 if err == nil {
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
237 err = writer.WriteAttribute(0, field, v)
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
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
241 write(0, name)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
242 write(1, objnam)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
243 if nobjnam.Valid {
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
244 write(2, nobjnam.String)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
245 }
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
246 write(3, lower)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
247 write(4, upper)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
248 write(5, source)
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
249 write(6, dateInfo.Format(time.RFC3339))
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
250 if len(countries) > 0 {
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
251 write(7, strings.Join(countries, ","))
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
78be85723eff stretch shape export: Write attributes, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4308
diff changeset
254 return err
4305
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
255 }(); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
256 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
257 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
258 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
259 return
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
262 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
263 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
264 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
265 return nil, err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
266 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
267 defer f.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
268 return f.Readdir(-1)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
269 }()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
270 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
271 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
272 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
273 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
274 return
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
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-Type", "application/zip")
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
278 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
279
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
280 zipfile := zip.NewWriter(rw)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
281 defer zipfile.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
282
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
283 now := time.Now()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
284
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
285 base := filepath.Base(shpDir)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
286 for _, info := range entries {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
287 if !info.Mode().IsRegular() {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
288 continue
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
289 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
290 if err := func() error {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
291 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
292 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
293 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
294 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
295 defer srcFile.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
296 header := &zip.FileHeader{
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
297 Name: base + "/" + info.Name(),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
298 Method: zip.Deflate,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
299 Modified: now,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
300 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
301 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
302 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
303 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
304 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
305 _, 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
306 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
307 }(); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4389
diff changeset
308 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
309 return
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 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
312 }