annotate pkg/imports/fm.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
3 //
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
6 //
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2020 by via donau
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
10 //
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
11 // Author(s):
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
12 // * Tom Gottfried <tom.gottfried@intevation.de>
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
13
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
14 package imports
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
15
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
16 import (
4931
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
17 "context"
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
18 "database/sql"
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
19 "encoding/json"
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
20 "fmt"
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
21 "io"
4938
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
22 "strings"
4931
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
23 "time"
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
24
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
25 "gemma.intevation.de/gemma/pkg/wfs"
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
26 )
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
27
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
28 // FairwayMarks is a struct
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
29 // to be used as the basis for imports of
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
30 // specific types for fairway marks.
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
31 type FairwayMarks struct {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
32 // URL the GetCapabilities URL of the WFS service.
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
33 URL string `json:"url"`
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
34 // FeatureType selects the feature type of the WFS service.
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
35 FeatureType string `json:"feature-type"`
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
36 // SortBy works around misconfigured services to
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
37 // establish a sort order to get the features.
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
38 SortBy string `json:"sort-by"`
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
39 // User is an optional username for Basic Auth.
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
40 User string `json:"user,omitempty"`
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
41 // Password is an optional password for Basic Auth.
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
42 Password string `json:"password,omitempty"`
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
43 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
44
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
45 // Properties common to all types of fairway marks
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
46 type fairwayMarksProperties struct {
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
47 Datsta *string `json:"hydro_datsta"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
48 Datend *string `json:"hydro_datend"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
49 Persta *string `json:"hydro_persta"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
50 Perend *string `json:"hydro_perend"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
51 Objnam *string `json:"hydro_objnam"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
52 Nobjnm *string `json:"hydro_nobjnm"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
53 Inform *string `json:"hydro_inform"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
54 Ninfom *string `json:"hydro_ninfom"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
55 Scamin *int `json:"hydro_scamin"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
56 Picrep *string `json:"hydro_picrep"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
57 Txtdsc *string `json:"hydro_txtdsc"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
58 Sordat *string `json:"hydro_sordat"`
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
59 Sorind *string `json:"hydro_sorind"`
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
60 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
61
4938
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
62 const (
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
63 // Format string to be completed with type and additional attributes
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
64 insertFairwayMarkSQL = `
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
65 WITH a AS (
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
66 SELECT users.current_user_area_utm() AS a
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
67 )
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
68 INSERT INTO waterway.fairway_marks_%s (
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
69 geom,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
70 datsta,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
71 datend,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
72 persta,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
73 perend,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
74 objnam,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
75 nobjnm,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
76 inform,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
77 ninfom,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
78 scamin,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
79 picrep,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
80 txtdsc,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
81 sordat,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
82 sorind,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
83 %s
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
84 )
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
85 SELECT newfm, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
86 %s
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
87 FROM ST_Transform(ST_GeomFromWKB($1, $2::integer), 4326) AS newfm (newfm)
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
88 WHERE pg_has_role('sys_admin', 'MEMBER')
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
89 OR ST_Intersects((select a from a),
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
90 ST_Transform(newfm, (select ST_SRID(a) from a)))
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
91 ON CONFLICT (
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
92 CAST((geom,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
93 datsta, datend, persta, perend, objnam, nobjnm, inform, ninfom,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
94 scamin, picrep, txtdsc, sordat, sorind,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
95 0, %[2]s
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
96 ) AS waterway.fairway_marks_%[1]s)
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
97 )
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
98 DO NOTHING
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
99 RETURNING id
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
100 `
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
101 )
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
102
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
103 // Create INSERT statement for specific fairway marks type
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
104 func getFMInsertSQL(fmType string, attributes []string) string {
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
105 attNums := "$16"
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
106 for i := 1; i < len(attributes); i++ {
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
107 attNums += fmt.Sprintf(",$%d", 16+i)
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
108 }
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
109
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
110 return fmt.Sprintf(
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
111 insertFairwayMarkSQL,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
112 fmType,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
113 strings.Join(attributes, ","),
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
114 attNums,
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
115 )
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
116 }
9f9d72a1d398 Save some typos and wrong parameter counts when adding new fairway mark types
Tom Gottfried <tom@intevation.de>
parents: 4931
diff changeset
117
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
118 // Common operation of FM imports to get features from WFS service
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
119 func getFMFeatures(
4931
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
120 ctx context.Context,
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
121 conn *sql.Conn,
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
122 feedback Feedback,
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
123 fm FairwayMarks,
4906
21fea90f4002 Avoid unnecessary pointer to pointer
Tom Gottfried <tom@intevation.de>
parents: 4905
diff changeset
124 // Constructor returning pointer to struct
21fea90f4002 Avoid unnecessary pointer to pointer
Tom Gottfried <tom@intevation.de>
parents: 4905
diff changeset
125 // representing featuretype's properties
4905
8cb201b551b3 Replace bogus usage of pointer for type intrusion
Tom Gottfried <tom@intevation.de>
parents: 4904
diff changeset
126 newProps func() interface{},
4907
644cb7c175f3 Use proper type to represent featuretype
Tom Gottfried <tom@intevation.de>
parents: 4906
diff changeset
127 // Construct pointer to featuretype from given pointSlice and properties
644cb7c175f3 Use proper type to represent featuretype
Tom Gottfried <tom@intevation.de>
parents: 4906
diff changeset
128 newFeat func(pointSlice, interface{}) interface{},
4931
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
129 // Store features in type specific database tables
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
130 storeFMs func(
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
131 tx *sql.Tx,
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
132 epsg int,
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
133 // Slice of features to be converted to featuretypes type
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
134 fms []interface{},
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
135 ) (outsideOrDup int, features int, err error),
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
136 ) (err error) {
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
137
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
138 start := time.Now()
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
139
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
140 feedback.Info("Import fairway marks")
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
141
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
142 feedback.Info("Loading capabilities from %s", fm.URL)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
143 caps, err := wfs.GetCapabilities(fm.URL)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
144 if err != nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
145 feedback.Error("Loading capabilities failed: %v", err)
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
146 return
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
147 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
148
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
149 ft := caps.FindFeatureType(fm.FeatureType)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
150 if ft == nil {
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
151 err = fmt.Errorf("unknown feature type '%s'", fm.FeatureType)
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
152 return
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
153 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
154
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
155 feedback.Info("Found feature type '%s", fm.FeatureType)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
156
4931
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
157 epsg, err := wfs.CRSToEPSG(ft.DefaultCRS)
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
158 if err != nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
159 feedback.Error("Unsupported CRS: '%s'", ft.DefaultCRS)
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
160 return
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
161 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
162
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
163 if fm.SortBy != "" {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
164 feedback.Info("Features will be sorted by '%s'", fm.SortBy)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
165 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
166
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
167 dl, err := wfs.GetFeatures(caps, fm.FeatureType, fm.SortBy)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
168 if err != nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
169 feedback.Error("Cannot create GetFeature URLs. %v", err)
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
170 return
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
171 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
172
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
173 var (
4931
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
174 fms []interface{}
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
175 unsupported = stringCounter{}
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
176 missingProperties int
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
177 badProperties int
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
178 )
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
179
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
180 err = dl.Download(fm.User, fm.Password, func(url string, r io.Reader) error {
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
181 feedback.Info("Get features from: '%s'", url)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
182 rfc, err := wfs.ParseRawFeatureCollection(r)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
183 if err != nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
184 return fmt.Errorf("parsing GetFeature document failed: %v", err)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
185 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
186 if rfc.CRS != nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
187 crsName := rfc.CRS.Properties.Name
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
188 if epsg, err = wfs.CRSToEPSG(crsName); err != nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
189 feedback.Error("Unsupported CRS: %d", crsName)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
190 return err
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
191 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
192 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
193
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
194 // No features -> ignore.
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
195 if rfc.Features == nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
196 return nil
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
197 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
198
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
199 feedback.Info("Using EPSG: %d", epsg)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
200
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
201 for _, feature := range rfc.Features {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
202 if feature.Properties == nil || feature.Geometry.Coordinates == nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
203 missingProperties++
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
204 continue
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
205 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
206
4905
8cb201b551b3 Replace bogus usage of pointer for type intrusion
Tom Gottfried <tom@intevation.de>
parents: 4904
diff changeset
207 props := newProps()
4906
21fea90f4002 Avoid unnecessary pointer to pointer
Tom Gottfried <tom@intevation.de>
parents: 4905
diff changeset
208 if err := json.Unmarshal(*feature.Properties, props); err != nil {
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
209 badProperties++
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
210 continue
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
211 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
212
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
213 switch feature.Geometry.Type {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
214 case "Point":
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
215 var p pointSlice
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
216 if err := json.Unmarshal(*feature.Geometry.Coordinates, &p); err != nil {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
217 return err
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
218 }
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
219
4907
644cb7c175f3 Use proper type to represent featuretype
Tom Gottfried <tom@intevation.de>
parents: 4906
diff changeset
220 f := newFeat(p, props)
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
221 fms = append(fms, f)
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
222 default:
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
223 unsupported[feature.Geometry.Type]++
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
224 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
225 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
226 return nil
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
227 })
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
228 if err != nil {
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
229 return
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
230 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
231
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
232 if badProperties > 0 {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
233 feedback.Warn("Bad properties: %d", badProperties)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
234 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
235
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
236 if missingProperties > 0 {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
237 feedback.Warn("Missing properties: %d", missingProperties)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
238 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
239
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
240 if len(unsupported) != 0 {
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
241 feedback.Warn("Unsupported types found: %s", unsupported)
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
242 }
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
243
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
244 feedback.Info("Found %d usable features in data source", len(fms))
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
245
4931
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
246 tx, err := conn.BeginTx(ctx, nil)
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
247 if err != nil {
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
248 return
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
249 }
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
250 defer tx.Rollback()
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
251
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
252 outsideOrDup, features, err := storeFMs(tx, epsg, fms)
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
253 if err != nil {
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
254 return
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
255 }
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
256
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
257 if outsideOrDup > 0 {
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
258 feedback.Info(
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
259 "Features outside responsibility area and duplicates: %d",
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
260 outsideOrDup)
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
261 }
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
262
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
263 if features == 0 {
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
264 return UnchangedError("no valid new features found")
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
265 }
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
266
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
267 if err = tx.Commit(); err == nil {
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
268 feedback.Info("Storing %d features took %s",
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
269 features, time.Since(start))
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
270 }
e41d42be0e13 One more callback to avoid code duplication more consequently
Tom Gottfried <tom@intevation.de>
parents: 4907
diff changeset
271
4904
53d929f658f3 Separate code common to all types of fairway mark imports
Tom Gottfried <tom@intevation.de>
parents: 4895
diff changeset
272 return
4894
8eb36d0d5bdf Draft implementation of fairway marks import
Tom Gottfried <tom@intevation.de>
parents:
diff changeset
273 }