view pkg/imports/fm_bcnlat.go @ 4941:df6c8a485979 fairway-marks-import

Improve handling of invalid dirimp values
author Tom Gottfried <tom@intevation.de>
date Mon, 17 Feb 2020 19:00:44 +0100
parents b3b2ba09a450
children
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2020 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Tom Gottfried <tom.gottfried@intevation.de>

package imports

import (
	"context"
	"database/sql"
	"strings"

	"gemma.intevation.de/gemma/pkg/pgxutils"
)

// Bcnlat is an import job to import
// fairway marks of type BCNLAT in form of point geometries
// and attribute data from a WFS service.
type Bcnlat struct {
	FairwayMarks
}

// Description gives a short info about relevant facts of this import.
func (bcnlat *Bcnlat) Description() (string, error) {
	return bcnlat.URL + "|" + bcnlat.FeatureType, nil
}

// BCNLATJobKind is the import queue type identifier.
const BCNLATJobKind JobKind = "fm_bcnlat"

type bcnlatJobCreator struct{}

func init() {
	RegisterJobCreator(BCNLATJobKind, bcnlatJobCreator{})
}

func (bcnlatJobCreator) Description() string { return "fairway marks bcnlat" }

func (bcnlatJobCreator) AutoAccept() bool { return true }

func (bcnlatJobCreator) Create() Job { return new(Bcnlat) }

func (bcnlatJobCreator) Depends() [2][]string {
	return [2][]string{
		{"fairway_marks_bcnlat"},
		{},
	}
}

// StageDone is a NOP for fairway marks imports.
func (bcnlatJobCreator) StageDone(context.Context, *sql.Tx, int64) error {
	return nil
}

// CleanUp for fairway marks imports is a NOP.
func (*Bcnlat) CleanUp() error { return nil }

type bcnlatProperties struct {
	fairwayMarksProperties
	Colour      *string `json:"hydro_colour"`
	Colpat      *string `json:"hydro_colpat"`
	Condtn      *int    `json:"hydro_condtn"`
	Bcnshp      *int    `json:"hydro_bcnshp"`
	HydroCatlam *int64  `json:"hydro_catlam,omitempty"`
	IENCCatlam  *int64  `json:"ienc_catlam,omitempty"`
	Dirimp      *string `json:"ienc_dirimp,omitempty"`
}

type bcnlatFeaturetype struct {
	geom  pointSlice
	props *bcnlatProperties
}

const (
	insertBcnlatDirimpSQL = `
INSERT INTO waterway.fairway_marks_bcnlat_dirimps (fm_bcnlat_id, dirimp)
  VALUES ($1, $2)
`
)

// Do executes the actual import.
func (fm *Bcnlat) Do(
	ctx context.Context,
	importID int64,
	conn *sql.Conn,
	feedback Feedback,
) (interface{}, error) {

	err := getFMFeatures(
		ctx,
		conn,
		feedback,
		fm.FairwayMarks,
		func() interface{} { return new(bcnlatProperties) },
		func(p pointSlice, props interface{}) interface{} {
			return &bcnlatFeaturetype{p, props.(*bcnlatProperties)}
		},
		func(
			tx *sql.Tx, epsg int, fms []interface{},
		) (outsideOrDup int, features int, err error) {

			feedback.Info("Store fairway marks of type BCNLAT/bcnlat")

			insertStmt, err := tx.PrepareContext(
				ctx,
				getFMInsertSQL("bcnlat",
					"colour", "colpat", "condtn", "bcnshp", "catlam"),
			)
			if err != nil {
				return
			}
			defer insertStmt.Close()

			insertBcnlatDirimpStmt, err := tx.PrepareContext(
				ctx, insertBcnlatDirimpSQL)
			if err != nil {
				return
			}
			defer insertBcnlatDirimpStmt.Close()

			savepoint := Savepoint(ctx, tx, "feature")

			for _, fm := range fms {

				f := fm.(*bcnlatFeaturetype)

				var catlam sql.NullInt64
				if f.props.HydroCatlam != nil {
					catlam = sql.NullInt64{
						Int64: *f.props.HydroCatlam, Valid: true}
				} else if f.props.IENCCatlam != nil {
					catlam = sql.NullInt64{
						Int64: *f.props.IENCCatlam, Valid: true}
				}

				var fmid int64
				err := savepoint(func() error {
					err := insertStmt.QueryRowContext(
						ctx,
						f.geom.asWKB(),
						epsg,
						f.props.Datsta,
						f.props.Datend,
						f.props.Persta,
						f.props.Perend,
						f.props.Objnam,
						f.props.Nobjnm,
						f.props.Inform,
						f.props.Ninfom,
						f.props.Scamin,
						f.props.Picrep,
						f.props.Txtdsc,
						f.props.Sordat,
						f.props.Sorind,
						f.props.Colour,
						f.props.Colpat,
						f.props.Condtn,
						f.props.Bcnshp,
						catlam,
					).Scan(&fmid)
					return err
				})
				switch {
				case err == sql.ErrNoRows:
					outsideOrDup++
					// ignore -> filtered by responsibility area or a duplicate
					// TODO: handle eventual changes to dirimp
				case err != nil:
					feedback.Error(pgxutils.ReadableError{Err: err}.Error())
				default:
					features++

					if f.props.Dirimp != nil && *f.props.Dirimp != "" {
						dirimps := strings.Split(*f.props.Dirimp, ",")
						for _, dirimp := range dirimps {
							if err := savepoint(func() error {
								_, err := insertBcnlatDirimpStmt.ExecContext(
									ctx, fmid, dirimp)
								return err
							}); err != nil {
								feedback.Warn(
									pgxutils.ReadableError{Err: err}.Error())
								feedback.Info(
									"Tried to import '%s' as dirimp value",
									dirimp)
							}
						}
					}
				}
			}
			return
		})

	return nil, err
}