annotate pkg/controllers/shapestretches.go @ 4305:0f467a839fe2

Moved stretches as shape file download to separate file.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 02 Sep 2019 18:01:45 +0200
parents
children e5a831ecd557
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 "log"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 "net/http"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 "os"
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 "path/filepath"
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"
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 FROM waterway.stretches WHERE
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 = `
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 SELECT country_code FROM waterway.stretch_countries
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 WHERE stretches_id = $1
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 ORDER BY country_code`
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 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
58 vars := mux.Vars(req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 name := vars["name"]
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 conn := middleware.GetDBConn(req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 ctx := req.Context()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 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
65 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 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
68 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 defer tx.Rollback()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 var (
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 id int64
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 lower, upper string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 data []byte
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 objnam string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 nobjnam sql.NullString
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 dateInfo time.Time
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 source string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 )
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 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
84 &id,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 &lower, &upper,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 &data,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 &objnam, &nobjnam,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 &dateInfo,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 &source,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 ); {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 case err == sql.ErrNoRows:
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 http.NotFound(rw, req)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 case err != nil:
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 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
97 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 var countries []string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 if err := func() error {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 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
105 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 defer rows.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 for rows.Next() {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 var country string
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 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
113 return err
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 countries = append(countries, country)
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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 return rows.Err()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 }(); 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
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 var geom wkb.MultiPolygonGeom
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 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
129 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 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
131 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 }
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 tmp := config.TmpDir()
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 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
138 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 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
141 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 return
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 defer os.RemoveAll(dir)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 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
147
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 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
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("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
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 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
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 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
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 writer.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 // TODO: Write geometry
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 return nil
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 }(); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 http.Error(
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 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
170 http.StatusInternalServerError)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 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
175 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
176 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 return nil, err
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 defer f.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 return f.Readdir(-1)
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 != 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("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
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 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
190 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
191
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192 zipfile := zip.NewWriter(rw)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 defer zipfile.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 now := time.Now()
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 base := filepath.Base(shpDir)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
198 for _, info := range entries {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
199 if !info.Mode().IsRegular() {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200 continue
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 := func() error {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
203 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
204 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205 return err
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 defer srcFile.Close()
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208 header := &zip.FileHeader{
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209 Name: base + "/" + info.Name(),
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
210 Method: zip.Deflate,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 Modified: now,
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
213 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
214 if err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
215 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
216 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
217 _, 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
218 return err
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
219 }(); err != nil {
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
220 log.Printf("error: cannot write file: %v\n", err)
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
221 return
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
222 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
223 }
0f467a839fe2 Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
224 }