Mercurial > gemma
annotate pkg/controllers/shapestretches.go @ 4308:2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 02 Sep 2019 21:32:11 +0200 |
parents | e5a831ecd557 |
children | 78be85723eff |
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 return ps |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
79 } |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
80 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
81 func asShpPolygon(mp wkb.MultiPolygonGeom) *shp.Polygon { |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
82 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
83 points := flattenPoints(mp) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
84 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
85 var numParts int |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
86 for _, pl := range mp { |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
87 numParts += len(pl) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
88 } |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
89 |
4308
2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4307
diff
changeset
|
90 p := &shp.Polygon{} |
4307
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
91 p.NumParts = int32(numParts) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
92 p.NumPoints = int32(len(points)) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
93 p.Parts = make([]int32, numParts) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
94 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
95 var marker int32 |
4308
2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4307
diff
changeset
|
96 var i int |
2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4307
diff
changeset
|
97 for _, pl := range mp { |
2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4307
diff
changeset
|
98 for _, r := range pl { |
2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4307
diff
changeset
|
99 p.Parts[i] = marker |
2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4307
diff
changeset
|
100 i++ |
2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4307
diff
changeset
|
101 marker += int32(len(r)) |
2c83d32bdd1b
Fixed converting WKB to SHAPE polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4307
diff
changeset
|
102 } |
4307
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
103 } |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
104 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
105 p.Points = points |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
106 p.Box = shp.BBoxFromPoints(points) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
107 return p |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
108 } |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
109 |
4305
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
110 func stretchShapeDownload(rw http.ResponseWriter, req *http.Request) { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 vars := mux.Vars(req) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 name := vars["name"] |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 conn := middleware.GetDBConn(req) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 ctx := req.Context() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 rw, fmt.Sprintf("DB error: %v.", err), |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 defer tx.Rollback() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
125 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
126 var ( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 id int64 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 lower, upper string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 data []byte |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 objnam string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 nobjnam sql.NullString |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
132 dateInfo time.Time |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
133 source string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
134 ) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
135 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
136 switch err := tx.QueryRowContext(ctx, selectStretchSQL, name).Scan( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
137 &id, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
138 &lower, &upper, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
139 &data, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 &objnam, &nobjnam, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 &dateInfo, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
142 &source, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 ); { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 case err == sql.ErrNoRows: |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
145 http.NotFound(rw, req) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
146 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 case err != nil: |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 rw, fmt.Sprintf("DB error: %v.", err), |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
154 var countries []string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
155 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
156 if err := func() error { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
157 rows, err := tx.Query(selectStretchCountriesSQL, id) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 return err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
160 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
161 defer rows.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
162 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
163 for rows.Next() { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
164 var country string |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
165 if err := rows.Scan(&country); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
166 return err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
167 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
168 countries = append(countries, country) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
169 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
170 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
171 return rows.Err() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 }(); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
173 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 rw, fmt.Sprintf("DB error: %v.", err), |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
175 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
176 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
177 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
178 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
179 var geom wkb.MultiPolygonGeom |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
180 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
181 if err := geom.FromWKB(data); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
182 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
183 rw, fmt.Sprintf("Decoding WKB error: %v.", err), |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
184 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
185 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
186 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
187 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
188 tmp := config.TmpDir() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
189 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
190 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
|
191 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
192 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
193 rw, fmt.Sprintf("Cannot create temp dir: %v.", err), |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
194 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
195 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
196 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
197 defer os.RemoveAll(dir) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
198 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
199 shpDir := filepath.Join(dir, "stretch") |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
200 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
201 if err := os.Mkdir(shpDir, os.ModePerm); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
202 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
203 rw, fmt.Sprintf("Cannot create temp dir: %v.", err), |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
204 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
205 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
206 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
207 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
208 filename := filepath.Join(shpDir, "stretch") |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
209 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
210 if err := func() error { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
211 writer, err := shp.Create(filename, shp.POLYGON) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
212 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
213 return err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
214 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
215 defer writer.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
216 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
217 // 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
|
218 p := asShpPolygon(geom) |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
219 |
e5a831ecd557
Started to write multi polygon into shape file. [WIP]
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4305
diff
changeset
|
220 writer.Write(p) |
4305
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
221 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
222 return nil |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
223 }(); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
224 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
225 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
|
226 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
227 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
228 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
229 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
230 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
|
231 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
|
232 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
233 return nil, err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
234 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
235 defer f.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
236 return f.Readdir(-1) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
237 }() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
238 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
239 http.Error( |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
240 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
|
241 http.StatusInternalServerError) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
242 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
243 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
244 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
245 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
|
246 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
|
247 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
248 zipfile := zip.NewWriter(rw) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
249 defer zipfile.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
250 |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
251 now := time.Now() |
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 base := filepath.Base(shpDir) |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
254 for _, info := range entries { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
255 if !info.Mode().IsRegular() { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
256 continue |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
257 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
258 if err := func() error { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
259 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
|
260 if err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
261 return err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
262 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
263 defer srcFile.Close() |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
264 header := &zip.FileHeader{ |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
265 Name: base + "/" + info.Name(), |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
266 Method: zip.Deflate, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
267 Modified: now, |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
268 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
269 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
|
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 return err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
272 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
273 _, 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
|
274 return err |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
275 }(); err != nil { |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
276 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
|
277 return |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
278 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
279 } |
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
280 } |