[QGIS Commit] r15290 - in trunk/qgis/python/plugins/fTools: . tools
svn_qgis at osgeo.org
svn_qgis at osgeo.org
Mon Feb 28 17:35:47 EST 2011
Author: cfarmer
Date: 2011-02-28 14:35:47 -0800 (Mon, 28 Feb 2011)
New Revision: 15290
Modified:
trunk/qgis/python/plugins/fTools/fTools.py
trunk/qgis/python/plugins/fTools/tools/doGeometry.py
Log:
[FEATURE] Adds 'Lines to polygons' tool to fTools. Thanks to Piotr Pociask for the patch.
Modified: trunk/qgis/python/plugins/fTools/fTools.py
===================================================================
--- trunk/qgis/python/plugins/fTools/fTools.py 2011-02-28 22:31:07 UTC (rev 15289)
+++ trunk/qgis/python/plugins/fTools/fTools.py 2011-02-28 22:35:47 UTC (rev 15290)
@@ -109,6 +109,7 @@
self.multiToSingle.setIcon( QIcon( self.getThemeIcon( "multi_to_single.png") ) )
self.singleToMulti.setIcon( QIcon( self.getThemeIcon( "single_to_multi.png") ) )
self.polysToLines.setIcon( QIcon( self.getThemeIcon( "to_lines.png") ) )
+ self.linesToPolys.setIcon( QIcon( self.getThemeIcon( "to_lines.png") ) )
self.dataManageMenu.setIcon( QIcon( self.getThemeIcon( "management.png") ) )
self.project.setIcon( QIcon( self.getThemeIcon( "export_projection.png") ) )
@@ -175,8 +176,9 @@
self.multiToSingle = QAction( QCoreApplication.translate( "fTools", "Multipart to singleparts" ),self.iface.mainWindow() )
self.singleToMulti = QAction( QCoreApplication.translate( "fTools", "Singleparts to multipart" ),self.iface.mainWindow() )
self.polysToLines = QAction( QCoreApplication.translate( "fTools", "Polygons to lines" ),self.iface.mainWindow() )
+ self.linesToPolys = QAction( QCoreApplication.translate( "fTools", "Lines to polygons" ),self.iface.mainWindow() )
self.conversionMenu.addActions( [ self.checkGeom, self.compGeo, self.centroids, self.delaunay, self.voronoi,
- self.simplify, self.multiToSingle, self.singleToMulti, self.polysToLines, self.extNodes] )
+ self.simplify, self.multiToSingle, self.singleToMulti, self.polysToLines, self.linesToPolys, self.extNodes] )
self.dataManageMenu = QMenu( QCoreApplication.translate( "fTools", "&Data Management Tools") )
self.project = QAction( QCoreApplication.translate( "fTools", "Export to new projection" ), self.iface.mainWindow() )
@@ -236,6 +238,7 @@
QObject.connect( self.delaunay, SIGNAL("triggered()"), self.dodelaunay )
QObject.connect( self.voronoi, SIGNAL("triggered()"), self.dovoronoi )
QObject.connect( self.polysToLines, SIGNAL("triggered()"), self.dopolysToLines )
+ QObject.connect( self.linesToPolys, SIGNAL("triggered()"), self.dolinesToPolys )
QObject.connect( self.compGeo, SIGNAL("triggered()"), self.docompGeo )
QObject.connect( self.extNodes, SIGNAL("triggered()"), self.doextNodes )
@@ -259,6 +262,10 @@
d = doGeometry.GeometryDialog( self.iface, 4 )
d.exec_()
+ def dolinesToPolys( self ):
+ d = doGeometry.GeometryDialog( self.iface, 11 )
+ d.exec_()
+
def docheckGeom( self ):
d = doValidate.ValidateDialog(self.iface)
d.show()
Modified: trunk/qgis/python/plugins/fTools/tools/doGeometry.py
===================================================================
--- trunk/qgis/python/plugins/fTools/tools/doGeometry.py 2011-02-28 22:31:07 UTC (rev 15289)
+++ trunk/qgis/python/plugins/fTools/tools/doGeometry.py 2011-02-28 22:35:47 UTC (rev 15290)
@@ -114,6 +114,15 @@
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)
+
else: # Polygon from layer extent
self.setWindowTitle( self.tr( "Polygon from layer extent" ) )
self.label_3.setText( self.tr( "Input layer" ) )
@@ -133,6 +142,8 @@
myList = ftools_utils.getLayerNames( [ QGis.Point ] )
elif self.myFunction == 9:
myList = ftools_utils.getLayerNames( "all" )
+ elif self.myFunction == 11:
+ myList = ftools_utils.getLayerNames( [ QGis.Line ] )
else:
myList = ftools_utils.getLayerNames( [ QGis.Point, QGis.Line, QGis.Polygon ] )
self.inShape.addItems( myList )
@@ -148,6 +159,7 @@
#8: Delaunay triangulation
#9: Polygon from layer extent
#10:Voronoi polygons
+#11: Lines to polygons
def geometry( self, myLayer, myParam, myField ):
if self.myFunction == 9:
@@ -242,6 +254,8 @@
success = self.layer_extent()
elif self.myFunction == 10: # Voronoi Polygons
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 )
@@ -393,6 +407,39 @@
del writer
return True
+ def lines_to_polygons( self ):
+ vprovider = self.vlayer.dataProvider()
+ allAttrs = vprovider.attributeIndexes()
+ vprovider.select( allAttrs )
+ fields = vprovider.fields()
+ 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):
+ outGeomList = []
+ multi = False
+ nElement += 1
+ 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:
+ outFeat.setGeometry( QgsGeometry.fromPolygon( polyGeom ) )
+ atMap = inFeat.attributeMap()
+ outFeat.setAttributeMap( atMap )
+ writer.addFeature( outFeat )
+ del writer
+ return True
+
def export_geometry_info( self ):
vprovider = self.vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
@@ -798,6 +845,17 @@
else:
return []
+ def remove_bad_lines( self, lines ):
+ temp_geom = []
+ 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]
+ return temp_geom
+
def singleToMultiGeom(self, wkbType):
try:
if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint,
More information about the QGIS-commit
mailing list