Mercurial > gemma
changeset 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 | 06d162ac0b9f |
children | 4dcdd8891770 |
files | pkg/imports/wg.go |
diffstat | 1 files changed, 117 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/imports/wg.go Wed Jan 16 18:12:41 2019 +0100 +++ b/pkg/imports/wg.go Wed Jan 16 18:34:43 2019 +0100 @@ -20,6 +20,9 @@ "fmt" "log" "strings" + "time" + + "github.com/jackc/pgx/pgtype" "gemma.intevation.de/gemma/pkg/common" "gemma.intevation.de/gemma/pkg/models" @@ -87,6 +90,31 @@ deleteGaugeSQL = ` DELETE FROM waterway.gauges WHERE location = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)` + + insertGaugeSQL = ` +INSERT INTO waterway.gauges ( + location, + objname, + geom, + applicability_from_km, + applicability_to_km, + validity, + zero_point, + geodref, + date_info, + source_organization +) VALUES ( + ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int), + $6, + ST_SetSRID(ST_MakePoint($7, $8), 4326)::geography, + $9, + $10, + $11, + $12, + $13, + $14, + $15 +)` ) func (wg *WaterwayGauge) Do( @@ -153,6 +181,8 @@ var news, olds []idxCode + const layout = "2006-01-02T15:04:05.999-07:00" + for i, dr := range data.RisdataReturn { if dr.RisidxCode == nil { ignored++ @@ -247,6 +277,93 @@ news = append(news, olds...) } + if len(news) == 0 { + return nil, errors.New("nothing to do") + } + + insertStmt, err := tx.PrepareContext(ctx, insertGaugeSQL) + if err != nil { + return nil, err + } + defer insertStmt.Close() + + // (re)-insert the gauges + for i := range news { + ic := &news[i] + dr := data.RisdataReturn[ic.idx] + + var from, to sql.NullInt64 + + if dr.Applicabilityfromkm != nil { + from = sql.NullInt64{ + Int64: int64(*dr.Applicabilityfromkm), + Valid: true, + } + } + if dr.Applicabilitytokm != nil { + to = sql.NullInt64{ + Int64: int64(*dr.Applicabilitytokm), + Valid: true, + } + } + + var tfrom, tto, dateInfo pgtype.Timestamptz + + if dr.Startdate != nil { + tfrom = pgtype.Timestamptz{Time: time.Time(*dr.Startdate)} + } else { + tfrom = pgtype.Timestamptz{Status: pgtype.Null} + } + + if dr.Enddate != nil { + tto = pgtype.Timestamptz{Time: time.Time(*dr.Enddate)} + } else { + tto = pgtype.Timestamptz{Status: pgtype.Null} + } + + validity := pgtype.Tstzrange{ + Lower: tfrom, + Upper: tto, + } + + if dr.Infodate != nil { + dateInfo = pgtype.Timestamptz{Time: time.Time(*dr.Infodate)} + } else { + dateInfo = pgtype.Timestamptz{Status: pgtype.Null} + } + + var geodref sql.NullString + if dr.Geodref != nil { + geodref = sql.NullString{String: string(*dr.Geodref), Valid: true} + } + + var source sql.NullString + if dr.Source != nil { + source = sql.NullString{String: string(*dr.Source), Valid: true} + } + + if _, err := insertStmt.ExecContext(ctx, + ic.code.CountryCode, + ic.code.LoCode, + ic.code.FairwaySection, + ic.code.Orc, + ic.code.Hectometre, + string(*dr.Objname.Loc), + int64(*dr.Lat), int64(*dr.Lon), + from, + to, + validity, + float64(*dr.Zeropoint), + geodref, + dateInfo, + source, + ); err != nil { + return nil, err + } + + // TODO: Reference water levels. + } + // TODO: Implement me! return nil, errors.New("Not implemented, yet!") }