comparison pkg/imports/wx.go @ 1679:2dc7768be0e4

Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 26 Dec 2018 21:01:29 +0100
parents 53304db85888
children de8089944b19
comparison
equal deleted inserted replaced
1678:8fdb57173e3f 1679:2dc7768be0e4
14 package imports 14 package imports
15 15
16 import ( 16 import (
17 "context" 17 "context"
18 "database/sql" 18 "database/sql"
19 "fmt"
20 "io"
21 "strings"
22 "time"
19 23
20 "gemma.intevation.de/gemma/pkg/common" 24 "gemma.intevation.de/gemma/pkg/common"
25 "gemma.intevation.de/gemma/pkg/wfs"
21 ) 26 )
22 27
23 type WaterwayAxis struct { 28 type WaterwayAxis struct {
24 URL string `json:"url"` 29 URL string `json:"url"`
25 FeatureType string `json:"feature-type"` 30 FeatureType string `json:"feature-type"`
66 importID int64, 71 importID int64,
67 conn *sql.Conn, 72 conn *sql.Conn,
68 feedback Feedback, 73 feedback Feedback,
69 ) (interface{}, error) { 74 ) (interface{}, error) {
70 75
71 // TODO: Implement me! 76 start := time.Now()
77
78 feedback.Info("Import waterway axis")
79
80 feedback.Info("Loading capabilities from %s", wx.URL)
81 caps, err := wfs.GetCapabilities(wx.URL)
82 if err != nil {
83 feedback.Error("Loading capabilities failed: %v", err)
84 return nil, err
85 }
86
87 ft := caps.FindFeatureType(wx.FeatureType)
88 if ft == nil {
89 err := fmt.Errorf("Unknown feature type '%s'", wx.FeatureType)
90 feedback.Error("%v", err)
91 return nil, err
92 }
93
94 urls, err := wfs.GetFeaturesGET(
95 caps, wx.FeatureType, "application/json", wx.SortBy)
96 if err != nil {
97 feedback.Error("Cannot create GetFeature URLs. %v", err)
98 return nil, err
99 }
100
101 var crsName string
102
103 unsupportedTypes := map[string]int{}
104
105 if err := wfs.DownloadURLs(urls, func(r io.Reader) error {
106 rfc, err := wfs.ParseRawFeatureCollection(r)
107 if err != nil {
108 return err
109 }
110 if crsName != "" && rfc.CRS != nil {
111 crsName = rfc.CRS.Properties.Name
112 }
113
114 // No features -> ignore.
115 if rfc.Features == nil {
116 return nil
117 }
118 for _, feature := range rfc.Features {
119 switch feature.Geometry.Type {
120 case "LineString":
121 // TODO: Parse concrete features.
122 case "MultiLineString":
123 // TODO: Parse concrete features.
124 default:
125 unsupportedTypes[feature.Geometry.Type]++
126 }
127 }
128 return nil
129 }); err != nil {
130 feedback.Error("Downloading features failed: %v", err)
131 return nil, err
132 }
133
134 if len(unsupportedTypes) != 0 {
135 var b strings.Builder
136 for t, c := range unsupportedTypes {
137 if b.Len() > 0 {
138 b.WriteString(", ")
139 }
140 b.WriteString(fmt.Sprintf("%s: %d", t, c))
141 }
142 feedback.Warn("Unsupported types found: %s", b.String())
143 }
144
145 if crsName == "" {
146 crsName = ft.DefaultCRS
147 }
148
149 epsg, err := wfs.CRSToEPSG(crsName)
150 if err != nil {
151 feedback.Error("Unsupported CRS name '%s'", crsName)
152 return nil, err
153 }
154
155 feedback.Info("using ESPG: %d", epsg)
156
157 // TODO: Store extracted features.
158
159 feedback.Info("Storing took %s", time.Since(start))
160
72 return nil, nil 161 return nil, nil
73 } 162 }