annotate pkg/imports/wx.go @ 2726:dff749c07ae0

Warn if imported axis linestrings cross each other
author Tom Gottfried <tom@intevation.de>
date Tue, 19 Mar 2019 12:57:11 +0100
parents a10022399e24
children 7cb027be277d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
2649
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
7 // Copyright (C) 2018, 2019 by via donau
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
2649
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
13 // * Tom Gottfried <tom.gottfried@intevation.de>
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 package imports
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import (
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "context"
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "database/sql"
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
20 "encoding/json"
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
21 "errors"
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
22 "fmt"
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
23 "io"
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
24 "time"
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
26 "gemma.intevation.de/gemma/pkg/wfs"
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 )
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
1696
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
29 // WaterwayAxis is an import job to import
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
30 // the waterway axes in form of line string geometries
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
31 // and attribute data from a WFS service.
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 type WaterwayAxis struct {
1696
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
33 // URL the GetCapabilities URL of the WFS service.
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
34 URL string `json:"url"`
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
35 // FeatureType selects the feature type of the WFS service.
1677
53304db85888 Waterway axis import: Added route for manual import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1675
diff changeset
36 FeatureType string `json:"feature-type"`
1696
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
37 // SortBy works around misconfigured services to
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
38 // establish a sort order to get the features.
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
39 SortBy string `json:"sort-by"`
2723
a10022399e24 WFS downloads: Fetch user and password from config.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2721
diff changeset
40 // User is an optional username for Basic Auth.
a10022399e24 WFS downloads: Fetch user and password from config.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2721
diff changeset
41 User string `json:"user,omitempty"`
a10022399e24 WFS downloads: Fetch user and password from config.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2721
diff changeset
42 // Password is an optional password for Basic Auth.
a10022399e24 WFS downloads: Fetch user and password from config.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2721
diff changeset
43 Password string `json:"password,omitempty"`
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 }
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
1696
ad5e1cddaa09 Imports: Resolved the remaining golint issues with this package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1683
diff changeset
46 // WXJobKind is the import queue type identifier.
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 const WXJobKind JobKind = "wx"
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 type wxJobCreator struct{}
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 func init() {
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 RegisterJobCreator(WXJobKind, wxJobCreator{})
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 }
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54
1754
807569b08513 Import queue: Auto acceptance is now a property of the import kind itself and is not configurable any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1706
diff changeset
55 func (wxJobCreator) Description() string { return "waterway axis" }
807569b08513 Import queue: Auto acceptance is now a property of the import kind itself and is not configurable any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1706
diff changeset
56
807569b08513 Import queue: Auto acceptance is now a property of the import kind itself and is not configurable any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1706
diff changeset
57 func (wxJobCreator) AutoAccept() bool { return true }
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
2187
7c83b5277c1c Import queue: Removed boilerplate code to deserialize jobs from JSON by making it part of the import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2168
diff changeset
59 func (wxJobCreator) Create() Job { return new(WaterwayAxis) }
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 func (wxJobCreator) Depends() []string {
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 return []string{
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 "waterway_axis",
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 }
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 // StageDone is a NOP for waterway axis imports.
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 func (wxJobCreator) StageDone(context.Context, *sql.Tx, int64) error {
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 return nil
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1764
diff changeset
72 // CleanUp for waterway axis imports is a NOP.
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 func (*WaterwayAxis) CleanUp() error { return nil }
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
75 type waterwayAxisProperties struct {
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
76 ObjNam string `json:"hydro_objnam"`
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
77 NObjNnm *string `json:"hydro_nobjnm"`
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
78 }
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
79
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
80 const (
1795
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
81 deleteWaterwayAxisSQL = `
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
82 WITH resp AS (
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
83 SELECT best_utm(area::geometry) AS t,
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
84 ST_Transform(area::geometry, best_utm(area::geometry)) AS a
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
85 FROM users.responsibility_areas
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
86 WHERE country = users.current_user_country()
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
87 )
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
88 DELETE FROM waterway.waterway_axis
2649
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
89 WHERE pg_has_role('sys_admin', 'MEMBER')
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
90 OR ST_Covers((SELECT a FROM resp),
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
91 ST_Transform(wtwaxs::geometry, (SELECT t FROM resp)))
1795
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
92 `
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
93
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
94 checkCrossingAxisSQL = `
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
95 SELECT ST_AsText(ST_Intersection(new_line.wtwaxs, axis.wtwaxs))
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
96 FROM waterway.waterway_axis AS axis, waterway.waterway_axis AS new_line
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
97 WHERE new_line.id = $1 AND axis.id <> $1
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
98 AND ST_Crosses(new_line.wtwaxs::geometry, axis.wtwaxs::geometry)
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
99 `
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
100
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
101 insertWaterwayAxisSQL = `
1795
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
102 WITH resp AS (
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
103 SELECT best_utm(area::geometry) AS t,
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
104 ST_Transform(area::geometry, best_utm(area::geometry)) AS a
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
105 FROM users.responsibility_areas
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
106 WHERE country = users.current_user_country()
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
107 )
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
108 INSERT INTO waterway.waterway_axis (wtwaxs, objnam, nobjnam)
2649
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
109 SELECT ST_Transform(clipped.geom, 4326)::geography, $3, $4
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
110 FROM resp,
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
111 ST_Node(ST_Transform(
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
112 ST_GeomFromWKB($1, $2::integer), t)) AS new_line (new_line),
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
113 LATERAL (SELECT (ST_Dump(
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
114 CASE WHEN pg_has_role('sys_admin', 'MEMBER')
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
115 THEN new_line
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
116 ELSE ST_Intersection(a, new_line)
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
117 END
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
118 )).geom AS geom
9b6a02923bb4 Allow sys_admin to import waterway axis and area without clipping
Tom Gottfried <tom@intevation.de>
parents: 2622
diff changeset
119 ) AS clipped
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
120 RETURNING id
1795
1333f96f18d0 Waterway axis: Clip features against responsibility area of importing user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1785
diff changeset
121 `
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
122 )
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
123
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1764
diff changeset
124 // Do executes the actual waterway axis import.
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 func (wx *WaterwayAxis) Do(
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 ctx context.Context,
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 importID int64,
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 conn *sql.Conn,
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 feedback Feedback,
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 ) (interface{}, error) {
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
132 start := time.Now()
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
133
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
134 feedback.Info("Import waterway axis")
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
135
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
136 feedback.Info("Loading capabilities from %s", wx.URL)
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
137 caps, err := wfs.GetCapabilities(wx.URL)
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
138 if err != nil {
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
139 feedback.Error("Loading capabilities failed: %v", err)
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
140 return nil, err
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
141 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
142
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
143 ft := caps.FindFeatureType(wx.FeatureType)
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
144 if ft == nil {
1682
3c99d599503a Waterway axis import: small simplifications.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1681
diff changeset
145 return nil, fmt.Errorf("Unknown feature type '%s'", wx.FeatureType)
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
146 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
147
1706
fb05027d93b6 Waterway axis: Be more verbose during import about feature type and sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1696
diff changeset
148 feedback.Info("Found feature type '%s", wx.FeatureType)
fb05027d93b6 Waterway axis: Be more verbose during import about feature type and sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1696
diff changeset
149
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
150 epsg, err := wfs.CRSToEPSG(ft.DefaultCRS)
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
151 if err != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
152 feedback.Error("Unsupported CRS name '%s'", ft.DefaultCRS)
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
153 return nil, err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
154 }
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
155
1706
fb05027d93b6 Waterway axis: Be more verbose during import about feature type and sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1696
diff changeset
156 if wx.SortBy != "" {
fb05027d93b6 Waterway axis: Be more verbose during import about feature type and sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1696
diff changeset
157 feedback.Info("Features will be sorted by '%s'", wx.SortBy)
fb05027d93b6 Waterway axis: Be more verbose during import about feature type and sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1696
diff changeset
158 }
fb05027d93b6 Waterway axis: Be more verbose during import about feature type and sorting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1696
diff changeset
159
2721
56c8ef515d93 WFS downloader: Use new GML/GeoJSON factory function in gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
160 dl, err := wfs.GetFeatures(caps, wx.FeatureType, wx.SortBy)
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
161 if err != nil {
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
162 feedback.Error("Cannot create GetFeature URLs. %v", err)
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
163 return nil, err
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
164 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
165
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
166 tx, err := conn.BeginTx(ctx, nil)
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
167 if err != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
168 return nil, err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
169 }
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
170 defer tx.Rollback()
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
171
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
172 insertStmt, err := tx.PrepareContext(ctx, insertWaterwayAxisSQL)
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
173 if err != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
174 return nil, err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
175 }
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
176 defer insertStmt.Close()
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
177
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
178 checkCrossingStmt, err := tx.PrepareContext(ctx, checkCrossingAxisSQL)
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
179 if err != nil {
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
180 return nil, err
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
181 }
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
182 defer checkCrossingStmt.Close()
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
183
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
184 // Delete the old features.
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
185 if _, err := tx.ExecContext(ctx, deleteWaterwayAxisSQL); err != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
186 return nil, err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
187 }
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
188
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
189 var (
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1764
diff changeset
190 unsupported = stringCounter{}
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
191 missingProperties int
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
192 badProperties int
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
193 features int
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
194 )
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
195
2719
2b6f73c30016 WFS downloader: Add BasicAuth support for downloading. TODO: Use it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2649
diff changeset
196 // TODO: Fetch from parameters.
2b6f73c30016 WFS downloader: Add BasicAuth support for downloading. TODO: Use it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2649
diff changeset
197 user, password := "", ""
2b6f73c30016 WFS downloader: Add BasicAuth support for downloading. TODO: Use it.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2649
diff changeset
198
2721
56c8ef515d93 WFS downloader: Use new GML/GeoJSON factory function in gemma.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2719
diff changeset
199 if err := dl.Download(user, password, func(url string, r io.Reader) error {
2106
2b72f5e005aa WFS imports: Write get GetFeature URLs into import log.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1828
diff changeset
200 feedback.Info("Get features from: '%s'", url)
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
201 rfc, err := wfs.ParseRawFeatureCollection(r)
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
202 if err != nil {
1764
bb4348ac52ab Waterway axis import: Be more verbose when parsing the GetFeature document fails.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1754
diff changeset
203 return fmt.Errorf("parsing GetFeature document failed: %v", err)
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
204 }
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
205 if rfc.CRS != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
206 crsName := rfc.CRS.Properties.Name
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
207 if epsg, err = wfs.CRSToEPSG(crsName); err != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
208 feedback.Error("Unsupported CRS: %d", crsName)
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
209 return err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
210 }
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
211 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
212
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
213 // No features -> ignore.
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
214 if rfc.Features == nil {
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
215 return nil
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
216 }
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
217
1683
509e8728e846 Fixed pre-calculated size of 2D WKB line strings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1682
diff changeset
218 feedback.Info("Using EPSG: %d", epsg)
509e8728e846 Fixed pre-calculated size of 2D WKB line strings.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1682
diff changeset
219
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
220 for _, feature := range rfc.Features {
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
221 if feature.Properties == nil || feature.Geometry.Coordinates == nil {
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
222 missingProperties++
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
223 continue
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
224 }
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
225
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
226 var props waterwayAxisProperties
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
227
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
228 if err := json.Unmarshal(*feature.Properties, &props); err != nil {
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
229 badProperties++
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
230 continue
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
231 }
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
232
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
233 var nobjnam sql.NullString
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
234 if props.NObjNnm != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
235 nobjnam = sql.NullString{String: *props.NObjNnm, Valid: true}
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
236 }
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
237
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
238 switch feature.Geometry.Type {
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
239 case "LineString":
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1764
diff changeset
240 var l lineSlice
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
241 if err := json.Unmarshal(*feature.Geometry.Coordinates, &l); err != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
242 return err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
243 }
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
244 if err := storeLinestring(
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
245 ctx,
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
246 tx,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
247 feedback,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
248 l,
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
249 epsg,
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
250 props,
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
251 nobjnam,
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
252 checkCrossingStmt,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
253 insertStmt); err != nil {
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
254 return err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
255 }
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
256 features++
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
257 case "MultiLineString":
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1764
diff changeset
258 var ls []lineSlice
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
259 if err := json.Unmarshal(*feature.Geometry.Coordinates, &ls); err != nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
260 return err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
261 }
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
262 for _, l := range ls {
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
263 if err := storeLinestring(
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
264 ctx,
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
265 tx,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
266 feedback,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
267 l,
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
268 epsg,
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
269 props,
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
270 nobjnam,
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
271 checkCrossingStmt,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
272 insertStmt); err != nil {
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
273 return err
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
274 }
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
275 features++
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
276 }
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
277 default:
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1764
diff changeset
278 unsupported[feature.Geometry.Type]++
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
279 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
280 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
281 return nil
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
282 }); err != nil {
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
283 return nil, err
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
284 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
285
1680
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
286 if badProperties > 0 {
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
287 feedback.Warn("Bad properties: %d", badProperties)
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
288 }
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
289
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
290 if missingProperties > 0 {
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
291 feedback.Warn("Missing properties: %d", missingProperties)
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
292 }
de8089944b19 Waterway axis import: Started extracting properties from the features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1679
diff changeset
293
1785
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1764
diff changeset
294 if len(unsupported) != 0 {
614c6c766691 Waterway area import: Implemented.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1764
diff changeset
295 feedback.Warn("Unsupported types found: %s", unsupported)
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
296 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
297
1828
1ecfcf46e4da Waterway area/axis: Be more informative if features are bad.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1796
diff changeset
298 if features == 0 {
1ecfcf46e4da Waterway area/axis: Be more informative if features are bad.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1796
diff changeset
299 err := errors.New("No features found")
1ecfcf46e4da Waterway area/axis: Be more informative if features are bad.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1796
diff changeset
300 feedback.Error("%v", err)
1ecfcf46e4da Waterway area/axis: Be more informative if features are bad.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1796
diff changeset
301 return nil, err
1ecfcf46e4da Waterway area/axis: Be more informative if features are bad.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1796
diff changeset
302 }
1ecfcf46e4da Waterway area/axis: Be more informative if features are bad.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1796
diff changeset
303
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
304 if err = tx.Commit(); err == nil {
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
305 feedback.Info("Storing %d features took %s",
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
306 features, time.Since(start))
1679
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
307 }
2dc7768be0e4 Waterway axis import: More on reading data from WFS. TODO: Parse to concrete features.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1677
diff changeset
308
1681
4d6ce621379e Waterway axis import: Completed, but the final commit unexpectedly resulted in rollback.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1680
diff changeset
309 return nil, err
1675
8fec3887c7e5 Waterway axis import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
310 }
2726
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
311
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
312 func storeLinestring(
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
313 ctx context.Context,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
314 tx *sql.Tx,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
315 feedback Feedback,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
316 l lineSlice,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
317 epsg int,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
318 props waterwayAxisProperties,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
319 nobjnam sql.NullString,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
320 checkCrossingStmt, insertStmt *sql.Stmt,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
321 ) error {
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
322 var id int
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
323 if err := insertStmt.QueryRowContext(
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
324 ctx,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
325 l.asWKB(),
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
326 epsg,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
327 props.ObjNam,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
328 nobjnam,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
329 ).Scan(&id); err != nil {
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
330 return err
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
331 }
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
332
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
333 var crossing string
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
334 switch err := checkCrossingStmt.QueryRowContext(
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
335 ctx,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
336 id,
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
337 ).Scan(&crossing); {
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
338 case err != nil && err != sql.ErrNoRows:
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
339 return err
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
340 case err == nil:
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
341 feedback.Warn(
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
342 "Linestring %d crosses previously imported linestring near %s. Finding a contiguous axis may not work here",
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
343 id, crossing)
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
344 }
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
345 return nil
dff749c07ae0 Warn if imported axis linestrings cross each other
Tom Gottfried <tom@intevation.de>
parents: 2723
diff changeset
346 }