annotate pkg/wfs/global.go @ 5490:5f47eeea988d logging

Use own logging package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Sep 2021 17:45:39 +0200
parents 16259efa828f
children 09346efa7f69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2019 by via donau
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package wfs
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
2711
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
17 "bufio"
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
18 "fmt"
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "io"
2711
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
20 "io/ioutil"
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
21 "os"
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "os/exec"
2711
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
23 "path/filepath"
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
24
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
25 "gemma.intevation.de/gemma/pkg/config"
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4990
diff changeset
26 "gemma.intevation.de/gemma/pkg/log"
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 )
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
2702
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
29 var (
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
30 // FormatGeoJSON is a list of MIME types of
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
31 // GeoJSON documents.
2702
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
32 FormatGeoJSON = []string{"geojson", "application/json"}
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
33 // FormatGML is a list of MIME types
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
34 // GML documents.
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
35 FormatGML = []string{
2711
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
36 "application/gml+xml; version=3.2",
2702
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
37 "gml2", "gml3", "gml32",
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
38 "text/xml; subtype=gml/2.1.2",
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
39 "text/xml; subtype=gml/3.1.1",
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
40 "text/xml; subtype=gml/3.2",
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
41 }
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
42 )
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
43
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 type (
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
45 // Downloader abstracts the idea of a download function for WFS.
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 Downloader interface {
2719
2b6f73c30016 WFS downloader: Add BasicAuth support for downloading. TODO: Use it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2711
diff changeset
47 Download(user, password string, handler func(string, io.Reader) error) error
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 }
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
50 // GeoJSONDownloader is a Downloader for GeoJSON.
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 GeoJSONDownloader []string
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
52 // GMLDownloader is a Downloader for GML.
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
53 GMLDownloader []string
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 )
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
56 // GetFeatures is the default Downloader in this Gemma server.
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 var GetFeatures = setup()
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 func setup() func(*Capabilities, string, string) (Downloader, error) {
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 path, err := exec.LookPath("ogr2ogr")
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 if err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4990
diff changeset
62 log.Infoln("ogr2ogr not installed. Using direct GeoJSON WFS download.")
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
63 return getFeaturesGeoJSON
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4990
diff changeset
65 log.Infof("ogr2ogr found at %s. Using GML WFS download.\n", path)
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
66 return getFeaturesGML
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 }
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
69 func getFeaturesGeoJSON(
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 caps *Capabilities,
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 featureTypeName string,
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 sortBy string,
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 ) (Downloader, error) {
2702
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
74 urls, err := GetFeaturesGET(
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
75 caps,
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
76 featureTypeName,
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
77 FormatGeoJSON,
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
78 sortBy)
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
79 return GeoJSONDownloader(urls), err
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 }
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
82 func getFeaturesGML(
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 caps *Capabilities,
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 featureTypeName string,
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 sortBy string,
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 ) (Downloader, error) {
2702
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
87 urls, err := GetFeaturesGET(
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
88 caps,
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
89 featureTypeName,
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
90 FormatGML,
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
91 sortBy)
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
92 return GMLDownloader(urls), err
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 }
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
95 // Download is the GeoJSON implementation of the Downloader.
2719
2b6f73c30016 WFS downloader: Add BasicAuth support for downloading. TODO: Use it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2711
diff changeset
96 func (gjd GeoJSONDownloader) Download(user, password string, handler func(string, io.Reader) error) error {
2b6f73c30016 WFS downloader: Add BasicAuth support for downloading. TODO: Use it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2711
diff changeset
97 return DownloadURLs(user, password, []string(gjd), handler)
2702
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
98 }
d7ef169fd0d3 WFS downloader: More on downloading GML. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2634
diff changeset
99
2711
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
100 func places(n int) int {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
101 places := 1
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
102 if n < 0 {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
103 places++
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
104 n = -n
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
105 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
106 for value := 10; n >= value; value *= 10 {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
107 places++
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
108 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
109 return places
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
110 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
111
4166
04876d865528 Made 'golint' and 'staticcheck' happy with wfs package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
112 // Download is GML implementaion of the Downloader.
2719
2b6f73c30016 WFS downloader: Add BasicAuth support for downloading. TODO: Use it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2711
diff changeset
113 func (gmd GMLDownloader) Download(user, password string, handler func(string, io.Reader) error) error {
2711
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
114
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
115 if len(gmd) == 0 {
4990
16259efa828f Dont try to download from WFS if they dont have features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4166
diff changeset
116 return nil
2711
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
117 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
118
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
119 tmpDir := config.TmpDir()
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
120 dlDir, err := ioutil.TempDir(tmpDir, "wfs-downloads")
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
121 if err != nil {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
122 return err
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
123 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
124 defer os.RemoveAll(dlDir)
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
125 digits := places(len(gmd))
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
126
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
127 var files []string
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
128
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
129 for i, url := range gmd {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
130 fname := filepath.Join(dlDir, fmt.Sprintf("%0*d.gml", digits, i))
2719
2b6f73c30016 WFS downloader: Add BasicAuth support for downloading. TODO: Use it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2711
diff changeset
131 if err := downloadURL(user, password, url, func(_ string, r io.Reader) error {
2711
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
132 f, err := os.Create(fname)
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
133 if err != nil {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
134 return err
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
135 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
136 if _, err = io.Copy(f, r); err != nil {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
137 f.Close()
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
138 return err
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
139 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
140 return f.Close()
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
141 }); err != nil {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
142 return err
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
143 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
144 files = append(files, fname)
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
145 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
146
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
147 out := filepath.Join(dlDir, "out.geojson")
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
148
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
149 cmd := exec.Command("ogr2ogr", append([]string{"-f", "GeoJSON", out}, files...)...)
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
150
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
151 msg, err := cmd.CombinedOutput()
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
152 if err != nil {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
153 return fmt.Errorf("ogr2ogr failed: %s", msg)
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
154 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
155
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
156 in, err := os.Open(out)
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
157 if err != nil {
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
158 return err
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
159 }
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
160 defer in.Close()
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
161
3956de9b6b32 WFS downloader: Implemented GML download to file, ogr2ogr to GeoJSON.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2702
diff changeset
162 return handler(gmd[0], bufio.NewReader(in))
2634
49ce3c11ca72 WFS downloads: Started with GML based downloads based on installation of ogr2ogr.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 }