Mercurial > gemma
annotate pkg/controllers/shapestretches.go @ 4307:e5a831ecd557
Started to write multi polygon into shape file. [WIP]
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 02 Sep 2019 18:38:27 +0200 |
parents | 0f467a839fe2 |
children | 2c83d32bdd1b |
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 |
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 log.Printf("info: total number points: %d\n", n) |
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 return ps |
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 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
83 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
|
84 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
85 points := flattenPoints(mp) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
86 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
87 p := &shp.Polygon{} |
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 var numParts int |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
90 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
|
91 numParts += len(pl) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
92 } |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
93 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
94 log.Printf("info: number parts: %d\n", 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 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
|
97 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
|
98 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
|
99 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
100 var marker int32 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
101 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
102 for i, pl := range mp { |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
103 p.Parts[i] = marker |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
104 marker += int32(len(pl)) |
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 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
107 p.Points = points |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
108 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
|
109 return p |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
110 } |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
111 |
4305
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 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
|
113 vars := mux.Vars(req) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 name := vars["name"] |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 conn := middleware.GetDBConn(req) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 ctx := req.Context() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 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
|
120 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 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
|
123 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 return |
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 defer tx.Rollback() |
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 var ( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 id int64 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 lower, upper string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 data []byte |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
132 objnam string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
133 nobjnam sql.NullString |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
134 dateInfo time.Time |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
135 source string |
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 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
138 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
|
139 &id, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 &lower, &upper, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 &data, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
142 &objnam, &nobjnam, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 &dateInfo, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 &source, |
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 case err == sql.ErrNoRows: |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 http.NotFound(rw, req) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 case err != nil: |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 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
|
152 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 return |
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 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
156 var countries []string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
157 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 if err := func() error { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 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
|
160 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
161 return err |
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 defer rows.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
164 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
165 for rows.Next() { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
166 var country string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
167 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
|
168 return err |
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 countries = append(countries, country) |
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 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
173 return rows.Err() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 }(); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
175 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
176 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
|
177 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
178 return |
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 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
181 var geom wkb.MultiPolygonGeom |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
182 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
183 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
|
184 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
185 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
|
186 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
187 return |
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 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
190 tmp := config.TmpDir() |
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 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
|
193 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
194 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
195 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
|
196 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
197 return |
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 defer os.RemoveAll(dir) |
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 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
|
202 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
203 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
|
204 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
205 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
|
206 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
207 return |
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 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
210 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
|
211 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
212 if err := func() error { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
213 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
|
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 defer writer.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
218 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
219 // TODO: Write geometry |
4307
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
220 p := asShpPolygon(geom) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
221 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
222 writer.Write(p) |
4305
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 return nil |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
225 }(); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
226 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
227 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
|
228 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
229 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
230 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
231 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
232 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
|
233 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
|
234 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
235 return nil, err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
236 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
237 defer f.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
238 return f.Readdir(-1) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
239 }() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
240 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
241 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
242 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
|
243 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
244 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
245 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
246 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
247 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
|
248 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
|
249 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
250 zipfile := zip.NewWriter(rw) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
251 defer zipfile.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
252 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
253 now := time.Now() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
254 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
255 base := filepath.Base(shpDir) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
256 for _, info := range entries { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
257 if !info.Mode().IsRegular() { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
258 continue |
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 if err := func() error { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
261 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
|
262 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
263 return err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
264 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
265 defer srcFile.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
266 header := &zip.FileHeader{ |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
267 Name: base + "/" + info.Name(), |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
268 Method: zip.Deflate, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
269 Modified: now, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
270 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
271 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
|
272 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
273 return err |
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 _, 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
|
276 return err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
277 }(); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
278 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
|
279 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
280 } |
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 } |