Skip to content

Commit 6dd571c

Browse files
author
cfarmer
committedFeb 28, 2011
[FEATURE] Adds 'Lines to polygons' tool to fTools. Thanks to Piotr Pociask for the patch.
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15290 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed
 

‎python/plugins/fTools/fTools.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def updateThemeIcons( self, theme ):
109109
self.multiToSingle.setIcon( QIcon( self.getThemeIcon( "multi_to_single.png") ) )
110110
self.singleToMulti.setIcon( QIcon( self.getThemeIcon( "single_to_multi.png") ) )
111111
self.polysToLines.setIcon( QIcon( self.getThemeIcon( "to_lines.png") ) )
112+
self.linesToPolys.setIcon( QIcon( self.getThemeIcon( "to_lines.png") ) )
112113

113114
self.dataManageMenu.setIcon( QIcon( self.getThemeIcon( "management.png") ) )
114115
self.project.setIcon( QIcon( self.getThemeIcon( "export_projection.png") ) )
@@ -175,8 +176,9 @@ def initGui( self ):
175176
self.multiToSingle = QAction( QCoreApplication.translate( "fTools", "Multipart to singleparts" ),self.iface.mainWindow() )
176177
self.singleToMulti = QAction( QCoreApplication.translate( "fTools", "Singleparts to multipart" ),self.iface.mainWindow() )
177178
self.polysToLines = QAction( QCoreApplication.translate( "fTools", "Polygons to lines" ),self.iface.mainWindow() )
179+
self.linesToPolys = QAction( QCoreApplication.translate( "fTools", "Lines to polygons" ),self.iface.mainWindow() )
178180
self.conversionMenu.addActions( [ self.checkGeom, self.compGeo, self.centroids, self.delaunay, self.voronoi,
179-
self.simplify, self.multiToSingle, self.singleToMulti, self.polysToLines, self.extNodes] )
181+
self.simplify, self.multiToSingle, self.singleToMulti, self.polysToLines, self.linesToPolys, self.extNodes] )
180182

181183
self.dataManageMenu = QMenu( QCoreApplication.translate( "fTools", "&Data Management Tools") )
182184
self.project = QAction( QCoreApplication.translate( "fTools", "Export to new projection" ), self.iface.mainWindow() )
@@ -236,6 +238,7 @@ def initGui( self ):
236238
QObject.connect( self.delaunay, SIGNAL("triggered()"), self.dodelaunay )
237239
QObject.connect( self.voronoi, SIGNAL("triggered()"), self.dovoronoi )
238240
QObject.connect( self.polysToLines, SIGNAL("triggered()"), self.dopolysToLines )
241+
QObject.connect( self.linesToPolys, SIGNAL("triggered()"), self.dolinesToPolys )
239242
QObject.connect( self.compGeo, SIGNAL("triggered()"), self.docompGeo )
240243
QObject.connect( self.extNodes, SIGNAL("triggered()"), self.doextNodes )
241244

@@ -259,6 +262,10 @@ def dopolysToLines( self ):
259262
d = doGeometry.GeometryDialog( self.iface, 4 )
260263
d.exec_()
261264

265+
def dolinesToPolys( self ):
266+
d = doGeometry.GeometryDialog( self.iface, 11 )
267+
d.exec_()
268+
262269
def docheckGeom( self ):
263270
d = doValidate.ValidateDialog(self.iface)
264271
d.show()

‎python/plugins/fTools/tools/doGeometry.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ def manageGui(self):
114114
self.lineEdit.setRange(0, 100)
115115
self.lineEdit.setSingleStep(5)
116116
self.lineEdit.setValue(0)
117+
elif self.myFunction == 11: #Lines to polygons
118+
self.setWindowTitle( self.tr( "Lines to polygons" ) )
119+
self.label_2.setText( self.tr( "Output shapefile" ) )
120+
self.label_3.setText( self.tr( "Input line vector layer" ) )
121+
self.label.setVisible(False)
122+
self.lineEdit.setVisible(False)
123+
self.cmbField.setVisible(False)
124+
self.field_label.setVisible(False)
125+
117126
else: # Polygon from layer extent
118127
self.setWindowTitle( self.tr( "Polygon from layer extent" ) )
119128
self.label_3.setText( self.tr( "Input layer" ) )
@@ -133,6 +142,8 @@ def manageGui(self):
133142
myList = ftools_utils.getLayerNames( [ QGis.Point ] )
134143
elif self.myFunction == 9:
135144
myList = ftools_utils.getLayerNames( "all" )
145+
elif self.myFunction == 11:
146+
myList = ftools_utils.getLayerNames( [ QGis.Line ] )
136147
else:
137148
myList = ftools_utils.getLayerNames( [ QGis.Point, QGis.Line, QGis.Polygon ] )
138149
self.inShape.addItems( myList )
@@ -148,6 +159,7 @@ def manageGui(self):
148159
#8: Delaunay triangulation
149160
#9: Polygon from layer extent
150161
#10:Voronoi polygons
162+
#11: Lines to polygons
151163

152164
def geometry( self, myLayer, myParam, myField ):
153165
if self.myFunction == 9:
@@ -242,6 +254,8 @@ def run( self ):
242254
success = self.layer_extent()
243255
elif self.myFunction == 10: # Voronoi Polygons
244256
success = self.voronoi_polygons()
257+
elif self.myFunction == 11: # Lines to polygons
258+
success = self.lines_to_polygons()
245259
self.emit( SIGNAL( "runFinished(PyQt_PyObject)" ), success )
246260
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
247261

@@ -393,6 +407,39 @@ def polygons_to_lines( self ):
393407
del writer
394408
return True
395409

410+
def lines_to_polygons( self ):
411+
vprovider = self.vlayer.dataProvider()
412+
allAttrs = vprovider.attributeIndexes()
413+
vprovider.select( allAttrs )
414+
fields = vprovider.fields()
415+
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
416+
fields, QGis.WKBPolygon, vprovider.crs() )
417+
inFeat = QgsFeature()
418+
outFeat = QgsFeature()
419+
inGeom = QgsGeometry()
420+
nFeat = vprovider.featureCount()
421+
nElement = 0
422+
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
423+
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
424+
while vprovider.nextFeature(inFeat):
425+
outGeomList = []
426+
multi = False
427+
nElement += 1
428+
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
429+
if inFeat.geometry().isMultipart():
430+
outGeomList = inFeat.geometry().asMultiPolyline()
431+
multi = True
432+
else:
433+
outGeomList.append( inFeat.geometry().asPolyline() )
434+
polyGeom = self.remove_bad_lines( outGeomList )
435+
if len(polyGeom) <> 0:
436+
outFeat.setGeometry( QgsGeometry.fromPolygon( polyGeom ) )
437+
atMap = inFeat.attributeMap()
438+
outFeat.setAttributeMap( atMap )
439+
writer.addFeature( outFeat )
440+
del writer
441+
return True
442+
396443
def export_geometry_info( self ):
397444
vprovider = self.vlayer.dataProvider()
398445
allAttrs = vprovider.attributeIndexes()
@@ -798,6 +845,17 @@ def extractAsLine( self, geom ):
798845
else:
799846
return []
800847

848+
def remove_bad_lines( self, lines ):
849+
temp_geom = []
850+
if len(lines)==1:
851+
if len(lines[0]) > 2:
852+
temp_geom = lines
853+
else:
854+
temp_geom = []
855+
else:
856+
temp_geom = [elem for elem in lines if len(elem) > 2]
857+
return temp_geom
858+
801859
def singleToMultiGeom(self, wkbType):
802860
try:
803861
if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint,

0 commit comments

Comments
 (0)
Please sign in to comment.