2010-11-13 17:30:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-03-07 23:29:15 +00:00
|
|
|
#-----------------------------------------------------------
|
|
|
|
#
|
|
|
|
# fTools
|
|
|
|
# Copyright (C) 2008-2011 Carson Farmer
|
|
|
|
# EMAIL: carson.farmer (at) gmail.com
|
|
|
|
# WEB : http://www.ftools.ca/fTools.html
|
|
|
|
#
|
|
|
|
# A collection of data management and analysis tools for vector data
|
|
|
|
#
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#
|
|
|
|
# licensed under the terms of GNU GPL 2
|
|
|
|
#
|
|
|
|
# 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 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
#---------------------------------------------------------------------
|
|
|
|
|
2010-11-13 17:30:06 +00:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
from qgis.core import *
|
|
|
|
from ui_frmVisual import Ui_Dialog
|
|
|
|
import ftools_utils
|
|
|
|
import math
|
2011-02-28 22:31:07 +00:00
|
|
|
from qgis import gui
|
|
|
|
|
|
|
|
class MarkerErrorGeometry():
|
|
|
|
def __init__(self, mapCanvas):
|
|
|
|
self.__canvas = mapCanvas
|
|
|
|
self.__marker = None
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
def __createMarker(self, point):
|
|
|
|
self.__marker = gui.QgsVertexMarker(self.__canvas)
|
|
|
|
self.__marker.setCenter(point)
|
|
|
|
self.__marker.setIconType(gui.QgsVertexMarker.ICON_X)
|
|
|
|
self.__marker.setPenWidth(3)
|
|
|
|
|
2012-05-25 19:24:48 +02:00
|
|
|
def setGeom(self, p):
|
2011-02-28 22:31:07 +00:00
|
|
|
if not self.__marker is None:
|
|
|
|
self.reset()
|
|
|
|
if self.__marker is None:
|
2012-05-25 19:24:48 +02:00
|
|
|
self.__createMarker(p)
|
2011-02-28 22:31:07 +00:00
|
|
|
else:
|
2012-05-25 19:24:48 +02:00
|
|
|
self.__marker.setCenter(p)
|
2011-02-28 22:31:07 +00:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
if not self.__marker is None:
|
2012-11-26 10:39:53 +02:00
|
|
|
self.__canvas.scene().removeItem(self.__marker)
|
2011-02-28 22:31:07 +00:00
|
|
|
del self.__marker
|
|
|
|
self.__marker = None
|
2010-11-13 17:30:06 +00:00
|
|
|
|
|
|
|
class ValidateDialog( QDialog, Ui_Dialog ):
|
|
|
|
def __init__(self, iface):
|
2012-01-26 00:54:56 +01:00
|
|
|
QDialog.__init__(self, iface.mainWindow())
|
2010-11-13 17:30:06 +00:00
|
|
|
self.iface = iface
|
|
|
|
self.setupUi(self)
|
2012-01-26 00:54:56 +01:00
|
|
|
# self.setModal(False) # we want to be able to interact with the featuresmc.extent().width()
|
|
|
|
# self.setWindowFlags( Qt.SubWindow )
|
2010-11-13 17:30:06 +00:00
|
|
|
# adjust user interface
|
|
|
|
self.setWindowTitle( self.tr( "Check geometry validity" ) )
|
|
|
|
self.cmbField.setVisible( False )
|
|
|
|
self.label.setVisible( False )
|
|
|
|
self.useSelected.setVisible( True )
|
|
|
|
self.label_2.setText( self.tr( "Geometry errors" ) )
|
|
|
|
self.label_4.setText( self.tr( "Total encountered errors" ) )
|
|
|
|
self.partProgressBar.setVisible( False )
|
|
|
|
self.tblUnique.setSelectionMode(QAbstractItemView.SingleSelection)
|
|
|
|
self.tblUnique.setSelectionBehavior(QAbstractItemView.SelectRows)
|
|
|
|
# populate list of available layers
|
|
|
|
myList = ftools_utils.getLayerNames( [ QGis.Point, QGis.Line, QGis.Polygon ] )
|
2012-11-26 10:39:53 +02:00
|
|
|
self.connect(self.tblUnique, SIGNAL("currentItemChanged(QTableWidgetItem*, QTableWidgetItem*)" ),
|
2010-11-13 17:30:06 +00:00
|
|
|
self.zoomToError)
|
|
|
|
self.inShape.addItems( myList )
|
2012-11-26 10:39:53 +02:00
|
|
|
self.buttonBox_2.setOrientation(Qt.Horizontal)
|
2010-11-13 17:30:06 +00:00
|
|
|
self.cancel_close = self.buttonBox_2.button(QDialogButtonBox.Close)
|
|
|
|
self.buttonOk = self.buttonBox_2.button(QDialogButtonBox.Ok)
|
|
|
|
self.progressBar.setValue(0)
|
|
|
|
self.storedScale = self.iface.mapCanvas().scale()
|
2011-02-28 22:31:07 +00:00
|
|
|
# create marker for error
|
|
|
|
self.marker = MarkerErrorGeometry(self.iface.mapCanvas())
|
2012-05-25 13:46:18 +02:00
|
|
|
|
|
|
|
settings = QSettings()
|
|
|
|
self.restoreGeometry( settings.value("/fTools/ValidateDialog/geometry").toByteArray() )
|
|
|
|
|
2012-11-26 10:39:53 +02:00
|
|
|
QObject.connect( self.browseShpError, SIGNAL( "clicked()" ), self.outFile )
|
|
|
|
QObject.connect( self.ckBoxShpError, SIGNAL( "stateChanged( int )" ), self.updateGui )
|
|
|
|
self.updateGui()
|
|
|
|
|
2012-05-25 13:46:18 +02:00
|
|
|
def closeEvent(self, e):
|
|
|
|
settings = QSettings()
|
|
|
|
settings.setValue( "/fTools/ValidateDialog/geometry", QVariant(self.saveGeometry()) )
|
2012-07-09 13:33:35 +02:00
|
|
|
QDialog.closeEvent(self, e)
|
2012-05-25 13:46:18 +02:00
|
|
|
del self.marker
|
2012-11-26 10:39:53 +02:00
|
|
|
|
2010-11-13 17:30:06 +00:00
|
|
|
def keyPressEvent( self, e ):
|
|
|
|
if ( e.modifiers() == Qt.ControlModifier or \
|
|
|
|
e.modifiers() == Qt.MetaModifier ) and \
|
|
|
|
e.key() == Qt.Key_C:
|
|
|
|
#selection = self.tblUnique.selectedItems()
|
|
|
|
items = QString()
|
|
|
|
for row in range( self.tblUnique.rowCount() ):
|
|
|
|
items.append( self.tblUnique.item( row, 0 ).text()
|
|
|
|
+ "," + self.tblUnique.item( row, 1 ).text() + "\n" )
|
|
|
|
if not items.isEmpty():
|
|
|
|
clip_board = QApplication.clipboard()
|
|
|
|
clip_board.setText( items )
|
|
|
|
else:
|
|
|
|
QDialog.keyPressEvent( self, e )
|
|
|
|
|
|
|
|
def accept( self ):
|
|
|
|
if self.inShape.currentText() == "":
|
|
|
|
QMessageBox.information( self, self.tr("Error!"), self.tr( "Please specify input vector layer" ) )
|
|
|
|
elif self.cmbField.isVisible() and self.cmbField.currentText() == "":
|
|
|
|
QMessageBox.information( self, self.tr("Error!"), self.tr( "Please specify input field" ) )
|
2012-11-26 10:39:53 +02:00
|
|
|
elif self.ckBoxShpError.isChecked() and self.lineEditShpError.text() == "":
|
|
|
|
QMessageBox.information( self, self.tr( "Error!" ), self.tr( "Please specify output shapefile" ) )
|
2010-11-13 17:30:06 +00:00
|
|
|
else:
|
2012-05-25 13:46:18 +02:00
|
|
|
self.vlayer = ftools_utils.getVectorLayerByName( self.inShape.currentText() )
|
|
|
|
self.validate( self.useSelected.checkState() )
|
2012-11-26 10:39:53 +02:00
|
|
|
|
|
|
|
def updateGui( self ):
|
|
|
|
if self.ckBoxShpError.isChecked():
|
|
|
|
self.lineEditShpError.setEnabled( True )
|
|
|
|
self.browseShpError.setEnabled( True )
|
|
|
|
self.tblUnique.setEnabled( False )
|
|
|
|
self.lstCount.setEnabled( False )
|
|
|
|
self.label_2.setEnabled( False )
|
|
|
|
self.label_4.setEnabled( False )
|
|
|
|
self.label_5.setEnabled( False )
|
|
|
|
else:
|
|
|
|
self.lineEditShpError.setEnabled( False )
|
|
|
|
self.browseShpError.setEnabled( False )
|
|
|
|
self.tblUnique.setEnabled( True )
|
|
|
|
self.lstCount.setEnabled( True )
|
|
|
|
self.label_2.setEnabled( True )
|
|
|
|
self.label_4.setEnabled( True )
|
|
|
|
self.label_5.setEnabled( True )
|
|
|
|
|
|
|
|
def outFile( self ):
|
|
|
|
self.lineEditShpError.clear()
|
|
|
|
(self.shapefileName, self.encoding) = ftools_utils.saveDialog( self )
|
|
|
|
if self.shapefileName is None or self.encoding is None:
|
|
|
|
return
|
|
|
|
self.lineEditShpError.setText( QString( self.shapefileName ) )
|
|
|
|
|
2010-11-13 17:30:06 +00:00
|
|
|
def zoomToError(self, curr, prev):
|
|
|
|
if curr is None:
|
|
|
|
return
|
|
|
|
row = curr.row() # if we clicked in the first column, we want the second
|
|
|
|
item = self.tblUnique.item(row, 1)
|
2012-05-24 21:26:48 +02:00
|
|
|
|
|
|
|
mc = self.iface.mapCanvas()
|
|
|
|
mc.zoomToPreviousExtent()
|
2012-05-25 13:46:18 +02:00
|
|
|
|
|
|
|
e = item.data(Qt.UserRole).toPyObject()
|
|
|
|
|
|
|
|
if type(e)==QgsPoint:
|
2012-05-25 19:24:48 +02:00
|
|
|
e = mc.mapRenderer().layerToMapCoordinates( self.vlayer, e )
|
2012-05-25 13:46:18 +02:00
|
|
|
|
2012-05-25 19:24:48 +02:00
|
|
|
self.marker.setGeom(e)
|
2012-05-25 13:46:18 +02:00
|
|
|
|
|
|
|
rect = mc.extent()
|
2013-01-04 01:25:02 +01:00
|
|
|
rect.scale( 0.5, e )
|
2012-05-25 13:46:18 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
self.marker.reset()
|
|
|
|
|
|
|
|
ft = QgsFeature()
|
|
|
|
(fid,ok) = self.tblUnique.item(row, 0).text().toInt()
|
|
|
|
if not ok or not self.vlayer.featureAtId( fid, ft, True):
|
|
|
|
return
|
|
|
|
|
2012-05-25 19:24:48 +02:00
|
|
|
rect = mc.mapRenderer().layerExtentToOutputExtent( self.vlayer, ft.geometry().boundingBox() )
|
2013-01-04 01:25:02 +01:00
|
|
|
rect.scale( 1.05 )
|
2012-05-25 13:46:18 +02:00
|
|
|
|
2012-05-24 21:26:48 +02:00
|
|
|
# Set the extent to our new rectangle
|
|
|
|
mc.setExtent(rect)
|
|
|
|
# Refresh the map
|
|
|
|
mc.refresh()
|
2010-11-13 17:30:06 +00:00
|
|
|
|
2012-05-25 13:46:18 +02:00
|
|
|
def validate( self, mySelection ):
|
2012-11-26 10:39:53 +02:00
|
|
|
if not self.ckBoxShpError.isChecked():
|
|
|
|
self.tblUnique.clearContents()
|
|
|
|
self.tblUnique.setRowCount( 0 )
|
|
|
|
self.lstCount.clear()
|
|
|
|
self.shapefileName = None
|
|
|
|
self.encoding = None
|
|
|
|
|
2010-11-13 17:30:06 +00:00
|
|
|
self.buttonOk.setEnabled( False )
|
2012-11-26 10:39:53 +02:00
|
|
|
|
|
|
|
self.testThread = validateThread( self.iface.mainWindow(), self, self.vlayer, mySelection, self.shapefileName, self.encoding, self.ckBoxShpError.isChecked() )
|
2010-11-13 17:30:06 +00:00
|
|
|
QObject.connect( self.testThread, SIGNAL( "runFinished(PyQt_PyObject)" ), self.runFinishedFromThread )
|
|
|
|
QObject.connect( self.testThread, SIGNAL( "runStatus(PyQt_PyObject)" ), self.runStatusFromThread )
|
|
|
|
QObject.connect( self.testThread, SIGNAL( "runRange(PyQt_PyObject)" ), self.runRangeFromThread )
|
|
|
|
self.cancel_close.setText( self.tr("Cancel") )
|
|
|
|
QObject.connect( self.cancel_close, SIGNAL( "clicked()" ), self.cancelThread )
|
|
|
|
QApplication.setOverrideCursor( Qt.WaitCursor )
|
|
|
|
self.testThread.start()
|
|
|
|
return True
|
|
|
|
|
2011-02-28 22:31:07 +00:00
|
|
|
def reject(self):
|
|
|
|
# Remove Marker
|
|
|
|
self.marker.reset()
|
|
|
|
QDialog.reject(self)
|
2012-11-26 10:39:53 +02:00
|
|
|
|
2010-11-13 17:30:06 +00:00
|
|
|
def cancelThread( self ):
|
|
|
|
self.testThread.stop()
|
|
|
|
QApplication.restoreOverrideCursor()
|
|
|
|
self.buttonOk.setEnabled( True )
|
2012-11-26 10:39:53 +02:00
|
|
|
|
|
|
|
def runFinishedFromThread( self, success ):
|
2010-11-13 17:30:06 +00:00
|
|
|
self.testThread.stop()
|
|
|
|
QApplication.restoreOverrideCursor()
|
|
|
|
self.buttonOk.setEnabled( True )
|
2012-11-26 10:39:53 +02:00
|
|
|
if success == "writeShape":
|
|
|
|
extra = ""
|
|
|
|
addToTOC = QMessageBox.question( self, self.tr("Geometry"),
|
|
|
|
self.tr( "Created output shapefile:\n%1\n%2\n\nWould you like to add the new layer to the TOC?" ).arg( unicode( self.shapefileName ) ).arg( extra ),
|
|
|
|
QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton )
|
|
|
|
if addToTOC == QMessageBox.Yes:
|
|
|
|
if not ftools_utils.addShapeToCanvas( unicode( self.shapefileName ) ):
|
|
|
|
QMessageBox.warning( self, self.tr( "Geometry"),
|
|
|
|
self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( self.shapefileName ) ) )
|
|
|
|
else:
|
|
|
|
self.tblUnique.setColumnCount( 2 )
|
|
|
|
count = 0
|
|
|
|
for rec in success:
|
|
|
|
if len(rec[1]) < 1:
|
|
|
|
continue
|
|
|
|
where = None
|
|
|
|
for err in rec[1]: # for each error we find
|
|
|
|
self.tblUnique.insertRow(count)
|
|
|
|
fidItem = QTableWidgetItem( str(rec[0]) )
|
|
|
|
self.tblUnique.setItem( count, 0, fidItem )
|
|
|
|
message = err.what()
|
|
|
|
errItem = QTableWidgetItem( message )
|
|
|
|
if err.hasWhere(): # if there is a location associated with the error
|
|
|
|
errItem.setData(Qt.UserRole, QVariant(err.where()))
|
|
|
|
self.tblUnique.setItem( count, 1, errItem )
|
|
|
|
count += 1
|
|
|
|
self.tblUnique.setHorizontalHeaderLabels( [ self.tr("Feature"), self.tr("Error(s)") ] )
|
|
|
|
self.tblUnique.horizontalHeader().setResizeMode( 0, QHeaderView.ResizeToContents )
|
|
|
|
self.tblUnique.horizontalHeader().show()
|
|
|
|
self.tblUnique.horizontalHeader().setResizeMode( 1, QHeaderView.Stretch )
|
|
|
|
self.tblUnique.resizeRowsToContents()
|
|
|
|
self.lstCount.insert(str(count))
|
2010-11-13 17:30:06 +00:00
|
|
|
self.cancel_close.setText( "Close" )
|
|
|
|
QObject.disconnect( self.cancel_close, SIGNAL( "clicked()" ), self.cancelThread )
|
|
|
|
return True
|
2012-11-26 10:39:53 +02:00
|
|
|
|
2010-11-13 17:30:06 +00:00
|
|
|
def runStatusFromThread( self, status ):
|
|
|
|
self.progressBar.setValue( status )
|
2012-11-26 10:39:53 +02:00
|
|
|
|
2010-11-13 17:30:06 +00:00
|
|
|
def runRangeFromThread( self, range_vals ):
|
|
|
|
self.progressBar.setRange( range_vals[ 0 ], range_vals[ 1 ] )
|
|
|
|
|
|
|
|
class validateThread( QThread ):
|
2012-11-26 10:39:53 +02:00
|
|
|
def __init__( self, parentThread, parentObject, vlayer, mySelection, myName, myEncoding, myNewShape ):
|
2010-11-13 17:30:06 +00:00
|
|
|
QThread.__init__( self, parentThread )
|
|
|
|
self.parent = parentObject
|
|
|
|
self.running = False
|
|
|
|
self.vlayer = vlayer
|
|
|
|
self.mySelection = mySelection
|
2012-11-26 10:39:53 +02:00
|
|
|
self.myName = myName
|
|
|
|
self.myEncoding = myEncoding
|
|
|
|
self.writeShape = myNewShape
|
2010-11-13 17:30:06 +00:00
|
|
|
|
|
|
|
def run( self ):
|
|
|
|
self.running = True
|
2012-11-26 10:39:53 +02:00
|
|
|
success = self.check_geometry( self.vlayer )
|
|
|
|
self.emit( SIGNAL( "runFinished(PyQt_PyObject)" ), success )
|
2010-11-13 17:30:06 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def check_geometry( self, vlayer ):
|
|
|
|
lstErrors = []
|
|
|
|
if self.mySelection:
|
|
|
|
layer = vlayer.selectedFeatures()
|
|
|
|
nFeat = len(layer)
|
|
|
|
else:
|
2010-11-14 14:02:42 +00:00
|
|
|
#layer = vlayer # requires SIP >= 4.9
|
|
|
|
layer = []
|
2010-11-14 14:20:21 +00:00
|
|
|
vlayer.select([]) # select all features, and ignore attributes
|
2010-11-14 14:02:42 +00:00
|
|
|
ft = QgsFeature()
|
2010-11-14 14:20:21 +00:00
|
|
|
while vlayer.nextFeature(ft):
|
|
|
|
layer.append(QgsFeature(ft))
|
|
|
|
nFeat = len(layer)
|
2010-11-13 17:30:06 +00:00
|
|
|
nElement = 0
|
|
|
|
if nFeat > 0:
|
|
|
|
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
|
|
|
|
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
|
|
|
|
for feat in layer:
|
|
|
|
if not self.running:
|
|
|
|
return list()
|
|
|
|
geom = QgsGeometry(feat.geometry()) # ger reference to geometry
|
|
|
|
self.emit(SIGNAL("runStatus(PyQt_PyObject)"), nElement)
|
|
|
|
nElement += 1
|
2011-02-28 22:31:07 +00:00
|
|
|
# Check Add error
|
2012-05-25 13:46:18 +02:00
|
|
|
if not geom.isGeosEmpty():
|
2011-02-28 22:31:07 +00:00
|
|
|
lstErrors.append((feat.id(), list(geom.validateGeometry())))
|
2010-11-13 17:30:06 +00:00
|
|
|
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nFeat )
|
2012-11-26 10:39:53 +02:00
|
|
|
|
|
|
|
if self.writeShape:
|
|
|
|
fields = { 0 : QgsField( "FEAT_ID", QVariant.Int ),
|
|
|
|
1 : QgsField( "ERROR", QVariant.String ) }
|
|
|
|
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
|
|
|
|
QGis.WKBPoint, vlayer.crs() )
|
|
|
|
for rec in lstErrors:
|
|
|
|
if len(rec[1]) < 1:
|
|
|
|
continue
|
|
|
|
for err in rec[1]:
|
|
|
|
fidItem = str(rec[0])
|
|
|
|
message = err.what()
|
|
|
|
if err.hasWhere():
|
|
|
|
locErr = err.where()
|
|
|
|
xP = locErr.x()
|
|
|
|
yP = locErr.y()
|
|
|
|
myPoint = QgsPoint( xP, yP )
|
|
|
|
geometry = QgsGeometry().fromPoint( myPoint )
|
|
|
|
ft = QgsFeature()
|
|
|
|
ft.setGeometry( geometry )
|
|
|
|
ft.setAttributeMap( { 0 : QVariant( fidItem ),
|
|
|
|
1 : QVariant( message ) } )
|
|
|
|
writer.addFeature( ft )
|
|
|
|
del writer
|
|
|
|
return "writeShape"
|
|
|
|
else:
|
|
|
|
return lstErrors
|