-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpgtopoeditor.py
479 lines (394 loc) · 18.1 KB
/
pgtopoeditor.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
"""
/***************************************************************************
PgTopoEditor
A QGIS plugin
Edit toolbar for PostGIS topology primitives (ISO SQL/MM based)
-------------------
begin : 2011-10-21
copyright : (C) 2011-2016 by Sandro Santilli <[email protected]>
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
# Import the PyQt and QGIS libraries
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QMessageBox, QProgressBar
from qgis.core import Qgis, QgsDataSourceUri
from qgis.gui import QgsMessageBar
# Initialize Qt resources from file resources.py
from . import resources
# Import the code for the dialog
from .pgtopoeditordialog import PgTopoEditorDialog
import psycopg2
def getIntAttributeByIndex(feature, index):
# QMessageBox.information(None, '?', "Version of qgis is " + str(QGis.QGIS_VERSION_INT))
if Qgis.QGIS_VERSION_INT < 10900:
return feature[index].toInt()[0]
else:
return feature[index]
class PgTopoEditor:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
def initGui(self):
self.toolbar = self.iface.addToolBar('PostGIS Topology Edit Toolbar');
self.toolbar.setObjectName('PostGIS_Topology_Edit_Toolbar');
# Create action for selecting dangling edges
action = QAction(QIcon(":/plugins/pgtopoeditor/icons/seldanglingedge.png"), \
"Select dangling edges", self.iface.mainWindow())
action.triggered.connect(self.doSelDanglingEdges)
self.toolbar.addAction(action)
# Create action for selecting edge's left ring
action = QAction(QIcon(":/plugins/pgtopoeditor/icons/selringleft.png"), \
"Select left ring", self.iface.mainWindow())
action.triggered.connect(self.doSelLeftRing)
self.toolbar.addAction(action)
# Create action for selecting edge's right ring
action = QAction(QIcon(":/plugins/pgtopoeditor/icons/selringright.png"), \
"Select right ring", self.iface.mainWindow())
action.triggered.connect(self.doSelRightRing)
self.toolbar.addAction(action)
# Create action for ST_RemEdgeModFace
action = QAction(QIcon(":/plugins/pgtopoeditor/icons/remedge.png"), \
"Remove selected edges", self.iface.mainWindow())
action.triggered.connect(self.doRemEdgeModFace)
self.toolbar.addAction(action)
# Create action for ST_ModEdgeHeal
action = QAction(QIcon(":/plugins/pgtopoeditor/icons/healedge.png"), \
"Remove selected nodes", self.iface.mainWindow())
action.triggered.connect(self.doRemoveNode)
self.toolbar.addAction(action)
# Create action for collecting orphaned topogeom
self.action_gctgeom = QAction(QIcon(":/plugins/pgtopoeditor/icons/gctgeom.png"), \
"Collect orphaned TopoGeoms", self.iface.mainWindow())
self.action_gctgeom.triggered.connect(self.doDropOrphanedTopoGeoms)
self.toolbar.addAction(self.action_gctgeom)
def unload(self):
# Remove the plugin menu item and icons
del self.toolbar
def requireEdgeLayerSelected(self, toolname):
# check that a layer is selected
layer = self.iface.activeLayer()
if not layer:
QMessageBox.information(None, toolname, "A topology edge layer must be selected")
return
# check that the selected layer is a postgis one
if layer.providerType() != 'postgres':
QMessageBox.information(None, toolname, "A PostGIS layer must be selected")
return
return layer
# Select dangling edges
def doSelDanglingEdges(self):
toolname = "SelectDanglingEdges"
# check that a layer is selected
layer = self.iface.activeLayer()
if not layer:
QMessageBox.information(None, toolname, "A topology edge layer must be selected")
return
# check that the selected layer is a postgis one
if layer.providerType() != 'postgres':
QMessageBox.information(None, toolname, "A PostGIS layer must be selected")
return
uri = QgsDataSourceUri(layer.source())
# get the layer schema
toponame = str(uri.schema())
if not toponame:
QMessageBox.information(None, toolname, "Layer " + layer.name() + " doesn't look like a topology edge layer.\n(no schema set in datasource)")
return;
edge_id_fno = layer.dataProvider().fieldNameIndex('edge_id')
if ( edge_id_fno < 0 ):
QMessageBox.information(None, toolname, "Layer " + layer.name() + " does not have an 'edge_id' field (not a topology edge layer?)")
return
# find dangling edges
conn = psycopg2.connect( str(uri.connectionInfo()) )
cur = conn.cursor()
#cur.execute('SELECT edge_id FROM "' + toponame + '".edge_data LIMIT 1')
cur.execute('''
select distinct (array_agg(e.edge_id))[1] eid
from "''' + toponame + '''".edge_data e, "''' + toponame + '''".node n
where ( n.node_id = e.start_node or n.node_id = e.end_node )
and e.start_node != e.end_node
group by n.node_id
having count(e.edge_id) = 1''')
fids = [r[0] for r in cur.fetchall()]
cur.close()
conn.close()
layer.select( fids )
# layer.select seems to automatically refresh canvas
#self.iface.mapCanvas().refresh()
# Select left ring of edge
def doSelLeftRing(self):
toolname = "SelectLeftRing"
# check that a layer is selected
layer = self.requireEdgeLayerSelected(toolname)
if not layer:
return
uri = QgsDataSourceUri(layer.source())
# get the layer schema
toponame = str(uri.schema())
if not toponame:
QMessageBox.information(None, toolname, "Layer " + layer.name() + " doesn't look like a topology edge layer.\n(no schema set in datasource)")
return
edge_id_fno = layer.dataProvider().fieldNameIndex('edge_id')
if ( edge_id_fno < 0 ):
QMessageBox.information(None, toolname, "Layer " + layer.name() + " does not have an 'edge_id' field (not a topology edge layer?)")
return
# get the selected features
errors = []
selected = layer.selectedFeatures()
if not selected:
QMessageBox.information(None, toolname, "Select the edge you want the left ring of")
return
if len(selected) > 1:
QMessageBox.information(None, toolname, "Select a single edge")
return
feature = selected[0]
edge_id = getIntAttributeByIndex(feature, edge_id_fno)
# find left ring
conn = psycopg2.connect( str(uri.connectionInfo()) )
cur = conn.cursor()
cur.execute("select distinct abs(edge) from GetRingEdges('{toponame}',{edge_id})".
format(toponame=toponame, edge_id=edge_id)
)
fids = [r[0] for r in cur.fetchall()]
cur.close()
conn.close()
layer.select( fids )
# Select right ring of edge
def doSelRightRing(self):
toolname = "SelectRightRing"
# check that a layer is selected
layer = self.requireEdgeLayerSelected(toolname)
if not layer:
return
uri = QgsDataSourceUri(layer.source())
# get the layer schema
toponame = str(uri.schema())
if not toponame:
QMessageBox.information(None, toolname, "Layer " + layer.name() + " doesn't look like a topology edge layer.\n(no schema set in datasource)")
return
edge_id_fno = layer.dataProvider().fieldNameIndex('edge_id')
if ( edge_id_fno < 0 ):
QMessageBox.information(None, toolname, "Layer " + layer.name() + " does not have an 'edge_id' field (not a topology edge layer?)")
return
# get the selected features
errors = []
selected = layer.selectedFeatures()
if not selected:
QMessageBox.information(None, toolname, "Select the edge you want the left ring of")
return
if len(selected) > 1:
QMessageBox.information(None, toolname, "Select a single edge")
return
feature = selected[0]
edge_id = getIntAttributeByIndex(feature, edge_id_fno)
# find right ring
conn = psycopg2.connect( str(uri.connectionInfo()) )
cur = conn.cursor()
cur.execute("select distinct abs(edge) from GetRingEdges('{toponame}',{edge_id})".
format(toponame=toponame, edge_id=-edge_id)
)
fids = [r[0] for r in cur.fetchall()]
cur.close()
conn.close()
layer.select( fids )
# Remove selected edge
def doRemEdgeModFace(self):
toolname = "RemoveEdge"
# check that a layer is selected
layer = self.iface.mapCanvas().currentLayer()
if not layer:
QMessageBox.information(None, toolname, "A topology edge layer must be selected")
return
# check that the selected layer is a postgis one
if layer.providerType() != 'postgres':
QMessageBox.information(None, toolname, "A PostGIS layer must be selected")
return
uri = QgsDataSourceUri(layer.source())
# get the layer schema
toponame = str(uri.schema())
if not toponame:
QMessageBox.information(None, toolname, "Layer " + layer.name() + " doesn't look like a topology edge layer.\n(no schema set in datasource)")
return;
edge_id_fno = layer.dataProvider().fieldNameIndex('edge_id')
if ( edge_id_fno < 0 ):
QMessageBox.information(None, toolname, "Layer " + layer.name() + " does not have an 'edge_id' field (not a topology edge layer?)")
return
# get the selected features
errors = []
selected = layer.selectedFeatures()
if not selected:
QMessageBox.information(None, toolname, "Select the edge(s) you want to remove")
return
msgBar = self.iface.messageBar()
pb = QProgressBar( msgBar )
msgBar.pushWidget( pb, Qgis.Info, 5 )
pb.setRange( 0, len(selected) )
pb.setValue( 0 )
conn = psycopg2.connect( str(uri.connectionInfo()) )
for feature in selected:
pb.setValue(pb.value()+1)
# get its edge_id
edge_id = getIntAttributeByIndex(feature, edge_id_fno)
try:
cur = conn.cursor()
cur.execute("SELECT topology.ST_RemEdgeModFace(%s, %s)", (toponame, edge_id))
conn.commit()
cur.close()
except psycopg2.Error as e:
errors.append("Removing edge " + str(edge_id) + ":\n" + str(e))
conn.commit()
cur.close()
continue
conn.close()
removed = len(selected) - len(errors)
report = "Removed " + str(removed) + " edges over " + str(len(selected)) + " selected\n"
if errors:
report += "\nFailures ("
if len(errors) > 5:
report += "first 5 of "
report += str(len(errors)) + "):\n\n" + "\n".join(errors[:5])
QMessageBox.information(None, toolname, report)
layer.removeSelection()
self.iface.mapCanvas().refresh()
# Remove selected nodes (of degree 2 or isolated)
def doRemoveNode(self):
toolname = "RemoveNode"
# check that a layer is selected
layer = self.iface.mapCanvas().currentLayer()
if not layer:
QMessageBox.information(None, toolname, "A topology node layer must be selected")
return
# check that the selected layer is a postgis one
if layer.providerType() != 'postgres':
QMessageBox.information(None, toolname, "A PostGIS layer must be selected")
return
uri = QgsDataSourceUri(layer.source())
# get the layer schema
toponame = str(uri.schema())
if not toponame:
QMessageBox.information(None, toolname, "Layer " + layer.name() + " doesn't look like a topology node layer.\n(no schema set in datasource)")
return;
node_id_fno = layer.dataProvider().fieldNameIndex('node_id')
if ( node_id_fno < 0 ):
QMessageBox.information(None, toolname, "Layer " + layer.name() + " does not have an 'node_id' field (not a topology node layer?)")
return
# get the selected features
errors = []
selected = layer.selectedFeatures()
if not selected:
QMessageBox.information(None, toolname, "Select the node(s) you want to remove")
return
msgBar = self.iface.messageBar()
pb = QProgressBar( msgBar )
msgBar.pushWidget( pb, Qgis.Info, 5 )
pb.setRange( 0, len(selected) )
pb.setValue( 0 )
conn = psycopg2.connect( str(uri.connectionInfo()) )
for feature in selected:
pb.setValue(pb.value()+1)
# get its node_id
node_id = getIntAttributeByIndex(feature, node_id_fno)
try:
cur = conn.cursor()
cur.execute("SELECT abs((topology.GetNodeEdges(%s, %s)).edge)", (toponame, node_id))
if cur.rowcount == 2:
(edge1_id) = cur.fetchone()
(edge2_id) = cur.fetchone()
cur.close()
if edge1_id != edge2_id:
cur = conn.cursor()
cur.execute("SELECT topology.ST_ModEdgeHeal(%s, %s, %s)", (toponame, edge1_id, edge2_id))
conn.commit()
cur.close()
else:
errors.append("Node " + str(node_id) + " is the only node in a ring, cannot be removed")
elif cur.rowcount == 0:
cur.close()
cur = conn.cursor()
cur.execute("SELECT topology.ST_RemIsoNode(%s, %s)", (toponame, node_id))
conn.commit()
cur.close()
else:
cur.close()
errors.append("Node " + str(node_id) + " is not of degree 2 nor isolated")
except psycopg2.Error as e:
errors.append("Removing node " + str(node_id) + ":\n" + str(e))
conn.commit()
cur.close()
continue
conn.close()
removed = len(selected) - len(errors)
report = "Removed " + str(removed) + " nodes over " + str(len(selected)) + " selected\n"
if errors:
report += "\nFailures ("
if len(errors) > 5:
report += "first 5 of "
report += str(len(errors)) + "):\n\n" + "\n".join(errors[:5])
QMessageBox.information(None, toolname, report)
layer.removeSelection()
self.iface.mapCanvas().refresh()
# Collect all topogeoms registered as being in the currently selected
# topolayer and not really present in it
#
def doDropOrphanedTopoGeoms(self):
toolname = "CollectTopogeoms"
# check that a layer is selected
layer = self.iface.mapCanvas().currentLayer()
if not layer:
QMessageBox.information(None, toolname, "A TopoGeom layer must be selected")
return
# check that the selected layer is a postgis one
if layer.providerType() != 'postgres':
QMessageBox.information(None, toolname, "A PostGIS layer must be selected")
return
uri = QgsDataSourceUri( layer.source() )
# get the layer schema
schema = str(uri.schema())
if not schema:
QMessageBox.information(None, toolname, "Selected layer must be a table, not a view\n"
"(no schema set in datasource " + str(uri.uri()) + ")")
return;
# get the layer table
table = str(uri.table())
if not table:
QMessageBox.information(None, toolname, "Selected layer must be a table, not a view\n"
"(no table set in datasource)")
return;
# get the layer column
col = str(uri.geometryColumn())
if not col:
QMessageBox.information(None, toolname, "Selected layer must be a table, not a view\n"
"(no column set in datasource)")
return;
layername = '"' + schema + '"."' + table + '"."' + col +'"'
try:
conn = psycopg2.connect( str(uri.connectionInfo()) )
cur = conn.cursor()
# get the layer topology
cur.execute("SELECT t.name, l.layer_id FROM topology.topology t, topology.layer l WHERE l.schema_name = %s AND l.table_name = %s AND l.feature_column = %s AND t.id = l.topology_id", (schema, table, col))
if cur.rowcount == 1:
(toponame, layer_id) = cur.fetchone()
# delete orphaned geoms...
cur.execute('DELETE FROM "' + toponame +
'"."relation" WHERE layer_id = %s AND topogeo_id NOT IN ( SELECT id("'
+ col + '") FROM "' + schema + '"."' + table + '")', (str(layer_id),))
QMessageBox.information(None, toolname, str(cur.rowcount)
+ ' orphaned topogeometry objects removed from layer ' + layername)
conn.commit()
else:
QMessageBox.information(None, toolname, layername + ' is not a topology layer (not registered in topology.layer table)')
cur.close()
conn.close()
except psycopg2.Error as e:
QMessageBox.information(None, toolname, "ERROR: " + str(e))
return
#QMessageBox.information(None, toolname, "Not implemeted yet, but we know the layer is " + schema + "." + table + "." + col + ", the topology name is " + toponame + " and layer_id is " + str(layer_id))
self.iface.mapCanvas().refresh()