Skip to content

Commit

Permalink
[FEATURE] Adds 'Lines to polygons' tool to fTools. Thanks to Piotr Po…
Browse files Browse the repository at this point in the history
…ciask for the patch.

git-svn-id: http://svn.osgeo.org/qgis/trunk@15290 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Feb 28, 2011
1 parent 8781ff4 commit 84bab93
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
9 changes: 8 additions & 1 deletion python/plugins/fTools/fTools.py
Expand Up @@ -109,6 +109,7 @@ def updateThemeIcons( self, theme ):
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") ) )
Expand Down Expand Up @@ -175,8 +176,9 @@ def initGui( self ):
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() )
Expand Down Expand Up @@ -236,6 +238,7 @@ def initGui( self ):
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 )

Expand All @@ -259,6 +262,10 @@ def dopolysToLines( self ):
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()
Expand Down
58 changes: 58 additions & 0 deletions python/plugins/fTools/tools/doGeometry.py
Expand Up @@ -114,6 +114,15 @@ def manageGui(self):
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" ) )
Expand All @@ -133,6 +142,8 @@ def manageGui(self):
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 )
Expand All @@ -148,6 +159,7 @@ def manageGui(self):
#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:
Expand Down Expand Up @@ -242,6 +254,8 @@ def run( self ):
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 )

Expand Down Expand Up @@ -393,6 +407,39 @@ def polygons_to_lines( self ):
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()
Expand Down Expand Up @@ -798,6 +845,17 @@ def extractAsLine( self, geom ):
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,
Expand Down

0 comments on commit 84bab93

Please sign in to comment.