view pkg/imports/fm_boylat.go @ 4938:9f9d72a1d398 fairway-marks-import

Save some typos and wrong parameter counts when adding new fairway mark types
author Tom Gottfried <tom@intevation.de>
date Mon, 17 Feb 2020 14:47:54 +0100
parents e41d42be0e13
children 39b67b910204
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"

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

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

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

// BOYLATJobKind is the import queue type identifier.
const BOYLATJobKind JobKind = "fm_boylat"

type boylatJobCreator struct{}

func init() {
	RegisterJobCreator(BOYLATJobKind, boylatJobCreator{})
}

func (boylatJobCreator) Description() string { return "fairway marks boylat" }

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

func (boylatJobCreator) Create() Job { return new(Boylat) }

func (boylatJobCreator) Depends() [2][]string {
	return [2][]string{
		{"fairway_marks_boylat"},
		{},
	}
}

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

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

type boylatProperties struct {
	fairwayMarksProperties
	Colour      *string `json:"hydro_colour"`
	Colpat      *string `json:"hydro_colpat"`
	Conrad      *int    `json:"hydro_conrad"`
	HydroMarsys *int64  `json:"hydro_marsys,omitempty"`
	IENCMarsys  *int64  `json:"ienc_marsys,omitempty"`
	Boyshp      *int    `json:"hydro_boyshp"`
	HydroCatlam *int64  `json:"hydro_catlam,omitempty"`
	IENCCatlam  *int64  `json:"ienc_catlam,omitempty"`
}

type boylatFeaturetype struct {
	geom  pointSlice
	props *boylatProperties
}

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

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

			feedback.Info("Store fairway marks of type BOYLAT")

			insertStmt, err := tx.PrepareContext(
				ctx,
				getFMInsertSQL("boylat", []string{
					"colour", "colpat", "conrad",
					"marsys", "boyshp", "catlam"}),
			)
			if err != nil {
				return
			}
			defer insertStmt.Close()

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

			for _, fm := range fms {

				f := fm.(*boylatFeaturetype)

				var marsys sql.NullInt64
				if f.props.HydroMarsys != nil {
					marsys = sql.NullInt64{
						Int64: *f.props.HydroMarsys, Valid: true}
				} else if f.props.IENCMarsys != nil {
					marsys = sql.NullInt64{
						Int64: *f.props.IENCMarsys, Valid: true}
				}

				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.Conrad,
						marsys,
						f.props.Boyshp,
						catlam,
					).Scan(&fmid)
					return err
				})
				switch {
				case err == sql.ErrNoRows:
					outsideOrDup++
					// ignore -> filtered by responsibility_areas
				case err != nil:
					feedback.Error(pgxutils.ReadableError{Err: err}.Error())
				default:
					features++
				}
			}
			return
		})

	return nil, err
}