[FATURE] fTools: option to export geometry column using layer CRS,

project CRS or ellipsoid
This commit is contained in:
Alexander Bruy 2012-02-01 20:31:58 +02:00
parent 5b800befeb
commit 8fda99366b
2 changed files with 470 additions and 428 deletions

View File

@ -39,7 +39,6 @@ import voronoi
from sets import Set from sets import Set
class GeometryDialog( QDialog, Ui_Dialog ): class GeometryDialog( QDialog, Ui_Dialog ):
def __init__( self, iface, function ): def __init__( self, iface, function ):
QDialog.__init__( self, iface.mainWindow() ) QDialog.__init__( self, iface.mainWindow() )
self.iface = iface self.iface = iface
@ -66,13 +65,17 @@ class GeometryDialog(QDialog, Ui_Dialog):
def accept( self ): def accept( self ):
if self.inShape.currentText() == "": 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() == "": 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: 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() == "": 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: else:
self.outShape.clear() self.outShape.clear()
self.geometry( self.inShape.currentText(), self.lineEdit.value(), self.cmbField.currentText() ) self.geometry( self.inShape.currentText(), self.lineEdit.value(), self.cmbField.currentText() )
@ -85,50 +88,51 @@ class GeometryDialog(QDialog, Ui_Dialog):
self.outShape.setText( QString( self.shapefileName ) ) self.outShape.setText( QString( self.shapefileName ) )
def manageGui( self ): def manageGui( self ):
self.lblField.setVisible( False )
self.cmbField.setVisible( False )
self.lblCalcType.setVisible( False )
self.cmbCalcType.setVisible( False )
if self.myFunction == 1: # Singleparts to multipart if self.myFunction == 1: # Singleparts to multipart
self.setWindowTitle( self.tr( "Singleparts to multipart" ) ) self.setWindowTitle( self.tr( "Singleparts to multipart" ) )
self.lineEdit.setVisible( False ) self.lineEdit.setVisible( False )
self.label.setVisible( False ) self.label.setVisible( False )
self.label_2.setText( self.tr( "Output shapefile" ) ) self.label_2.setText( self.tr( "Output shapefile" ) )
self.cmbField.setVisible( True ) self.cmbField.setVisible( True )
self.field_label.setVisible(True) self.lblField.setVisible( True )
elif self.myFunction == 2: # Multipart to singleparts elif self.myFunction == 2: # Multipart to singleparts
self.setWindowTitle( self.tr( "Multipart to singleparts" ) ) self.setWindowTitle( self.tr( "Multipart to singleparts" ) )
self.lineEdit.setVisible( False ) self.lineEdit.setVisible( False )
self.label.setVisible( False ) self.label.setVisible( False )
self.label_2.setText( self.tr( "Output shapefile" ) ) self.label_2.setText( self.tr( "Output shapefile" ) )
self.cmbField.setVisible(False)
self.field_label.setVisible(False)
elif self.myFunction == 3: # Extract nodes elif self.myFunction == 3: # Extract nodes
self.setWindowTitle( self.tr( "Extract nodes" ) ) self.setWindowTitle( self.tr( "Extract nodes" ) )
self.lineEdit.setVisible( False ) self.lineEdit.setVisible( False )
self.label.setVisible( False ) self.label.setVisible( False )
self.cmbField.setVisible(False)
self.field_label.setVisible(False)
elif self.myFunction == 4: # Polygons to lines elif self.myFunction == 4: # Polygons to lines
self.setWindowTitle( self.tr( "Polygons to lines" ) ) self.setWindowTitle( self.tr( "Polygons to lines" ) )
self.label_2.setText( self.tr( "Output shapefile" ) ) self.label_2.setText( self.tr( "Output shapefile" ) )
self.label_3.setText( self.tr( "Input polygon vector layer" ) ) self.label_3.setText( self.tr( "Input polygon vector layer" ) )
self.label.setVisible( False ) self.label.setVisible( False )
self.lineEdit.setVisible( False ) self.lineEdit.setVisible( False )
self.cmbField.setVisible(False)
self.field_label.setVisible(False)
elif self.myFunction == 5: # Export/Add geometry columns elif self.myFunction == 5: # Export/Add geometry columns
self.setWindowTitle( self.tr( "Export/Add geometry columns" ) ) self.setWindowTitle( self.tr( "Export/Add geometry columns" ) )
self.label_2.setText( self.tr( "Output shapefile" ) ) self.label_2.setText( self.tr( "Output shapefile" ) )
self.label_3.setText( self.tr( "Input vector layer" ) ) self.label_3.setText( self.tr( "Input vector layer" ) )
self.label.setVisible( False ) self.label.setVisible( False )
self.lineEdit.setVisible( False ) self.lineEdit.setVisible( False )
self.cmbField.setVisible(False) # populate calculation types
self.field_label.setVisible(False) 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 elif self.myFunction == 7: # Polygon centroids
self.setWindowTitle( self.tr( "Polygon centroids" ) ) self.setWindowTitle( self.tr( "Polygon centroids" ) )
self.label_2.setText( self.tr( "Output point shapefile" ) ) self.label_2.setText( self.tr( "Output point shapefile" ) )
self.label_3.setText( self.tr( "Input polygon vector layer" ) ) self.label_3.setText( self.tr( "Input polygon vector layer" ) )
self.label.setVisible( False ) self.label.setVisible( False )
self.lineEdit.setVisible( False ) self.lineEdit.setVisible( False )
self.cmbField.setVisible( False )
self.field_label.setVisible( False )
else: else:
if self.myFunction == 8: # Delaunay triangulation if self.myFunction == 8: # Delaunay triangulation
self.setWindowTitle( self.tr( "Delaunay triangulation" ) ) self.setWindowTitle( self.tr( "Delaunay triangulation" ) )
@ -149,17 +153,12 @@ class GeometryDialog(QDialog, Ui_Dialog):
self.label_3.setText( self.tr( "Input line vector layer" ) ) self.label_3.setText( self.tr( "Input line vector layer" ) )
self.label.setVisible( False ) self.label.setVisible( False )
self.lineEdit.setVisible( False ) self.lineEdit.setVisible( False )
self.cmbField.setVisible(False)
self.field_label.setVisible(False)
else: # Polygon from layer extent else: # Polygon from layer extent
self.setWindowTitle( self.tr( "Polygon from layer extent" ) ) self.setWindowTitle( self.tr( "Polygon from layer extent" ) )
self.label_3.setText( self.tr( "Input layer" ) ) self.label_3.setText( self.tr( "Input layer" ) )
self.label.setVisible( False ) self.label.setVisible( False )
self.lineEdit.setVisible( False ) self.lineEdit.setVisible( False )
self.label_2.setText( self.tr( "Output polygon shapefile" ) ) self.label_2.setText( self.tr( "Output polygon shapefile" ) )
self.cmbField.setVisible( False )
self.field_label.setVisible( False )
self.resize( 381, 100 ) self.resize( 381, 100 )
self.populateLayers() self.populateLayers()
@ -200,11 +199,12 @@ class GeometryDialog(QDialog, Ui_Dialog):
check = QFile( self.shapefileName ) check = QFile( self.shapefileName )
if check.exists(): if check.exists():
if not QgsVectorFileWriter.deleteShapeFile( self.shapefileName ): 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 return
self.buttonOk.setEnabled( False ) self.buttonOk.setEnabled( False )
self.testThread = geometryThread( self.iface.mainWindow(), self, self.myFunction, vlayer, myParam, self.testThread = geometryThread( self.iface.mainWindow(), self, self.myFunction, vlayer, myParam,
myField, self.shapefileName, self.encoding ) myField, self.shapefileName, self.encoding, self.cmbCalcType.currentIndex() )
QObject.connect( self.testThread, SIGNAL( "runFinished( PyQt_PyObject )" ), self.runFinishedFromThread ) 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( "runStatus( PyQt_PyObject )" ), self.runStatusFromThread )
QObject.connect( self.testThread, SIGNAL( "runRange( PyQt_PyObject )" ), self.runRangeFromThread ) QObject.connect( self.testThread, SIGNAL( "runRange( PyQt_PyObject )" ), self.runRangeFromThread )
@ -221,13 +221,17 @@ class GeometryDialog(QDialog, Ui_Dialog):
self.buttonOk.setEnabled( True ) self.buttonOk.setEnabled( True )
extra = "" extra = ""
if success == "math_error": 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 ): 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": 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 ): 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: else:
if success == "valid_error": if success == "valid_error":
extra = self.tr("One or more features in the output layer may have invalid " extra = self.tr("One or more features in the output layer may have invalid "
@ -241,7 +245,8 @@ class GeometryDialog(QDialog, Ui_Dialog):
QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton ) QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton )
if addToTOC == QMessageBox.Yes: if addToTOC == QMessageBox.Yes:
if not ftools_utils.addShapeToCanvas( unicode( self.shapefileName ) ): 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() self.populateLayers()
else: 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." ) )
@ -253,7 +258,7 @@ class GeometryDialog(QDialog, Ui_Dialog):
self.progressBar.setRange( range_vals[ 0 ], range_vals[ 1 ] ) self.progressBar.setRange( range_vals[ 0 ], range_vals[ 1 ] )
class geometryThread( QThread ): 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 ) QThread.__init__( self, parentThread )
self.parent = parentObject self.parent = parentObject
self.running = False self.running = False
@ -263,6 +268,7 @@ class geometryThread( QThread ):
self.myField = myField self.myField = myField
self.myName = myName self.myName = myName
self.myEncoding = myEncoding self.myEncoding = myEncoding
self.myCalcType = myCalcType
def run( self ): def run( self ):
self.running = True self.running = True
@ -300,8 +306,8 @@ class geometryThread( QThread ):
fields = vprovider.fields() fields = vprovider.fields()
allValid = True allValid = True
geomType = self.singleToMultiGeom( vprovider.geometryType() ) geomType = self.singleToMultiGeom( vprovider.geometryType() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, geomType, vprovider.crs() ) geomType, vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
inGeom = QgsGeometry() inGeom = QgsGeometry()
@ -316,8 +322,7 @@ class geometryThread( QThread ):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 ) self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) ) self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
merge_all = self.myField == QString( "--- " + self.tr( "Merge all" ) + " ---" ) merge_all = self.myField == QString( "--- " + self.tr( "Merge all" ) + " ---" )
if not len( unique ) == self.vlayer.featureCount() \ if not len( unique ) == self.vlayer.featureCount() or merge_all:
or merge_all:
for i in unique: for i in unique:
vprovider.rewind() vprovider.rewind()
multi_feature= [] multi_feature= []
@ -329,8 +334,7 @@ class geometryThread( QThread ):
idVar = atMap[ index ] idVar = atMap[ index ]
else: else:
idVar = QVariant( QString() ) idVar = QVariant( QString() )
if idVar.toString().trimmed() == i.toString().trimmed() \ if idVar.toString().trimmed() == i.toString().trimmed() or merge_all:
or merge_all:
if first: if first:
atts = atMap atts = atMap
first = False first = False
@ -357,8 +361,8 @@ class geometryThread( QThread ):
vprovider.select( allAttrs ) vprovider.select( allAttrs )
fields = vprovider.fields() fields = vprovider.fields()
geomType = self.multiToSingleGeom( vprovider.geometryType() ) geomType = self.multiToSingleGeom( vprovider.geometryType() )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, geomType, vprovider.crs() ) geomType, vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
inGeom = QgsGeometry() inGeom = QgsGeometry()
@ -385,8 +389,8 @@ class geometryThread( QThread ):
allAttrs = vprovider.attributeIndexes() allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs ) vprovider.select( allAttrs )
fields = vprovider.fields() fields = vprovider.fields()
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, QGis.WKBPoint, vprovider.crs() ) QGis.WKBPoint, vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
inGeom = QgsGeometry() inGeom = QgsGeometry()
@ -413,8 +417,8 @@ class geometryThread( QThread ):
allAttrs = vprovider.attributeIndexes() allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs ) vprovider.select( allAttrs )
fields = vprovider.fields() fields = vprovider.fields()
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, QGis.WKBLineString, vprovider.crs() ) QGis.WKBLineString, vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
inGeom = QgsGeometry() inGeom = QgsGeometry()
@ -444,8 +448,8 @@ class geometryThread( QThread ):
allAttrs = vprovider.attributeIndexes() allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs ) vprovider.select( allAttrs )
fields = vprovider.fields() fields = vprovider.fields()
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, QGis.WKBPolygon, vprovider.crs() ) QGis.WKBPolygon, vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
inGeom = QgsGeometry() inGeom = QgsGeometry()
@ -477,20 +481,41 @@ class geometryThread( QThread ):
allAttrs = vprovider.attributeIndexes() allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs ) vprovider.select( allAttrs )
( fields, index1, index2 ) = self.checkGeometryFields( self.vlayer ) ( fields, index1, index2 ) = self.checkGeometryFields( self.vlayer )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, vprovider.geometryType(), vprovider.crs() ) vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
inGeom = QgsGeometry() inGeom = QgsGeometry()
nFeat = vprovider.featureCount() nFeat = vprovider.featureCount()
nElement = 0 nElement = 0
# 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( "runStatus( PyQt_PyObject )" ), 0)
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) ) self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
while vprovider.nextFeature(inFeat): while vprovider.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement ) self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
nElement += 1 nElement += 1
inGeom = inFeat.geometry() 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 ) outFeat.setGeometry( inGeom )
atMap = inFeat.attributeMap() atMap = inFeat.attributeMap()
outFeat.setAttributeMap( atMap ) outFeat.setAttributeMap( atMap )
@ -505,8 +530,8 @@ class geometryThread( QThread ):
allAttrs = vprovider.attributeIndexes() allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs ) vprovider.select( allAttrs )
fields = vprovider.fields() fields = vprovider.fields()
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, QGis.WKBPoint, vprovider.crs() ) QGis.WKBPoint, vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
nFeat = vprovider.featureCount() nFeat = vprovider.featureCount()
@ -533,12 +558,11 @@ class geometryThread( QThread ):
vprovider = self.vlayer.dataProvider() vprovider = self.vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes() allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs ) vprovider.select( allAttrs )
fields = { fields = { 0 : QgsField( "POINTA", QVariant.Double ),
0 : QgsField( "POINTA", QVariant.Double ),
1 : QgsField( "POINTB", QVariant.Double ), 1 : QgsField( "POINTB", QVariant.Double ),
2 : QgsField( "POINTC", QVariant.Double ) } 2 : QgsField( "POINTC", QVariant.Double ) }
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, QGis.WKBPolygon, vprovider.crs() ) QGis.WKBPolygon, vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
c = voronoi.Context() c = voronoi.Context()
pts = [] pts = []
@ -589,8 +613,8 @@ class geometryThread( QThread ):
vprovider = self.vlayer.dataProvider() vprovider = self.vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes() allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs ) vprovider.select( allAttrs )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, vprovider.fields(),
vprovider.fields(), QGis.WKBPolygon, vprovider.crs() ) QGis.WKBPolygon, vprovider.crs() )
inFeat = QgsFeature() inFeat = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
extent = self.vlayer.extent() extent = self.vlayer.extent()
@ -635,7 +659,6 @@ class geometryThread( QThread ):
del writer del writer
return True 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 """ Clip voronoi function based on code written for Inkscape
Copyright (C) 2010 Alvin Penner, penner@vaxxine.com Copyright (C) 2010 Alvin Penner, penner@vaxxine.com
@ -732,8 +755,7 @@ class geometryThread( QThread ):
def layer_extent( self ): def layer_extent( self ):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 ) self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, 0 ) ) self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, 0 ) )
fields = { fields = { 0 : QgsField( "MINX", QVariant.Double ),
0 : QgsField( "MINX", QVariant.Double ),
1 : QgsField( "MINY", QVariant.Double ), 1 : QgsField( "MINY", QVariant.Double ),
2 : QgsField( "MAXX", QVariant.Double ), 2 : QgsField( "MAXX", QVariant.Double ),
3 : QgsField( "MAXY", QVariant.Double ), 3 : QgsField( "MAXY", QVariant.Double ),
@ -744,8 +766,8 @@ class geometryThread( QThread ):
8 : QgsField( "HEIGHT", QVariant.Double ), 8 : QgsField( "HEIGHT", QVariant.Double ),
9 : QgsField( "WIDTH", QVariant.Double ) } 9 : QgsField( "WIDTH", QVariant.Double ) }
writer = QgsVectorFileWriter( self.myName, self.myEncoding, writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
fields, QGis.WKBPolygon, self.vlayer.crs() ) QGis.WKBPolygon, self.vlayer.crs() )
rect = self.vlayer.extent() rect = self.vlayer.extent()
minx = rect.xMinimum() minx = rect.xMinimum()
miny = rect.yMinimum() miny = rect.yMinimum()
@ -757,8 +779,7 @@ class geometryThread( QThread ):
cnty = miny + ( height / 2.0 ) cnty = miny + ( height / 2.0 )
area = width * height area = width * height
perim = ( 2 * width ) + (2 * height ) perim = ( 2 * width ) + (2 * height )
rect = [ rect = [ QgsPoint( minx, miny ),
QgsPoint( minx, miny ),
QgsPoint( minx, maxy ), QgsPoint( minx, maxy ),
QgsPoint( maxx, maxy ), QgsPoint( maxx, maxy ),
QgsPoint( maxx, miny ), QgsPoint( maxx, miny ),
@ -766,8 +787,7 @@ class geometryThread( QThread ):
geometry = QgsGeometry().fromPolygon( [ rect ] ) geometry = QgsGeometry().fromPolygon( [ rect ] )
feat = QgsFeature() feat = QgsFeature()
feat.setGeometry( geometry ) feat.setGeometry( geometry )
feat.setAttributeMap( { feat.setAttributeMap( { 0 : QVariant( minx ),
0 : QVariant( minx ),
1 : QVariant( miny ), 1 : QVariant( miny ),
2 : QVariant( maxx ), 2 : QVariant( maxx ),
3 : QVariant( maxy ), 3 : QVariant( maxy ),
@ -784,7 +804,7 @@ class geometryThread( QThread ):
return True return True
def simpleMeasure( self, inGeom ): def simpleMeasure( self, inGeom, calcType, ellips, crs ):
if inGeom.wkbType() in ( QGis.WKBPoint, QGis.WKBPoint25D ): if inGeom.wkbType() in ( QGis.WKBPoint, QGis.WKBPoint25D ):
pt = QgsPoint() pt = QgsPoint()
pt = inGeom.asPoint() pt = inGeom.asPoint()
@ -792,6 +812,12 @@ class geometryThread( QThread ):
attr2 = pt.y() attr2 = pt.y()
else: else:
measure = QgsDistanceArea() measure = QgsDistanceArea()
if calcType == 2:
measure.setSourceCrs( crs )
measure.setEllipsoid( ellips )
measure.setProjectionsEnabled( True )
attr1 = measure.measure( inGeom ) attr1 = measure.measure( inGeom )
if inGeom.type() == QGis.Polygon: if inGeom.type() == QGis.Polygon:
attr2 = self.perimMeasure( inGeom, measure ) attr2 = self.perimMeasure( inGeom, measure )
@ -830,6 +856,7 @@ class geometryThread( QThread ):
if geomType == QGis.Polygon: if geomType == QGis.Polygon:
plp = "Poly" plp = "Poly"
( found, index1 ) = self.checkForField( nameList, "AREA" ) ( found, index1 ) = self.checkForField( nameList, "AREA" )
if not found: 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() ) index1 = len( fieldList.keys() )

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>Dialog</class> <class>Dialog</class>
<widget class="QDialog" name="Dialog"> <widget class="QDialog" name="Dialog">
@ -33,6 +34,97 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="2" column="0" colspan="2">
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QLabel" name="lblField">
<property name="text">
<string>Unique ID field</string>
</property>
</widget>
<widget class="QComboBox" name="cmbField"/>
</widget>
</item>
<item row="4" column="0">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>21</height>
</size>
</property>
</spacer>
</item>
<item row="5" column="0" colspan="2">
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Output point shapefile</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLineEdit" name="outShape">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolOut">
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item row="6" column="0">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>21</height>
</size>
</property>
</spacer>
</item>
<item row="7" column="0">
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>0</number>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="textVisible">
<bool>true</bool>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QDialogButtonBox" name="buttonBox_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2"> <item row="1" column="0" colspan="2">
<layout class="QHBoxLayout"> <layout class="QHBoxLayout">
<item> <item>
@ -76,98 +168,21 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="2" column="0" colspan="2" > <item row="3" column="0" colspan="2">
<widget class="QSplitter" name="splitter" > <layout class="QHBoxLayout" name="_2">
<property name="orientation" > <item>
<enum>Qt::Horizontal</enum> <widget class="QLabel" name="lblCalcType">
</property>
<widget class="QLabel" name="field_label" >
<property name="text"> <property name="text">
<string>Unique ID field</string> <string>Calculate using</string>
</property>
</widget>
<widget class="QComboBox" name="cmbField" />
</widget>
</item>
<item row="3" column="0" >
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<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>
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>Output point shapefile</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" > <widget class="QComboBox" name="cmbCalcType"/>
<item>
<widget class="QLineEdit" name="outShape" >
<property name="readOnly" >
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolOut" >
<property name="text" >
<string>Browse</string>
</property>
</widget>
</item> </item>
</layout> </layout>
</item> </item>
</layout> </layout>
</item>
<item row="5" column="0" >
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<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" >
<number>0</number>
</property>
<property name="alignment" >
<set>Qt::AlignCenter</set>
</property>
<property name="textVisible" >
<bool>true</bool>
</property>
</widget>
</item>
<item row="6" column="1" >
<widget class="QDialogButtonBox" name="buttonBox_2" >
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons" >
<set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget> </widget>
<resources/> <resources/>
<connections> <connections>