Skip to content

Commit

Permalink
add option to select shapefile geometry in merge shapes tool
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@15370 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
alexbruy committed Mar 6, 2011
1 parent 19e123f commit 8d1b9e5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
25 changes: 17 additions & 8 deletions python/plugins/fTools/tools/doMergeShapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ def changeMode( self ):
self.label.setText( self.tr( "Input files" ) )
QObject.disconnect( self.btnSelectDir, SIGNAL( "clicked()" ), self.inputDir )
QObject.connect( self.btnSelectDir, SIGNAL( "clicked()" ), self.inputFile )
self.lblGeometry.setEnabled( False )
self.cmbGeometry.setEnabled( False )
else:
self.label.setText( self.tr( "Input directory" ) )
QObject.disconnect( self.btnSelectDir, SIGNAL( "clicked()" ), self.inputFile )
QObject.connect( self.btnSelectDir, SIGNAL( "clicked()" ), self.inputDir )
self.lblGeometry.setEnabled( True )
self.cmbGeometry.setEnabled( True )

def reject( self ):
QDialog.reject( self )
Expand All @@ -95,25 +99,28 @@ def accept( self ):
self.inputFiles = None
return

self.progressFiles.setRange( 0, self.inputFiles.count() )

if self.outFileName is None:
QMessageBox.warning( self, self.tr( "No output file" ),
self.tr( "Please specify output file." ) )
return

outFile = QFile( self.outFileName )
if outFile.exists():
if not QgsVectorFileWriter.deleteShapeFile( self.outFileName ):
QMessageBox.warning( self, self.tr( "Delete error" ), self.tr( "Can't delete file %1" ).arg( outFileName ) )
return

if self.chkListMode.isChecked():
files = self.leInputDir.text().split( ";" )
baseDir = QFileInfo( files[ 0 ] ).absolutePath()
else:
baseDir = self.leInputDir.text()

# look for shapes with specified geometry type
self.inputFiles = ftools_utils.getShapesByGeometryType( baseDir, self.inputFiles, self.cmbGeometry.currentIndex() )

self.progressFiles.setRange( 0, self.inputFiles.count() )

outFile = QFile( self.outFileName )
if outFile.exists():
if not QgsVectorFileWriter.deleteShapeFile( self.outFileName ):
QMessageBox.warning( self, self.tr( "Delete error" ), self.tr( "Can't delete file %1" ).arg( outFileName ) )
return

QApplication.setOverrideCursor( QCursor( Qt.WaitCursor ) )
self.btnOk.setEnabled( False )

Expand Down Expand Up @@ -197,6 +204,8 @@ def run( self ):
for fileName in self.shapes:
layerPath = QFileInfo( self.baseDir + "/" + fileName ).absoluteFilePath()
newLayer = QgsVectorLayer( layerPath, QFileInfo( layerPath ).baseName(), "ogr" )
if not newLayer.isValid():
continue
vprovider = newLayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
Expand Down
32 changes: 31 additions & 1 deletion python/plugins/fTools/tools/frmMergeShapes.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>377</width>
<height>273</height>
<height>302</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -21,6 +21,36 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="lblGeometry">
<property name="text">
<string>Shapefile type</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cmbGeometry">
<item>
<property name="text">
<string>Polygon</string>
</property>
</item>
<item>
<property name="text">
<string>Line</string>
</property>
</item>
<item>
<property name="text">
<string>Point</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
Expand Down
20 changes: 20 additions & 0 deletions python/plugins/fTools/tools/ftools_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,23 @@ def getUniqueValuesCount( vlayer, fieldIndex, useSelection ):
values.append( feat.attributeMap()[ fieldIndex ].toString() )
count += 1
return count

def getShapesByGeometryType( baseDir, inShapes, geomType ):
outShapes = QStringList()
for fileName in inShapes:
layerPath = QFileInfo( baseDir + "/" + fileName ).absoluteFilePath()
vLayer = QgsVectorLayer( layerPath, QFileInfo( layerPath ).baseName(), "ogr" )
if not vLayer.isValid():
continue
layerGeometry = vLayer.geometryType()
if layerGeometry == QGis.Polygon and geomType == 0:
outShapes << fileName
elif layerGeometry == QGis.Line and geomType == 1:
outShapes << fileName
elif layerGeometry == QGis.Point and geomType == 2:
outShapes << fileName

if outShapes.count() == 0:
return None

return outShapes

0 comments on commit 8d1b9e5

Please sign in to comment.