Skip to content

Commit 8fda993

Browse files
committedFeb 2, 2012
[FATURE] fTools: option to export geometry column using layer CRS,
project CRS or ellipsoid
1 parent 5b800be commit 8fda993

File tree

2 files changed

+472
-430
lines changed

2 files changed

+472
-430
lines changed
 

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

Lines changed: 368 additions & 341 deletions
Original file line numberDiff line numberDiff line change
@@ -38,97 +38,101 @@
3838
import voronoi
3939
from sets import Set
4040

41-
class GeometryDialog(QDialog, Ui_Dialog):
42-
43-
def __init__(self, iface, function):
44-
QDialog.__init__(self, iface.mainWindow())
41+
class GeometryDialog( QDialog, Ui_Dialog ):
42+
def __init__( self, iface, function ):
43+
QDialog.__init__( self, iface.mainWindow() )
4544
self.iface = iface
46-
self.setupUi(self)
45+
self.setupUi( self )
4746
self.myFunction = function
4847
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
49-
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
48+
QObject.connect( self.toolOut, SIGNAL( "clicked()" ), self.outFile )
5049
if self.myFunction == 1:
51-
QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.update)
50+
QObject.connect( self.inShape, SIGNAL( "currentIndexChanged( QString )" ), self.update )
5251
self.manageGui()
5352
self.success = False
5453
self.cancel_close = self.buttonBox_2.button( QDialogButtonBox.Close )
55-
self.progressBar.setValue(0)
54+
self.progressBar.setValue( 0 )
5655

57-
def update(self):
56+
def update( self ):
5857
self.cmbField.clear()
59-
inputLayer = unicode(self.inShape.currentText())
58+
inputLayer = unicode( self.inShape.currentText() )
6059
if inputLayer != "":
61-
changedLayer = ftools_utils.getVectorLayerByName(inputLayer)
62-
changedField = ftools_utils.getFieldList(changedLayer)
60+
changedLayer = ftools_utils.getVectorLayerByName( inputLayer )
61+
changedField = ftools_utils.getFieldList( changedLayer )
6362
for i in changedField:
64-
self.cmbField.addItem(unicode(changedField[i].name()))
65-
self.cmbField.addItem("--- " + self.tr( "Merge all" ) + " ---")
63+
self.cmbField.addItem( unicode( changedField[ i ].name() ) )
64+
self.cmbField.addItem( "--- " + self.tr( "Merge all" ) + " ---" )
6665

67-
def accept(self):
66+
def accept( self ):
6867
if self.inShape.currentText() == "":
69-
QMessageBox.information(self, self.tr("Geometry"), self.tr( "Please specify input vector layer" ) )
68+
QMessageBox.information( self, self.tr( "Geometry" ),
69+
self.tr( "Please specify input vector layer" ) )
7070
elif self.outShape.text() == "":
71-
QMessageBox.information(self, self.tr("Geometry"), self.tr( "Please specify output shapefile" ) )
71+
QMessageBox.information( self, self.tr( "Geometry" ),
72+
self.tr( "Please specify output shapefile" ) )
7273
elif self.lineEdit.isVisible() and self.lineEdit.value() < 0.00:
73-
QMessageBox.information(self, self.tr("Geometry"), self.tr( "Please specify valid tolerance value" ) )
74+
QMessageBox.information( self, self.tr( "Geometry" ),
75+
self.tr( "Please specify valid tolerance value" ) )
7476
elif self.cmbField.isVisible() and self.cmbField.currentText() == "":
75-
QMessageBox.information(self, self.tr("Geometry"), self.tr( "Please specify valid UID field" ) )
77+
QMessageBox.information( self, self.tr( "Geometry" ),
78+
self.tr( "Please specify valid UID field" ) )
7679
else:
7780
self.outShape.clear()
7881
self.geometry( self.inShape.currentText(), self.lineEdit.value(), self.cmbField.currentText() )
7982

8083
def outFile(self):
8184
self.outShape.clear()
82-
(self.shapefileName, self.encoding) = ftools_utils.saveDialog(self)
85+
(self.shapefileName, self.encoding) = ftools_utils.saveDialog( self )
8386
if self.shapefileName is None or self.encoding is None:
8487
return
85-
self.outShape.setText(QString(self.shapefileName))
88+
self.outShape.setText( QString( self.shapefileName ) )
89+
90+
def manageGui( self ):
91+
self.lblField.setVisible( False )
92+
self.cmbField.setVisible( False )
8693

87-
def manageGui(self):
94+
self.lblCalcType.setVisible( False )
95+
self.cmbCalcType.setVisible( False )
8896
if self.myFunction == 1: # Singleparts to multipart
8997
self.setWindowTitle( self.tr( "Singleparts to multipart" ) )
90-
self.lineEdit.setVisible(False)
91-
self.label.setVisible(False)
98+
self.lineEdit.setVisible( False )
99+
self.label.setVisible( False )
92100
self.label_2.setText( self.tr( "Output shapefile" ) )
93-
self.cmbField.setVisible(True)
94-
self.field_label.setVisible(True)
101+
self.cmbField.setVisible( True )
102+
self.lblField.setVisible( True )
95103
elif self.myFunction == 2: # Multipart to singleparts
96104
self.setWindowTitle( self.tr( "Multipart to singleparts" ) )
97-
self.lineEdit.setVisible(False)
98-
self.label.setVisible(False)
99-
self.label_2.setText(self.tr( "Output shapefile" ) )
100-
self.cmbField.setVisible(False)
101-
self.field_label.setVisible(False)
105+
self.lineEdit.setVisible( False )
106+
self.label.setVisible( False )
107+
self.label_2.setText( self.tr( "Output shapefile" ) )
102108
elif self.myFunction == 3: # Extract nodes
103109
self.setWindowTitle( self.tr( "Extract nodes" ) )
104-
self.lineEdit.setVisible(False)
105-
self.label.setVisible(False)
106-
self.cmbField.setVisible(False)
107-
self.field_label.setVisible(False)
110+
self.lineEdit.setVisible( False )
111+
self.label.setVisible( False )
108112
elif self.myFunction == 4: # Polygons to lines
109113
self.setWindowTitle( self.tr( "Polygons to lines" ) )
110114
self.label_2.setText( self.tr( "Output shapefile" ) )
111115
self.label_3.setText( self.tr( "Input polygon vector layer" ) )
112-
self.label.setVisible(False)
113-
self.lineEdit.setVisible(False)
114-
self.cmbField.setVisible(False)
115-
self.field_label.setVisible(False)
116+
self.label.setVisible( False )
117+
self.lineEdit.setVisible( False )
116118
elif self.myFunction == 5: # Export/Add geometry columns
117119
self.setWindowTitle( self.tr( "Export/Add geometry columns" ) )
118120
self.label_2.setText( self.tr( "Output shapefile" ) )
119121
self.label_3.setText( self.tr( "Input vector layer" ) )
120-
self.label.setVisible(False)
121-
self.lineEdit.setVisible(False)
122-
self.cmbField.setVisible(False)
123-
self.field_label.setVisible(False)
122+
self.label.setVisible( False )
123+
self.lineEdit.setVisible( False )
124+
# populate calculation types
125+
self.lblCalcType.setVisible( True )
126+
self.cmbCalcType.setVisible( True )
127+
self.cmbCalcType.addItem( self.tr( "Layer CRS" ) )
128+
self.cmbCalcType.addItem( self.tr( "Project CRS" ) )
129+
self.cmbCalcType.addItem( self.tr( "Ellipsoid" ) )
124130
elif self.myFunction == 7: # Polygon centroids
125131
self.setWindowTitle( self.tr( "Polygon centroids" ) )
126132
self.label_2.setText( self.tr( "Output point shapefile" ) )
127133
self.label_3.setText( self.tr( "Input polygon vector layer" ) )
128134
self.label.setVisible( False )
129135
self.lineEdit.setVisible( False )
130-
self.cmbField.setVisible( False )
131-
self.field_label.setVisible( False )
132136
else:
133137
if self.myFunction == 8: # Delaunay triangulation
134138
self.setWindowTitle( self.tr( "Delaunay triangulation" ) )
@@ -139,27 +143,22 @@ def manageGui(self):
139143
self.setWindowTitle( self.tr( "Voronoi polygon" ) )
140144
self.label_3.setText( self.tr( "Input point vector layer" ) )
141145
self.label.setText( self.tr( "Buffer region" ) )
142-
self.lineEdit.setSuffix(" %")
143-
self.lineEdit.setRange(0, 100)
144-
self.lineEdit.setSingleStep(5)
145-
self.lineEdit.setValue(0)
146+
self.lineEdit.setSuffix( " %" )
147+
self.lineEdit.setRange( 0, 100 )
148+
self.lineEdit.setSingleStep( 5 )
149+
self.lineEdit.setValue( 0 )
146150
elif self.myFunction == 11: #Lines to polygons
147151
self.setWindowTitle( self.tr( "Lines to polygons" ) )
148152
self.label_2.setText( self.tr( "Output shapefile" ) )
149153
self.label_3.setText( self.tr( "Input line vector layer" ) )
150-
self.label.setVisible(False)
151-
self.lineEdit.setVisible(False)
152-
self.cmbField.setVisible(False)
153-
self.field_label.setVisible(False)
154-
154+
self.label.setVisible( False )
155+
self.lineEdit.setVisible( False )
155156
else: # Polygon from layer extent
156157
self.setWindowTitle( self.tr( "Polygon from layer extent" ) )
157158
self.label_3.setText( self.tr( "Input layer" ) )
158159
self.label.setVisible( False )
159160
self.lineEdit.setVisible( False )
160161
self.label_2.setText( self.tr( "Output polygon shapefile" ) )
161-
self.cmbField.setVisible( False )
162-
self.field_label.setVisible( False )
163162
self.resize( 381, 100 )
164163
self.populateLayers()
165164

@@ -186,9 +185,9 @@ def populateLayers( self ):
186185
#5: Export/Add geometry columns
187186
#6: Simplify geometries (disabled)
188187
#7: Polygon centroids
189-
#8: Delaunay triangulation
190-
#9: Polygon from layer extent
191-
#10:Voronoi polygons
188+
#8: Delaunay triangulation
189+
#9: Polygon from layer extent
190+
#10: Voronoi polygons
192191
#11: Lines to polygons
193192

194193
def geometry( self, myLayer, myParam, myField ):
@@ -200,15 +199,16 @@ def geometry( self, myLayer, myParam, myField ):
200199
check = QFile( self.shapefileName )
201200
if check.exists():
202201
if not QgsVectorFileWriter.deleteShapeFile( self.shapefileName ):
203-
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Unable to delete existing shapefile." ) )
202+
QMessageBox.warning( self, self.tr( "Geometry"),
203+
self.tr( "Unable to delete existing shapefile." ) )
204204
return
205205
self.buttonOk.setEnabled( False )
206206
self.testThread = geometryThread( self.iface.mainWindow(), self, self.myFunction, vlayer, myParam,
207-
myField, self.shapefileName, self.encoding )
208-
QObject.connect( self.testThread, SIGNAL( "runFinished(PyQt_PyObject)" ), self.runFinishedFromThread )
209-
QObject.connect( self.testThread, SIGNAL( "runStatus(PyQt_PyObject)" ), self.runStatusFromThread )
210-
QObject.connect( self.testThread, SIGNAL( "runRange(PyQt_PyObject)" ), self.runRangeFromThread )
211-
self.cancel_close.setText( self.tr("Cancel") )
207+
myField, self.shapefileName, self.encoding, self.cmbCalcType.currentIndex() )
208+
QObject.connect( self.testThread, SIGNAL( "runFinished( PyQt_PyObject )" ), self.runFinishedFromThread )
209+
QObject.connect( self.testThread, SIGNAL( "runStatus( PyQt_PyObject )" ), self.runStatusFromThread )
210+
QObject.connect( self.testThread, SIGNAL( "runRange( PyQt_PyObject )" ), self.runRangeFromThread )
211+
self.cancel_close.setText( self.tr( "Cancel" ) )
212212
QObject.connect( self.cancel_close, SIGNAL( "clicked()" ), self.cancelThread )
213213
self.testThread.start()
214214

@@ -221,13 +221,17 @@ def runFinishedFromThread( self, success ):
221221
self.buttonOk.setEnabled( True )
222222
extra = ""
223223
if success == "math_error":
224-
QMessageBox.warning( self, self.tr("Geometry"), self.tr("Error processing specified tolerance!\nPlease choose larger tolerance...") )
224+
QMessageBox.warning( self, self.tr( "Geometry" ),
225+
self.tr( "Error processing specified tolerance!\nPlease choose larger tolerance..." ) )
225226
if not QgsVectorFileWriter.deleteShapeFile( self.shapefileName ):
226-
QMessageBox.warning( self, self.tr("Geometry"), self.tr( "Unable to delete incomplete shapefile." ) )
227+
QMessageBox.warning( self, self.tr( "Geometry" ),
228+
self.tr( "Unable to delete incomplete shapefile." ) )
227229
elif success == "attr_error":
228-
QMessageBox.warning( self, self.tr("Geometry"), self.tr("At least two features must have same attribute value!\nPlease choose another field...") )
230+
QMessageBox.warning( self, self.tr( "Geometry" ),
231+
self.tr( "At least two features must have same attribute value!\nPlease choose another field..." ) )
229232
if not QgsVectorFileWriter.deleteShapeFile( self.shapefileName ):
230-
QMessageBox.warning( self, self.tr("Geometry"), self.tr( "Unable to delete incomplete shapefile." ) )
233+
QMessageBox.warning( self, self.tr( "Geometry" ),
234+
self.tr( "Unable to delete incomplete shapefile." ) )
231235
else:
232236
if success == "valid_error":
233237
extra = self.tr("One or more features in the output layer may have invalid "
@@ -237,14 +241,15 @@ def runFinishedFromThread( self, success ):
237241
QObject.disconnect( self.cancel_close, SIGNAL( "clicked()" ), self.cancelThread )
238242
if success:
239243
addToTOC = QMessageBox.question( self, self.tr("Geometry"),
240-
self.tr( "Created output shapefile:\n%1\n%2\n\nWould you like to add the new layer to the TOC?" ).arg( unicode( self.shapefileName ) ).arg( extra ),
241-
QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton )
244+
self.tr( "Created output shapefile:\n%1\n%2\n\nWould you like to add the new layer to the TOC?" ).arg( unicode( self.shapefileName ) ).arg( extra ),
245+
QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton )
242246
if addToTOC == QMessageBox.Yes:
243247
if not ftools_utils.addShapeToCanvas( unicode( self.shapefileName ) ):
244-
QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( self.shapefileName ) ))
248+
QMessageBox.warning( self, self.tr( "Geometry"),
249+
self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( self.shapefileName ) ) )
245250
self.populateLayers()
246251
else:
247-
QMessageBox.warning( self, self.tr("Geometry"), self.tr( "Error writing output shapefile." ) )
252+
QMessageBox.warning( self, self.tr( "Geometry" ), self.tr( "Error writing output shapefile." ) )
248253

249254
def runStatusFromThread( self, status ):
250255
self.progressBar.setValue( status )
@@ -253,7 +258,7 @@ def runRangeFromThread( self, range_vals ):
253258
self.progressBar.setRange( range_vals[ 0 ], range_vals[ 1 ] )
254259

255260
class geometryThread( QThread ):
256-
def __init__( self, parentThread, parentObject, function, vlayer, myParam, myField, myName, myEncoding ):
261+
def __init__( self, parentThread, parentObject, function, vlayer, myParam, myField, myName, myEncoding, myCalcType ):
257262
QThread.__init__( self, parentThread )
258263
self.parent = parentObject
259264
self.running = False
@@ -263,6 +268,7 @@ def __init__( self, parentThread, parentObject, function, vlayer, myParam, myFie
263268
self.myField = myField
264269
self.myName = myName
265270
self.myEncoding = myEncoding
271+
self.myCalcType = myCalcType
266272

267273
def run( self ):
268274
self.running = True
@@ -287,10 +293,10 @@ def run( self ):
287293
success = self.voronoi_polygons()
288294
elif self.myFunction == 11: # Lines to polygons
289295
success = self.lines_to_polygons()
290-
self.emit( SIGNAL( "runFinished(PyQt_PyObject)" ), success )
291-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
296+
self.emit( SIGNAL( "runFinished( PyQt_PyObject )" ), success )
297+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
292298

293-
def stop(self):
299+
def stop( self ):
294300
self.running = False
295301

296302
def single_to_multi( self ):
@@ -299,9 +305,9 @@ def single_to_multi( self ):
299305
vprovider.select( allAttrs )
300306
fields = vprovider.fields()
301307
allValid = True
302-
geomType = self.singleToMultiGeom(vprovider.geometryType())
303-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
304-
fields, geomType, vprovider.crs() )
308+
geomType = self.singleToMultiGeom( vprovider.geometryType() )
309+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
310+
geomType, vprovider.crs() )
305311
inFeat = QgsFeature()
306312
outFeat = QgsFeature()
307313
inGeom = QgsGeometry()
@@ -310,27 +316,25 @@ def single_to_multi( self ):
310316
if not index == -1:
311317
unique = ftools_utils.getUniqueValues( vprovider, int( index ) )
312318
else:
313-
unique = [QVariant(QString())]
319+
unique = [ QVariant( QString() ) ]
314320
nFeat = vprovider.featureCount() * len( unique )
315321
nElement = 0
316-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
317-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
318-
merge_all = self.myField == QString("--- " + self.tr( "Merge all" ) + " ---")
319-
if not len( unique ) == self.vlayer.featureCount() \
320-
or merge_all:
322+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
323+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
324+
merge_all = self.myField == QString( "--- " + self.tr( "Merge all" ) + " ---" )
325+
if not len( unique ) == self.vlayer.featureCount() or merge_all:
321326
for i in unique:
322327
vprovider.rewind()
323328
multi_feature= []
324329
first = True
325-
vprovider.select(allAttrs)
330+
vprovider.select( allAttrs )
326331
while vprovider.nextFeature( inFeat ):
327332
atMap = inFeat.attributeMap()
328333
if not merge_all:
329334
idVar = atMap[ index ]
330335
else:
331-
idVar = QVariant(QString())
332-
if idVar.toString().trimmed() == i.toString().trimmed() \
333-
or merge_all:
336+
idVar = QVariant( QString() )
337+
if idVar.toString().trimmed() == i.toString().trimmed() or merge_all:
334338
if first:
335339
atts = atMap
336340
first = False
@@ -339,13 +343,13 @@ def single_to_multi( self ):
339343
feature_list = self.extractAsMulti( inGeom )
340344
multi_feature.extend( feature_list )
341345
nElement += 1
342-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
346+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
343347
outFeat.setAttributeMap( atts )
344-
outGeom = QgsGeometry( self.convertGeometry(multi_feature, vType) )
348+
outGeom = QgsGeometry( self.convertGeometry( multi_feature, vType ) )
345349
if not outGeom.isGeosValid():
346350
allValid = "valid_error"
347-
outFeat.setGeometry(outGeom)
348-
writer.addFeature(outFeat)
351+
outFeat.setGeometry( outGeom )
352+
writer.addFeature( outFeat )
349353
del writer
350354
else:
351355
return "attr_error"
@@ -356,20 +360,20 @@ def multi_to_single( self ):
356360
allAttrs = vprovider.attributeIndexes()
357361
vprovider.select( allAttrs )
358362
fields = vprovider.fields()
359-
geomType = self.multiToSingleGeom(vprovider.geometryType())
360-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
361-
fields, geomType, vprovider.crs() )
363+
geomType = self.multiToSingleGeom( vprovider.geometryType() )
364+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
365+
geomType, vprovider.crs() )
362366
inFeat = QgsFeature()
363367
outFeat = QgsFeature()
364368
inGeom = QgsGeometry()
365369
outGeom = QgsGeometry()
366370
nFeat = vprovider.featureCount()
367371
nElement = 0
368-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
369-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
372+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
373+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
370374
while vprovider.nextFeature( inFeat ):
371375
nElement += 1
372-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
376+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
373377
inGeom = inFeat.geometry()
374378
atMap = inFeat.attributeMap()
375379
featList = self.extractAsSingle( inGeom )
@@ -385,19 +389,19 @@ def extract_nodes( self ):
385389
allAttrs = vprovider.attributeIndexes()
386390
vprovider.select( allAttrs )
387391
fields = vprovider.fields()
388-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
389-
fields, QGis.WKBPoint, vprovider.crs() )
392+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
393+
QGis.WKBPoint, vprovider.crs() )
390394
inFeat = QgsFeature()
391395
outFeat = QgsFeature()
392396
inGeom = QgsGeometry()
393397
outGeom = QgsGeometry()
394398
nFeat = vprovider.featureCount()
395399
nElement = 0
396-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
397-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
400+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
401+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
398402
while vprovider.nextFeature( inFeat ):
399403
nElement += 1
400-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
404+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
401405
inGeom = inFeat.geometry()
402406
atMap = inFeat.attributeMap()
403407
pointList = ftools_utils.extractPoints( inGeom )
@@ -413,20 +417,20 @@ def polygons_to_lines( self ):
413417
allAttrs = vprovider.attributeIndexes()
414418
vprovider.select( allAttrs )
415419
fields = vprovider.fields()
416-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
417-
fields, QGis.WKBLineString, vprovider.crs() )
420+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
421+
QGis.WKBLineString, vprovider.crs() )
418422
inFeat = QgsFeature()
419423
outFeat = QgsFeature()
420424
inGeom = QgsGeometry()
421425
outGeom = QgsGeometry()
422426
nFeat = vprovider.featureCount()
423427
nElement = 0
424-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
425-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
426-
while vprovider.nextFeature(inFeat):
428+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
429+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
430+
while vprovider.nextFeature( inFeat ):
427431
multi = False
428432
nElement += 1
429-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
433+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
430434
inGeom = inFeat.geometry()
431435
if inGeom.isMultipart():
432436
multi = True
@@ -444,27 +448,27 @@ def lines_to_polygons( self ):
444448
allAttrs = vprovider.attributeIndexes()
445449
vprovider.select( allAttrs )
446450
fields = vprovider.fields()
447-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
448-
fields, QGis.WKBPolygon, vprovider.crs() )
451+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
452+
QGis.WKBPolygon, vprovider.crs() )
449453
inFeat = QgsFeature()
450454
outFeat = QgsFeature()
451455
inGeom = QgsGeometry()
452456
nFeat = vprovider.featureCount()
453457
nElement = 0
454-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
455-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
456-
while vprovider.nextFeature(inFeat):
458+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
459+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
460+
while vprovider.nextFeature( inFeat ):
457461
outGeomList = []
458462
multi = False
459463
nElement += 1
460-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
464+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
461465
if inFeat.geometry().isMultipart():
462466
outGeomList = inFeat.geometry().asMultiPolyline()
463467
multi = True
464468
else:
465469
outGeomList.append( inFeat.geometry().asPolyline() )
466470
polyGeom = self.remove_bad_lines( outGeomList )
467-
if len(polyGeom) <> 0:
471+
if len( polyGeom ) <> 0:
468472
outFeat.setGeometry( QgsGeometry.fromPolygon( polyGeom ) )
469473
atMap = inFeat.attributeMap()
470474
outFeat.setAttributeMap( atMap )
@@ -477,20 +481,41 @@ def export_geometry_info( self ):
477481
allAttrs = vprovider.attributeIndexes()
478482
vprovider.select( allAttrs )
479483
( fields, index1, index2 ) = self.checkGeometryFields( self.vlayer )
480-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
481-
fields, vprovider.geometryType(), vprovider.crs() )
484+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
485+
vprovider.geometryType(), vprovider.crs() )
482486
inFeat = QgsFeature()
483487
outFeat = QgsFeature()
484488
inGeom = QgsGeometry()
485489
nFeat = vprovider.featureCount()
486490
nElement = 0
487-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
488-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
491+
492+
# calculate with:
493+
# 0 - layer CRS
494+
# 1 - project CRS
495+
# 2 - ellipsoidal
496+
ellips = None
497+
crs = None
498+
coordTransform = None
499+
if self.myCalcType == 2:
500+
settings = QSettings()
501+
ellips = settings.value( "/qgis/measure/ellipsoid", "WGS84" ).toString()
502+
crs = self.parent.iface.mapCanvas().mapRenderer().destinationCrs().srsid()
503+
elif self.myCalcType == 1:
504+
mapCRS = self.parent.iface.mapCanvas().mapRenderer().destinationCrs()
505+
layCRS = self.vlayer.crs()
506+
coordTransform = QgsCoordinateTransform( layCRS, mapCRS )
507+
508+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
509+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
489510
while vprovider.nextFeature(inFeat):
490-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
511+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
491512
nElement += 1
492513
inGeom = inFeat.geometry()
493-
( attr1, attr2 ) = self.simpleMeasure( inGeom )
514+
515+
if self.myCalcType == 1:
516+
inGeom.transform( coordTransform )
517+
( attr1, attr2 ) = self.simpleMeasure( inGeom, self.myCalcType, ellips, crs )
518+
494519
outFeat.setGeometry( inGeom )
495520
atMap = inFeat.attributeMap()
496521
outFeat.setAttributeMap( atMap )
@@ -505,20 +530,20 @@ def polygon_centroids( self ):
505530
allAttrs = vprovider.attributeIndexes()
506531
vprovider.select( allAttrs )
507532
fields = vprovider.fields()
508-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
509-
fields, QGis.WKBPoint, vprovider.crs() )
533+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
534+
QGis.WKBPoint, vprovider.crs() )
510535
inFeat = QgsFeature()
511536
outFeat = QgsFeature()
512537
nFeat = vprovider.featureCount()
513538
nElement = 0
514-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
515-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
539+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
540+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
516541
while vprovider.nextFeature( inFeat ):
517542
nElement += 1
518-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
543+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
519544
inGeom = inFeat.geometry()
520545
atMap = inFeat.attributeMap()
521-
outGeom = QgsGeometry(inGeom.centroid())
546+
outGeom = QgsGeometry( inGeom.centroid() )
522547
if outGeom is None:
523548
return "math_error"
524549
outFeat.setAttributeMap( atMap )
@@ -533,219 +558,216 @@ def delaunay_triangulation( self ):
533558
vprovider = self.vlayer.dataProvider()
534559
allAttrs = vprovider.attributeIndexes()
535560
vprovider.select( allAttrs )
536-
fields = {
537-
0 : QgsField( "POINTA", QVariant.Double ),
538-
1 : QgsField( "POINTB", QVariant.Double ),
539-
2 : QgsField( "POINTC", QVariant.Double ) }
540-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
541-
fields, QGis.WKBPolygon, vprovider.crs() )
561+
fields = { 0 : QgsField( "POINTA", QVariant.Double ),
562+
1 : QgsField( "POINTB", QVariant.Double ),
563+
2 : QgsField( "POINTC", QVariant.Double ) }
564+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
565+
QGis.WKBPolygon, vprovider.crs() )
542566
inFeat = QgsFeature()
543567
c = voronoi.Context()
544568
pts = []
545569
ptDict = {}
546570
ptNdx = -1
547-
while vprovider.nextFeature(inFeat):
548-
geom = QgsGeometry(inFeat.geometry())
571+
while vprovider.nextFeature( inFeat ):
572+
geom = QgsGeometry( inFeat.geometry() )
549573
point = geom.asPoint()
550574
x = point.x()
551575
y = point.y()
552-
pts.append((x, y))
576+
pts.append( ( x, y ) )
553577
ptNdx +=1
554578
ptDict[ptNdx] = inFeat.id()
555579
if len(pts) < 3:
556580
return False
557-
uniqueSet = Set(item for item in pts)
558-
ids = [pts.index(item) for item in uniqueSet]
559-
sl = voronoi.SiteList([voronoi.Site(*i) for i in uniqueSet])
581+
uniqueSet = Set( item for item in pts )
582+
ids = [ pts.index( item ) for item in uniqueSet ]
583+
sl = voronoi.SiteList( [ voronoi.Site( *i ) for i in uniqueSet ] )
560584
c.triangulate = True
561-
voronoi.voronoi(sl, c)
585+
voronoi.voronoi( sl, c )
562586
triangles = c.triangles
563587
feat = QgsFeature()
564588
nFeat = len( triangles )
565589
nElement = 0
566-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
567-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
590+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
591+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
568592
for triangle in triangles:
569-
indicies = list(triangle)
570-
indicies.append(indicies[0])
593+
indicies = list( triangle )
594+
indicies.append( indicies[ 0 ] )
571595
polygon = []
572596
step = 0
573597
for index in indicies:
574-
vprovider.featureAtId(ptDict[ids[index]], inFeat, True, allAttrs)
575-
geom = QgsGeometry(inFeat.geometry())
576-
point = QgsPoint(geom.asPoint())
577-
polygon.append(point)
578-
if step <= 3: feat.addAttribute(step, QVariant(ids[index]))
598+
vprovider.featureAtId( ptDict[ ids[ index ] ], inFeat, True, allAttrs )
599+
geom = QgsGeometry( inFeat.geometry() )
600+
point = QgsPoint( geom.asPoint() )
601+
polygon.append( point )
602+
if step <= 3: feat.addAttribute( step, QVariant( ids[ index ] ) )
579603
step += 1
580-
geometry = QgsGeometry().fromPolygon([polygon])
581-
feat.setGeometry(geometry)
582-
writer.addFeature(feat)
604+
geometry = QgsGeometry().fromPolygon( [ polygon ] )
605+
feat.setGeometry( geometry )
606+
writer.addFeature( feat )
583607
nElement += 1
584-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
608+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
585609
del writer
586610
return True
587611

588612
def voronoi_polygons( self ):
589613
vprovider = self.vlayer.dataProvider()
590614
allAttrs = vprovider.attributeIndexes()
591615
vprovider.select( allAttrs )
592-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
593-
vprovider.fields(), QGis.WKBPolygon, vprovider.crs() )
616+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vprovider.fields(),
617+
QGis.WKBPolygon, vprovider.crs() )
594618
inFeat = QgsFeature()
595619
outFeat = QgsFeature()
596620
extent = self.vlayer.extent()
597-
extraX = extent.height()*(self.myParam/100.00)
598-
extraY = extent.width()*(self.myParam/100.00)
621+
extraX = extent.height() * ( self.myParam / 100.00 )
622+
extraY = extent.width() * ( self.myParam / 100.00 )
599623
height = extent.height()
600624
width = extent.width()
601625
c = voronoi.Context()
602626
pts = []
603627
ptDict = {}
604628
ptNdx = -1
605-
while vprovider.nextFeature(inFeat):
606-
geom = QgsGeometry(inFeat.geometry())
629+
while vprovider.nextFeature( inFeat ):
630+
geom = QgsGeometry( inFeat.geometry() )
607631
point = geom.asPoint()
608-
x = point.x()-extent.xMinimum()
609-
y = point.y()-extent.yMinimum()
610-
pts.append((x, y))
632+
x = point.x() - extent.xMinimum()
633+
y = point.y() - extent.yMinimum()
634+
pts.append( ( x, y ) )
611635
ptNdx +=1
612-
ptDict[ptNdx] = inFeat.id()
636+
ptDict[ ptNdx ] = inFeat.id()
613637
self.vlayer = None
614-
if len(pts) < 3:
638+
if len( pts ) < 3:
615639
return False
616-
uniqueSet = Set(item for item in pts)
617-
ids = [pts.index(item) for item in uniqueSet]
618-
sl = voronoi.SiteList([voronoi.Site(i[0], i[1], sitenum=j) for j, i in enumerate(uniqueSet)])
619-
voronoi.voronoi(sl, c)
640+
uniqueSet = Set( item for item in pts )
641+
ids = [ pts.index( item ) for item in uniqueSet ]
642+
sl = voronoi.SiteList( [ voronoi.Site( i[ 0 ], i[ 1 ], sitenum = j ) for j, i in enumerate( uniqueSet ) ] )
643+
voronoi.voronoi( sl, c )
620644
inFeat = QgsFeature()
621-
nFeat = len(c.polygons)
645+
nFeat = len( c.polygons )
622646
nElement = 0
623-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
624-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
647+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
648+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, nFeat ) )
625649
for site, edges in c.polygons.iteritems():
626-
vprovider.featureAtId(ptDict[ids[site]], inFeat, True, allAttrs)
627-
lines = self.clip_voronoi(edges, c, width, height, extent, extraX, extraY)
628-
geom = QgsGeometry.fromMultiPoint(lines)
629-
geom = QgsGeometry(geom.convexHull())
630-
outFeat.setGeometry(geom)
631-
outFeat.setAttributeMap(inFeat.attributeMap())
632-
writer.addFeature(outFeat)
650+
vprovider.featureAtId( ptDict[ ids[ site ] ], inFeat, True, allAttrs )
651+
lines = self.clip_voronoi( edges, c, width, height, extent, extraX, extraY )
652+
geom = QgsGeometry.fromMultiPoint( lines )
653+
geom = QgsGeometry( geom.convexHull() )
654+
outFeat.setGeometry( geom )
655+
outFeat.setAttributeMap( inFeat.attributeMap() )
656+
writer.addFeature( outFeat )
633657
nElement += 1
634-
self.emit(SIGNAL("runStatus(PyQt_PyObject)" ), nElement)
658+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
635659
del writer
636660
return True
637661

638-
639-
def clip_voronoi(self, edges, c, width, height, extent, exX, exY):
662+
def clip_voronoi( self, edges, c, width, height, extent, exX, exY ):
640663
""" Clip voronoi function based on code written for Inkscape
641664
Copyright (C) 2010 Alvin Penner, penner@vaxxine.com
642665
"""
643-
def clip_line(x1, y1, x2, y2, w, h, x, y):
644-
if x1 < 0-x and x2 < 0-x:
645-
return [0, 0, 0, 0]
646-
if x1 > w+x and x2 > w+x:
647-
return [0, 0, 0, 0]
648-
if x1 < 0-x:
649-
y1 = (y1*x2 - y2*x1)/(x2 - x1)
650-
x1 = 0-x
651-
if x2 < 0-x:
652-
y2 = (y1*x2 - y2*x1)/(x2 - x1)
653-
x2 = 0-x
654-
if x1 > w+x:
655-
y1 = y1 + (w+x - x1)*(y2 - y1)/(x2 - x1)
656-
x1 = w+x
657-
if x2 > w+x:
658-
y2 = y1 + (w+x - x1)*(y2 - y1)/(x2 - x1)
659-
x2 = w+x
660-
if y1 < 0-y and y2 < 0-y:
661-
return [0, 0, 0, 0]
662-
if y1 > h+y and y2 > h+y:
663-
return [0, 0, 0, 0]
666+
def clip_line( x1, y1, x2, y2, w, h, x, y ):
667+
if x1 < 0 - x and x2 < 0 - x:
668+
return [ 0, 0, 0, 0 ]
669+
if x1 > w + x and x2 > w + x:
670+
return [ 0, 0, 0, 0 ]
671+
if x1 < 0 - x:
672+
y1 = ( y1 * x2 - y2 * x1 ) / ( x2 - x1 )
673+
x1 = 0 - x
674+
if x2 < 0 - x:
675+
y2 = ( y1 * x2 - y2 * x1 ) / ( x2 - x1 )
676+
x2 = 0 - x
677+
if x1 > w + x:
678+
y1 = y1 + ( w + x - x1 ) * ( y2 - y1 ) / ( x2 - x1 )
679+
x1 = w + x
680+
if x2 > w + x:
681+
y2 = y1 + ( w + x - x1 ) *( y2 - y1 ) / ( x2 - x1 )
682+
x2 = w + x
683+
if y1 < 0 - y and y2 < 0 - y:
684+
return [ 0, 0, 0, 0 ]
685+
if y1 > h + y and y2 > h + y:
686+
return [ 0, 0, 0, 0 ]
664687
if x1 == x2 and y1 == y2:
665-
return [0, 0, 0, 0]
666-
if y1 < 0-y:
667-
x1 = (x1*y2 - x2*y1)/(y2 - y1)
668-
y1 = 0-y
669-
if y2 < 0-y:
670-
x2 = (x1*y2 - x2*y1)/(y2 - y1)
671-
y2 = 0-y
672-
if y1 > h+y:
673-
x1 = x1 + (h+y - y1)*(x2 - x1)/(y2 - y1)
674-
y1 = h+y
675-
if y2 > h+y:
676-
x2 = x1 + (h+y - y1)*(x2 - x1)/(y2 - y1)
677-
y2 = h+y
678-
return [x1, y1, x2, y2]
688+
return [ 0, 0, 0, 0 ]
689+
if y1 < 0 - y:
690+
x1 = ( x1 * y2 - x2 * y1 ) / ( y2 - y1 )
691+
y1 = 0 - y
692+
if y2 < 0 - y:
693+
x2 = ( x1 * y2 - x2 * y1 ) / ( y2 - y1 )
694+
y2 = 0 - y
695+
if y1 > h + y:
696+
x1 = x1 + ( h + y - y1 ) * ( x2 - x1 ) / ( y2 - y1 )
697+
y1 = h + y
698+
if y2 > h + y:
699+
x2 = x1 + ( h + y - y1) * ( x2 - x1 ) / ( y2 - y1 )
700+
y2 = h + y
701+
return [ x1, y1, x2, y2 ]
679702
lines = []
680703
hasXMin = False
681704
hasYMin = False
682705
hasXMax = False
683706
hasYMax = False
684707
for edge in edges:
685-
if edge[1] >= 0 and edge[2] >= 0: # two vertices
686-
[x1, y1, x2, y2] = clip_line(c.vertices[edge[1]][0], c.vertices[edge[1]][1], c.vertices[edge[2]][0], c.vertices[edge[2]][1], width, height, exX, exY)
687-
elif edge[1] >= 0: # only one vertex
688-
if c.lines[edge[0]][1] == 0: # vertical line
689-
xtemp = c.lines[edge[0]][2]/c.lines[edge[0]][0]
690-
if c.vertices[edge[1]][1] > (height+exY)/2:
691-
ytemp = height+exY
708+
if edge[ 1 ] >= 0 and edge[ 2 ] >= 0: # two vertices
709+
[ x1, y1, x2, y2 ] = clip_line( c.vertices[ edge[ 1 ] ][ 0 ], c.vertices[ edge[ 1 ] ][ 1 ], c.vertices[ edge[ 2 ] ][ 0 ], c.vertices[ edge[ 2 ] ][ 1 ], width, height, exX, exY )
710+
elif edge[ 1 ] >= 0: # only one vertex
711+
if c.lines[ edge[ 0 ] ][ 1 ] == 0: # vertical line
712+
xtemp = c.lines[ edge[ 0 ] ][ 2 ] / c.lines[ edge[ 0 ] ][ 0 ]
713+
if c.vertices[ edge[ 1 ] ][ 1 ] > ( height + exY ) / 2:
714+
ytemp = height + exY
692715
else:
693-
ytemp = 0-exX
716+
ytemp = 0 - exX
694717
else:
695-
xtemp = width+exX
696-
ytemp = (c.lines[edge[0]][2] - (width+exX)*c.lines[edge[0]][0])/c.lines[edge[0]][1]
697-
[x1, y1, x2, y2] = clip_line(c.vertices[edge[1]][0], c.vertices[edge[1]][1], xtemp, ytemp, width, height, exX, exY)
698-
elif edge[2] >= 0: # only one vertex
699-
if c.lines[edge[0]][1] == 0: # vertical line
700-
xtemp = c.lines[edge[0]][2]/c.lines[edge[0]][0]
701-
if c.vertices[edge[2]][1] > (height+exY)/2:
702-
ytemp = height+exY
718+
xtemp = width + exX
719+
ytemp = ( c.lines[ edge[ 0 ] ][ 2 ] - ( width + exX ) * c.lines[ edge[ 0 ] ][ 0 ] ) / c.lines[ edge[ 0 ] ][ 1 ]
720+
[ x1, y1, x2, y2 ] = clip_line( c.vertices[ edge[ 1 ] ][ 0 ], c.vertices[ edge[ 1 ] ][ 1 ], xtemp, ytemp, width, height, exX, exY )
721+
elif edge[ 2 ] >= 0: # only one vertex
722+
if c.lines[ edge[ 0 ] ][ 1 ] == 0: # vertical line
723+
xtemp = c.lines[ edge[ 0 ] ][ 2 ] / c.lines[ edge[ 0 ] ][ 0 ]
724+
if c.vertices[ edge[ 2 ] ][ 1 ] > ( height + exY ) / 2:
725+
ytemp = height + exY
703726
else:
704-
ytemp = 0.0-exY
727+
ytemp = 0.0 - exY
705728
else:
706-
xtemp = 0.0-exX
707-
ytemp = c.lines[edge[0]][2]/c.lines[edge[0]][1]
708-
[x1, y1, x2, y2] = clip_line(xtemp, ytemp, c.vertices[edge[2]][0], c.vertices[edge[2]][1], width, height, exX, exY)
729+
xtemp = 0.0 - exX
730+
ytemp = c.lines[ edge[ 0 ] ][ 2 ] / c.lines[ edge[ 0 ] ][ 1 ]
731+
[ x1, y1, x2, y2 ] = clip_line( xtemp, ytemp, c.vertices[ edge[ 2 ] ][ 0 ], c.vertices[ edge[ 2 ] ][ 1 ], width, height, exX, exY )
709732
if x1 or x2 or y1 or y2:
710-
lines.append(QgsPoint(x1+extent.xMinimum(),y1+extent.yMinimum()))
711-
lines.append(QgsPoint(x2+extent.xMinimum(),y2+extent.yMinimum()))
712-
if 0-exX in (x1, x2):
733+
lines.append( QgsPoint( x1 + extent.xMinimum(), y1 + extent.yMinimum() ) )
734+
lines.append( QgsPoint( x2 + extent.xMinimum(), y2 + extent.yMinimum() ) )
735+
if 0 - exX in ( x1, x2 ):
713736
hasXMin = True
714-
if 0-exY in (y1, y2):
737+
if 0 - exY in ( y1, y2 ):
715738
hasYMin = True
716-
if height+exY in (y1, y2):
739+
if height + exY in ( y1, y2 ):
717740
hasYMax = True
718-
if width+exX in (x1, x2):
741+
if width + exX in ( x1, x2 ):
719742
hasXMax = True
720743
if hasXMin:
721744
if hasYMax:
722-
lines.append(QgsPoint(extent.xMinimum()-exX, height+extent.yMinimum()+exY))
745+
lines.append( QgsPoint( extent.xMinimum() - exX, height + extent.yMinimum() + exY ) )
723746
if hasYMin:
724-
lines.append(QgsPoint(extent.xMinimum()-exX, extent.yMinimum()-exY))
747+
lines.append( QgsPoint( extent.xMinimum() - exX, extent.yMinimum() - exY ) )
725748
if hasXMax:
726749
if hasYMax:
727-
lines.append(QgsPoint(width+extent.xMinimum()+exX, height+extent.yMinimum()+exY))
750+
lines.append( QgsPoint( width + extent.xMinimum() + exX, height + extent.yMinimum() + exY ) )
728751
if hasYMin:
729-
lines.append(QgsPoint(width+extent.xMinimum()+exX, extent.yMinimum()-exY))
752+
lines.append( QgsPoint( width + extent.xMinimum() + exX, extent.yMinimum() - exY ) )
730753
return lines
731754

732755
def layer_extent( self ):
733-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
734-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, 0 ) )
735-
fields = {
736-
0 : QgsField( "MINX", QVariant.Double ),
737-
1 : QgsField( "MINY", QVariant.Double ),
738-
2 : QgsField( "MAXX", QVariant.Double ),
739-
3 : QgsField( "MAXY", QVariant.Double ),
740-
4 : QgsField( "CNTX", QVariant.Double ),
741-
5 : QgsField( "CNTY", QVariant.Double ),
742-
6 : QgsField( "AREA", QVariant.Double ),
743-
7 : QgsField( "PERIM", QVariant.Double ),
744-
8 : QgsField( "HEIGHT", QVariant.Double ),
745-
9 : QgsField( "WIDTH", QVariant.Double ) }
746-
747-
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
748-
fields, QGis.WKBPolygon, self.vlayer.crs() )
756+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
757+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, 0 ) )
758+
fields = { 0 : QgsField( "MINX", QVariant.Double ),
759+
1 : QgsField( "MINY", QVariant.Double ),
760+
2 : QgsField( "MAXX", QVariant.Double ),
761+
3 : QgsField( "MAXY", QVariant.Double ),
762+
4 : QgsField( "CNTX", QVariant.Double ),
763+
5 : QgsField( "CNTY", QVariant.Double ),
764+
6 : QgsField( "AREA", QVariant.Double ),
765+
7 : QgsField( "PERIM", QVariant.Double ),
766+
8 : QgsField( "HEIGHT", QVariant.Double ),
767+
9 : QgsField( "WIDTH", QVariant.Double ) }
768+
769+
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
770+
QGis.WKBPolygon, self.vlayer.crs() )
749771
rect = self.vlayer.extent()
750772
minx = rect.xMinimum()
751773
miny = rect.yMinimum()
@@ -757,42 +779,46 @@ def layer_extent( self ):
757779
cnty = miny + ( height / 2.0 )
758780
area = width * height
759781
perim = ( 2 * width ) + (2 * height )
760-
rect = [
761-
QgsPoint( minx, miny ),
762-
QgsPoint( minx, maxy ),
763-
QgsPoint( maxx, maxy ),
764-
QgsPoint( maxx, miny ),
765-
QgsPoint( minx, miny ) ]
782+
rect = [ QgsPoint( minx, miny ),
783+
QgsPoint( minx, maxy ),
784+
QgsPoint( maxx, maxy ),
785+
QgsPoint( maxx, miny ),
786+
QgsPoint( minx, miny ) ]
766787
geometry = QgsGeometry().fromPolygon( [ rect ] )
767788
feat = QgsFeature()
768789
feat.setGeometry( geometry )
769-
feat.setAttributeMap( {
770-
0 : QVariant( minx ),
771-
1 : QVariant( miny ),
772-
2 : QVariant( maxx ),
773-
3 : QVariant( maxy ),
774-
4 : QVariant( cntx ),
775-
5 : QVariant( cnty ),
776-
6 : QVariant( area ),
777-
7 : QVariant( perim ),
778-
8 : QVariant( height ),
779-
9 : QVariant( width ) } )
790+
feat.setAttributeMap( { 0 : QVariant( minx ),
791+
1 : QVariant( miny ),
792+
2 : QVariant( maxx ),
793+
3 : QVariant( maxy ),
794+
4 : QVariant( cntx ),
795+
5 : QVariant( cnty ),
796+
6 : QVariant( area ),
797+
7 : QVariant( perim ),
798+
8 : QVariant( height ),
799+
9 : QVariant( width ) } )
780800
writer.addFeature( feat )
781-
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, 100 ) )
782-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
801+
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, 100 ) )
802+
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
783803
del writer
784804

785805
return True
786806

787-
def simpleMeasure( self, inGeom ):
788-
if inGeom.wkbType() in (QGis.WKBPoint, QGis.WKBPoint25D):
807+
def simpleMeasure( self, inGeom, calcType, ellips, crs ):
808+
if inGeom.wkbType() in ( QGis.WKBPoint, QGis.WKBPoint25D ):
789809
pt = QgsPoint()
790810
pt = inGeom.asPoint()
791811
attr1 = pt.x()
792812
attr2 = pt.y()
793813
else:
794814
measure = QgsDistanceArea()
795-
attr1 = measure.measure(inGeom)
815+
816+
if calcType == 2:
817+
measure.setSourceCrs( crs )
818+
measure.setEllipsoid( ellips )
819+
measure.setProjectionsEnabled( True )
820+
821+
attr1 = measure.measure( inGeom )
796822
if inGeom.type() == QGis.Polygon:
797823
attr2 = self.perimMeasure( inGeom, measure )
798824
else:
@@ -814,7 +840,7 @@ def perimMeasure( self, inGeom, measure ):
814840

815841
def checkForField( self, L, e ):
816842
e = QString( e ).toLower()
817-
fieldRange = range( 0,len( L ) )
843+
fieldRange = range( 0, len( L ) )
818844
for item in fieldRange:
819845
if L[ item ].toLower() == e:
820846
return True, item
@@ -830,37 +856,38 @@ def checkGeometryFields( self, vlayer ):
830856
if geomType == QGis.Polygon:
831857
plp = "Poly"
832858
( found, index1 ) = self.checkForField( nameList, "AREA" )
859+
833860
if not found:
834-
field = QgsField( "AREA", QVariant.Double, "double", 21, 6, self.tr("Polygon area") )
861+
field = QgsField( "AREA", QVariant.Double, "double", 21, 6, self.tr( "Polygon area" ) )
835862
index1 = len( fieldList.keys() )
836863
fieldList[ index1 ] = field
837864
( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
838865

839866
if not found:
840-
field = QgsField( "PERIMETER", QVariant.Double, "double", 21, 6, self.tr("Polygon perimeter") )
867+
field = QgsField( "PERIMETER", QVariant.Double, "double", 21, 6, self.tr( "Polygon perimeter" ) )
841868
index2 = len( fieldList.keys() )
842869
fieldList[ index2 ] = field
843870
elif geomType == QGis.Line:
844871
plp = "Line"
845-
(found, index1) = self.checkForField(nameList, "LENGTH")
872+
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
846873
if not found:
847-
field = QgsField("LENGTH", QVariant.Double, "double", 21, 6, self.tr("Line length") )
848-
index1 = len(fieldList.keys())
849-
fieldList[index1] = field
874+
field = QgsField( "LENGTH", QVariant.Double, "double", 21, 6, self.tr( "Line length" ) )
875+
index1 = len( fieldList.keys() )
876+
fieldList[ index1 ] = field
850877
index2 = index1
851878
else:
852879
plp = "Point"
853-
(found, index1) = self.checkForField(nameList, "XCOORD")
880+
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
854881
if not found:
855-
field = QgsField("XCOORD", QVariant.Double, "double", 21, 6, self.tr("Point x coordinate") )
856-
index1 = len(fieldList.keys())
857-
fieldList[index1] = field
858-
(found, index2) = self.checkForField(nameList, "YCOORD")
882+
field = QgsField( "XCOORD", QVariant.Double, "double", 21, 6, self.tr( "Point x coordinate" ) )
883+
index1 = len( fieldList.keys() )
884+
fieldList[ index1 ] = field
885+
( found, index2 ) = self.checkForField( nameList, "YCOORD" )
859886
if not found:
860-
field = QgsField("YCOORD", QVariant.Double, "double", 21, 6, self.tr("Point y coordinate") )
861-
index2 = len(fieldList.keys())
862-
fieldList[index2] = field
863-
return (fieldList, index1, index2)
887+
field = QgsField( "YCOORD", QVariant.Double, "double", 21, 6, self.tr( "Point y coordinate" ) )
888+
index2 = len( fieldList.keys() )
889+
fieldList[ index2 ] = field
890+
return ( fieldList, index1, index2 )
864891

865892
def extractAsLine( self, geom ):
866893
multi_geom = QgsGeometry()
@@ -869,7 +896,7 @@ def extractAsLine( self, geom ):
869896
if geom.isMultipart():
870897
multi_geom = geom.asMultiPolygon()
871898
for i in multi_geom:
872-
temp_geom.extend(i)
899+
temp_geom.extend( i )
873900
else:
874901
multi_geom = geom.asPolygon()
875902
temp_geom = multi_geom
@@ -879,46 +906,46 @@ def extractAsLine( self, geom ):
879906

880907
def remove_bad_lines( self, lines ):
881908
temp_geom = []
882-
if len(lines)==1:
883-
if len(lines[0]) > 2:
909+
if len( lines ) == 1:
910+
if len( lines[ 0 ] ) > 2:
884911
temp_geom = lines
885912
else:
886913
temp_geom = []
887914
else:
888-
temp_geom = [elem for elem in lines if len(elem) > 2]
915+
temp_geom = [ elem for elem in lines if len( elem ) > 2 ]
889916
return temp_geom
890917

891-
def singleToMultiGeom(self, wkbType):
892-
try:
893-
if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint,
894-
QGis.WKBPoint25D, QGis.WKBMultiPoint25D):
895-
return QGis.WKBMultiPoint
896-
elif wkbType in (QGis.WKBLineString, QGis.WKBMultiLineString,
897-
QGis.WKBMultiLineString25D, QGis.WKBLineString25D):
898-
return QGis.WKBMultiLineString
899-
elif wkbType in (QGis.WKBPolygon, QGis.WKBMultiPolygon,
900-
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D):
901-
return QGis.WKBMultiPolygon
902-
else:
903-
return QGis.WKBUnknown
904-
except Exception, err:
905-
print str(err)
906-
907-
def multiToSingleGeom(self, wkbType):
908-
try:
909-
if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint,
910-
QGis.WKBPoint25D, QGis.WKBMultiPoint25D):
911-
return QGis.WKBPoint
912-
elif wkbType in (QGis.WKBLineString, QGis.WKBMultiLineString,
913-
QGis.WKBMultiLineString25D, QGis.WKBLineString25D):
914-
return QGis.WKBLineString
915-
elif wkbType in (QGis.WKBPolygon, QGis.WKBMultiPolygon,
916-
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D):
917-
return QGis.WKBPolygon
918-
else:
919-
return QGis.WKBUnknown
920-
except Exception, err:
921-
print str(err)
918+
def singleToMultiGeom( self, wkbType ):
919+
try:
920+
if wkbType in ( QGis.WKBPoint, QGis.WKBMultiPoint,
921+
QGis.WKBPoint25D, QGis.WKBMultiPoint25D ):
922+
return QGis.WKBMultiPoint
923+
elif wkbType in ( QGis.WKBLineString, QGis.WKBMultiLineString,
924+
QGis.WKBMultiLineString25D, QGis.WKBLineString25D ):
925+
return QGis.WKBMultiLineString
926+
elif wkbType in ( QGis.WKBPolygon, QGis.WKBMultiPolygon,
927+
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D ):
928+
return QGis.WKBMultiPolygon
929+
else:
930+
return QGis.WKBUnknown
931+
except Exception, err:
932+
print str( err )
933+
934+
def multiToSingleGeom( self, wkbType ):
935+
try:
936+
if wkbType in ( QGis.WKBPoint, QGis.WKBMultiPoint,
937+
QGis.WKBPoint25D, QGis.WKBMultiPoint25D ):
938+
return QGis.WKBPoint
939+
elif wkbType in ( QGis.WKBLineString, QGis.WKBMultiLineString,
940+
QGis.WKBMultiLineString25D, QGis.WKBLineString25D ):
941+
return QGis.WKBLineString
942+
elif wkbType in ( QGis.WKBPolygon, QGis.WKBMultiPolygon,
943+
QGis.WKBMultiPolygon25D, QGis.WKBPolygon25D ):
944+
return QGis.WKBPolygon
945+
else:
946+
return QGis.WKBUnknown
947+
except Exception, err:
948+
print str( err )
922949

923950
def extractAsSingle( self, geom ):
924951
multi_geom = QgsGeometry()
@@ -966,8 +993,8 @@ def extractAsMulti( self, geom ):
966993

967994
def convertGeometry( self, geom_list, vType ):
968995
if vType == 0:
969-
return QgsGeometry().fromMultiPoint(geom_list)
996+
return QgsGeometry().fromMultiPoint( geom_list )
970997
elif vType == 1:
971-
return QgsGeometry().fromMultiPolyline(geom_list)
998+
return QgsGeometry().fromMultiPolyline( geom_list )
972999
else:
973-
return QgsGeometry().fromMultiPolygon(geom_list)
1000+
return QgsGeometry().fromMultiPolygon( geom_list )

‎python/plugins/fTools/tools/frmGeometry.ui

Lines changed: 104 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,86 @@
1-
<ui version="4.0" >
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
23
<class>Dialog</class>
3-
<widget class="QDialog" name="Dialog" >
4-
<property name="windowModality" >
4+
<widget class="QDialog" name="Dialog">
5+
<property name="windowModality">
56
<enum>Qt::NonModal</enum>
67
</property>
7-
<property name="geometry" >
8+
<property name="geometry">
89
<rect>
910
<x>0</x>
1011
<y>0</y>
1112
<width>390</width>
1213
<height>296</height>
1314
</rect>
1415
</property>
15-
<property name="windowTitle" >
16+
<property name="windowTitle">
1617
<string>Extract Nodes</string>
1718
</property>
18-
<property name="sizeGripEnabled" >
19+
<property name="sizeGripEnabled">
1920
<bool>true</bool>
2021
</property>
21-
<layout class="QGridLayout" name="gridLayout" >
22-
<item row="0" column="0" colspan="2" >
23-
<layout class="QVBoxLayout" >
22+
<layout class="QGridLayout" name="gridLayout">
23+
<item row="0" column="0" colspan="2">
24+
<layout class="QVBoxLayout">
2425
<item>
25-
<widget class="QLabel" name="label_3" >
26-
<property name="text" >
26+
<widget class="QLabel" name="label_3">
27+
<property name="text">
2728
<string>Input line or polygon vector layer</string>
2829
</property>
2930
</widget>
3031
</item>
3132
<item>
32-
<widget class="QComboBox" name="inShape" />
33+
<widget class="QComboBox" name="inShape"/>
3334
</item>
3435
</layout>
3536
</item>
36-
<item row="1" column="0" colspan="2" >
37-
<layout class="QHBoxLayout" >
38-
<item>
39-
<widget class="QLabel" name="label" >
40-
<property name="text" >
41-
<string>Tolerance</string>
42-
</property>
43-
</widget>
44-
</item>
45-
<item>
46-
<spacer>
47-
<property name="orientation" >
48-
<enum>Qt::Horizontal</enum>
49-
</property>
50-
<property name="sizeHint" stdset="0" >
51-
<size>
52-
<width>40</width>
53-
<height>20</height>
54-
</size>
55-
</property>
56-
</spacer>
57-
</item>
58-
<item>
59-
<widget class="QDoubleSpinBox" name="lineEdit" >
60-
<property name="minimumSize" >
61-
<size>
62-
<width>150</width>
63-
<height>0</height>
64-
</size>
65-
</property>
66-
<property name="decimals" >
67-
<number>4</number>
68-
</property>
69-
<property name="maximum" >
70-
<double>100000.000000000000000</double>
71-
</property>
72-
<property name="singleStep" >
73-
<double>0.500000000000000</double>
74-
</property>
75-
</widget>
76-
</item>
77-
</layout>
78-
</item>
79-
<item row="2" column="0" colspan="2" >
80-
<widget class="QSplitter" name="splitter" >
81-
<property name="orientation" >
37+
<item row="2" column="0" colspan="2">
38+
<widget class="QSplitter" name="splitter">
39+
<property name="orientation">
8240
<enum>Qt::Horizontal</enum>
8341
</property>
84-
<widget class="QLabel" name="field_label" >
85-
<property name="text" >
42+
<widget class="QLabel" name="lblField">
43+
<property name="text">
8644
<string>Unique ID field</string>
8745
</property>
8846
</widget>
89-
<widget class="QComboBox" name="cmbField" />
47+
<widget class="QComboBox" name="cmbField"/>
9048
</widget>
9149
</item>
92-
<item row="3" column="0" >
50+
<item row="4" column="0">
9351
<spacer>
94-
<property name="orientation" >
52+
<property name="orientation">
9553
<enum>Qt::Vertical</enum>
9654
</property>
97-
<property name="sizeHint" stdset="0" >
55+
<property name="sizeHint" stdset="0">
9856
<size>
9957
<width>20</width>
10058
<height>21</height>
10159
</size>
10260
</property>
10361
</spacer>
10462
</item>
105-
<item row="4" column="0" colspan="2" >
106-
<layout class="QVBoxLayout" >
63+
<item row="5" column="0" colspan="2">
64+
<layout class="QVBoxLayout">
10765
<item>
108-
<widget class="QLabel" name="label_2" >
109-
<property name="text" >
66+
<widget class="QLabel" name="label_2">
67+
<property name="text">
11068
<string>Output point shapefile</string>
11169
</property>
11270
</widget>
11371
</item>
11472
<item>
115-
<layout class="QHBoxLayout" >
73+
<layout class="QHBoxLayout">
11674
<item>
117-
<widget class="QLineEdit" name="outShape" >
118-
<property name="readOnly" >
75+
<widget class="QLineEdit" name="outShape">
76+
<property name="readOnly">
11977
<bool>true</bool>
12078
</property>
12179
</widget>
12280
</item>
12381
<item>
124-
<widget class="QToolButton" name="toolOut" >
125-
<property name="text" >
82+
<widget class="QToolButton" name="toolOut">
83+
<property name="text">
12684
<string>Browse</string>
12785
</property>
12886
</widget>
@@ -131,42 +89,99 @@
13189
</item>
13290
</layout>
13391
</item>
134-
<item row="5" column="0" >
92+
<item row="6" column="0">
13593
<spacer>
136-
<property name="orientation" >
94+
<property name="orientation">
13795
<enum>Qt::Vertical</enum>
13896
</property>
139-
<property name="sizeHint" stdset="0" >
97+
<property name="sizeHint" stdset="0">
14098
<size>
14199
<width>20</width>
142100
<height>21</height>
143101
</size>
144102
</property>
145103
</spacer>
146104
</item>
147-
<item row="6" column="0" >
148-
<widget class="QProgressBar" name="progressBar" >
149-
<property name="value" >
105+
<item row="7" column="0">
106+
<widget class="QProgressBar" name="progressBar">
107+
<property name="value">
150108
<number>0</number>
151109
</property>
152-
<property name="alignment" >
110+
<property name="alignment">
153111
<set>Qt::AlignCenter</set>
154112
</property>
155-
<property name="textVisible" >
113+
<property name="textVisible">
156114
<bool>true</bool>
157115
</property>
158116
</widget>
159117
</item>
160-
<item row="6" column="1" >
161-
<widget class="QDialogButtonBox" name="buttonBox_2" >
162-
<property name="orientation" >
118+
<item row="7" column="1">
119+
<widget class="QDialogButtonBox" name="buttonBox_2">
120+
<property name="orientation">
163121
<enum>Qt::Horizontal</enum>
164122
</property>
165-
<property name="standardButtons" >
123+
<property name="standardButtons">
166124
<set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set>
167125
</property>
168126
</widget>
169127
</item>
128+
<item row="1" column="0" colspan="2">
129+
<layout class="QHBoxLayout">
130+
<item>
131+
<widget class="QLabel" name="label">
132+
<property name="text">
133+
<string>Tolerance</string>
134+
</property>
135+
</widget>
136+
</item>
137+
<item>
138+
<spacer>
139+
<property name="orientation">
140+
<enum>Qt::Horizontal</enum>
141+
</property>
142+
<property name="sizeHint" stdset="0">
143+
<size>
144+
<width>40</width>
145+
<height>20</height>
146+
</size>
147+
</property>
148+
</spacer>
149+
</item>
150+
<item>
151+
<widget class="QDoubleSpinBox" name="lineEdit">
152+
<property name="minimumSize">
153+
<size>
154+
<width>150</width>
155+
<height>0</height>
156+
</size>
157+
</property>
158+
<property name="decimals">
159+
<number>4</number>
160+
</property>
161+
<property name="maximum">
162+
<double>100000.000000000000000</double>
163+
</property>
164+
<property name="singleStep">
165+
<double>0.500000000000000</double>
166+
</property>
167+
</widget>
168+
</item>
169+
</layout>
170+
</item>
171+
<item row="3" column="0" colspan="2">
172+
<layout class="QHBoxLayout" name="_2">
173+
<item>
174+
<widget class="QLabel" name="lblCalcType">
175+
<property name="text">
176+
<string>Calculate using</string>
177+
</property>
178+
</widget>
179+
</item>
180+
<item>
181+
<widget class="QComboBox" name="cmbCalcType"/>
182+
</item>
183+
</layout>
184+
</item>
170185
</layout>
171186
</widget>
172187
<resources/>
@@ -177,11 +192,11 @@
177192
<receiver>Dialog</receiver>
178193
<slot>accept()</slot>
179194
<hints>
180-
<hint type="sourcelabel" >
195+
<hint type="sourcelabel">
181196
<x>133</x>
182197
<y>512</y>
183198
</hint>
184-
<hint type="destinationlabel" >
199+
<hint type="destinationlabel">
185200
<x>215</x>
186201
<y>290</y>
187202
</hint>
@@ -193,11 +208,11 @@
193208
<receiver>Dialog</receiver>
194209
<slot>close()</slot>
195210
<hints>
196-
<hint type="sourcelabel" >
211+
<hint type="sourcelabel">
197212
<x>59</x>
198213
<y>512</y>
199214
</hint>
200-
<hint type="destinationlabel" >
215+
<hint type="destinationlabel">
201216
<x>132</x>
202217
<y>239</y>
203218
</hint>

0 commit comments

Comments
 (0)
Please sign in to comment.