709 changes: 368 additions & 341 deletions python/plugins/fTools/tools/doGeometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,97 +38,101 @@
import voronoi
from sets import Set

class GeometryDialog(QDialog, Ui_Dialog):

def __init__(self, iface, function):
QDialog.__init__(self, iface.mainWindow())
class GeometryDialog( QDialog, Ui_Dialog ):
def __init__( self, iface, function ):
QDialog.__init__( self, iface.mainWindow() )
self.iface = iface
self.setupUi(self)
self.setupUi( self )
self.myFunction = function
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
QObject.connect( self.toolOut, SIGNAL( "clicked()" ), self.outFile )
if self.myFunction == 1:
QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.update)
QObject.connect( self.inShape, SIGNAL( "currentIndexChanged( QString )" ), self.update )
self.manageGui()
self.success = False
self.cancel_close = self.buttonBox_2.button( QDialogButtonBox.Close )
self.progressBar.setValue(0)
self.progressBar.setValue( 0 )

def update(self):
def update( self ):
self.cmbField.clear()
inputLayer = unicode(self.inShape.currentText())
inputLayer = unicode( self.inShape.currentText() )
if inputLayer != "":
changedLayer = ftools_utils.getVectorLayerByName(inputLayer)
changedField = ftools_utils.getFieldList(changedLayer)
changedLayer = ftools_utils.getVectorLayerByName( inputLayer )
changedField = ftools_utils.getFieldList( changedLayer )
for i in changedField:
self.cmbField.addItem(unicode(changedField[i].name()))
self.cmbField.addItem("--- " + self.tr( "Merge all" ) + " ---")
self.cmbField.addItem( unicode( changedField[ i ].name() ) )
self.cmbField.addItem( "--- " + self.tr( "Merge all" ) + " ---" )

def accept(self):
def accept( self ):
if self.inShape.currentText() == "":
QMessageBox.information(self, self.tr("Geometry"), self.tr( "Please specify input vector layer" ) )
QMessageBox.information( self, self.tr( "Geometry" ),
self.tr( "Please specify input vector layer" ) )
elif self.outShape.text() == "":
QMessageBox.information(self, self.tr("Geometry"), self.tr( "Please specify output shapefile" ) )
QMessageBox.information( self, self.tr( "Geometry" ),
self.tr( "Please specify output shapefile" ) )
elif self.lineEdit.isVisible() and self.lineEdit.value() < 0.00:
QMessageBox.information(self, self.tr("Geometry"), self.tr( "Please specify valid tolerance value" ) )
QMessageBox.information( self, self.tr( "Geometry" ),
self.tr( "Please specify valid tolerance value" ) )
elif self.cmbField.isVisible() and self.cmbField.currentText() == "":
QMessageBox.information(self, self.tr("Geometry"), self.tr( "Please specify valid UID field" ) )
QMessageBox.information( self, self.tr( "Geometry" ),
self.tr( "Please specify valid UID field" ) )
else:
self.outShape.clear()
self.geometry( self.inShape.currentText(), self.lineEdit.value(), self.cmbField.currentText() )

def outFile(self):
self.outShape.clear()
(self.shapefileName, self.encoding) = ftools_utils.saveDialog(self)
(self.shapefileName, self.encoding) = ftools_utils.saveDialog( self )
if self.shapefileName is None or self.encoding is None:
return
self.outShape.setText(QString(self.shapefileName))
self.outShape.setText( QString( self.shapefileName ) )

def manageGui( self ):
self.lblField.setVisible( False )
self.cmbField.setVisible( False )

def manageGui(self):
self.lblCalcType.setVisible( False )
self.cmbCalcType.setVisible( False )
if self.myFunction == 1: # Singleparts to multipart
self.setWindowTitle( self.tr( "Singleparts to multipart" ) )
self.lineEdit.setVisible(False)
self.label.setVisible(False)
self.lineEdit.setVisible( False )
self.label.setVisible( False )
self.label_2.setText( self.tr( "Output shapefile" ) )
self.cmbField.setVisible(True)
self.field_label.setVisible(True)
self.cmbField.setVisible( True )
self.lblField.setVisible( True )
elif self.myFunction == 2: # Multipart to singleparts
self.setWindowTitle( self.tr( "Multipart to singleparts" ) )
self.lineEdit.setVisible(False)
self.label.setVisible(False)
self.label_2.setText(self.tr( "Output shapefile" ) )
self.cmbField.setVisible(False)
self.field_label.setVisible(False)
self.lineEdit.setVisible( False )
self.label.setVisible( False )
self.label_2.setText( self.tr( "Output shapefile" ) )
elif self.myFunction == 3: # Extract nodes
self.setWindowTitle( self.tr( "Extract nodes" ) )
self.lineEdit.setVisible(False)
self.label.setVisible(False)
self.cmbField.setVisible(False)
self.field_label.setVisible(False)
self.lineEdit.setVisible( False )
self.label.setVisible( False )
elif self.myFunction == 4: # Polygons to lines
self.setWindowTitle( self.tr( "Polygons to lines" ) )
self.label_2.setText( self.tr( "Output shapefile" ) )
self.label_3.setText( self.tr( "Input polygon vector layer" ) )
self.label.setVisible(False)
self.lineEdit.setVisible(False)
self.cmbField.setVisible(False)
self.field_label.setVisible(False)
self.label.setVisible( False )
self.lineEdit.setVisible( False )
elif self.myFunction == 5: # Export/Add geometry columns
self.setWindowTitle( self.tr( "Export/Add geometry columns" ) )
self.label_2.setText( self.tr( "Output shapefile" ) )
self.label_3.setText( self.tr( "Input vector layer" ) )
self.label.setVisible(False)
self.lineEdit.setVisible(False)
self.cmbField.setVisible(False)
self.field_label.setVisible(False)
self.label.setVisible( False )
self.lineEdit.setVisible( False )
# populate calculation types
self.lblCalcType.setVisible( True )
self.cmbCalcType.setVisible( True )
self.cmbCalcType.addItem( self.tr( "Layer CRS" ) )
self.cmbCalcType.addItem( self.tr( "Project CRS" ) )
self.cmbCalcType.addItem( self.tr( "Ellipsoid" ) )
elif self.myFunction == 7: # Polygon centroids
self.setWindowTitle( self.tr( "Polygon centroids" ) )
self.label_2.setText( self.tr( "Output point shapefile" ) )
self.label_3.setText( self.tr( "Input polygon vector layer" ) )
self.label.setVisible( False )
self.lineEdit.setVisible( False )
self.cmbField.setVisible( False )
self.field_label.setVisible( False )
else:
if self.myFunction == 8: # Delaunay triangulation
self.setWindowTitle( self.tr( "Delaunay triangulation" ) )
Expand All @@ -139,27 +143,22 @@ def manageGui(self):
self.setWindowTitle( self.tr( "Voronoi polygon" ) )
self.label_3.setText( self.tr( "Input point vector layer" ) )
self.label.setText( self.tr( "Buffer region" ) )
self.lineEdit.setSuffix(" %")
self.lineEdit.setRange(0, 100)
self.lineEdit.setSingleStep(5)
self.lineEdit.setValue(0)
self.lineEdit.setSuffix( " %" )
self.lineEdit.setRange( 0, 100 )
self.lineEdit.setSingleStep( 5 )
self.lineEdit.setValue( 0 )
elif self.myFunction == 11: #Lines to polygons
self.setWindowTitle( self.tr( "Lines to polygons" ) )
self.label_2.setText( self.tr( "Output shapefile" ) )
self.label_3.setText( self.tr( "Input line vector layer" ) )
self.label.setVisible(False)
self.lineEdit.setVisible(False)
self.cmbField.setVisible(False)
self.field_label.setVisible(False)

self.label.setVisible( False )
self.lineEdit.setVisible( False )
else: # Polygon from layer extent
self.setWindowTitle( self.tr( "Polygon from layer extent" ) )
self.label_3.setText( self.tr( "Input layer" ) )
self.label.setVisible( False )
self.lineEdit.setVisible( False )
self.label_2.setText( self.tr( "Output polygon shapefile" ) )
self.cmbField.setVisible( False )
self.field_label.setVisible( False )
self.resize( 381, 100 )
self.populateLayers()

Expand All @@ -186,9 +185,9 @@ def populateLayers( self ):
#5: Export/Add geometry columns
#6: Simplify geometries (disabled)
#7: Polygon centroids
#8: Delaunay triangulation
#9: Polygon from layer extent
#10:Voronoi polygons
#8: Delaunay triangulation
#9: Polygon from layer extent
#10: Voronoi polygons
#11: Lines to polygons

def geometry( self, myLayer, myParam, myField ):
Expand All @@ -200,15 +199,16 @@ def geometry( self, myLayer, myParam, myField ):
check = QFile( self.shapefileName )
if check.exists():
if not QgsVectorFileWriter.deleteShapeFile( self.shapefileName ):
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Unable to delete existing shapefile." ) )
QMessageBox.warning( self, self.tr( "Geometry"),
self.tr( "Unable to delete existing shapefile." ) )
return
self.buttonOk.setEnabled( False )
self.testThread = geometryThread( self.iface.mainWindow(), self, self.myFunction, vlayer, myParam,
myField, self.shapefileName, self.encoding )
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") )
myField, self.shapefileName, self.encoding, self.cmbCalcType.currentIndex() )
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 )
self.testThread.start()

Expand All @@ -221,13 +221,17 @@ def runFinishedFromThread( self, success ):
self.buttonOk.setEnabled( True )
extra = ""
if success == "math_error":
QMessageBox.warning( self, self.tr("Geometry"), self.tr("Error processing specified tolerance!\nPlease choose larger tolerance...") )
QMessageBox.warning( self, self.tr( "Geometry" ),
self.tr( "Error processing specified tolerance!\nPlease choose larger tolerance..." ) )
if not QgsVectorFileWriter.deleteShapeFile( self.shapefileName ):
QMessageBox.warning( self, self.tr("Geometry"), self.tr( "Unable to delete incomplete shapefile." ) )
QMessageBox.warning( self, self.tr( "Geometry" ),
self.tr( "Unable to delete incomplete shapefile." ) )
elif success == "attr_error":
QMessageBox.warning( self, self.tr("Geometry"), self.tr("At least two features must have same attribute value!\nPlease choose another field...") )
QMessageBox.warning( self, self.tr( "Geometry" ),
self.tr( "At least two features must have same attribute value!\nPlease choose another field..." ) )
if not QgsVectorFileWriter.deleteShapeFile( self.shapefileName ):
QMessageBox.warning( self, self.tr("Geometry"), self.tr( "Unable to delete incomplete shapefile." ) )
QMessageBox.warning( self, self.tr( "Geometry" ),
self.tr( "Unable to delete incomplete shapefile." ) )
else:
if success == "valid_error":
extra = self.tr("One or more features in the output layer may have invalid "
Expand All @@ -237,14 +241,15 @@ def runFinishedFromThread( self, success ):
QObject.disconnect( self.cancel_close, SIGNAL( "clicked()" ), self.cancelThread )
if success:
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 )
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("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( self.shapefileName ) ))
QMessageBox.warning( self, self.tr( "Geometry"),
self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( self.shapefileName ) ) )
self.populateLayers()
else:
QMessageBox.warning( self, self.tr("Geometry"), self.tr( "Error writing output shapefile." ) )
QMessageBox.warning( self, self.tr( "Geometry" ), self.tr( "Error writing output shapefile." ) )

def runStatusFromThread( self, status ):
self.progressBar.setValue( status )
Expand All @@ -253,7 +258,7 @@ def runRangeFromThread( self, range_vals ):
self.progressBar.setRange( range_vals[ 0 ], range_vals[ 1 ] )

class geometryThread( QThread ):
def __init__( self, parentThread, parentObject, function, vlayer, myParam, myField, myName, myEncoding ):
def __init__( self, parentThread, parentObject, function, vlayer, myParam, myField, myName, myEncoding, myCalcType ):
QThread.__init__( self, parentThread )
self.parent = parentObject
self.running = False
Expand All @@ -263,6 +268,7 @@ def __init__( self, parentThread, parentObject, function, vlayer, myParam, myFie
self.myField = myField
self.myName = myName
self.myEncoding = myEncoding
self.myCalcType = myCalcType

def run( self ):
self.running = True
Expand All @@ -287,10 +293,10 @@ def run( self ):
success = self.voronoi_polygons()
elif self.myFunction == 11: # Lines to polygons
success = self.lines_to_polygons()
self.emit( SIGNAL( "runFinished(PyQt_PyObject)" ), success )
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runFinished( PyQt_PyObject )" ), success )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )

def stop(self):
def stop( self ):
self.running = False

def single_to_multi( self ):
Expand All @@ -299,9 +305,9 @@ def single_to_multi( self ):
vprovider.select( allAttrs )
fields = vprovider.fields()
allValid = True
geomType = self.singleToMultiGeom(vprovider.geometryType())
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, geomType, vprovider.crs() )
geomType = self.singleToMultiGeom( vprovider.geometryType() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
geomType, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
Expand All @@ -310,27 +316,25 @@ def single_to_multi( self ):
if not index == -1:
unique = ftools_utils.getUniqueValues( vprovider, int( index ) )
else:
unique = [QVariant(QString())]
unique = [ QVariant( QString() ) ]
nFeat = vprovider.featureCount() * len( unique )
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
merge_all = self.myField == QString("--- " + self.tr( "Merge all" ) + " ---")
if not len( unique ) == self.vlayer.featureCount() \
or merge_all:
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
merge_all = self.myField == QString( "--- " + self.tr( "Merge all" ) + " ---" )
if not len( unique ) == self.vlayer.featureCount() or merge_all:
for i in unique:
vprovider.rewind()
multi_feature= []
first = True
vprovider.select(allAttrs)
vprovider.select( allAttrs )
while vprovider.nextFeature( inFeat ):
atMap = inFeat.attributeMap()
if not merge_all:
idVar = atMap[ index ]
else:
idVar = QVariant(QString())
if idVar.toString().trimmed() == i.toString().trimmed() \
or merge_all:
idVar = QVariant( QString() )
if idVar.toString().trimmed() == i.toString().trimmed() or merge_all:
if first:
atts = atMap
first = False
Expand All @@ -339,13 +343,13 @@ def single_to_multi( self ):
feature_list = self.extractAsMulti( inGeom )
multi_feature.extend( feature_list )
nElement += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
outFeat.setAttributeMap( atts )
outGeom = QgsGeometry( self.convertGeometry(multi_feature, vType) )
outGeom = QgsGeometry( self.convertGeometry( multi_feature, vType ) )
if not outGeom.isGeosValid():
allValid = "valid_error"
outFeat.setGeometry(outGeom)
writer.addFeature(outFeat)
outFeat.setGeometry( outGeom )
writer.addFeature( outFeat )
del writer
else:
return "attr_error"
Expand All @@ -356,20 +360,20 @@ def multi_to_single( self ):
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
geomType = self.multiToSingleGeom(vprovider.geometryType())
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, geomType, vprovider.crs() )
geomType = self.multiToSingleGeom( vprovider.geometryType() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
geomType, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
outGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
while vprovider.nextFeature( inFeat ):
nElement += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
inGeom = inFeat.geometry()
atMap = inFeat.attributeMap()
featList = self.extractAsSingle( inGeom )
Expand All @@ -385,19 +389,19 @@ def extract_nodes( self ):
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, QGis.WKBPoint, vprovider.crs() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
QGis.WKBPoint, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
outGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
while vprovider.nextFeature( inFeat ):
nElement += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
inGeom = inFeat.geometry()
atMap = inFeat.attributeMap()
pointList = ftools_utils.extractPoints( inGeom )
Expand All @@ -413,20 +417,20 @@ def polygons_to_lines( self ):
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, QGis.WKBLineString, vprovider.crs() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
QGis.WKBLineString, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
outGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
while vprovider.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
while vprovider.nextFeature( inFeat ):
multi = False
nElement += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
inGeom = inFeat.geometry()
if inGeom.isMultipart():
multi = True
Expand All @@ -444,27 +448,27 @@ def lines_to_polygons( self ):
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, QGis.WKBPolygon, vprovider.crs() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
QGis.WKBPolygon, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
while vprovider.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
while vprovider.nextFeature( inFeat ):
outGeomList = []
multi = False
nElement += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
if inFeat.geometry().isMultipart():
outGeomList = inFeat.geometry().asMultiPolyline()
multi = True
else:
outGeomList.append( inFeat.geometry().asPolyline() )
polyGeom = self.remove_bad_lines( outGeomList )
if len(polyGeom) <> 0:
if len( polyGeom ) <> 0:
outFeat.setGeometry( QgsGeometry.fromPolygon( polyGeom ) )
atMap = inFeat.attributeMap()
outFeat.setAttributeMap( atMap )
Expand All @@ -477,20 +481,41 @@ def export_geometry_info( self ):
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
( fields, index1, index2 ) = self.checkGeometryFields( self.vlayer )
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, vprovider.geometryType(), vprovider.crs() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )

# calculate with:
# 0 - layer CRS
# 1 - project CRS
# 2 - ellipsoidal
ellips = None
crs = None
coordTransform = None
if self.myCalcType == 2:
settings = QSettings()
ellips = settings.value( "/qgis/measure/ellipsoid", "WGS84" ).toString()
crs = self.parent.iface.mapCanvas().mapRenderer().destinationCrs().srsid()
elif self.myCalcType == 1:
mapCRS = self.parent.iface.mapCanvas().mapRenderer().destinationCrs()
layCRS = self.vlayer.crs()
coordTransform = QgsCoordinateTransform( layCRS, mapCRS )

self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
while vprovider.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
nElement += 1
inGeom = inFeat.geometry()
( attr1, attr2 ) = self.simpleMeasure( inGeom )

if self.myCalcType == 1:
inGeom.transform( coordTransform )
( attr1, attr2 ) = self.simpleMeasure( inGeom, self.myCalcType, ellips, crs )

outFeat.setGeometry( inGeom )
atMap = inFeat.attributeMap()
outFeat.setAttributeMap( atMap )
Expand All @@ -505,20 +530,20 @@ def polygon_centroids( self ):
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, QGis.WKBPoint, vprovider.crs() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
QGis.WKBPoint, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
nFeat = vprovider.featureCount()
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
while vprovider.nextFeature( inFeat ):
nElement += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
inGeom = inFeat.geometry()
atMap = inFeat.attributeMap()
outGeom = QgsGeometry(inGeom.centroid())
outGeom = QgsGeometry( inGeom.centroid() )
if outGeom is None:
return "math_error"
outFeat.setAttributeMap( atMap )
Expand All @@ -533,219 +558,216 @@ def delaunay_triangulation( self ):
vprovider = self.vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = {
0 : QgsField( "POINTA", QVariant.Double ),
1 : QgsField( "POINTB", QVariant.Double ),
2 : QgsField( "POINTC", QVariant.Double ) }
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, QGis.WKBPolygon, vprovider.crs() )
fields = { 0 : QgsField( "POINTA", QVariant.Double ),
1 : QgsField( "POINTB", QVariant.Double ),
2 : QgsField( "POINTC", QVariant.Double ) }
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
QGis.WKBPolygon, vprovider.crs() )
inFeat = QgsFeature()
c = voronoi.Context()
pts = []
ptDict = {}
ptNdx = -1
while vprovider.nextFeature(inFeat):
geom = QgsGeometry(inFeat.geometry())
while vprovider.nextFeature( inFeat ):
geom = QgsGeometry( inFeat.geometry() )
point = geom.asPoint()
x = point.x()
y = point.y()
pts.append((x, y))
pts.append( ( x, y ) )
ptNdx +=1
ptDict[ptNdx] = inFeat.id()
if len(pts) < 3:
return False
uniqueSet = Set(item for item in pts)
ids = [pts.index(item) for item in uniqueSet]
sl = voronoi.SiteList([voronoi.Site(*i) for i in uniqueSet])
uniqueSet = Set( item for item in pts )
ids = [ pts.index( item ) for item in uniqueSet ]
sl = voronoi.SiteList( [ voronoi.Site( *i ) for i in uniqueSet ] )
c.triangulate = True
voronoi.voronoi(sl, c)
voronoi.voronoi( sl, c )
triangles = c.triangles
feat = QgsFeature()
nFeat = len( triangles )
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
for triangle in triangles:
indicies = list(triangle)
indicies.append(indicies[0])
indicies = list( triangle )
indicies.append( indicies[ 0 ] )
polygon = []
step = 0
for index in indicies:
vprovider.featureAtId(ptDict[ids[index]], inFeat, True, allAttrs)
geom = QgsGeometry(inFeat.geometry())
point = QgsPoint(geom.asPoint())
polygon.append(point)
if step <= 3: feat.addAttribute(step, QVariant(ids[index]))
vprovider.featureAtId( ptDict[ ids[ index ] ], inFeat, True, allAttrs )
geom = QgsGeometry( inFeat.geometry() )
point = QgsPoint( geom.asPoint() )
polygon.append( point )
if step <= 3: feat.addAttribute( step, QVariant( ids[ index ] ) )
step += 1
geometry = QgsGeometry().fromPolygon([polygon])
feat.setGeometry(geometry)
writer.addFeature(feat)
geometry = QgsGeometry().fromPolygon( [ polygon ] )
feat.setGeometry( geometry )
writer.addFeature( feat )
nElement += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
del writer
return True

def voronoi_polygons( self ):
vprovider = self.vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
vprovider.fields(), QGis.WKBPolygon, vprovider.crs() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vprovider.fields(),
QGis.WKBPolygon, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
extent = self.vlayer.extent()
extraX = extent.height()*(self.myParam/100.00)
extraY = extent.width()*(self.myParam/100.00)
extraX = extent.height() * ( self.myParam / 100.00 )
extraY = extent.width() * ( self.myParam / 100.00 )
height = extent.height()
width = extent.width()
c = voronoi.Context()
pts = []
ptDict = {}
ptNdx = -1
while vprovider.nextFeature(inFeat):
geom = QgsGeometry(inFeat.geometry())
while vprovider.nextFeature( inFeat ):
geom = QgsGeometry( inFeat.geometry() )
point = geom.asPoint()
x = point.x()-extent.xMinimum()
y = point.y()-extent.yMinimum()
pts.append((x, y))
x = point.x() - extent.xMinimum()
y = point.y() - extent.yMinimum()
pts.append( ( x, y ) )
ptNdx +=1
ptDict[ptNdx] = inFeat.id()
ptDict[ ptNdx ] = inFeat.id()
self.vlayer = None
if len(pts) < 3:
if len( pts ) < 3:
return False
uniqueSet = Set(item for item in pts)
ids = [pts.index(item) for item in uniqueSet]
sl = voronoi.SiteList([voronoi.Site(i[0], i[1], sitenum=j) for j, i in enumerate(uniqueSet)])
voronoi.voronoi(sl, c)
uniqueSet = Set( item for item in pts )
ids = [ pts.index( item ) for item in uniqueSet ]
sl = voronoi.SiteList( [ voronoi.Site( i[ 0 ], i[ 1 ], sitenum = j ) for j, i in enumerate( uniqueSet ) ] )
voronoi.voronoi( sl, c )
inFeat = QgsFeature()
nFeat = len(c.polygons)
nFeat = len( c.polygons )
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
for site, edges in c.polygons.iteritems():
vprovider.featureAtId(ptDict[ids[site]], inFeat, True, allAttrs)
lines = self.clip_voronoi(edges, c, width, height, extent, extraX, extraY)
geom = QgsGeometry.fromMultiPoint(lines)
geom = QgsGeometry(geom.convexHull())
outFeat.setGeometry(geom)
outFeat.setAttributeMap(inFeat.attributeMap())
writer.addFeature(outFeat)
vprovider.featureAtId( ptDict[ ids[ site ] ], inFeat, True, allAttrs )
lines = self.clip_voronoi( edges, c, width, height, extent, extraX, extraY )
geom = QgsGeometry.fromMultiPoint( lines )
geom = QgsGeometry( geom.convexHull() )
outFeat.setGeometry( geom )
outFeat.setAttributeMap( inFeat.attributeMap() )
writer.addFeature( outFeat )
nElement += 1
self.emit(SIGNAL("runStatus(PyQt_PyObject)" ), nElement)
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
del writer
return True


def clip_voronoi(self, edges, c, width, height, extent, exX, exY):
def clip_voronoi( self, edges, c, width, height, extent, exX, exY ):
""" Clip voronoi function based on code written for Inkscape
Copyright (C) 2010 Alvin Penner, penner@vaxxine.com
"""
def clip_line(x1, y1, x2, y2, w, h, x, y):
if x1 < 0-x and x2 < 0-x:
return [0, 0, 0, 0]
if x1 > w+x and x2 > w+x:
return [0, 0, 0, 0]
if x1 < 0-x:
y1 = (y1*x2 - y2*x1)/(x2 - x1)
x1 = 0-x
if x2 < 0-x:
y2 = (y1*x2 - y2*x1)/(x2 - x1)
x2 = 0-x
if x1 > w+x:
y1 = y1 + (w+x - x1)*(y2 - y1)/(x2 - x1)
x1 = w+x
if x2 > w+x:
y2 = y1 + (w+x - x1)*(y2 - y1)/(x2 - x1)
x2 = w+x
if y1 < 0-y and y2 < 0-y:
return [0, 0, 0, 0]
if y1 > h+y and y2 > h+y:
return [0, 0, 0, 0]
def clip_line( x1, y1, x2, y2, w, h, x, y ):
if x1 < 0 - x and x2 < 0 - x:
return [ 0, 0, 0, 0 ]
if x1 > w + x and x2 > w + x:
return [ 0, 0, 0, 0 ]
if x1 < 0 - x:
y1 = ( y1 * x2 - y2 * x1 ) / ( x2 - x1 )
x1 = 0 - x
if x2 < 0 - x:
y2 = ( y1 * x2 - y2 * x1 ) / ( x2 - x1 )
x2 = 0 - x
if x1 > w + x:
y1 = y1 + ( w + x - x1 ) * ( y2 - y1 ) / ( x2 - x1 )
x1 = w + x
if x2 > w + x:
y2 = y1 + ( w + x - x1 ) *( y2 - y1 ) / ( x2 - x1 )
x2 = w + x
if y1 < 0 - y and y2 < 0 - y:
return [ 0, 0, 0, 0 ]
if y1 > h + y and y2 > h + y:
return [ 0, 0, 0, 0 ]
if x1 == x2 and y1 == y2:
return [0, 0, 0, 0]
if y1 < 0-y:
x1 = (x1*y2 - x2*y1)/(y2 - y1)
y1 = 0-y
if y2 < 0-y:
x2 = (x1*y2 - x2*y1)/(y2 - y1)
y2 = 0-y
if y1 > h+y:
x1 = x1 + (h+y - y1)*(x2 - x1)/(y2 - y1)
y1 = h+y
if y2 > h+y:
x2 = x1 + (h+y - y1)*(x2 - x1)/(y2 - y1)
y2 = h+y
return [x1, y1, x2, y2]
return [ 0, 0, 0, 0 ]
if y1 < 0 - y:
x1 = ( x1 * y2 - x2 * y1 ) / ( y2 - y1 )
y1 = 0 - y
if y2 < 0 - y:
x2 = ( x1 * y2 - x2 * y1 ) / ( y2 - y1 )
y2 = 0 - y
if y1 > h + y:
x1 = x1 + ( h + y - y1 ) * ( x2 - x1 ) / ( y2 - y1 )
y1 = h + y
if y2 > h + y:
x2 = x1 + ( h + y - y1) * ( x2 - x1 ) / ( y2 - y1 )
y2 = h + y
return [ x1, y1, x2, y2 ]
lines = []
hasXMin = False
hasYMin = False
hasXMax = False
hasYMax = False
for edge in edges:
if edge[1] >= 0 and edge[2] >= 0: # two vertices
[x1, y1, x2, y2] = clip_line(c.vertices[edge[1]][0], c.vertices[edge[1]][1], c.vertices[edge[2]][0], c.vertices[edge[2]][1], width, height, exX, exY)
elif edge[1] >= 0: # only one vertex
if c.lines[edge[0]][1] == 0: # vertical line
xtemp = c.lines[edge[0]][2]/c.lines[edge[0]][0]
if c.vertices[edge[1]][1] > (height+exY)/2:
ytemp = height+exY
if edge[ 1 ] >= 0 and edge[ 2 ] >= 0: # two vertices
[ x1, y1, x2, y2 ] = clip_line( c.vertices[ edge[ 1 ] ][ 0 ], c.vertices[ edge[ 1 ] ][ 1 ], c.vertices[ edge[ 2 ] ][ 0 ], c.vertices[ edge[ 2 ] ][ 1 ], width, height, exX, exY )
elif edge[ 1 ] >= 0: # only one vertex
if c.lines[ edge[ 0 ] ][ 1 ] == 0: # vertical line
xtemp = c.lines[ edge[ 0 ] ][ 2 ] / c.lines[ edge[ 0 ] ][ 0 ]
if c.vertices[ edge[ 1 ] ][ 1 ] > ( height + exY ) / 2:
ytemp = height + exY
else:
ytemp = 0-exX
ytemp = 0 - exX
else:
xtemp = width+exX
ytemp = (c.lines[edge[0]][2] - (width+exX)*c.lines[edge[0]][0])/c.lines[edge[0]][1]
[x1, y1, x2, y2] = clip_line(c.vertices[edge[1]][0], c.vertices[edge[1]][1], xtemp, ytemp, width, height, exX, exY)
elif edge[2] >= 0: # only one vertex
if c.lines[edge[0]][1] == 0: # vertical line
xtemp = c.lines[edge[0]][2]/c.lines[edge[0]][0]
if c.vertices[edge[2]][1] > (height+exY)/2:
ytemp = height+exY
xtemp = width + exX
ytemp = ( c.lines[ edge[ 0 ] ][ 2 ] - ( width + exX ) * c.lines[ edge[ 0 ] ][ 0 ] ) / c.lines[ edge[ 0 ] ][ 1 ]
[ x1, y1, x2, y2 ] = clip_line( c.vertices[ edge[ 1 ] ][ 0 ], c.vertices[ edge[ 1 ] ][ 1 ], xtemp, ytemp, width, height, exX, exY )
elif edge[ 2 ] >= 0: # only one vertex
if c.lines[ edge[ 0 ] ][ 1 ] == 0: # vertical line
xtemp = c.lines[ edge[ 0 ] ][ 2 ] / c.lines[ edge[ 0 ] ][ 0 ]
if c.vertices[ edge[ 2 ] ][ 1 ] > ( height + exY ) / 2:
ytemp = height + exY
else:
ytemp = 0.0-exY
ytemp = 0.0 - exY
else:
xtemp = 0.0-exX
ytemp = c.lines[edge[0]][2]/c.lines[edge[0]][1]
[x1, y1, x2, y2] = clip_line(xtemp, ytemp, c.vertices[edge[2]][0], c.vertices[edge[2]][1], width, height, exX, exY)
xtemp = 0.0 - exX
ytemp = c.lines[ edge[ 0 ] ][ 2 ] / c.lines[ edge[ 0 ] ][ 1 ]
[ x1, y1, x2, y2 ] = clip_line( xtemp, ytemp, c.vertices[ edge[ 2 ] ][ 0 ], c.vertices[ edge[ 2 ] ][ 1 ], width, height, exX, exY )
if x1 or x2 or y1 or y2:
lines.append(QgsPoint(x1+extent.xMinimum(),y1+extent.yMinimum()))
lines.append(QgsPoint(x2+extent.xMinimum(),y2+extent.yMinimum()))
if 0-exX in (x1, x2):
lines.append( QgsPoint( x1 + extent.xMinimum(), y1 + extent.yMinimum() ) )
lines.append( QgsPoint( x2 + extent.xMinimum(), y2 + extent.yMinimum() ) )
if 0 - exX in ( x1, x2 ):
hasXMin = True
if 0-exY in (y1, y2):
if 0 - exY in ( y1, y2 ):
hasYMin = True
if height+exY in (y1, y2):
if height + exY in ( y1, y2 ):
hasYMax = True
if width+exX in (x1, x2):
if width + exX in ( x1, x2 ):
hasXMax = True
if hasXMin:
if hasYMax:
lines.append(QgsPoint(extent.xMinimum()-exX, height+extent.yMinimum()+exY))
lines.append( QgsPoint( extent.xMinimum() - exX, height + extent.yMinimum() + exY ) )
if hasYMin:
lines.append(QgsPoint(extent.xMinimum()-exX, extent.yMinimum()-exY))
lines.append( QgsPoint( extent.xMinimum() - exX, extent.yMinimum() - exY ) )
if hasXMax:
if hasYMax:
lines.append(QgsPoint(width+extent.xMinimum()+exX, height+extent.yMinimum()+exY))
lines.append( QgsPoint( width + extent.xMinimum() + exX, height + extent.yMinimum() + exY ) )
if hasYMin:
lines.append(QgsPoint(width+extent.xMinimum()+exX, extent.yMinimum()-exY))
lines.append( QgsPoint( width + extent.xMinimum() + exX, extent.yMinimum() - exY ) )
return lines

def layer_extent( self ):
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, 0 ) )
fields = {
0 : QgsField( "MINX", QVariant.Double ),
1 : QgsField( "MINY", QVariant.Double ),
2 : QgsField( "MAXX", QVariant.Double ),
3 : QgsField( "MAXY", QVariant.Double ),
4 : QgsField( "CNTX", QVariant.Double ),
5 : QgsField( "CNTY", QVariant.Double ),
6 : QgsField( "AREA", QVariant.Double ),
7 : QgsField( "PERIM", QVariant.Double ),
8 : QgsField( "HEIGHT", QVariant.Double ),
9 : QgsField( "WIDTH", QVariant.Double ) }

writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, QGis.WKBPolygon, self.vlayer.crs() )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, 0 ) )
fields = { 0 : QgsField( "MINX", QVariant.Double ),
1 : QgsField( "MINY", QVariant.Double ),
2 : QgsField( "MAXX", QVariant.Double ),
3 : QgsField( "MAXY", QVariant.Double ),
4 : QgsField( "CNTX", QVariant.Double ),
5 : QgsField( "CNTY", QVariant.Double ),
6 : QgsField( "AREA", QVariant.Double ),
7 : QgsField( "PERIM", QVariant.Double ),
8 : QgsField( "HEIGHT", QVariant.Double ),
9 : QgsField( "WIDTH", QVariant.Double ) }

writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
QGis.WKBPolygon, self.vlayer.crs() )
rect = self.vlayer.extent()
minx = rect.xMinimum()
miny = rect.yMinimum()
Expand All @@ -757,42 +779,46 @@ def layer_extent( self ):
cnty = miny + ( height / 2.0 )
area = width * height
perim = ( 2 * width ) + (2 * height )
rect = [
QgsPoint( minx, miny ),
QgsPoint( minx, maxy ),
QgsPoint( maxx, maxy ),
QgsPoint( maxx, miny ),
QgsPoint( minx, miny ) ]
rect = [ QgsPoint( minx, miny ),
QgsPoint( minx, maxy ),
QgsPoint( maxx, maxy ),
QgsPoint( maxx, miny ),
QgsPoint( minx, miny ) ]
geometry = QgsGeometry().fromPolygon( [ rect ] )
feat = QgsFeature()
feat.setGeometry( geometry )
feat.setAttributeMap( {
0 : QVariant( minx ),
1 : QVariant( miny ),
2 : QVariant( maxx ),
3 : QVariant( maxy ),
4 : QVariant( cntx ),
5 : QVariant( cnty ),
6 : QVariant( area ),
7 : QVariant( perim ),
8 : QVariant( height ),
9 : QVariant( width ) } )
feat.setAttributeMap( { 0 : QVariant( minx ),
1 : QVariant( miny ),
2 : QVariant( maxx ),
3 : QVariant( maxy ),
4 : QVariant( cntx ),
5 : QVariant( cnty ),
6 : QVariant( area ),
7 : QVariant( perim ),
8 : QVariant( height ),
9 : QVariant( width ) } )
writer.addFeature( feat )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, 100 ) )
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, 100 ) )
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
del writer

return True

def simpleMeasure( self, inGeom ):
if inGeom.wkbType() in (QGis.WKBPoint, QGis.WKBPoint25D):
def simpleMeasure( self, inGeom, calcType, ellips, crs ):
if inGeom.wkbType() in ( QGis.WKBPoint, QGis.WKBPoint25D ):
pt = QgsPoint()
pt = inGeom.asPoint()
attr1 = pt.x()
attr2 = pt.y()
else:
measure = QgsDistanceArea()
attr1 = measure.measure(inGeom)

if calcType == 2:
measure.setSourceCrs( crs )
measure.setEllipsoid( ellips )
measure.setProjectionsEnabled( True )

attr1 = measure.measure( inGeom )
if inGeom.type() == QGis.Polygon:
attr2 = self.perimMeasure( inGeom, measure )
else:
Expand All @@ -814,7 +840,7 @@ def perimMeasure( self, inGeom, measure ):

def checkForField( self, L, e ):
e = QString( e ).toLower()
fieldRange = range( 0,len( L ) )
fieldRange = range( 0, len( L ) )
for item in fieldRange:
if L[ item ].toLower() == e:
return True, item
Expand All @@ -830,37 +856,38 @@ def checkGeometryFields( self, vlayer ):
if geomType == QGis.Polygon:
plp = "Poly"
( found, index1 ) = self.checkForField( nameList, "AREA" )

if not found:
field = QgsField( "AREA", QVariant.Double, "double", 21, 6, self.tr("Polygon area") )
field = QgsField( "AREA", QVariant.Double, "double", 21, 6, self.tr( "Polygon area" ) )
index1 = len( fieldList.keys() )
fieldList[ index1 ] = field
( found, index2 ) = self.checkForField( nameList, "PERIMETER" )

if not found:
field = QgsField( "PERIMETER", QVariant.Double, "double", 21, 6, self.tr("Polygon perimeter") )
field = QgsField( "PERIMETER", QVariant.Double, "double", 21, 6, self.tr( "Polygon perimeter" ) )
index2 = len( fieldList.keys() )
fieldList[ index2 ] = field
elif geomType == QGis.Line:
plp = "Line"
(found, index1) = self.checkForField(nameList, "LENGTH")
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
if not found:
field = QgsField("LENGTH", QVariant.Double, "double", 21, 6, self.tr("Line length") )
index1 = len(fieldList.keys())
fieldList[index1] = field
field = QgsField( "LENGTH", QVariant.Double, "double", 21, 6, self.tr( "Line length" ) )
index1 = len( fieldList.keys() )
fieldList[ index1 ] = field
index2 = index1
else:
plp = "Point"
(found, index1) = self.checkForField(nameList, "XCOORD")
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
if not found:
field = QgsField("XCOORD", QVariant.Double, "double", 21, 6, self.tr("Point x coordinate") )
index1 = len(fieldList.keys())
fieldList[index1] = field
(found, index2) = self.checkForField(nameList, "YCOORD")
field = QgsField( "XCOORD", QVariant.Double, "double", 21, 6, self.tr( "Point x coordinate" ) )
index1 = len( fieldList.keys() )
fieldList[ index1 ] = field
( found, index2 ) = self.checkForField( nameList, "YCOORD" )
if not found:
field = QgsField("YCOORD", QVariant.Double, "double", 21, 6, self.tr("Point y coordinate") )
index2 = len(fieldList.keys())
fieldList[index2] = field
return (fieldList, index1, index2)
field = QgsField( "YCOORD", QVariant.Double, "double", 21, 6, self.tr( "Point y coordinate" ) )
index2 = len( fieldList.keys() )
fieldList[ index2 ] = field
return ( fieldList, index1, index2 )

def extractAsLine( self, geom ):
multi_geom = QgsGeometry()
Expand All @@ -869,7 +896,7 @@ def extractAsLine( self, geom ):
if geom.isMultipart():
multi_geom = geom.asMultiPolygon()
for i in multi_geom:
temp_geom.extend(i)
temp_geom.extend( i )
else:
multi_geom = geom.asPolygon()
temp_geom = multi_geom
Expand All @@ -879,46 +906,46 @@ def extractAsLine( self, geom ):

def remove_bad_lines( self, lines ):
temp_geom = []
if len(lines)==1:
if len(lines[0]) > 2:
if len( lines ) == 1:
if len( lines[ 0 ] ) > 2:
temp_geom = lines
else:
temp_geom = []
else:
temp_geom = [elem for elem in lines if len(elem) > 2]
temp_geom = [ elem for elem in lines if len( elem ) > 2 ]
return temp_geom

def singleToMultiGeom(self, wkbType):
try:
if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint,
QGis.WKBPoint25D, QGis.WKBMultiPoint25D):
return QGis.WKBMultiPoint
elif wkbType in (QGis.WKBLineString, QGis.WKBMultiLineString,
QGis.WKBMultiLineString25D, QGis.WKBLineString25D):
return QGis.WKBMultiLineString
elif wkbType in (QGis.WKBPolygon, QGis.WKBMultiPolygon,
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D):
return QGis.WKBMultiPolygon
else:
return QGis.WKBUnknown
except Exception, err:
print str(err)

def multiToSingleGeom(self, wkbType):
try:
if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint,
QGis.WKBPoint25D, QGis.WKBMultiPoint25D):
return QGis.WKBPoint
elif wkbType in (QGis.WKBLineString, QGis.WKBMultiLineString,
QGis.WKBMultiLineString25D, QGis.WKBLineString25D):
return QGis.WKBLineString
elif wkbType in (QGis.WKBPolygon, QGis.WKBMultiPolygon,
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D):
return QGis.WKBPolygon
else:
return QGis.WKBUnknown
except Exception, err:
print str(err)
def singleToMultiGeom( self, wkbType ):
try:
if wkbType in ( QGis.WKBPoint, QGis.WKBMultiPoint,
QGis.WKBPoint25D, QGis.WKBMultiPoint25D ):
return QGis.WKBMultiPoint
elif wkbType in ( QGis.WKBLineString, QGis.WKBMultiLineString,
QGis.WKBMultiLineString25D, QGis.WKBLineString25D ):
return QGis.WKBMultiLineString
elif wkbType in ( QGis.WKBPolygon, QGis.WKBMultiPolygon,
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D ):
return QGis.WKBMultiPolygon
else:
return QGis.WKBUnknown
except Exception, err:
print str( err )

def multiToSingleGeom( self, wkbType ):
try:
if wkbType in ( QGis.WKBPoint, QGis.WKBMultiPoint,
QGis.WKBPoint25D, QGis.WKBMultiPoint25D ):
return QGis.WKBPoint
elif wkbType in ( QGis.WKBLineString, QGis.WKBMultiLineString,
QGis.WKBMultiLineString25D, QGis.WKBLineString25D ):
return QGis.WKBLineString
elif wkbType in ( QGis.WKBPolygon, QGis.WKBMultiPolygon,
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D ):
return QGis.WKBPolygon
else:
return QGis.WKBUnknown
except Exception, err:
print str( err )

def extractAsSingle( self, geom ):
multi_geom = QgsGeometry()
Expand Down Expand Up @@ -966,8 +993,8 @@ def extractAsMulti( self, geom ):

def convertGeometry( self, geom_list, vType ):
if vType == 0:
return QgsGeometry().fromMultiPoint(geom_list)
return QgsGeometry().fromMultiPoint( geom_list )
elif vType == 1:
return QgsGeometry().fromMultiPolyline(geom_list)
return QgsGeometry().fromMultiPolyline( geom_list )
else:
return QgsGeometry().fromMultiPolygon(geom_list)
return QgsGeometry().fromMultiPolygon( geom_list )
193 changes: 104 additions & 89 deletions python/plugins/fTools/tools/frmGeometry.ui
Original file line number Diff line number Diff line change
@@ -1,128 +1,86 @@
<ui version="4.0" >
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog" >
<property name="windowModality" >
<widget class="QDialog" name="Dialog">
<property name="windowModality">
<enum>Qt::NonModal</enum>
</property>
<property name="geometry" >
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>390</width>
<height>296</height>
</rect>
</property>
<property name="windowTitle" >
<property name="windowTitle">
<string>Extract Nodes</string>
</property>
<property name="sizeGripEnabled" >
<property name="sizeGripEnabled">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout" >
<item row="0" column="0" colspan="2" >
<layout class="QVBoxLayout" >
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="label_3" >
<property name="text" >
<widget class="QLabel" name="label_3">
<property name="text">
<string>Input line or polygon vector layer</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="inShape" />
<widget class="QComboBox" name="inShape"/>
</item>
</layout>
</item>
<item row="1" column="0" colspan="2" >
<layout class="QHBoxLayout" >
<item>
<widget class="QLabel" name="label" >
<property name="text" >
<string>Tolerance</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0" >
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDoubleSpinBox" name="lineEdit" >
<property name="minimumSize" >
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="decimals" >
<number>4</number>
</property>
<property name="maximum" >
<double>100000.000000000000000</double>
</property>
<property name="singleStep" >
<double>0.500000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0" colspan="2" >
<widget class="QSplitter" name="splitter" >
<property name="orientation" >
<item row="2" column="0" colspan="2">
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QLabel" name="field_label" >
<property name="text" >
<widget class="QLabel" name="lblField">
<property name="text">
<string>Unique ID field</string>
</property>
</widget>
<widget class="QComboBox" name="cmbField" />
<widget class="QComboBox" name="cmbField"/>
</widget>
</item>
<item row="3" column="0" >
<item row="4" column="0">
<spacer>
<property name="orientation" >
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0" >
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>21</height>
</size>
</property>
</spacer>
</item>
<item row="4" column="0" colspan="2" >
<layout class="QVBoxLayout" >
<item row="5" column="0" colspan="2">
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="label_2" >
<property name="text" >
<widget class="QLabel" name="label_2">
<property name="text">
<string>Output point shapefile</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" >
<layout class="QHBoxLayout">
<item>
<widget class="QLineEdit" name="outShape" >
<property name="readOnly" >
<widget class="QLineEdit" name="outShape">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolOut" >
<property name="text" >
<widget class="QToolButton" name="toolOut">
<property name="text">
<string>Browse</string>
</property>
</widget>
Expand All @@ -131,42 +89,99 @@
</item>
</layout>
</item>
<item row="5" column="0" >
<item row="6" column="0">
<spacer>
<property name="orientation" >
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0" >
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>21</height>
</size>
</property>
</spacer>
</item>
<item row="6" column="0" >
<widget class="QProgressBar" name="progressBar" >
<property name="value" >
<item row="7" column="0">
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>0</number>
</property>
<property name="alignment" >
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="textVisible" >
<property name="textVisible">
<bool>true</bool>
</property>
</widget>
</item>
<item row="6" column="1" >
<widget class="QDialogButtonBox" name="buttonBox_2" >
<property name="orientation" >
<item row="7" column="1">
<widget class="QDialogButtonBox" name="buttonBox_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons" >
<property name="standardButtons">
<set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Tolerance</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDoubleSpinBox" name="lineEdit">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="decimals">
<number>4</number>
</property>
<property name="maximum">
<double>100000.000000000000000</double>
</property>
<property name="singleStep">
<double>0.500000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0" colspan="2">
<layout class="QHBoxLayout" name="_2">
<item>
<widget class="QLabel" name="lblCalcType">
<property name="text">
<string>Calculate using</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cmbCalcType"/>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
Expand All @@ -177,11 +192,11 @@
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel" >
<hint type="sourcelabel">
<x>133</x>
<y>512</y>
</hint>
<hint type="destinationlabel" >
<hint type="destinationlabel">
<x>215</x>
<y>290</y>
</hint>
Expand All @@ -193,11 +208,11 @@
<receiver>Dialog</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel" >
<hint type="sourcelabel">
<x>59</x>
<y>512</y>
</hint>
<hint type="destinationlabel" >
<hint type="destinationlabel">
<x>132</x>
<y>239</y>
</hint>
Expand Down