-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmmu.py
More file actions
160 lines (139 loc) · 8.27 KB
/
Copy pathmmu.py
File metadata and controls
160 lines (139 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
DESCRIPTION = "Minimum mapping unit"
IS_SYSTEM = False
log = logging.getLogger(__name__)
def run_check(params, status):
from qc_tool.vector.helper import ComplexChangeProperty
from qc_tool.vector.helper import create_pg_has_comment
from qc_tool.vector.helper import create_pg_neighbours
from qc_tool.vector.helper import do_layers
from qc_tool.vector.helper import get_failed_items_message
from qc_tool.vector.helper import MarginalProperty
from qc_tool.vector.helper import NeighbourTable
from qc_tool.vector.helper import PartitionedLayer
cursor = params["connection_manager"].get_connection().cursor()
for layer_def in do_layers(params):
log.debug("Started mmu check for the layer {:s}.".format(layer_def["pg_layer_name"]))
# Check for number of polygons in vector layer
sql_params = {"layer_name": layer_def["pg_layer_name"]}
sql = "SELECT EXISTS (SELECT 1 FROM {layer_name});"
sql = sql.format(**sql_params)
cursor.execute(sql)
any_polygon_in_vector = cursor.fetchone()[0]
if not any_polygon_in_vector:
status.info("There is no polygon to check in the {:s} layer.".format(layer_def["pg_layer_name"]))
continue
# Prepare support data.
partitioned_layer = PartitionedLayer(cursor.connection, layer_def["pg_layer_name"], layer_def["pg_fid_name"])
neighbour_table = NeighbourTable(partitioned_layer)
neighbour_table.make()
marginal_property = MarginalProperty(partitioned_layer)
marginal_property.make()
if params["complex_change"] is not None:
complex_change_property = ComplexChangeProperty(neighbour_table,
params["complex_change"]["initial_code_column_name"],
params["complex_change"]["final_code_column_name"],
params["complex_change"]["area_column_name"])
complex_change_property.make()
create_pg_neighbours(cursor.connection, neighbour_table.neighbour_table_name,
layer_def["pg_layer_name"],
layer_def["pg_fid_name"])
create_pg_has_comment(cursor.connection)
# Prepare parameters used in sql clauses.
sql_params = {"meta_table_name": marginal_property.meta_table.meta_table_name,
"layer_name": layer_def["pg_layer_name"],
"fid_name": layer_def["pg_fid_name"],
"general_table": "s{:02d}_{:s}_general".format(params["step_nr"], layer_def["pg_layer_name"]),
"exception_table": "s{:02d}_{:s}_exception".format(params["step_nr"], layer_def["pg_layer_name"]),
"warning_table": "s{:02d}_{:s}_warning".format(params["step_nr"], layer_def["pg_layer_name"]),
"error_table": "s{:02d}_{:s}_error".format(params["step_nr"], layer_def["pg_layer_name"]),
"general_where": "\n".join(params["general_where"]),
"exception_where": "\n".join(params["exception_where"]),
"warning_where": "\n".join(params["warning_where"])}
# Create table of general items.
sql = "CREATE TABLE {general_table} ({fid_name} integer PRIMARY KEY);"
sql = sql.format(**sql_params)
cursor.execute(sql)
sql = ("INSERT INTO {general_table}\n"
"SELECT layer.{fid_name}\n"
"FROM\n"
" {layer_name} AS layer\n"
" INNER JOIN {meta_table_name} AS meta ON layer.{fid_name} = meta.fid\n"
"WHERE {general_where};")
sql = sql.format(**sql_params)
cursor.execute(sql)
log.info("General table {:s} has been inserted {:d} items.".format(sql_params["general_table"], cursor.rowcount))
# Create table of exception items.
sql = ("CREATE TABLE {exception_table} ({fid_name} integer PRIMARY KEY);")
sql = sql.format(**sql_params)
cursor.execute(sql)
sql = ("INSERT INTO {exception_table}\n"
"SELECT layer.{fid_name}\n"
"FROM\n"
" {layer_name} AS layer\n"
" INNER JOIN {meta_table_name} AS meta ON layer.{fid_name} = meta.fid\n"
" LEFT JOIN {general_table} AS gen ON layer.{fid_name} = gen.{fid_name}\n"
"WHERE\n"
" gen.{fid_name} IS NULL\n"
" AND ({exception_where});")
sql = sql.format(**sql_params)
cursor.execute(sql)
log.info("Exception table {:s} has been inserted {:d} items.".format(sql_params["exception_table"], cursor.rowcount))
# Report exception items.
items_message = get_failed_items_message(cursor, sql_params["exception_table"], layer_def["pg_fid_name"])
if items_message is not None:
status.info("Layer {:s} has exception features with {:s}: {:s}."
.format(layer_def["pg_layer_name"], layer_def["fid_display_name"], items_message))
status.add_error_table(sql_params["exception_table"], layer_def["pg_layer_name"], layer_def["pg_fid_name"])
# Create table of warning items.
sql = ("CREATE TABLE {warning_table} ({fid_name} integer PRIMARY KEY);")
sql = sql.format(**sql_params)
cursor.execute(sql)
sql = ("INSERT INTO {warning_table}\n"
"SELECT layer.{fid_name}\n"
"FROM\n"
" {layer_name} AS layer\n"
" INNER JOIN {meta_table_name} AS meta ON layer.{fid_name} = meta.fid\n"
" LEFT JOIN {general_table} AS gen ON layer.{fid_name} = gen.{fid_name}\n"
" LEFT JOIN {exception_table} AS exc ON layer.{fid_name} = exc.{fid_name}\n"
"WHERE\n"
" gen.{fid_name} IS NULL\n"
" AND exc.{fid_name} IS NULL\n"
" AND ({warning_where});")
sql = sql.format(**sql_params)
cursor.execute(sql)
log.info("Warning table {:s} has been inserted {:d} items.".format(sql_params["warning_table"], cursor.rowcount))
# Report warning items.
items_message = get_failed_items_message(cursor, sql_params["warning_table"], layer_def["pg_fid_name"])
if items_message is not None:
status.info("Layer {:s} has warning features with {:s}: {:s}."
.format(layer_def["pg_layer_name"], layer_def["fid_display_name"], items_message))
status.add_error_table(sql_params["warning_table"], layer_def["pg_layer_name"], layer_def["pg_fid_name"])
# Create table of error items.
sql = ("CREATE TABLE {error_table} ({fid_name} integer PRIMARY KEY);")
sql = sql.format(**sql_params)
cursor.execute(sql)
sql = ("INSERT INTO {error_table}\n"
"SELECT layer.{fid_name}\n"
"FROM\n"
" {layer_name} AS layer\n"
" INNER JOIN {meta_table_name} AS meta ON layer.{fid_name} = meta.fid\n"
" LEFT JOIN {general_table} AS gen ON layer.{fid_name} = gen.{fid_name}\n"
" LEFT JOIN {exception_table} AS exc ON layer.{fid_name} = exc.{fid_name}\n"
" LEFT JOIN {warning_table} AS warn ON layer.{fid_name} = warn.{fid_name}\n"
"WHERE\n"
" gen.{fid_name} IS NULL\n"
" AND exc.{fid_name} IS NULL\n"
" AND warn.{fid_name} IS NULL;")
sql = sql.format(**sql_params)
cursor.execute(sql)
log.info("Error table {:s} has been inserted {:d} items.".format(sql_params["error_table"], cursor.rowcount))
# Report error items.
items_message = get_failed_items_message(cursor, sql_params["error_table"], layer_def["pg_fid_name"])
if items_message is not None:
status.failed("Layer {:s} has error features with {:s}: {:s}."
.format(layer_def["pg_layer_name"], layer_def["fid_display_name"], items_message))
status.add_error_table(sql_params["error_table"], layer_def["pg_layer_name"], layer_def["pg_fid_name"])
log.info("MMU check for the layer {:s} has been finished.".format(layer_def["pg_layer_name"]))