comparison pkg/imports/wg.go @ 1835:f7b926440449

Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 16 Jan 2019 18:34:43 +0100
parents b4b9089c2d79
children 4dcdd8891770
comparison
equal deleted inserted replaced
1834:06d162ac0b9f 1835:f7b926440449
18 "database/sql" 18 "database/sql"
19 "errors" 19 "errors"
20 "fmt" 20 "fmt"
21 "log" 21 "log"
22 "strings" 22 "strings"
23 "time"
24
25 "github.com/jackc/pgx/pgtype"
23 26
24 "gemma.intevation.de/gemma/pkg/common" 27 "gemma.intevation.de/gemma/pkg/common"
25 "gemma.intevation.de/gemma/pkg/models" 28 "gemma.intevation.de/gemma/pkg/models"
26 "gemma.intevation.de/gemma/pkg/soap" 29 "gemma.intevation.de/gemma/pkg/soap"
27 "gemma.intevation.de/gemma/pkg/soap/erdms" 30 "gemma.intevation.de/gemma/pkg/soap/erdms"
85 WHERE gauge_id = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)` 88 WHERE gauge_id = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)`
86 89
87 deleteGaugeSQL = ` 90 deleteGaugeSQL = `
88 DELETE FROM waterway.gauges 91 DELETE FROM waterway.gauges
89 WHERE location = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)` 92 WHERE location = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)`
93
94 insertGaugeSQL = `
95 INSERT INTO waterway.gauges (
96 location,
97 objname,
98 geom,
99 applicability_from_km,
100 applicability_to_km,
101 validity,
102 zero_point,
103 geodref,
104 date_info,
105 source_organization
106 ) VALUES (
107 ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int),
108 $6,
109 ST_SetSRID(ST_MakePoint($7, $8), 4326)::geography,
110 $9,
111 $10,
112 $11,
113 $12,
114 $13,
115 $14,
116 $15
117 )`
90 ) 118 )
91 119
92 func (wg *WaterwayGauge) Do( 120 func (wg *WaterwayGauge) Do(
93 ctx context.Context, 121 ctx context.Context,
94 importID int64, 122 importID int64,
150 idx int 178 idx int
151 code *models.Isrs 179 code *models.Isrs
152 } 180 }
153 181
154 var news, olds []idxCode 182 var news, olds []idxCode
183
184 const layout = "2006-01-02T15:04:05.999-07:00"
155 185
156 for i, dr := range data.RisdataReturn { 186 for i, dr := range data.RisdataReturn {
157 if dr.RisidxCode == nil { 187 if dr.RisidxCode == nil {
158 ignored++ 188 ignored++
159 continue 189 continue
245 } 275 }
246 // treat them as new 276 // treat them as new
247 news = append(news, olds...) 277 news = append(news, olds...)
248 } 278 }
249 279
280 if len(news) == 0 {
281 return nil, errors.New("nothing to do")
282 }
283
284 insertStmt, err := tx.PrepareContext(ctx, insertGaugeSQL)
285 if err != nil {
286 return nil, err
287 }
288 defer insertStmt.Close()
289
290 // (re)-insert the gauges
291 for i := range news {
292 ic := &news[i]
293 dr := data.RisdataReturn[ic.idx]
294
295 var from, to sql.NullInt64
296
297 if dr.Applicabilityfromkm != nil {
298 from = sql.NullInt64{
299 Int64: int64(*dr.Applicabilityfromkm),
300 Valid: true,
301 }
302 }
303 if dr.Applicabilitytokm != nil {
304 to = sql.NullInt64{
305 Int64: int64(*dr.Applicabilitytokm),
306 Valid: true,
307 }
308 }
309
310 var tfrom, tto, dateInfo pgtype.Timestamptz
311
312 if dr.Startdate != nil {
313 tfrom = pgtype.Timestamptz{Time: time.Time(*dr.Startdate)}
314 } else {
315 tfrom = pgtype.Timestamptz{Status: pgtype.Null}
316 }
317
318 if dr.Enddate != nil {
319 tto = pgtype.Timestamptz{Time: time.Time(*dr.Enddate)}
320 } else {
321 tto = pgtype.Timestamptz{Status: pgtype.Null}
322 }
323
324 validity := pgtype.Tstzrange{
325 Lower: tfrom,
326 Upper: tto,
327 }
328
329 if dr.Infodate != nil {
330 dateInfo = pgtype.Timestamptz{Time: time.Time(*dr.Infodate)}
331 } else {
332 dateInfo = pgtype.Timestamptz{Status: pgtype.Null}
333 }
334
335 var geodref sql.NullString
336 if dr.Geodref != nil {
337 geodref = sql.NullString{String: string(*dr.Geodref), Valid: true}
338 }
339
340 var source sql.NullString
341 if dr.Source != nil {
342 source = sql.NullString{String: string(*dr.Source), Valid: true}
343 }
344
345 if _, err := insertStmt.ExecContext(ctx,
346 ic.code.CountryCode,
347 ic.code.LoCode,
348 ic.code.FairwaySection,
349 ic.code.Orc,
350 ic.code.Hectometre,
351 string(*dr.Objname.Loc),
352 int64(*dr.Lat), int64(*dr.Lon),
353 from,
354 to,
355 validity,
356 float64(*dr.Zeropoint),
357 geodref,
358 dateInfo,
359 source,
360 ); err != nil {
361 return nil, err
362 }
363
364 // TODO: Reference water levels.
365 }
366
250 // TODO: Implement me! 367 // TODO: Implement me!
251 return nil, errors.New("Not implemented, yet!") 368 return nil, errors.New("Not implemented, yet!")
252 } 369 }